KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
13 import org.jgap.*;
14 import org.jgap.gp.impl.*;
15
16 /**
17  * Evaluates the board. Generates one unique number for each board position.
18  *
19  * @author Klaus Meffert
20  * @since 3.2
21  */

22 public class EvaluateBoard
23     extends CommandGene {
24   /** String containing the CVS revision. Read out via reflection!*/
25   private final static String JavaDoc CVS_REVISION = "$Revision: 1.2 $";
26
27   private Board m_board;
28
29   private int m_index;
30
31   private Class JavaDoc m_type;
32
33   public EvaluateBoard(final GPConfiguration a_conf, Board a_board,
34                        int a_index)
35       throws InvalidConfigurationException {
36     this(a_conf, a_board, a_index, 0);
37   }
38
39   public EvaluateBoard(final GPConfiguration a_conf, Board a_board,
40                        int a_index, int a_subReturnType)
41       throws InvalidConfigurationException {
42     super(a_conf, 0, CommandGene.VoidClass, a_subReturnType, null);
43     m_board = a_board;
44     m_index = a_index;
45   }
46
47   public EvaluateBoard(final GPConfiguration a_conf, Board a_board,
48                        Class JavaDoc a_type)
49       throws InvalidConfigurationException {
50     this(a_conf, a_board, a_type, 0, 0);
51   }
52
53   public EvaluateBoard(final GPConfiguration a_conf, Board a_board,
54                        Class JavaDoc a_type, int a_subReturnType, int a_subChildType)
55       throws InvalidConfigurationException {
56     super(a_conf, 1, CommandGene.VoidClass, a_subReturnType, a_subChildType);
57     m_board = a_board;
58     m_index = -1;
59     m_type = a_type;
60   }
61
62   public String JavaDoc toString() {
63     if (m_index >= 0) {
64       return "eval_board(" + m_index + ")";
65     }
66     else {
67       return "eval_board(&1)";
68     }
69   }
70
71   public Class JavaDoc getChildType(IGPProgram a_ind, int a_chromNum) {
72     if (m_index < 0) {
73       return m_type;
74     }
75     else {
76       return null;
77     }
78   }
79
80   /**
81    * @return textual name of this command
82    *
83    * @author Klaus Meffert
84    * @since 3.2
85    */

86   public String JavaDoc getName() {
87     if (m_index >= 0) {
88       return "Evaluate Board(" + m_index + ")";
89     }
90     else {
91       return "Evaluate Board(" + m_index + ")";
92     }
93   }
94
95   public void execute_void(ProgramChromosome c, int n, Object JavaDoc[] args) {
96     check(c);
97     int evaluation = 0;
98     int index = 0;
99     for (int x = 0; x < Board.WIDTH; x++) {
100       for (int y = 0; y < Board.HEIGHT; y++) {
101         // Add 1 to board value to eliminate zeros.
102
// ----------------------------------------
103
int boardValue = m_board.readField(x + 1, y + 1) + 1;
104         // Adapt base of exponentiation to get unique values.
105
// --------------------------------------------------
106
evaluation += Math.pow(3 + (index * 2), (boardValue));
107         index++;
108       }
109     }
110     int memoryIndex;
111     if (m_index < 0) {
112       memoryIndex = c.execute_int(n, 0, args);
113       /**@todo support other types than integer*/
114     }
115     else {
116       memoryIndex = m_index;
117     }
118     getGPConfiguration().storeIndexedMemory(memoryIndex, new Integer JavaDoc(evaluation));
119   }
120 }
121
Popular Tags