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.jeneva;
017    
018    import java.util.List;
019    
020    import org.apache.hadoop.conf.Configurable;
021    
022    import de.kumpe.hadooptimizer.EaOptimizerConfiguration;
023    import de.kumpe.hadooptimizer.Evaluator;
024    import de.kumpe.hadooptimizer.Optimizer;
025    import de.kumpe.hadooptimizer.examples.OptimizerExample;
026    import de.kumpe.hadooptimizer.impl.IdentityMutator;
027    
028    public class JenevaSleepExample extends OptimizerExample<double[]> {
029            private final static class SleepingEvaluator implements Evaluator<double[]> {
030                    private static final long serialVersionUID = 1L;
031    
032                    @Override
033                    public double evaluate(final double[] individual) {
034                            final long nanosToSleep = (long) individual[0];
035                            if (0 == nanosToSleep) {
036                                    return 0;
037                            }
038                            if (log.isDebugEnabled()) {
039                                    log.debug(String.format("Sleeping %,dns...", nanosToSleep));
040                            }
041                            final long start = System.nanoTime();
042                            try {
043                                    Thread.sleep(nanosToSleep / 1000000,
044                                                    (int) (nanosToSleep % 1000000));
045                            } catch (final InterruptedException e) {
046                                    throw new RuntimeException(e);
047                            }
048                            final long sleptNanos = System.nanoTime() - start;
049                            if (log.isDebugEnabled()) {
050                                    log.debug(String.format("Slept %,dns.", sleptNanos));
051                            }
052                            return Math.abs(sleptNanos - nanosToSleep);
053                    }
054            }
055    
056            @Override
057            protected EaOptimizerConfiguration<double[]> createEaOptimizerConfiguration() {
058                    final EaOptimizerConfiguration<double[]> conf = new EaOptimizerConfiguration<double[]>();
059    
060                    @SuppressWarnings("unchecked")
061                    final List<String> remainingArgs = commandLine.getArgList();
062                    final int size = Integer.parseInt(remainingArgs.get(1));
063                    final double[] startIndividual = new double[size];
064                    startIndividual[0] = Double.parseDouble(remainingArgs.get(0));
065    
066                    initConfiguration(conf, startIndividual);
067    
068                    conf.setMutator(new IdentityMutator<double[]>());
069                    conf.setEvaluator(new SleepingEvaluator());
070                    return conf;
071            }
072    
073            @Override
074            protected void execute() throws Exception {
075                    final Optimizer<?> optimizer = new EaJenevaOptimizer<double[]>(
076                                    createEaOptimizerConfiguration());
077    
078                    if (optimizer instanceof Configurable) {
079                            ((Configurable) optimizer).setConf(getConf());
080                    }
081    
082                    optimizer.optimize();
083            }
084    
085            @Override
086            protected String getVersionInfo() {
087                    return "$Id: Sleep.java 3890 2011-04-20 14:58:14Z baumbart $";
088            }
089    
090            public static void main(final String[] args) throws Exception {
091                    new JenevaSleepExample().run(args);
092            }
093    }