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.vector; |
---|
9 | import ec.*; |
---|
10 | import ec.util.*; |
---|
11 | |
---|
12 | /* |
---|
13 | * IntegerVectorSpecies.java |
---|
14 | * |
---|
15 | * Created: Tue Feb 20 13:26:00 2001 |
---|
16 | * By: Sean Luke |
---|
17 | */ |
---|
18 | |
---|
19 | /** |
---|
20 | * IntegerVectorSpecies is a subclass of VectorSpecies with special constraints |
---|
21 | * for integral vectors, namely ByteVectorIndividual, ShortVectorIndividual, |
---|
22 | * IntegerVectorIndividual, and LongVectorIndividual. |
---|
23 | |
---|
24 | * <p>IntegerVectorSpecies can specify min/max numeric constraints on gene values |
---|
25 | * in three different ways. |
---|
26 | * |
---|
27 | * <ol> |
---|
28 | * <li> You may provide a default min and max value. |
---|
29 | * This is done by specifying: |
---|
30 | * <p><i>base</i>.<tt>min-gene</tt> |
---|
31 | * <br><i>base</i>.<tt>max-gene</tt> |
---|
32 | * <p><i>Note:</i> you <b>must</b> provide these values even if you don't use them, |
---|
33 | * as they're used as defaults by #2 and #3 below. |
---|
34 | *<p> |
---|
35 | * <li> You may provide min and max values for genes in segments (regions) along |
---|
36 | * the genome. This is done by specifying: |
---|
37 | * <p><i>base</i>.<tt>num-segments</tt> |
---|
38 | * The segments may be defined by either start or end indices of genes. |
---|
39 | * This is controlled by specifying the value of: |
---|
40 | * <p><i>base</i>.<tt>segment-type</tt> |
---|
41 | * which can assume the value of start or end, with start being the default. |
---|
42 | * The indices are defined using Java array style, i.e. the first gene has the index of 0, |
---|
43 | * and the last gene has the index of genome-size - 1. |
---|
44 | * <p>Using this method, each segment is specified by<i>j</i>... |
---|
45 | * <p><i>base</i>.<tt>segment.</tt><i>j</i><tt>.start</tt> |
---|
46 | * <br><i>base</i>.<tt>segment.</tt><i>j</i><tt>.min-gene</tt> |
---|
47 | * <br><i>base</i>.<tt>segment.</tt><i>j</i><tt>.max-gene</tt> |
---|
48 | * if segment-type value was chosen as start or by: |
---|
49 | * <p><i>base</i>.<tt>segment.</tt><i>j</i><tt>.end</tt> |
---|
50 | * <br><i>base</i>.<tt>segment.</tt><i>j</i><tt>.min-gene</tt> |
---|
51 | * <br><i>base</i>.<tt>segment.</tt><i>j</i><tt>.max-gene</tt> |
---|
52 | * if segment-type value is equal to end. |
---|
53 | *<p> |
---|
54 | * <li> You may provide min and max values for each separate gene. |
---|
55 | * This is done by specifying (for each gene location <i>i</i> you wish to specify). |
---|
56 | * <p><i>base</i>.<tt>min-gene</tt>.<i>i</i> |
---|
57 | * <br><i>base</i>.<tt>max-gene</tt>.<i>i</i> |
---|
58 | * </ol> |
---|
59 | * |
---|
60 | * <p>Any settings for #3 override #2, and both override #1. |
---|
61 | * |
---|
62 | * |
---|
63 | * <p><b>Parameters</b><br> |
---|
64 | * <table> |
---|
65 | * <tr><td valign=top><i>base</i>.<tt>min-gene</tt><br> |
---|
66 | * <font size=-1>long (default=0)</font></td> |
---|
67 | * <td valign=top>(the minimum gene value)</td></tr> |
---|
68 | * |
---|
69 | * <tr><td valign=top><i>base</i>.<tt>max-gene</tt><br> |
---|
70 | * <font size=-1>long >= <i>base</i>.min-gene</font></td> |
---|
71 | * <td valign=top>(the maximum gene value)</td></tr> |
---|
72 | * |
---|
73 | * <tr><td valign=top><i>base</i>.<tt>min-gene</tt>.<i>i</i><br> |
---|
74 | * <font size=-1>long (default=<i>base</i>.<tt>min-gene</tt>)</font></td> |
---|
75 | * <td valign=top>(the minimum gene value for gene <i>i</i>)</td></tr> |
---|
76 | * |
---|
77 | * <tr><td valign=top><i>base</i>.<tt>max-gene</tt>.<i>i</i><br> |
---|
78 | * <font size=-1>long >= <i>base</i>.min-gene.<i>i</i> (default=<i>base</i>.<tt>max-gene</tt>)</font></td> |
---|
79 | * <td valign=top>(the maximum gene value for gene <i>i</i>)</td></tr> |
---|
80 | * |
---|
81 | * <tr><td valign=top><i>base.</i>.<tt>num-segments</tt><br> |
---|
82 | * <font size=-1>int >= 1 (default=no segments used)</font></td> |
---|
83 | * <td valign=top>(the number of gene segments defined)</td> |
---|
84 | * </tr> |
---|
85 | * |
---|
86 | * <tr><td valign=top><i>base.</i>.<tt>segment-type</tt><br> |
---|
87 | * <font size=-1>start (default) or end</font></td> |
---|
88 | * <td valign=top>(defines the way in which segments are defined: either by providing start indices (segment-type=start) or by providing end indices (segment-type=end)</td> |
---|
89 | * </tr> |
---|
90 | * |
---|
91 | * <tr><td valign=top><i>base.</i>.<tt>segment</tt>.<i>j</i>.<tt>start</tt><br> |
---|
92 | * <font size=-1>0 <= int < genome length</font></td> |
---|
93 | * <td valign=top>(the start index of gene segment <i>j</i> -- the end of a segment is before the start of the next segment)</td> |
---|
94 | * <td valign=top>(used when the value of segment-type parameter is equal to start)</td> |
---|
95 | * </tr> |
---|
96 | * |
---|
97 | * <tr><td valign=top><i>base.</i>.<tt>segment</tt>.<i>j</i>.<tt>end</tt><br> |
---|
98 | * <font size=-1>0 <= int < genome length</font></td> |
---|
99 | * <td valign=top>(the end of gene segment <i>j</i> -- the start of a segment is after the end of the previous segment)</td> |
---|
100 | * <td valign=top>(used when the value of segment-type parameter is equal to end)</td> |
---|
101 | * </tr> |
---|
102 | * |
---|
103 | * <tr><td valign=top><i>base.</i>.<tt>segment</tt>.<i>j</i>.<tt>min-gene</tt><br> |
---|
104 | * <font size=-1>double (default=0.0)</font></td> |
---|
105 | * <td valign=top>(the minimum gene value for segment <i>j</i>)</td> |
---|
106 | * </tr> |
---|
107 | * |
---|
108 | * <tr><td valign=top><i>base.</i>.<tt>segment</tt>.<i>j</i>.<tt>max-gene</tt><br> |
---|
109 | * <font size=-1>double >= <i>base.</i>.<tt>segment</tt>.<i>j</i>.<tt>min-gene</tt></td> |
---|
110 | * <td valign=top>(the maximum gene value for segment <i>j</i>)</td> |
---|
111 | * </tr> |
---|
112 | * |
---|
113 | * </table> |
---|
114 | * @author Sean Luke, Rafal Kicinger |
---|
115 | * @version 1.0 |
---|
116 | */ |
---|
117 | public class IntegerVectorSpecies extends VectorSpecies |
---|
118 | { |
---|
119 | public final static String P_MINGENE = "min-gene"; |
---|
120 | public final static String P_MAXGENE = "max-gene"; |
---|
121 | |
---|
122 | public final static String P_NUM_SEGMENTS = "num-segments"; |
---|
123 | |
---|
124 | public final static String P_SEGMENT_TYPE = "segment-type"; |
---|
125 | |
---|
126 | public final static String P_SEGMENT_START = "start"; |
---|
127 | |
---|
128 | public final static String P_SEGMENT_END = "end"; |
---|
129 | |
---|
130 | public final static String P_SEGMENT = "segment"; |
---|
131 | |
---|
132 | public long[] minGenes; |
---|
133 | public long[] maxGenes; |
---|
134 | |
---|
135 | public long maxGene(int gene) |
---|
136 | { |
---|
137 | long[] m = maxGenes; |
---|
138 | if (m.length <= gene) |
---|
139 | { |
---|
140 | if (!dynamicInitialSize && !warned) warnAboutGene(gene); |
---|
141 | gene = m.length - 1; |
---|
142 | } |
---|
143 | return m[gene]; |
---|
144 | } |
---|
145 | |
---|
146 | public long minGene(int gene) |
---|
147 | { |
---|
148 | long[] m = minGenes; |
---|
149 | if (m.length <= gene) |
---|
150 | { |
---|
151 | if (!dynamicInitialSize && !warned) warnAboutGene(gene); |
---|
152 | gene = m.length - 1; |
---|
153 | } |
---|
154 | return m[gene]; |
---|
155 | } |
---|
156 | |
---|
157 | public boolean inNumericalTypeRange(long geneVal) |
---|
158 | { |
---|
159 | if (i_prototype instanceof ByteVectorIndividual) |
---|
160 | return (geneVal <= Byte.MAX_VALUE && geneVal >= Byte.MIN_VALUE); |
---|
161 | else if (i_prototype instanceof ShortVectorIndividual) |
---|
162 | return (geneVal <= Short.MAX_VALUE && geneVal >= Short.MIN_VALUE); |
---|
163 | else if (i_prototype instanceof IntegerVectorIndividual) |
---|
164 | return (geneVal <= Integer.MAX_VALUE && geneVal >= Integer.MIN_VALUE); |
---|
165 | else if (i_prototype instanceof LongVectorIndividual) |
---|
166 | return true; // geneVal is valid for all longs |
---|
167 | else return false; // dunno what the individual is... |
---|
168 | } |
---|
169 | |
---|
170 | public void setup(final EvolutionState state, final Parameter base) |
---|
171 | { |
---|
172 | // keep in mind that the *species* variable has not been set yet. |
---|
173 | super.setup(state,base); |
---|
174 | |
---|
175 | Parameter def = defaultBase(); |
---|
176 | |
---|
177 | // create the arrays |
---|
178 | minGenes = new long[genomeSize]; |
---|
179 | maxGenes = new long[genomeSize]; |
---|
180 | |
---|
181 | |
---|
182 | |
---|
183 | |
---|
184 | // LOADING GLOBAL MIN/MAX GENES |
---|
185 | long minGene = state.parameters.getLongWithDefault(base.push(P_MINGENE),def.push(P_MINGENE),0); |
---|
186 | long maxGene = state.parameters.getLong(base.push(P_MAXGENE),def.push(P_MAXGENE),minGene); |
---|
187 | if (maxGene < minGene) |
---|
188 | state.output.fatal("IntegerVectorSpecies must have a default min-gene which is <= the default max-gene", |
---|
189 | base.push(P_MAXGENE),def.push(P_MAXGENE)); |
---|
190 | |
---|
191 | for (int x = 0; x < genomeSize; x++) |
---|
192 | { |
---|
193 | minGenes[x] = minGene; |
---|
194 | maxGenes[x] = maxGene; |
---|
195 | } |
---|
196 | |
---|
197 | |
---|
198 | |
---|
199 | |
---|
200 | // LOADING SEGMENTS |
---|
201 | |
---|
202 | //Set number of segments to 0 by default |
---|
203 | int numSegments = 0; |
---|
204 | // Now check to see if segments of genes (genes having the same min and |
---|
205 | // max values) exist |
---|
206 | if (state.parameters.exists(base.push(P_NUM_SEGMENTS), def.push(P_NUM_SEGMENTS))) |
---|
207 | { |
---|
208 | if (dynamicInitialSize) |
---|
209 | state.output.warnOnce("Using dynamic initial sizing, but per-segment min/max gene declarations. This is probably wrong. You probably want to use global min/max declarations.", |
---|
210 | base.push(P_NUM_SEGMENTS), def.push(P_NUM_SEGMENTS)); |
---|
211 | |
---|
212 | numSegments = state.parameters.getIntWithDefault(base.push(P_NUM_SEGMENTS), |
---|
213 | def.push(P_NUM_SEGMENTS), 0); |
---|
214 | |
---|
215 | if(numSegments == 0) |
---|
216 | state.output.warning( |
---|
217 | "The number of genome segments has been defined to be equal to 0.\n" |
---|
218 | + "Hence, no genome segments will be defined.", |
---|
219 | base.push(P_NUM_SEGMENTS), |
---|
220 | def.push(P_NUM_SEGMENTS)); |
---|
221 | else if(numSegments < 0) |
---|
222 | state.output.fatal( |
---|
223 | "Invalid number of genome segments: " + numSegments |
---|
224 | + "\nIt must be a nonnegative value.", |
---|
225 | base.push(P_NUM_SEGMENTS), |
---|
226 | def.push(P_NUM_SEGMENTS)); |
---|
227 | |
---|
228 | //read the type of segment definition using the default start value |
---|
229 | String segmentType = state.parameters.getStringWithDefault(base.push(P_SEGMENT_TYPE), |
---|
230 | def.push(P_SEGMENT_TYPE), P_SEGMENT_START); |
---|
231 | |
---|
232 | if(segmentType.equalsIgnoreCase(P_SEGMENT_START)) |
---|
233 | initializeGenomeSegmentsByStartIndices(state, base, def, numSegments, minGene, maxGene); |
---|
234 | else if(segmentType.equalsIgnoreCase(P_SEGMENT_END)) |
---|
235 | initializeGenomeSegmentsByEndIndices(state, base, def, numSegments, minGene, maxGene); |
---|
236 | else |
---|
237 | state.output.fatal( |
---|
238 | "Invalid specification of genome segment type: " + segmentType |
---|
239 | + "\nThe " + P_SEGMENT_TYPE + " parameter must have the value of " + P_SEGMENT_START + " or " + P_SEGMENT_END, |
---|
240 | base.push(P_SEGMENT_TYPE), |
---|
241 | def.push(P_SEGMENT_TYPE)); |
---|
242 | |
---|
243 | |
---|
244 | } |
---|
245 | |
---|
246 | |
---|
247 | |
---|
248 | // LOADING PER-GENE VALUES |
---|
249 | |
---|
250 | boolean foundStuff = false; |
---|
251 | boolean warnedMin=false; |
---|
252 | boolean warnedMax=false; |
---|
253 | for(int x=0;x<genomeSize;x++) |
---|
254 | { |
---|
255 | if (!state.parameters.exists(base.push(P_MINGENE).push(""+x),base.push(P_MINGENE).push(""+x))) |
---|
256 | { |
---|
257 | if (foundStuff && !warnedMin) |
---|
258 | { |
---|
259 | state.output.warning("IntegerVectorSpecies has missing min-gene values for some genes.\n" + |
---|
260 | "The first one is gene #"+x+".", base.push(P_MINGENE).push(""+x),base.push(P_MINGENE).push(""+x)); |
---|
261 | warnedMin = true; |
---|
262 | } |
---|
263 | } |
---|
264 | else |
---|
265 | { |
---|
266 | if (dynamicInitialSize) |
---|
267 | state.output.warnOnce("Using dynamic initial sizing, but per-gene min/max gene declarations. This is probably wrong. You probably want to use global min/max declarations.", |
---|
268 | base.push(P_MINGENE).push(""+x),base.push(P_MINGENE).push(""+x)); |
---|
269 | |
---|
270 | minGenes[x] = state.parameters.getLongWithDefault(base.push(P_MINGENE).push(""+x),base.push(P_MINGENE).push(""+x),minGene); |
---|
271 | foundStuff = true; |
---|
272 | } |
---|
273 | |
---|
274 | if (!state.parameters.exists(base.push(P_MAXGENE).push(""+x),base.push(P_MAXGENE).push(""+x))) |
---|
275 | { |
---|
276 | if (foundStuff && !warnedMax) |
---|
277 | { |
---|
278 | state.output.warning("IntegerVectorSpecies has missing max-gene values for some genes.\n" + |
---|
279 | "The first one is gene #"+x+".", base.push(P_MAXGENE).push(""+x),base.push(P_MAXGENE).push(""+x)); |
---|
280 | warnedMax = true; |
---|
281 | } |
---|
282 | } |
---|
283 | else |
---|
284 | { |
---|
285 | if (dynamicInitialSize) |
---|
286 | state.output.warnOnce("Using dynamic initial sizing, but per-gene min/max gene declarations. This is probably wrong. You probably want to use global min/max declarations.", |
---|
287 | base.push(P_MINGENE).push(""+x),base.push(P_MINGENE).push(""+x)); |
---|
288 | |
---|
289 | maxGenes[x] = state.parameters.getLongWithDefault(base.push(P_MAXGENE).push(""+x),base.push(P_MAXGENE).push(""+x),maxGene); |
---|
290 | foundStuff = true; |
---|
291 | } |
---|
292 | } |
---|
293 | |
---|
294 | |
---|
295 | |
---|
296 | |
---|
297 | // VERIFY |
---|
298 | for(int x=0; x< genomeSize; x++) |
---|
299 | { |
---|
300 | if (maxGenes[x] < minGenes[x]) |
---|
301 | state.output.fatal("IntegerVectorSpecies must have a min-gene["+x+"] which is <= the max-gene["+x+"]"); |
---|
302 | |
---|
303 | // check to see if these longs are within the data type of the particular individual |
---|
304 | if (!inNumericalTypeRange(minGenes[x])) |
---|
305 | state.output.fatal("This IntegerVectorSpecies has a prototype of the kind: " |
---|
306 | + i_prototype.getClass().getName() + |
---|
307 | ", but doesn't have a min-gene["+x+"] value within the range of this prototype's genome's data types"); |
---|
308 | if (!inNumericalTypeRange(maxGenes[x])) |
---|
309 | state.output.fatal("This IntegerVectorSpecies has a prototype of the kind: " |
---|
310 | + i_prototype.getClass().getName() + |
---|
311 | ", but doesn't have a max-gene["+x+"] value within the range of this prototype's genome's data types"); |
---|
312 | } |
---|
313 | |
---|
314 | |
---|
315 | /* |
---|
316 | //Debugging |
---|
317 | for(int i = 0; i < minGenes.length; i++) |
---|
318 | System.out.println("Min: " + minGenes[i] + ", Max: " + maxGenes[i]); |
---|
319 | */ |
---|
320 | } |
---|
321 | |
---|
322 | |
---|
323 | private void initializeGenomeSegmentsByStartIndices(final EvolutionState state, |
---|
324 | final Parameter base, |
---|
325 | final Parameter def, |
---|
326 | int numSegments, |
---|
327 | long minGene, long maxGene) |
---|
328 | { |
---|
329 | boolean warnedMin = false; |
---|
330 | boolean warnedMax = false; |
---|
331 | long currentSegmentMinGeneValue = Long.MAX_VALUE; |
---|
332 | long currentSegmentMaxGeneValue = Long.MIN_VALUE; |
---|
333 | |
---|
334 | //loop in reverse order |
---|
335 | int previousSegmentEnd = genomeSize; |
---|
336 | int currentSegmentEnd = 0; |
---|
337 | |
---|
338 | for (int i = numSegments - 1; i >= 0; i--) |
---|
339 | { |
---|
340 | //check if the segment data exist |
---|
341 | if (state.parameters.exists(base.push(P_SEGMENT).push(""+i).push(P_SEGMENT_START), |
---|
342 | def.push(P_SEGMENT).push(""+i).push(P_SEGMENT_START))) |
---|
343 | { |
---|
344 | //Read the index of the end gene specifying current segment |
---|
345 | currentSegmentEnd = state.parameters.getInt(base.push(P_SEGMENT).push(""+i).push(P_SEGMENT_START), |
---|
346 | def.push(P_SEGMENT).push(""+i).push(P_SEGMENT_START)); |
---|
347 | |
---|
348 | } |
---|
349 | else |
---|
350 | { |
---|
351 | state.output.fatal("Genome segment " + i + " has not been defined!" + |
---|
352 | "\nYou must specify start indices for " + numSegments + " segment(s)", |
---|
353 | base.push(P_SEGMENT).push(""+i).push(P_SEGMENT_START), |
---|
354 | base.push(P_SEGMENT).push(""+i).push(P_SEGMENT_START)); |
---|
355 | } |
---|
356 | |
---|
357 | //check if the start index is valid |
---|
358 | if(currentSegmentEnd >= previousSegmentEnd || currentSegmentEnd < 0) |
---|
359 | state.output.fatal( |
---|
360 | "Invalid start index value for segment " + i + ": " + currentSegmentEnd |
---|
361 | + "\nThe value must be smaller than " + previousSegmentEnd + |
---|
362 | " and greater than or equal to " + 0); |
---|
363 | |
---|
364 | //check if the index of the first segment is equal to 0 |
---|
365 | if(i == 0 && currentSegmentEnd != 0) |
---|
366 | state.output.fatal( |
---|
367 | "Invalid start index value for the first segment " + i + ": " + currentSegmentEnd |
---|
368 | + "\nThe value must be equal to " + 0); |
---|
369 | |
---|
370 | |
---|
371 | //get min and max values of genes in this segment |
---|
372 | if (!state.parameters.exists(base.push(P_SEGMENT).push(""+i).push(P_MINGENE), |
---|
373 | base.push(P_SEGMENT).push(""+i).push(P_MINGENE))) |
---|
374 | { |
---|
375 | if (!warnedMin) |
---|
376 | { |
---|
377 | state.output.warning( |
---|
378 | "IntegerVectorSpecies has missing min-gene values for some segments.\n" |
---|
379 | + "The first segment is #" + i + ".", |
---|
380 | base.push(P_SEGMENT).push(""+i), |
---|
381 | base.push(P_SEGMENT).push(""+i)); |
---|
382 | warnedMin = true; |
---|
383 | } |
---|
384 | |
---|
385 | //the min-gene value has not been defined for this segment so assume the global min value |
---|
386 | currentSegmentMinGeneValue = minGene; |
---|
387 | } |
---|
388 | else //get the min value for this segment |
---|
389 | { |
---|
390 | currentSegmentMinGeneValue = state.parameters.getLongWithDefault( |
---|
391 | base.push(P_SEGMENT).push(""+i).push(P_MINGENE), |
---|
392 | base.push(P_SEGMENT).push(""+i).push(P_MINGENE), |
---|
393 | minGene); |
---|
394 | |
---|
395 | } |
---|
396 | |
---|
397 | if (!state.parameters.exists(base.push(P_SEGMENT).push(""+i).push(P_MAXGENE), |
---|
398 | base.push(P_SEGMENT).push(""+i).push(P_MAXGENE))) |
---|
399 | { |
---|
400 | if (!warnedMax) |
---|
401 | { |
---|
402 | state.output.warning( |
---|
403 | "IntegerVectorSpecies has missing max-gene values for some segments.\n" |
---|
404 | + "The first segment is #" + i + ".", |
---|
405 | base.push(P_SEGMENT).push(""+i), |
---|
406 | base.push(P_SEGMENT).push(""+i)); |
---|
407 | warnedMax = true; |
---|
408 | } |
---|
409 | |
---|
410 | //the max-gen value has not been defined for this segment so assume the global max value |
---|
411 | currentSegmentMaxGeneValue = maxGene; |
---|
412 | |
---|
413 | } |
---|
414 | else //get the max value for this segment |
---|
415 | { |
---|
416 | currentSegmentMaxGeneValue = state.parameters.getLongWithDefault( |
---|
417 | base.push(P_SEGMENT).push(""+i).push(P_MAXGENE), |
---|
418 | base.push(P_SEGMENT).push(""+i).push(P_MAXGENE), |
---|
419 | maxGene); |
---|
420 | } |
---|
421 | |
---|
422 | //check is min is smaller than or equal to max |
---|
423 | if (currentSegmentMaxGeneValue < currentSegmentMinGeneValue) |
---|
424 | state.output.fatal( |
---|
425 | "IntegerVectorSpecies must have a min-gene value for segment " |
---|
426 | + i + " which is <= the max-gene value", |
---|
427 | base.push(P_SEGMENT).push(""+i).push(P_MAXGENE), |
---|
428 | base.push(P_SEGMENT).push(""+i).push(P_MAXGENE)); |
---|
429 | |
---|
430 | |
---|
431 | //and assign min and max values for all genes in this segment |
---|
432 | for(int j = previousSegmentEnd-1; j >= currentSegmentEnd; j--) |
---|
433 | { |
---|
434 | minGenes[j] = currentSegmentMinGeneValue; |
---|
435 | maxGenes[j] = currentSegmentMaxGeneValue; |
---|
436 | } |
---|
437 | |
---|
438 | previousSegmentEnd = currentSegmentEnd; |
---|
439 | |
---|
440 | } |
---|
441 | |
---|
442 | } |
---|
443 | |
---|
444 | private void initializeGenomeSegmentsByEndIndices(final EvolutionState state, |
---|
445 | final Parameter base, |
---|
446 | final Parameter def, |
---|
447 | int numSegments, |
---|
448 | long minGene, long maxGene) |
---|
449 | { |
---|
450 | boolean warnedMin = false; |
---|
451 | boolean warnedMax = false; |
---|
452 | long currentSegmentMinGeneValue = Long.MAX_VALUE; |
---|
453 | long currentSegmentMaxGeneValue = Long.MIN_VALUE; |
---|
454 | |
---|
455 | int previousSegmentEnd = -1; |
---|
456 | int currentSegmentEnd = 0; |
---|
457 | // iterate over segments and set genes values for each segment |
---|
458 | for (int i = 0; i < numSegments; i++) |
---|
459 | { |
---|
460 | //check if the segment data exist |
---|
461 | if (state.parameters.exists(base.push(P_SEGMENT).push(""+i).push(P_SEGMENT_END), def.push(P_SEGMENT).push(""+i).push(P_SEGMENT_END))) |
---|
462 | { |
---|
463 | //Read the index of the end gene specifying current segment |
---|
464 | currentSegmentEnd = state.parameters.getInt(base.push(P_SEGMENT).push(""+i).push(P_SEGMENT_END), |
---|
465 | def.push(P_SEGMENT).push(""+i).push(P_SEGMENT_END)); |
---|
466 | |
---|
467 | } |
---|
468 | else |
---|
469 | { |
---|
470 | state.output.fatal("Genome segment " + i + " has not been defined!" + |
---|
471 | "\nYou must specify end indices for " + numSegments + " segment(s)", |
---|
472 | base.push(P_SEGMENT).push(""+i).push(P_SEGMENT_END), |
---|
473 | base.push(P_SEGMENT).push(""+i).push(P_SEGMENT_END)); |
---|
474 | } |
---|
475 | |
---|
476 | //check if the end index is valid |
---|
477 | if(currentSegmentEnd <= previousSegmentEnd || currentSegmentEnd >= genomeSize) |
---|
478 | state.output.fatal( |
---|
479 | "Invalid end index value for segment " + i + ": " + currentSegmentEnd |
---|
480 | + "\nThe value must be greater than " + previousSegmentEnd + |
---|
481 | " and smaller than " + genomeSize); |
---|
482 | |
---|
483 | //check if the index of the final segment is equal to the genomeSize |
---|
484 | if(i == numSegments - 1 && currentSegmentEnd != (genomeSize-1)) |
---|
485 | state.output.fatal( |
---|
486 | "Invalid end index value for the last segment " + i + ": " + currentSegmentEnd |
---|
487 | + "\nThe value must be equal to the index of the last gene in the genome: " + (genomeSize-1)); |
---|
488 | |
---|
489 | |
---|
490 | //get min and max values of genes in this segment |
---|
491 | if (!state.parameters.exists(base.push(P_SEGMENT).push(""+i).push(P_MINGENE), |
---|
492 | base.push(P_SEGMENT).push(""+i).push(P_MINGENE))) |
---|
493 | { |
---|
494 | if (!warnedMin) |
---|
495 | { |
---|
496 | state.output.warning( |
---|
497 | "IntegerVectorSpecies has missing min-gene values for some segments.\n" |
---|
498 | + "The first segment is #" + i + ".", |
---|
499 | base.push(P_SEGMENT).push(""+i), |
---|
500 | base.push(P_SEGMENT).push(""+i)); |
---|
501 | warnedMin = true; |
---|
502 | } |
---|
503 | |
---|
504 | //the min-gene value has not been defined for this segment so assume the global min value |
---|
505 | currentSegmentMinGeneValue = minGene; |
---|
506 | } |
---|
507 | else //get the min value for this segment |
---|
508 | { |
---|
509 | currentSegmentMinGeneValue = state.parameters.getLongWithDefault( |
---|
510 | base.push(P_SEGMENT).push(""+i).push(P_MINGENE), |
---|
511 | base.push(P_SEGMENT).push(""+i).push(P_MINGENE), |
---|
512 | minGene); |
---|
513 | |
---|
514 | } |
---|
515 | |
---|
516 | if (!state.parameters.exists(base.push(P_SEGMENT).push(""+i).push(P_MAXGENE), |
---|
517 | base.push(P_SEGMENT).push(""+i).push(P_MAXGENE))) |
---|
518 | { |
---|
519 | if (!warnedMax) |
---|
520 | { |
---|
521 | state.output.warning( |
---|
522 | "IntegerVectorSpecies has missing max-gene values for some segments.\n" |
---|
523 | + "The first segment is #" + i + ".", |
---|
524 | base.push(P_SEGMENT).push(""+i), |
---|
525 | base.push(P_SEGMENT).push(""+i)); |
---|
526 | warnedMax = true; |
---|
527 | } |
---|
528 | |
---|
529 | //the max-gen value has not been defined for this segment so assume the global max value |
---|
530 | currentSegmentMaxGeneValue = maxGene; |
---|
531 | } |
---|
532 | else //get the max value for this segment |
---|
533 | { |
---|
534 | currentSegmentMaxGeneValue = state.parameters.getLongWithDefault( |
---|
535 | base.push(P_SEGMENT).push(""+i).push(P_MAXGENE), |
---|
536 | base.push(P_SEGMENT).push(""+i).push(P_MAXGENE), |
---|
537 | maxGene); |
---|
538 | } |
---|
539 | |
---|
540 | //check is min is smaller than or equal to max |
---|
541 | if (currentSegmentMaxGeneValue < currentSegmentMinGeneValue) |
---|
542 | state.output.fatal( |
---|
543 | "IntegerVectorSpecies must have a min-gene value for segment " |
---|
544 | + i + " which is <= the max-gene value", |
---|
545 | base.push(P_SEGMENT).push(""+i).push(P_MAXGENE), |
---|
546 | base.push(P_SEGMENT).push(""+i).push(P_MAXGENE)); |
---|
547 | |
---|
548 | //and assign min and max values for all genes in this segment |
---|
549 | for(int j = previousSegmentEnd+1; j <= currentSegmentEnd; j++) |
---|
550 | { |
---|
551 | minGenes[j] = currentSegmentMinGeneValue; |
---|
552 | maxGenes[j] = currentSegmentMaxGeneValue; |
---|
553 | } |
---|
554 | |
---|
555 | previousSegmentEnd = currentSegmentEnd; |
---|
556 | } |
---|
557 | } |
---|
558 | |
---|
559 | } |
---|
560 | |
---|