[6152] | 1 | /* |
---|
| 2 | Copyright 2006 by Sean Luke and George Mason University |
---|
| 3 | Licensed under the Academic Free License version 3.0 |
---|
| 4 | See the file "LICENSE" for more information |
---|
| 5 | */ |
---|
| 6 | |
---|
| 7 | package ec.spatial; |
---|
| 8 | |
---|
| 9 | import ec.Initializer; |
---|
| 10 | import ec.Individual; |
---|
| 11 | import ec.BreedingPipeline; |
---|
| 12 | import ec.Breeder; |
---|
| 13 | import ec.simple.*; |
---|
| 14 | import ec.EvolutionState; |
---|
| 15 | import ec.Population; |
---|
| 16 | import ec.util.Parameter; |
---|
| 17 | import ec.util.*; |
---|
| 18 | |
---|
| 19 | /* |
---|
| 20 | * SpatialBreeder.java |
---|
| 21 | * |
---|
| 22 | * By: Liviu Panait |
---|
| 23 | */ |
---|
| 24 | |
---|
| 25 | /** |
---|
| 26 | * A slight modification of the simple breeder for spatially-embedded EAs. |
---|
| 27 | |
---|
| 28 | * Breeds each subpopulation separately, with no inter-population exchange, |
---|
| 29 | * and using a generational approach. A SpatialBreeder may have multiple |
---|
| 30 | * threads; it divvys up a subpopulation into chunks and hands one chunk |
---|
| 31 | * to each thread to populate. One array of BreedingPipelines is obtained |
---|
| 32 | * from a population's Species for each operating breeding thread. |
---|
| 33 | * |
---|
| 34 | * |
---|
| 35 | * |
---|
| 36 | * |
---|
| 37 | * @author Liviu Panait |
---|
| 38 | * @version 1.0 |
---|
| 39 | */ |
---|
| 40 | |
---|
| 41 | public class SpatialBreeder extends SimpleBreeder |
---|
| 42 | { |
---|
| 43 | public void setup(final EvolutionState state, final Parameter base) |
---|
| 44 | { |
---|
| 45 | super.setup(state, base); |
---|
| 46 | |
---|
| 47 | // check for elitism and warn about it |
---|
| 48 | for(int i = 0 ; i < elite.length; i++) |
---|
| 49 | if (elite[i] > 0) |
---|
| 50 | { |
---|
| 51 | state.output.warning("You're using elitism with SpatialBreeder. This is unwise as elitism is done by moving individuals around in the population, thus messing up the spatial nature of breeding.", |
---|
| 52 | base.push(P_ELITE).push(""+i)); |
---|
| 53 | break; |
---|
| 54 | } |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | protected void breedPopChunk(Population newpop, EvolutionState state, |
---|
| 58 | int[] numinds, int[] from, int threadnum) |
---|
| 59 | { |
---|
| 60 | for(int subpop=0;subpop<newpop.subpops.length;subpop++) |
---|
| 61 | { |
---|
| 62 | BreedingPipeline bp = (BreedingPipeline)newpop.subpops[subpop]. |
---|
| 63 | species.pipe_prototype.clone(); |
---|
| 64 | |
---|
| 65 | if (!(state.population.subpops[subpop] instanceof Space)) |
---|
| 66 | state.output.fatal("Subpopulation " + subpop + " does not implement the Space interface."); |
---|
| 67 | Space space = (Space)(state.population.subpops[subpop]); |
---|
| 68 | |
---|
| 69 | // check to make sure that the breeding pipeline produces |
---|
| 70 | // the right kind of individuals. Don't want a mistake there! :-) |
---|
| 71 | if (!bp.produces(state,newpop,subpop,threadnum)) |
---|
| 72 | state.output.fatal("The Breeding Pipeline of subpopulation " + subpop + " does not produce individuals of the expected species " + newpop.subpops[subpop].species.getClass().getName() + " or fitness " + newpop.subpops[subpop].species.f_prototype ); |
---|
| 73 | bp.prepareToProduce(state,subpop,threadnum); |
---|
| 74 | |
---|
| 75 | // start breedin'! |
---|
| 76 | for(int x = from[subpop]; x < from[subpop] + numinds[subpop]; x++) |
---|
| 77 | { |
---|
| 78 | space.setIndex(threadnum, x); |
---|
| 79 | if (bp.produce(1, 1, x, subpop, newpop.subpops[subpop].individuals, state, threadnum) != 1) |
---|
| 80 | state.output.fatal( "The pipelines should produce one individual at a time!" ); |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | bp.finishProducing(state,subpop,threadnum); |
---|
| 84 | } |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | |
---|