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.simple;
017
018 import de.kumpe.hadooptimizer.EsIndividual;
019 import de.kumpe.hadooptimizer.EsOptimizerConfiguration;
020 import de.kumpe.hadooptimizer.EvaluationResult;
021 import de.kumpe.hadooptimizer.Optimizer;
022
023 /**
024 * A multithreaded {@link Optimizer} implementation for evolution strategies.
025 *
026 * @see Optimizer
027 * @see ThreadedOptimizerBase
028 *
029 * @author <a href="http://kumpe.de/christian/java">Christian Kumpe</a>
030 */
031 public class ThreadedEsOptimizer extends ThreadedOptimizerBase<EsIndividual> {
032 public ThreadedEsOptimizer(final EsOptimizerConfiguration configuration) {
033 super(configuration);
034 }
035
036 @Override
037 protected EsOptimizerConfiguration getConfiguration() {
038 return (EsOptimizerConfiguration) super.getConfiguration();
039 }
040
041 @Override
042 void processPopulation() throws Exception {
043 // duplicate and add children to processing queue
044 final int nrOfParents = evaluationResults.size();
045 @SuppressWarnings("unchecked")
046 final EvaluationResult<EsIndividual>[] parentsAsArray = (EvaluationResult<EsIndividual>[]) evaluationResults
047 .toArray(new EvaluationResult<?>[nrOfParents]);
048
049 if (!getConfiguration().isPreserveParents()) {
050 evaluationResults.clear();
051 }
052
053 resetEvaluationResultCounters(getConfiguration().getOffspring());
054
055 for (int i = 0; i < getConfiguration().getOffspring(); i++) {
056 final int selectedParent = getRandomGenerator()
057 .nextInt(nrOfParents);
058 individualsToProcess.add(parentsAsArray[selectedParent]
059 .getIndividual());
060 }
061
062 waitForEvaluationResults();
063 }
064 }