KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > examples > supergene > WithoutSupergeneChangeFitFuncForTest


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 examples.supergene;
11
12 import org.jgap.*;
13 import org.jgap.impl.IntegerGene;
14 import examples.supergene.*;
15
16 /**
17  * Fitness function for test implementations without using supergenes.
18  *
19  * @author Neil Rotstan, Klaus Meffert
20  * @author Audrius Meskauskas (subsequent adaptation)
21  */

22 class WithoutSupergeneChangeFitFuncForTest
23     extends SupergeneChangeFitnessFunction {
24   /** String containing the CVS revision. Read out via reflection!*/
25   private final static String JavaDoc CVS_REVISION = "$Revision: 1.2 $";
26
27   public WithoutSupergeneChangeFitFuncForTest(int a_targetAmount) {
28     super(a_targetAmount);
29   }
30
31   public Gene getResponsibleGene(IChromosome a_chromosome, int a_code) {
32     return a_chromosome.getGene(a_code);
33   }
34
35   /**
36    * Additionall check that the number of nickels and pennies should
37    * be both even or odd.
38    */

39   public double evaluate(IChromosome a_subject) {
40     IntegerGene nickels =
41         (IntegerGene) a_subject.getGene(SupergeneSample.NICKELS);
42     IntegerGene pennies =
43         (IntegerGene) a_subject.getGene(SupergeneSample.PENNIES);
44     boolean valid = nickels.intValue() % 2 == pennies.intValue() % 2;
45     // valid = true; // uncomment for testing without the condition above
46
double r;
47     if (!valid) {
48       r = 0;
49     }
50     else {
51       r = super.evaluate(a_subject);
52     }
53     return r;
54   }
55 }
56
Popular Tags