[6152] | 1 | /* |
---|
| 2 | Copyright 2006 by Sean Luke |
---|
| 3 | Licensed under the Academic Free License version 3.0 |
---|
| 4 | See the file "LICENSE" for more information |
---|
| 5 | */ |
---|
| 6 | |
---|
| 7 | |
---|
| 8 | package ec.breed; |
---|
| 9 | import ec.*; |
---|
| 10 | import ec.util.*; |
---|
| 11 | |
---|
| 12 | /* |
---|
| 13 | * MultiBreedingPipeline.java |
---|
| 14 | * |
---|
| 15 | * Created: December 28, 1999 |
---|
| 16 | * By: Sean Luke |
---|
| 17 | */ |
---|
| 18 | |
---|
| 19 | /** |
---|
| 20 | * MultiBreedingPipeline is a BreedingPipeline stores some <i>n</i> child sources; |
---|
| 21 | * each time it must produce an individual or two, |
---|
| 22 | * it picks one of these sources at random and has it do the production. |
---|
| 23 | |
---|
| 24 | <p><b>Typical Number of Individuals Produced Per <tt>produce(...)</tt> call</b><br> |
---|
| 25 | If by <i>base</i>.<tt>generate-max</tt> is <tt>true</tt>, then always the maximum |
---|
| 26 | number of the typical numbers of any child source. If <tt>false</itt>, then varies |
---|
| 27 | depending on the child source picked. |
---|
| 28 | |
---|
| 29 | <p><b>Number of Sources</b><br> |
---|
| 30 | Dynamic. As many as the user specifies. |
---|
| 31 | |
---|
| 32 | <p><b>Parameters</b><br> |
---|
| 33 | <table> |
---|
| 34 | <tr><td valign=top><i>base</i>.<tt>generate-max</tt><br> |
---|
| 35 | <font size=-1> bool = <tt>true</tt> (default) or <tt>false</tt></font></td> |
---|
| 36 | <td valign=top>(Each time produce(...) is called, should the MultiBreedingPipeline |
---|
| 37 | force all its sources to produce exactly the same number of individuals as the largest |
---|
| 38 | typical number of individuals produced by any source in the group?)</td></tr> |
---|
| 39 | </table> |
---|
| 40 | |
---|
| 41 | <p><b>Default Base</b><br> |
---|
| 42 | breed.multibreed |
---|
| 43 | |
---|
| 44 | * |
---|
| 45 | * @author Sean Luke |
---|
| 46 | * @version 1.0 |
---|
| 47 | */ |
---|
| 48 | |
---|
| 49 | public class MultiBreedingPipeline extends BreedingPipeline |
---|
| 50 | { |
---|
| 51 | public static final String P_GEN_MAX = "generate-max"; |
---|
| 52 | public static final String P_MULTIBREED = "multibreed"; |
---|
| 53 | |
---|
| 54 | public int maxGeneratable; |
---|
| 55 | public boolean generateMax; |
---|
| 56 | |
---|
| 57 | public Parameter defaultBase() |
---|
| 58 | { |
---|
| 59 | return BreedDefaults.base().push(P_MULTIBREED); |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | public int numSources() { return DYNAMIC_SOURCES; } |
---|
| 63 | |
---|
| 64 | public void setup(final EvolutionState state, final Parameter base) |
---|
| 65 | { |
---|
| 66 | super.setup(state,base); |
---|
| 67 | |
---|
| 68 | Parameter def = defaultBase(); |
---|
| 69 | |
---|
| 70 | float total = 0.0f; |
---|
| 71 | |
---|
| 72 | for(int x=0;x<sources.length;x++) |
---|
| 73 | { |
---|
| 74 | // make sure the sources are actually breeding pipelines |
---|
| 75 | if (!(sources[x] instanceof BreedingPipeline)) |
---|
| 76 | state.output.error("Source #" + x + "is not a BreedingPipeline",base); |
---|
| 77 | else if (sources[x].probability<0.0) // null checked from state.output.error above |
---|
| 78 | state.output.error("Pipe #" + x + " must have a probability >= 0.0",base); // convenient that NO_PROBABILITY is -1... |
---|
| 79 | else total += sources[x].probability; |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | state.output.exitIfErrors(); |
---|
| 83 | |
---|
| 84 | // Now check for nonzero probability (we know it's positive) |
---|
| 85 | if (total == 0.0) |
---|
| 86 | state.output.warning("MultiBreedingPipeline's children have all zero probabilities -- this will be treated as a uniform distribution. This could be an error.", base); |
---|
| 87 | |
---|
| 88 | // allow all zero probabilities |
---|
| 89 | BreedingSource.setupProbabilities(sources); |
---|
| 90 | |
---|
| 91 | generateMax = state.parameters.getBoolean(base.push(P_GEN_MAX),def.push(P_GEN_MAX),true); |
---|
| 92 | maxGeneratable=0; // indicates that I don't know what it is yet. |
---|
| 93 | |
---|
| 94 | // declare that likelihood isn't used |
---|
| 95 | if (likelihood < 1.0f) |
---|
| 96 | state.output.warning("MultiBreedingPipeline does not respond to the 'likelihood' parameter.", |
---|
| 97 | base.push(P_LIKELIHOOD), def.push(P_LIKELIHOOD)); |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | /** Returns the max of typicalIndsProduced() of all its children */ |
---|
| 101 | public int typicalIndsProduced() |
---|
| 102 | { |
---|
| 103 | if (maxGeneratable==0) // not determined yet |
---|
| 104 | maxGeneratable = maxChildProduction(); |
---|
| 105 | return maxGeneratable; |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | |
---|
| 109 | public int produce(final int min, |
---|
| 110 | final int max, |
---|
| 111 | final int start, |
---|
| 112 | final int subpopulation, |
---|
| 113 | final Individual[] inds, |
---|
| 114 | final EvolutionState state, |
---|
| 115 | final int thread) |
---|
| 116 | |
---|
| 117 | { |
---|
| 118 | BreedingSource s = sources[BreedingSource.pickRandom( |
---|
| 119 | sources,state.random[thread].nextFloat())]; |
---|
| 120 | int total; |
---|
| 121 | |
---|
| 122 | if (generateMax) |
---|
| 123 | { |
---|
| 124 | if (maxGeneratable==0) |
---|
| 125 | maxGeneratable = maxChildProduction(); |
---|
| 126 | int n = maxGeneratable; |
---|
| 127 | if (n < min) n = min; |
---|
| 128 | if (n > max) n = max; |
---|
| 129 | |
---|
| 130 | total = s.produce( |
---|
| 131 | n,n,start,subpopulation,inds,state,thread); |
---|
| 132 | } |
---|
| 133 | else |
---|
| 134 | { |
---|
| 135 | total = s.produce( |
---|
| 136 | min,max,start,subpopulation,inds,state,thread); |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | // clone if necessary |
---|
| 140 | if (s instanceof SelectionMethod) |
---|
| 141 | for(int q=start; q < total+start; q++) |
---|
| 142 | inds[q] = (Individual)(inds[q].clone()); |
---|
| 143 | |
---|
| 144 | return total; |
---|
| 145 | } |
---|
| 146 | } |
---|