KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > examples > gp > tictactoe > GameNodeValidator


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.gp.tictactoe;
11
12 import org.jgap.gp.impl.*;
13 import org.jgap.gp.*;
14 import org.jgap.gp.function.*;
15 import org.jgap.gp.terminal.*;
16
17 /**
18  * Validates evolved nodes for the Tic Tac Toe problem.
19  *
20  * @author Klaus Meffert
21  * @since 3.2
22  */

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

52   public boolean validate(ProgramChromosome a_chrom, CommandGene a_node,
53                           CommandGene a_rootNode,
54                           int a_tries, int a_num, int a_recurseLevel,
55                           Class JavaDoc a_type, CommandGene[] a_functionSet,
56                           int a_depth, boolean a_grow, int a_childIndex) {
57     // Guard to avoid endless validation.
58
// ----------------------------------
59
if (a_tries > 10) {
60       return true;
61     }
62     // Chromosome 1.
63
// -------------
64
if (a_num == 1) {
65       // Program must start with a loop.
66
// -------------------------------
67
if (a_recurseLevel == 0 && a_node.getClass() != Loop.class) {
68         return false;
69       }
70       if (a_recurseLevel == 1 && a_node.getClass() != EvaluateBoard.class) {
71         return false;
72       }
73     }
74     // Chromosome 2.
75
// -------------
76
if (a_num == 2) {
77       // SubProgram needed as root
78
if (a_recurseLevel == 0 && a_node.getClass() != SubProgram.class) {
79         return false;
80       }
81       // SubProgram forbidden other than at beginning
82
// if (a_recurseLevel > 1 && a_node.getClass() == SubProgram.class) {
83
// return false;
84
// }
85
// EvaluateBoard forbidden other than under root node and as not-first
86
// child
87
if (a_recurseLevel > 1 && a_node.getClass() == EvaluateBoard.class
88           && a_childIndex > 0) {
89         return false;
90       }
91       if (a_recurseLevel == 1 && a_childIndex == 0 &&
92           a_node.getClass() != EvaluateBoard.class) {
93         return false;
94       }
95       if (a_rootNode != null && a_rootNode.getClass() != SubProgram.class
96           && a_rootNode.getClass() != IfElse.class
97           && a_node.getClass() == IfElse.class
98           && a_childIndex <= 1) {
99         return false;
100       }
101       if (a_rootNode != null && a_rootNode.getClass() != IfElse.class &&
102           a_node.getClass() != Equals.class) {
103         return false;
104       }
105       // CountStones forbidden other than under SubProgram
106
// if ( (a_rootNode == null || a_rootNode.getClass() != SubProgram.class) &&
107
// a_node.getClass() == CountStones.class) {
108
// return false;
109
// }
110
// CountStones needed one under root
111
// if (a_recurseLevel == 1 && a_node.getClass() != CountStones.class) {
112
// return false;
113
// }
114
}
115     // Chromosome 3.
116
// -------------
117
if (a_num == 3) {
118       if (a_recurseLevel == 0 && a_node.getClass() != PutStone1.class) {
119         return false;
120       }
121       if (a_recurseLevel == 1 && a_node.getClass() != ReadTerminalIndexed.class) {
122         return false;
123       }
124     }
125     return true;
126   }
127 }
128
Popular Tags