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 simple (not multithreaded) {@link Optimizer} implementation for evolution
025     * strategies.
026     * 
027     * @see Optimizer
028     * @see SimpleOptimizerBase
029     * 
030     * @author <a href="http://kumpe.de/christian/java">Christian Kumpe</a>
031     */
032    public class SimpleEsOptimizer extends SimpleOptimizerBase<EsIndividual> {
033            public SimpleEsOptimizer(final EsOptimizerConfiguration configuration) {
034                    super(configuration);
035            }
036    
037            @Override
038            protected EsOptimizerConfiguration getConfiguration() {
039                    return (EsOptimizerConfiguration) super.getConfiguration();
040            }
041    
042            @Override
043            void processPopulation() {
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                    for (int i = 0; i < getConfiguration().getOffspring(); i++) {
054                            final int selectedParent = getRandomGenerator()
055                                            .nextInt(nrOfParents);
056                            processChild(parentsAsArray[selectedParent].getIndividual(),
057                                            getConfiguration());
058                    }
059            }
060    }