01: /*
02: * This file is part of JGAP.
03: *
04: * JGAP offers a dual license model containing the LGPL as well as the MPL.
05: *
06: * For licencing information please see the file license.txt included with JGAP
07: * or have a look at the top of class org.jgap.Chromosome which representatively
08: * includes the JGAP license policy applicable for any file delivered with JGAP.
09: */
10: package examples.distinctGenes;
11:
12: import org.jgap.*;
13: import org.jgap.impl.*;
14:
15: /**
16: * Fitness function for our example. It's without sense, it's just to
17: * demonstrate how to evaluate a chromosome with 40 4-field-genes and one
18: * 3-field gene. Each toplevel-gene is a CompositeGene, each "field" within
19: * a CompositeGene is a BooleanGene here (arbitrarily chosen).
20: *
21: * @author Klaus Meffert
22: * @since 3.0
23: */
24: public class SampleFitnessFunction extends FitnessFunction {
25: /** String containing the CVS revision. Read out via reflection!*/
26: private final static String CVS_REVISION = "$Revision: 1.2 $";
27:
28: /**
29: * Calculate the fitness value of a Chromosome.
30: * @param a_subject the Chromosome to be evaluated
31: * @return defect rate of our problem (the smaller the better)
32: *
33: * @author Klaus Meffert
34: * @since 3.0
35: */
36: public double evaluate(IChromosome a_subject) {
37: int total = 0;
38: // Process the first 40 genes with 4 fields per gene
39: for (int i = 0; i < a_subject.size() - 1; i++) {
40: CompositeGene gene = (CompositeGene) a_subject.getGene(i);
41: for (int j = 0; j < 4; j++) {
42: // Now evaluate the fields within the gene somehow (here: just an
43: // example without sense).
44: // --------------------------------------------------------------
45: BooleanGene field = (BooleanGene) gene.geneAt(j);
46: if (field.booleanValue() == true) {
47: // A field with value true is seen as defect
48: total++;
49: }
50: }
51: }
52: // Process the last gene with 3 fields. Here, a field with value false
53: // is seen as defect (in opposition to the other genes)
54: CompositeGene gene = (CompositeGene) a_subject
55: .getGene(a_subject.size() - 1);
56: for (int j = 0; j < 3; j++) {
57: BooleanGene field = (BooleanGene) gene.geneAt(j);
58: if (field.booleanValue() == false) {
59: // A field with value false is seen as defect
60: total++;
61: }
62: }
63: return total;
64: }
65: }
|