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 import org.apache.commons.logging.Log;
021 import org.apache.commons.logging.LogFactory;
022
023 import de.kumpe.hadooptimizer.EaOptimizerConfiguration;
024 import de.kumpe.hadooptimizer.Evaluator;
025 import de.kumpe.hadooptimizer.examples.OptimizerExample;
026 import de.kumpe.hadooptimizer.impl.IdentityMutator;
027
028 /**
029 * A benchmark example which loops a specified number of times in the evaluator.
030 *
031 * @author <a href="http://kumpe.de/christian/java">Christian Kumpe</a>
032 */
033 public final class Loop extends OptimizerExample<double[]> {
034 private static final Log log = LogFactory.getLog(Loop.class);
035
036 private final static class LoopingEvaluator implements Evaluator<double[]> {
037 private static final long serialVersionUID = 1L;
038
039 @Override
040 public double evaluate(final double[] individual) {
041 final long nrOfLoops = (long) individual[0];
042 if (0 == nrOfLoops) {
043 return 0;
044 }
045 final long start = System.nanoTime();
046 for (long i = 0; i < nrOfLoops; i++) {
047 }
048 final long loopedNanos = System.nanoTime() - start;
049 if (log.isDebugEnabled()) {
050 log.debug(String.format("Looped %,dns.", loopedNanos));
051 }
052 return Math.abs(loopedNanos - nrOfLoops);
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 LoopingEvaluator());
070 return conf;
071 }
072
073 @Override
074 protected String getVersionInfo() {
075 return "$Id: Loop.java 3890 2011-04-20 14:58:14Z baumbart $";
076 }
077 }