001: /*
002: * This file is part of JGAP.
003: *
004: * JGAP offers a dual license model containing the LGPL as well as the MPL.
005: *
006: * For licensing information please see the file license.txt included with JGAP
007: * or have a look at the top of class org.jgap.Chromosome which representatively
008: * includes the JGAP license policy applicable for any file delivered with JGAP.
009: */
010: package examples.gp.tictactoe;
011:
012: import org.jgap.gp.*;
013: import org.jgap.*;
014: import org.jgap.gp.impl.*;
015:
016: /**
017: * Evaluates the board. Generates one unique number for each board position.
018: *
019: * @author Klaus Meffert
020: * @since 3.2
021: */
022: public class EvaluateBoard extends CommandGene {
023: /** String containing the CVS revision. Read out via reflection!*/
024: private final static String CVS_REVISION = "$Revision: 1.3 $";
025:
026: private Board m_board;
027:
028: private int m_index;
029:
030: private Class m_type;
031:
032: public EvaluateBoard(final GPConfiguration a_conf, Board a_board,
033: int a_index) throws InvalidConfigurationException {
034: this (a_conf, a_board, a_index, 0);
035: }
036:
037: public EvaluateBoard(final GPConfiguration a_conf, Board a_board,
038: int a_index, int a_subReturnType)
039: throws InvalidConfigurationException {
040: super (a_conf, 0, CommandGene.VoidClass, a_subReturnType, null);
041: m_board = a_board;
042: m_index = a_index;
043: }
044:
045: public EvaluateBoard(final GPConfiguration a_conf, Board a_board,
046: Class a_type) throws InvalidConfigurationException {
047: this (a_conf, a_board, a_type, 0, 0);
048: }
049:
050: public EvaluateBoard(final GPConfiguration a_conf, Board a_board,
051: Class a_type, int a_subReturnType, int a_subChildType)
052: throws InvalidConfigurationException {
053: super (a_conf, 1, CommandGene.VoidClass, a_subReturnType,
054: a_subChildType);
055: m_board = a_board;
056: m_index = -1;
057: m_type = a_type;
058: }
059:
060: public String toString() {
061: if (m_index >= 0) {
062: return "eval_board(" + m_index + ")";
063: } else {
064: return "eval_board(&1)";
065: }
066: }
067:
068: public Class getChildType(IGPProgram a_ind, int a_chromNum) {
069: if (m_index < 0) {
070: return m_type;
071: } else {
072: return null;
073: }
074: }
075:
076: /**
077: * @return textual name of this command
078: *
079: * @author Klaus Meffert
080: * @since 3.2
081: */
082: public String getName() {
083: if (m_index >= 0) {
084: return "Evaluate Board(" + m_index + ")";
085: } else {
086: return "Evaluate Board(" + m_index + ")";
087: }
088: }
089:
090: public void execute_void(ProgramChromosome c, int n, Object[] args) {
091: check(c);
092: int evaluation = 0;
093: int index = 0;
094: for (int x = 0; x < Board.WIDTH; x++) {
095: for (int y = 0; y < Board.HEIGHT; y++) {
096: // Add 1 to board value to eliminate zeros.
097: // ----------------------------------------
098: int boardValue = m_board.readField(x + 1, y + 1) + 1;
099: // Adapt base of exponentiation to get unique values.
100: // --------------------------------------------------
101: evaluation += Math.pow(3 + (index * 2), (boardValue));
102: index++;
103: }
104: }
105: int memoryIndex;
106: if (m_index < 0) {
107: memoryIndex = c.execute_int(n, 0, args);
108: /**@todo support other types than integer*/
109: } else {
110: memoryIndex = m_index;
111: }
112: getGPConfiguration().storeIndexedMemory(memoryIndex,
113: new Integer(evaluation));
114: }
115: }
|