KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > examples > gp > FibonacciNodeValidator


1 package examples.gp;
2
3 import org.jgap.gp.impl.*;
4 import org.jgap.gp.*;
5 import org.jgap.gp.function.*;
6 import org.jgap.gp.terminal.*;
7
8 /**
9  * Validates evolved nodes for the Fibonacci problem. This is for
10  * demonstrating how the node validator works.
11  *
12  * @author Klaus Meffert
13  * @since 3.0
14  */

15 public class FibonacciNodeValidator
16     implements INodeValidator {
17   /**
18    * Validates a_node in the context of a_chrom during evolution. Considers the
19    * recursion level (a_recursLevel), the type needed (a_type) for the node, the
20    * functions available (a_functionSet) and the depth of the whole chromosome
21    * needed (a_depth), and whether grow mode is used (a_grow is true) or not.
22    *
23    * @param a_chrom the chromosome that will contain the node, if valid (ignored
24    * in this implementation)
25    * @param a_node the node selected and to be validated
26    * @param a_rootNode the root node of a_node, may be null for top nodes
27    * @param a_tries number of times the validator has been called, useful for
28    * stopping by returning true if the number exceeds a limit
29    * @param a_num the chromosome's index in the individual of this chromosome
30    * @param a_recurseLevel level of recursion
31    * @param a_type the return type of the node needed
32    * @param a_functionSet the array of available functions (ignored in this
33    * implementation)
34    * @param a_depth the needed depth of the program chromosome
35    * @param a_grow true: use grow mode, false: use full mode (ignored in this
36    * implementation)
37    * @param a_childIndex index of the child in the parent node to which it
38    * belongs (-1 if node is root node)
39    * @return true: node is valid; false: node is invalid
40    *
41    * @author Klaus Meffert
42    * @since 3.0
43    */

44   public boolean validate(ProgramChromosome a_chrom, CommandGene a_node,
45                           CommandGene a_rootNode,
46                           int a_tries, int a_num, int a_recurseLevel,
47                           Class JavaDoc a_type, CommandGene[] a_functionSet,
48                           int a_depth, boolean a_grow, int a_childIndex) {
49     // Guard to avoid endless validation.
50
// ----------------------------------
51
if (a_tries > 10) {
52       return true;
53     }
54     // Chromosome 0.
55
// -------------
56
if (a_num == 0) {
57       // SubProgram forbidden other than as root
58
if (a_recurseLevel > 0 && a_node.getClass() == SubProgram.class) {
59         return false;
60       }
61       if (a_recurseLevel == 0 && a_node.getClass() != SubProgram.class) {
62         return false;
63       }
64       if (a_recurseLevel == 1 && a_node.getClass() != StoreTerminal.class) {
65         return false;
66       }
67     }
68     // Chromosome 1.
69
// -------------
70
if (a_num == 1) {
71       // ForLoop forbidden under root node
72
if (a_recurseLevel > 0 && a_node.getClass() == ForLoop.class) {
73         return false;
74       }
75       // ForLoop needed as root
76
if (a_recurseLevel == 0 && a_node.getClass() != ForLoop.class) {
77         return false;
78       }
79       // Variable forbidden other than directly under root
80
if (a_recurseLevel > 1 && a_node.getClass() == Variable.class) {
81         return false;
82       }
83       // Variable needed directly under root
84
if (a_recurseLevel == 1 && a_depth == 1
85           && a_node.getClass() != Variable.class) {
86         return false;
87       }
88       // SubProgram forbidden other than directly under root
89
if (a_recurseLevel > 1 && a_depth > 1
90           && a_node.getClass() == SubProgram.class) {
91         return false;
92       }
93       // SubProgram needed directly under root
94
if (a_recurseLevel == 1 && a_depth > 1 && a_type == CommandGene.VoidClass
95           && a_node.getClass() != SubProgram.class) {
96         return false;
97       }
98       // AddAndStore or TransferMemory needed 2 under root
99
if (a_recurseLevel == 2 && a_depth > 1 && a_type == CommandGene.VoidClass
100           && a_node.getClass() != AddAndStore.class
101           && a_node.getClass() != TransferMemory.class) {
102         return false;
103       }
104       // AddAndStore or TransferMemory forbidden other than 2 under root
105
if (a_recurseLevel != 2 && a_depth > 1 && a_type == CommandGene.VoidClass
106           && (a_node.getClass() == AddAndStore.class
107               || a_node.getClass() == TransferMemory.class)) {
108         return false;
109       }
110     }
111     return true;
112   }
113 }
114
Popular Tags