1 | /* |
---|
2 | Copyright 2010 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.gp.ge.breed; |
---|
8 | import ec.gp.ge.*; |
---|
9 | import ec.*; |
---|
10 | import ec.util.*; |
---|
11 | |
---|
12 | /* |
---|
13 | * GEProblem.java |
---|
14 | * |
---|
15 | * Created: Sat Oct 16 23:21:01 EDT 2010 |
---|
16 | * By: Sean Luke, Joseph Zelibor III, and Eric Kangas |
---|
17 | */ |
---|
18 | |
---|
19 | |
---|
20 | /** |
---|
21 | * <p>GETruncationPipeline removes the unused genes from the end of the vector. |
---|
22 | * The number of used chromosomes are tracked by GESpecies' <b>comsumed(...)</b> function. |
---|
23 | * |
---|
24 | * Note: truncaton only occurs if the number of comsumed genes is greater than 1.</p> |
---|
25 | * |
---|
26 | * <p><b>Default Base</b><br> |
---|
27 | * ec.gp.ge.breed.GETruncationPipeline |
---|
28 | * |
---|
29 | * @author Sean Luke, Joseph Zelibor III, and Eric Kangas |
---|
30 | * @version 1.0 |
---|
31 | */ |
---|
32 | |
---|
33 | public class GETruncationPipeline extends BreedingPipeline |
---|
34 | { |
---|
35 | public static final String P_TRUNCATION = "truncate"; |
---|
36 | public static final int NUM_SOURCES = 1; |
---|
37 | |
---|
38 | public int numSources() { return NUM_SOURCES; } |
---|
39 | |
---|
40 | public Parameter defaultBase() |
---|
41 | { |
---|
42 | return GEDefaults.base().push(P_TRUNCATION); |
---|
43 | } |
---|
44 | |
---|
45 | public int produce(final int min, |
---|
46 | final int max, |
---|
47 | final int start, |
---|
48 | final int subpopulation, |
---|
49 | final Individual[] inds, |
---|
50 | final EvolutionState state, |
---|
51 | final int thread) |
---|
52 | { |
---|
53 | // grab individuals from our source and stick 'em right into inds. |
---|
54 | // we'll modify them from there |
---|
55 | int n = sources[0].produce(min,max,start,subpopulation,inds,state,thread); |
---|
56 | |
---|
57 | |
---|
58 | // should we bother? |
---|
59 | if (!state.random[thread].nextBoolean(likelihood)) |
---|
60 | return reproduce(n, start, subpopulation, inds, state, thread, false); // DON'T produce children from source -- we already did |
---|
61 | |
---|
62 | |
---|
63 | |
---|
64 | // now let's mutate 'em |
---|
65 | for(int q=start; q < n+start; q++) |
---|
66 | { |
---|
67 | if (sources[0] instanceof SelectionMethod) |
---|
68 | inds[q] = (Individual)(inds[q].clone()); |
---|
69 | |
---|
70 | GEIndividual ind = (GEIndividual)(inds[q]); |
---|
71 | GESpecies species = (GESpecies) (ind.species); |
---|
72 | |
---|
73 | int consumed = species.consumed(state, ind, thread); |
---|
74 | if (consumed > 1) |
---|
75 | { |
---|
76 | Object[] pieces = new Object[2]; |
---|
77 | //System.err.println(consumed); |
---|
78 | ind.split(new int[] { consumed }, pieces); |
---|
79 | ind.join(new Object[] {pieces[0]}); |
---|
80 | } |
---|
81 | } |
---|
82 | return n; |
---|
83 | } |
---|
84 | |
---|
85 | } |
---|