001    /*
002     * Copyright 2011 Christian Kumpe http://kumpe.de/christian/java
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *     http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package de.kumpe.hadooptimizer.examples.benchmark;
017    
018    import java.util.List;
019    
020    
021    import de.kumpe.hadooptimizer.EaOptimizerConfiguration;
022    import de.kumpe.hadooptimizer.Evaluator;
023    import de.kumpe.hadooptimizer.examples.OptimizerExample;
024    import de.kumpe.hadooptimizer.impl.IdentityMutator;
025    
026    /**
027     * A benchmark example which sleeps a specified number of nanoseconds in the
028     * evaluator.
029     * 
030     * @author <a href="http://kumpe.de/christian/java">Christian Kumpe</a>
031     */
032    public class Sleep extends OptimizerExample<double[]> {
033            private final static class SleepingEvaluator implements Evaluator<double[]> {
034                    private static final long serialVersionUID = 1L;
035    
036                    @Override
037                    public double evaluate(final double[] individual) {
038                            final long nanosToSleep = (long) individual[0];
039                            if (0 == nanosToSleep) {
040                                    return 0;
041                            }
042                            if (log.isDebugEnabled()) {
043                                    log.debug(String.format("Sleeping %,dns...", nanosToSleep));
044                            }
045                            final long start = System.nanoTime();
046                            try {
047                                    Thread.sleep(nanosToSleep / 1000000,
048                                                    (int) (nanosToSleep % 1000000));
049                            } catch (final InterruptedException e) {
050                                    throw new RuntimeException(e);
051                            }
052                            final long sleptNanos = System.nanoTime() - start;
053                            if (log.isDebugEnabled()) {
054                                    log.debug(String.format("Slept %,dns.", sleptNanos));
055                            }
056                            return Math.abs(sleptNanos - nanosToSleep);
057                    }
058            }
059    
060            @Override
061            protected EaOptimizerConfiguration<double[]> createEaOptimizerConfiguration() {
062                    final EaOptimizerConfiguration<double[]> conf = new EaOptimizerConfiguration<double[]>();
063    
064                    @SuppressWarnings("unchecked")
065                    final List<String> remainingArgs = commandLine.getArgList();
066                    final int size = Integer.parseInt(remainingArgs.get(1));
067                    final double[] startIndividual = new double[size];
068                    startIndividual[0] = Double.parseDouble(remainingArgs.get(0));
069    
070                    initConfiguration(conf, startIndividual);
071    
072                    conf.setMutator(new IdentityMutator<double[]>());
073                    conf.setEvaluator(new SleepingEvaluator());
074                    return conf;
075            }
076    
077            @Override
078            protected String getVersionInfo() {
079                    return "$Id: Sleep.java 3890 2011-04-20 14:58:14Z baumbart $";
080            }
081    }