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 java.util.Collection;
019    import java.util.concurrent.ExecutionException;
020    
021    import de.kumpe.hadooptimizer.EaOptimizerConfiguration;
022    import de.kumpe.hadooptimizer.Optimizer;
023    
024    /**
025     * A multithreaded {@link Optimizer} implementation for general evolutionary
026     * algorithms.
027     * 
028     * @param <I>
029     *            the individuals' type
030     * 
031     * @see Optimizer
032     * @see ThreadedOptimizerBase
033     * 
034     * @author <a href="http://kumpe.de/christian/java">Christian Kumpe</a>
035     */
036    public class ThreadedEaOptimizer<I> extends ThreadedOptimizerBase<I> {
037            public ThreadedEaOptimizer(final EaOptimizerConfiguration<I> configuration) {
038                    super(configuration);
039            }
040    
041            @Override
042            protected EaOptimizerConfiguration<I> getConfiguration() {
043                    return (EaOptimizerConfiguration<I>) super.getConfiguration();
044            }
045    
046            @Override
047            void processPopulation() throws InterruptedException, ExecutionException {
048                    // recombine
049                    final Collection<I> children = getConfiguration().getRecombiner()
050                                    .recombine(evaluationResults);
051    
052                    if (!getConfiguration().isPreserveParents()) {
053                            evaluationResults.clear();
054                    }
055    
056                    resetEvaluationResultCounters(children.size());
057    
058                    // add children to processing queue
059                    individualsToProcess.addAll(children);
060    
061                    waitForEvaluationResults();
062            }
063    }