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.vector.breed;
|
---|
8 |
|
---|
9 | import ec.BreedingPipeline;
|
---|
10 | import ec.EvolutionState;
|
---|
11 | import ec.Individual;
|
---|
12 | import ec.SelectionMethod;
|
---|
13 | import ec.util.Parameter;
|
---|
14 | import ec.vector.*;
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * <p>GeneDuplicationPipeline is designed to duplicate a sequence of genes from the chromosome and append
|
---|
18 | * them to the end of the chromosome. The sequence of genes copied are randomly determined. That is to
|
---|
19 | * say a random begining index is selected and a random ending index is selected from the chromosome. Then
|
---|
20 | * this area is then copied (begining inclusive, ending exclusive) and appended to the end of the chromosome.
|
---|
21 | * Since randomness is a factor several checks are performed to make sure the begining and ending indicies are
|
---|
22 | * valid. For example, since the ending index is exclusive, the ending index cannot equal the begining index (a
|
---|
23 | * new ending index would be randomly seleceted in this case). Likewise the begining index cannot be larger than the
|
---|
24 | * ending index (they would be swapped in this case).</p>
|
---|
25 | *
|
---|
26 | * <p><b>Default Base</b><br>
|
---|
27 | * ec.vector.breed.GeneDuplicationPipeline
|
---|
28 | *
|
---|
29 | * @author Sean Luke, Joseph Zelibor III, and Eric Kangas
|
---|
30 | * @version 1.0
|
---|
31 | */
|
---|
32 | public class GeneDuplicationPipeline extends BreedingPipeline
|
---|
33 | {
|
---|
34 | public static final String P_DUPLICATION = "duplicate";
|
---|
35 | public static final int NUM_SOURCES = 1;
|
---|
36 |
|
---|
37 | public Parameter defaultBase()
|
---|
38 | {
|
---|
39 | return VectorDefaults.base().push(P_DUPLICATION);
|
---|
40 | }
|
---|
41 |
|
---|
42 | public int numSources() { return NUM_SOURCES; }
|
---|
43 |
|
---|
44 | public int produce(int min,
|
---|
45 | int max,
|
---|
46 | int start,
|
---|
47 | int subpopulation,
|
---|
48 | Individual[] inds,
|
---|
49 | EvolutionState state,
|
---|
50 | int thread)
|
---|
51 | {
|
---|
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 | // now let's mutate 'em
|
---|
64 | for(int q=start; q < n+start; q++)
|
---|
65 | {
|
---|
66 | if (sources[0] instanceof SelectionMethod)
|
---|
67 | inds[q] = (Individual)(inds[q].clone());
|
---|
68 |
|
---|
69 | //duplicate from the genome between a random begin and end point,
|
---|
70 | //and put that at the end of the new genome.
|
---|
71 | VectorIndividual ind = (VectorIndividual)(inds[q]);
|
---|
72 |
|
---|
73 | int len = ind.genomeLength();
|
---|
74 |
|
---|
75 | //zero length individual, just return
|
---|
76 | if (len == 0)
|
---|
77 | {
|
---|
78 | return n;
|
---|
79 | }
|
---|
80 |
|
---|
81 | int end = 0;
|
---|
82 | int begin = state.random[thread].nextInt(len+1);
|
---|
83 | do
|
---|
84 | {
|
---|
85 | end = state.random[thread].nextInt(len+1);
|
---|
86 | }
|
---|
87 | while (begin == end); //because the end is exclusive, start cannot be
|
---|
88 | //equal to end.
|
---|
89 |
|
---|
90 |
|
---|
91 | if (end < begin)
|
---|
92 | {
|
---|
93 | int temp = end; //swap if necessary
|
---|
94 | end = begin;
|
---|
95 | begin = temp;
|
---|
96 | }
|
---|
97 |
|
---|
98 | // copy the original into a new array.
|
---|
99 | Object[] original = new Object[2];
|
---|
100 | ind.split(new int[] {0, len}, original);
|
---|
101 |
|
---|
102 | // copy the splice into a new array
|
---|
103 | Object[] splice = new Object[3];
|
---|
104 | ind.split(new int[] {begin, end}, splice);
|
---|
105 |
|
---|
106 | // clone the genes in splice[1] (which we'll concatenate back in) in case we're using GeneVectorIndividual
|
---|
107 | ind.cloneGenes(splice[1]);
|
---|
108 |
|
---|
109 | // appends the pieces together with the splice at the end.
|
---|
110 | ind.join(new Object[] {original[1], splice[1]});
|
---|
111 | }
|
---|
112 | return n; // number of individuals produced, 1 here.
|
---|
113 | }
|
---|
114 |
|
---|
115 | }
|
---|