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.hadoop;
017    
018    import java.io.IOException;
019    
020    import org.apache.hadoop.mapreduce.Job;
021    
022    import de.kumpe.hadooptimizer.EsIndividual;
023    import de.kumpe.hadooptimizer.EsOptimizerConfiguration;
024    import de.kumpe.hadooptimizer.Halter;
025    import de.kumpe.hadooptimizer.NeedsRandomGenerator;
026    
027    public final class EsMultiPopulationsHadoOptimizer extends EsHadoOptimizer {
028            private static final long serialVersionUID = 1L;
029    
030            private final Halter<EsIndividual> outerHalter;
031    
032            public EsMultiPopulationsHadoOptimizer(
033                            final EsOptimizerConfiguration configuration) {
034                    super(configuration);
035                    outerHalter = getConfiguration().getHalter();
036            }
037    
038            public EsMultiPopulationsHadoOptimizer(
039                            final EsOptimizerConfiguration configuration,
040                            final Halter<EsIndividual> outerHalter) {
041                    super(configuration);
042                    if (null == outerHalter) {
043                            throw new NullPointerException("outerHalter may not be null");
044                    }
045                    if (outerHalter instanceof NeedsRandomGenerator) {
046                            ((NeedsRandomGenerator) outerHalter)
047                                            .setRandomGenerator(getRandomGenerator());
048                    }
049    
050                    this.outerHalter = outerHalter;
051            }
052    
053            public Halter<EsIndividual> getOuterHalter() {
054                    assert null != outerHalter;
055    
056                    return outerHalter;
057            }
058    
059            @Override
060            boolean checkForHalt() {
061                    return outerHalter.halt(evaluationResults);
062            }
063    
064            @Override
065            void configureJob(final Job job) throws IOException {
066                    final int maxMapTasks = getMaxMapTasks();
067                    job.setMapperClass(EvolutionCycleMapper.class);
068                    IndividualInputFormat.setSplits(job, maxMapTasks);
069                    IndividualInputFormat.setOffspring(job, maxMapTasks
070                                    * getConfiguration().getParents());
071            }
072    }