1 10 package examples.gp.tictactoe; 11 12 import org.jgap.gp.*; 13 import org.jgap.*; 14 import org.jgap.gp.impl.*; 15 16 22 public class EvaluateBoard 23 extends CommandGene { 24 25 private final static String CVS_REVISION = "$Revision: 1.2 $"; 26 27 private Board m_board; 28 29 private int m_index; 30 31 private Class 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 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 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 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 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 86 public String 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 [] 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 int boardValue = m_board.readField(x + 1, y + 1) + 1; 104 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 114 } 115 else { 116 memoryIndex = m_index; 117 } 118 getGPConfiguration().storeIndexedMemory(memoryIndex, new Integer (evaluation)); 119 } 120 } 121 | Popular Tags |