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 IncrementingSleep extends OptimizerExample<double[]> {
039 private static final Log log = LogFactory.getLog(IncrementingSleep.class);
040
041 private final static class IncrementingRecombiner implements
042 Recombiner<double[]> {
043 private static final long serialVersionUID = 1L;
044
045 private final int offspring;
046 private long cycle;
047
048 public IncrementingRecombiner(final int offspring) {
049 if (offspring <= 0) {
050 throw new IllegalArgumentException(
051 "offspring may not be zero or negative");
052 }
053 this.offspring = offspring;
054 }
055
056 @Override
057 public Collection<double[]> recombine(
058 final Collection<EvaluationResult<double[]>> parents) {
059 final double[] individual = parents.iterator().next()
060 .getIndividual();
061 final double[] newIndividual = new double[individual.length];
062 for (int i = 0; i < individual.length; i++) {
063 newIndividual[i] = cycle * cycle * 1000000;
064 }
065 final Collection<double[]> children = new ArrayList<double[]>();
066 for (int i = 0; i < offspring; i++) {
067 children.add(newIndividual);
068 }
069 cycle++;
070 return children;
071 }
072
073 }
074
075 private final static class SleepingEvaluator implements Evaluator<double[]> {
076 private static final long serialVersionUID = 1L;
077
078 @Override
079 public double evaluate(final double[] individual) {
080 final long nanosToSleep = (long) individual[0];
081 if (0 == nanosToSleep) {
082 return 0;
083 }
084 if (log.isDebugEnabled()) {
085 log.debug(String.format("Sleeping %,dns...", nanosToSleep));
086 }
087 final long start = System.nanoTime();
088 try {
089 Thread.sleep(nanosToSleep / 1000000,
090 (int) (nanosToSleep % 1000000));
091 } catch (final InterruptedException e) {
092 throw new RuntimeException(e);
093 }
094 final long sleptNanos = System.nanoTime() - start;
095 if (log.isDebugEnabled()) {
096 log.debug(String.format("Slept %,dns.", sleptNanos));
097 }
098 return Math.abs(sleptNanos - nanosToSleep);
099 }
100 }
101
102 @Override
103 protected EaOptimizerConfiguration<double[]> createEaOptimizerConfiguration() {
104 final EaOptimizerConfiguration<double[]> conf = new EaOptimizerConfiguration<double[]>();
105
106 @SuppressWarnings("unchecked")
107 final List<String> remainingArgs = commandLine.getArgList();
108 final int size = Integer.parseInt(remainingArgs.get(1));
109 final double[] startIndividual = new double[size];
110 startIndividual[0] = Double.parseDouble(remainingArgs.get(0));
111
112 initConfiguration(conf, startIndividual);
113
114 conf.setRecombiner(new IncrementingRecombiner(offspring));
115 conf.setMutator(new IdentityMutator<double[]>());
116 conf.setEvaluator(new SleepingEvaluator());
117 return conf;
118 }
119
120 @Override
121 protected String getVersionInfo() {
122 return "$Id: IncrementingSleep.java 3891 2011-04-20 15:16:25Z baumbart $";
123 }
124 }