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.ArrayList;
019    import java.util.Collection;
020    import java.util.List;
021    
022    import org.apache.commons.logging.Log;
023    import org.apache.commons.logging.LogFactory;
024    
025    import de.kumpe.hadooptimizer.EaOptimizerConfiguration;
026    import de.kumpe.hadooptimizer.EvaluationResult;
027    import de.kumpe.hadooptimizer.Evaluator;
028    import de.kumpe.hadooptimizer.Recombiner;
029    import de.kumpe.hadooptimizer.examples.OptimizerExample;
030    import de.kumpe.hadooptimizer.impl.IdentityMutator;
031    
032    /**
033     * A benchmark example which sleeps a specified number of nanoseconds in the
034     * evaluator.
035     * 
036     * @author <a href="http://kumpe.de/christian/java">Christian Kumpe</a>
037     */
038    public class IncrementingOffspring extends OptimizerExample<double[]> {
039            private static final Log log = LogFactory
040                            .getLog(IncrementingOffspring.class);
041    
042            private final static class IncrementingRecombiner implements
043                            Recombiner<double[]> {
044                    private static final long serialVersionUID = 1L;
045    
046                    private long cycle;
047    
048                    @Override
049                    public Collection<double[]> recombine(
050                                    final Collection<EvaluationResult<double[]>> parents) {
051                            cycle++;
052                            final double[] individual = parents.iterator().next()
053                                            .getIndividual();
054                            final long offspring = cycle;
055                            final Collection<double[]> children = new ArrayList<double[]>();
056                            for (int i = 0; i < offspring; i++) {
057                                    children.add(individual);
058                            }
059                            return children;
060                    }
061    
062            }
063    
064            private final static class SleepingEvaluator implements Evaluator<double[]> {
065                    private static final long serialVersionUID = 1L;
066    
067                    @Override
068                    public double evaluate(final double[] individual) {
069                            final long nanosToSleep = (long) individual[0];
070                            if (0 == nanosToSleep) {
071                                    return 0;
072                            }
073                            if (log.isDebugEnabled()) {
074                                    log.debug(String.format("Sleeping %,dns...", nanosToSleep));
075                            }
076                            final long start = System.nanoTime();
077                            try {
078                                    Thread.sleep(nanosToSleep / 1000000,
079                                                    (int) (nanosToSleep % 1000000));
080                            } catch (final InterruptedException e) {
081                                    throw new RuntimeException(e);
082                            }
083                            final long sleptNanos = System.nanoTime() - start;
084                            if (log.isDebugEnabled()) {
085                                    log.debug(String.format("Slept %,dns.", sleptNanos));
086                            }
087                            return Math.abs(sleptNanos - nanosToSleep);
088                    }
089            }
090    
091            @Override
092            protected EaOptimizerConfiguration<double[]> createEaOptimizerConfiguration() {
093                    final EaOptimizerConfiguration<double[]> conf = new EaOptimizerConfiguration<double[]>();
094    
095                    @SuppressWarnings("unchecked")
096                    final List<String> remainingArgs = commandLine.getArgList();
097                    final int size = Integer.parseInt(remainingArgs.get(1));
098                    final double[] startIndividual = new double[size];
099                    startIndividual[0] = Double.parseDouble(remainingArgs.get(0));
100    
101                    initConfiguration(conf, startIndividual);
102    
103                    conf.setRecombiner(new IncrementingRecombiner());
104                    conf.setMutator(new IdentityMutator<double[]>());
105                    conf.setEvaluator(new SleepingEvaluator());
106                    return conf;
107            }
108    
109            @Override
110            protected String getVersionInfo() {
111                    return "$Id: IncrementingOffspring.java 3891 2011-04-20 15:16:25Z baumbart $";
112            }
113    }