KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgap > impl > NumberGeneTest


1 /*
2  * This file is part of JGAP.
3  *
4  * JGAP offers a dual license model containing the LGPL as well as the MPL.
5  *
6  * For licencing information please see the file license.txt included with JGAP
7  * or have a look at the top of class org.jgap.Chromosome which representatively
8  * includes the JGAP license policy applicable for any file delivered with JGAP.
9  */

10 package org.jgap.impl;
11
12 import java.util.*;
13 import org.jgap.*;
14 import junit.framework.*;
15
16 /**
17  * Tests the (abstract) NumberGene class
18  *
19  * @author Klaus Meffert
20  * @since 1.1
21  */

22 public class NumberGeneTest
23     extends JGAPTestCase {
24   /** String containing the CVS revision. Read out via reflection!*/
25   private final static String JavaDoc CVS_REVISION = "$Revision: 1.25 $";
26
27   public static Test suite() {
28     TestSuite suite = new TestSuite(NumberGeneTest.class);
29     return suite;
30   }
31
32   /**
33    * @throws Exception
34    *
35    * @author Klaus Meffert
36    * @since 1.1
37    */

38   public void testConstruct_0()
39       throws Exception JavaDoc {
40     Gene gene = new NumberGeneImpl(conf, 1, 100);
41     //following should be possible without exception
42
gene.setAllele(new Integer JavaDoc(101));
43   }
44
45   /**
46    * @throws Exception
47    *
48    * @author Klaus Meffert
49    * @since 1.1
50    */

51   public void testToString_0()
52       throws Exception JavaDoc {
53     Gene gene = new NumberGeneImpl(conf, 1, 100);
54     gene.setAllele(new Integer JavaDoc(47));
55     assertEquals("47, " + BaseGene.S_APPLICATION_DATA + ":null",
56                  gene.toString());
57   }
58
59   /**
60    * @throws Exception
61    *
62    * @author Klaus Meffert
63    * @since 1.1
64    */

65   public void testToString_1()
66       throws Exception JavaDoc {
67     Gene gene = new NumberGeneImpl(conf, 1, 100);
68     gene.setAllele(new Integer JavaDoc(102));
69     int indexComma = gene.toString().indexOf(',');
70     int toString = Integer.parseInt(gene.toString().substring(0, indexComma));
71     assertTrue(toString >= 1 && toString <= 100);
72   }
73
74   /**
75    * @throws Exception
76    *
77    * @author Klaus Meffert
78    * @since 1.1
79    */

80   public void testGetAllele_0()
81       throws Exception JavaDoc {
82     Gene gene = new NumberGeneImpl(conf, 1, 100);
83     gene.setAllele(new Integer JavaDoc(33));
84     assertEquals(new Integer JavaDoc(33), gene.getAllele());
85   }
86
87   /**
88    * @throws Exception
89    *
90    * @author Klaus Meffert
91    * @since 1.1
92    */

93   public void testGetAllele_1()
94       throws Exception JavaDoc {
95     Gene gene = new NumberGeneImpl(conf, 1, 100);
96     gene.setAllele(new Integer JavaDoc(1));
97     assertEquals(new Integer JavaDoc(1), gene.getAllele());
98   }
99
100   /**
101    * @throws Exception
102    *
103    * @author Klaus Meffert
104    * @since 1.1
105    */

106   public void testGetAllele_2()
107       throws Exception JavaDoc {
108     Gene gene = new NumberGeneImpl(conf, 1, 100);
109     gene.setAllele(new Integer JavaDoc(100));
110     assertEquals(new Integer JavaDoc(100), gene.getAllele());
111   }
112
113   /**
114    * @throws Exception
115    *
116    * @author Klaus Meffert
117    * @since 1.1
118    */

119   public void testHashCode_0()
120       throws Exception JavaDoc {
121     Gene gene1 = new NumberGeneImpl(conf, 1, 100);
122     Gene gene2 = new NumberGeneImpl(conf, 1, 100);
123     assertEquals(gene1.hashCode(), gene2.hashCode());
124   }
125
126   /**
127    * @throws Exception
128    *
129    * @author Klaus Meffert
130    * @since 1.1
131    */

132   public void testHashCode_1()
133       throws Exception JavaDoc {
134     Gene gene1 = new NumberGeneImpl(conf, 1, 100);
135     gene1.setAllele(new Integer JavaDoc(43));
136     Gene gene2 = new NumberGeneImpl(conf, 1, 100);
137     gene2.setAllele(new Integer JavaDoc(43));
138     assertEquals(gene1.hashCode(), gene2.hashCode());
139   }
140
141   /**
142    * @throws Exception
143    *
144    * @author Klaus Meffert
145    * @since 1.1
146    */

147   public void testHashCode_2()
148       throws Exception JavaDoc {
149     Gene gene1 = new NumberGeneImpl(conf, 1, 100);
150     gene1.setAllele(new Integer JavaDoc(55));
151     Gene gene2 = new NumberGeneImpl(conf, 1, 100);
152     gene1.setAllele(new Integer JavaDoc(43));
153     assertFalse(gene1.hashCode() == gene2.hashCode());
154   }
155
156   /**
157    * @throws Exception
158    *
159    * @author Klaus Meffert
160    * @since 1.1
161    */

162   public void testEquals_0()
163       throws Exception JavaDoc {
164     Gene gene1 = new NumberGeneImpl(conf, 1, 100);
165     Gene gene2 = new NumberGeneImpl(conf, 1, 100);
166     assertEquals(gene1, gene2);
167     assertEquals(gene2, gene1);
168   }
169
170   /**
171    * @throws Exception
172    *
173    * @author Klaus Meffert
174    * @since 1.1
175    */

176   public void testEquals_1()
177       throws Exception JavaDoc {
178     Gene gene1 = new NumberGeneImpl(conf, 1, 100);
179     assertFalse(gene1.equals(null));
180   }
181
182   /**
183    * @throws Exception
184    *
185    * @author Klaus Meffert
186    * @since 1.1
187    */

188   public void testEquals_2()
189       throws Exception JavaDoc {
190     Gene gene1 = new NumberGeneImpl(conf, 1, 100);
191     assertFalse(gene1.equals(new BooleanGene(conf)));
192   }
193
194   /**
195    * @throws Exception
196    *
197    * @author Klaus Meffert
198    * @since 1.1
199    */

200   public void testEquals_3()
201       throws Exception JavaDoc {
202     Gene gene1 = new NumberGeneImpl(conf, 1, 100);
203     assertFalse(gene1.equals(new Vector()));
204   }
205
206   /**
207    * @throws Exception
208    *
209    * @author Klaus Meffert
210    * @since 1.1
211    */

212   public void testEquals_4()
213       throws Exception JavaDoc {
214     Gene gene1 = new NumberGeneImpl(conf, 1, 100);
215     Gene gene2 = new NumberGeneImpl(conf, 1, 99);
216     assertEquals(gene1, gene2);
217     assertEquals(gene2, gene1);
218   }
219
220   /**
221    * @throws Exception
222    *
223    * @author Klaus Meffert
224    * @since 2.4
225    */

226   public void testEquals_5()
227       throws Exception JavaDoc {
228     Gene gene1 = new NumberGeneImpl(conf, 1, 100);
229     gene1.setAllele(new Integer JavaDoc(2));
230     Gene gene2 = new NumberGeneImpl(conf, 1, 99);
231     gene1.setAllele(new Integer JavaDoc(3));
232     assertFalse(gene1.equals(gene2));
233     assertFalse(gene2.equals(gene1));
234   }
235
236   /**
237    * Set Allele to null, no exception should occur
238    * @throws Exception
239    *
240    * @author Klaus Meffert
241    * @since 1.1
242    */

243   public void testSetAllele_0()
244       throws Exception JavaDoc {
245     Gene gene1 = new NumberGeneImpl(conf, 1, 10000);
246     gene1.setAllele(null);
247   }
248
249   /**
250    * @throws Exception
251    *
252    * @author Klaus Meffert
253    * @since 1.1
254    */

255   public void testSetAllele_1()
256       throws Exception JavaDoc {
257     Gene gene1 = new NumberGeneImpl(conf, 1, 10000);
258     try {
259       gene1.setAllele("22");
260       fail();
261     }
262     catch (ClassCastException JavaDoc classex) {
263       ; //this is OK
264
}
265   }
266
267   /**
268    * @throws Exception
269    *
270    * @author Klaus Meffert
271    * @since 2.2
272    */

273   public void testSetAllele_2()
274       throws Exception JavaDoc {
275     NumberGene gene1 = new NumberGeneImpl(conf, 1, 10000);
276     gene1.setConstraintChecker(new IGeneConstraintChecker() {
277       public boolean verify(Gene a_gene, Object JavaDoc a_alleleValue,
278                             IChromosome a_chrom, int a_index) {
279         return false;
280       }
281     }
282     );
283     gene1.setAllele("22");
284   }
285
286   /**
287    * @throws Exception
288    *
289    * @author Klaus Meffert
290    * @since 2.2
291    */

292   public void testSetAllele_3()
293       throws Exception JavaDoc {
294     NumberGene gene1 = new NumberGeneImpl(conf, 1, 10000);
295     gene1.setConstraintChecker(new IGeneConstraintChecker() {
296       public boolean verify(Gene a_gene, Object JavaDoc a_alleleValue,
297                             IChromosome a_chrom, int a_index) {
298         return true;
299       }
300     }
301     );
302     try {
303       gene1.setAllele("22");
304       fail();
305     }
306     catch (ClassCastException JavaDoc classex) {
307       ; //this is OK
308
}
309   }
310
311   /**
312    * @throws Exception
313    *
314    * @author Klaus Meffert
315    * @since 1.1
316    */

317   public void testNewGene_0()
318       throws Exception JavaDoc {
319     Gene gene1 = new NumberGeneImpl(conf, 1, 10000);
320     gene1.setAllele(new Integer JavaDoc(4711));
321     Integer JavaDoc lower1 = (Integer JavaDoc) privateAccessor.getField(gene1,
322         "m_lowerBounds");
323     Integer JavaDoc upper1 = (Integer JavaDoc) privateAccessor.getField(gene1,
324         "m_upperBounds");
325     Gene gene2 = gene1.newGene();
326     Integer JavaDoc lower2 = (Integer JavaDoc) privateAccessor.getField(gene2,
327         "m_lowerBounds");
328     Integer JavaDoc upper2 = (Integer JavaDoc) privateAccessor.getField(gene2,
329         "m_upperBounds");
330     assertEquals(lower1, lower2);
331     assertEquals(upper1, upper2);
332   }
333
334   /**
335    * @throws Exception
336    *
337    * @author Klaus Meffert
338    * @since 2.2
339    */

340   public void testCompareTo_0()
341       throws Exception JavaDoc {
342     Gene gene1 = new NumberGeneImpl(conf, 1, 10000);
343     gene1.setAllele(new Integer JavaDoc(4711));
344     Gene gene2 = gene1.newGene();
345     assertEquals(0, gene1.compareTo(gene1));
346     assertEquals(1, gene1.compareTo(gene2));
347     gene2.setAllele(new Integer JavaDoc(4711));
348     assertEquals(0, gene1.compareTo(gene2));
349     assertEquals(0, gene2.compareTo(gene1));
350   }
351
352   /**
353    * @throws Exception
354    *
355    * @author Klaus Meffert
356    * @since 2.2
357    */

358   public void testCompareTo_1()
359       throws Exception JavaDoc {
360     Gene gene1 = new NumberGeneImpl(conf, 1, 10000);
361     gene1.setAllele(new Integer JavaDoc(4711));
362     Gene gene2 = new DoubleGene(conf);
363     try {
364       assertEquals(0, gene1.compareTo(gene2));
365       fail();
366     }
367     catch (ClassCastException JavaDoc cex) {
368       ; //this is OK
369
}
370   }
371
372   /**
373    * @throws Exception
374    *
375    * @author Klaus Meffert
376    * @since 2.2
377    */

378   public void testCompareTo_2()
379       throws Exception JavaDoc {
380     Gene gene1 = new NumberGeneImpl(conf, 1, 10000);
381     gene1.setAllele(new Integer JavaDoc(4711));
382     Gene gene2 = new DoubleGene(conf);
383     gene2.setAllele(new Double JavaDoc(4711.0d));
384     try {
385       assertEquals(0, gene1.compareTo(gene2));
386       fail();
387     }
388     catch (ClassCastException JavaDoc cex) {
389       ; //this is OK
390
}
391   }
392
393   /**
394    * Using application data.
395    * @throws Exception
396    *
397    * @author Klaus Meffert
398    * @since 2.4
399    */

400   public void testCompareTo_3()
401       throws Exception JavaDoc {
402     Gene gene1 = new NumberGeneImpl(conf, 1, 10000);
403     gene1.setAllele(new Integer JavaDoc(4711));
404     gene1.setApplicationData(new Integer JavaDoc(2));
405     gene1.setCompareApplicationData(true);
406     Gene gene2 = gene1.newGene();
407     gene2.setAllele(new Integer JavaDoc(4711));
408     assertEquals(1, gene1.compareTo(gene2));
409     assertEquals(0, gene2.compareTo(gene1));
410     gene2.setCompareApplicationData(true);
411     assertEquals( -1, gene2.compareTo(gene1));
412     gene2.setApplicationData(new Integer JavaDoc(3));
413     assertEquals(1, gene2.compareTo(gene1));
414     assertEquals( -1, gene1.compareTo(gene2));
415   }
416
417   public void testSetConstraintChecker_0()
418       throws Exception JavaDoc {
419     NumberGene gene1 = new NumberGeneImpl(conf, 1, 3);
420     gene1.setConstraintChecker(new IGeneConstraintChecker() {
421       public boolean verify(Gene a_gene, Object JavaDoc a_alleleValue,
422                             IChromosome a_chrom, int a_index) {
423         return false;
424       }
425     }
426     );
427     assertNotNull(gene1.getConstraintChecker());
428   }
429
430   /**
431    * @throws Exception
432    *
433    * @author Klaus Meffert
434    * @since 2.4
435    */

436   public void testSetEnergy_0()
437       throws Exception JavaDoc {
438     BaseGene gene = new NumberGeneImpl(conf, 0, 1);
439     assertEquals(0.0, gene.getEnergy(), DELTA);
440   }
441
442   /**
443    * @throws Exception
444    *
445    * @author Klaus Meffert
446    * @since 2.4
447    */

448   public void testSetEnergy_1()
449       throws Exception JavaDoc {
450     BaseGene gene = new NumberGeneImpl(conf, 2, 4);
451     gene.setEnergy(2.3);
452     assertEquals(2.3, gene.getEnergy(), DELTA);
453     gene.setEnergy( -55.8);
454     assertEquals( -55.8, gene.getEnergy(), DELTA);
455     gene.setEnergy(0.5);
456     gene.setEnergy(0.8);
457     assertEquals(0.8, gene.getEnergy(), DELTA);
458   }
459
460   /**
461    * Test implementation of NumberGene, based on IntegerGene.
462    *
463    * @author Klaus Meffert
464    * @since 1.1
465    */

466   private class NumberGeneImpl
467       extends NumberGene {
468     protected final static long INTEGER_RANGE = (long) Integer.MAX_VALUE
469         - (long) Integer.MIN_VALUE;
470
471     private int m_upperBounds;
472
473     private int m_lowerBounds;
474
475     private long m_boundsUnitsToIntegerUnits;
476
477     public NumberGeneImpl(final Configuration a_conf, int a_lowerBounds,
478                           int a_upperBounds)
479         throws InvalidConfigurationException {
480       super(a_conf);
481       m_lowerBounds = a_lowerBounds;
482       m_upperBounds = a_upperBounds;
483       calculateBoundsUnitsToIntegerUnitsRatio();
484     }
485
486     protected Gene newGeneInternal() {
487       try {
488         return new NumberGeneImpl(getConfiguration(), m_lowerBounds,
489                                   m_upperBounds);
490       }
491       catch (InvalidConfigurationException iex) {
492         throw new IllegalStateException JavaDoc(iex.getMessage());
493       }
494     }
495
496     public String JavaDoc getPersistentRepresentation() {
497       return toString() + PERSISTENT_FIELD_DELIMITER + m_lowerBounds
498           + PERSISTENT_FIELD_DELIMITER + m_upperBounds;
499     }
500
501     public void setValueFromPersistentRepresentation(String JavaDoc a_representation) {
502       // not implemented here!
503
// ---------------------
504
}
505
506     public void setToRandomValue(RandomGenerator a_numberGenerator) {
507       // not implemented here!
508
// ---------------------
509
}
510
511     public void applyMutation(int a_index, double a_percentage) {
512       // not implemented here!
513
// ---------------------
514
}
515
516     protected void calculateBoundsUnitsToIntegerUnitsRatio() {
517       int divisor = m_upperBounds - m_lowerBounds + 1;
518       if (divisor == 0) {
519         m_boundsUnitsToIntegerUnits = INTEGER_RANGE;
520       }
521       else {
522         m_boundsUnitsToIntegerUnits = INTEGER_RANGE / divisor;
523       }
524     }
525
526     protected void mapValueToWithinBounds() {
527       if (getAllele() != null) {
528         Integer JavaDoc i_value = ( (Integer JavaDoc) getAllele());
529         // If the value exceeds either the upper or lower bounds, then
530
// map the value to within the legal range. To do this, we basically
531
// calculate the distance between the value and the integer min,
532
// determine how many bounds units that represents, and then add
533
// that number of units to the upper bound.
534
// -----------------------------------------------------------------
535
if (i_value.intValue() > m_upperBounds
536             || i_value.intValue() < m_lowerBounds) {
537           long differenceFromIntMin = (long) Integer.MIN_VALUE
538               + (long) i_value.intValue();
539           int differenceFromBoundsMin = (int) (differenceFromIntMin
540                                                / m_boundsUnitsToIntegerUnits);
541           setAllele(new Integer JavaDoc(m_upperBounds + differenceFromBoundsMin));
542         }
543       }
544     }
545
546     protected int compareToNative(Object JavaDoc a_o1, Object JavaDoc a_o2) {
547       return ( (Integer JavaDoc) a_o1).compareTo( (Integer JavaDoc) a_o2);
548     }
549
550     public boolean equals(Object JavaDoc a_other) {
551       try {
552         return compareTo(a_other) == 0;
553       }
554       catch (ClassCastException JavaDoc e) {
555         return false;
556       }
557     }
558   }
559 }
560
Popular Tags