01: /*
02: * This file is part of JGAP.
03: *
04: * JGAP offers a dual license model containing the LGPL as well as the MPL.
05: *
06: * For licensing information please see the file license.txt included with JGAP
07: * or have a look at the top of class org.jgap.Chromosome which representatively
08: * includes the JGAP license policy applicable for any file delivered with JGAP.
09: */
10: package examples.gp.tictactoe;
11:
12: import org.jgap.*;
13: import org.jgap.gp.*;
14: import org.jgap.gp.impl.*;
15:
16: /**
17: * The if-then construct. If-Condition is: if specific color, then
18: * execute X else do nothing.
19: *
20: * @author Klaus Meffert
21: * @since 3.2
22: */
23: public class IfIsFree extends CommandGene {
24: /** String containing the CVS revision. Read out via reflection!*/
25: private final static String CVS_REVISION = "$Revision: 1.2 $";
26:
27: private Class m_type;
28:
29: private Board m_board;
30:
31: public IfIsFree(final GPConfiguration a_conf, Board a_board,
32: Class a_type) throws InvalidConfigurationException {
33: this (a_conf, a_board, a_type, 0, null);
34: }
35:
36: public IfIsFree(final GPConfiguration a_conf, Board a_board,
37: Class a_type, int a_subReturnType, int[] a_subChildTypes)
38: throws InvalidConfigurationException {
39: super (a_conf, 3, CommandGene.VoidClass, a_subReturnType,
40: a_subChildTypes);
41: m_type = a_type;
42: m_board = a_board;
43: }
44:
45: public String toString() {
46: return "if free(&1, &2) then (&3)";
47: }
48:
49: /**
50: * @return textual name of this command
51: *
52: * @author Klaus Meffert
53: * @since 3.2
54: */
55: public String getName() {
56: return "If Free";
57: }
58:
59: public void execute_void(ProgramChromosome c, int n, Object[] args) {
60: check(c);
61: boolean condition;
62: if (m_type == CommandGene.IntegerClass) {
63: int x = c.execute_int(n, 0, args);
64: if (x < 0 || x >= Board.WIDTH) {
65: throw new IllegalStateException("x must be 0.."
66: + Board.WIDTH);
67: }
68: int y = c.execute_int(n, 1, args);
69: if (y < 0 || x >= Board.HEIGHT) {
70: throw new IllegalStateException("y must be 0.."
71: + Board.HEIGHT);
72: }
73: condition = m_board.readField(x, y) == 0;
74: } else {
75: throw new IllegalStateException(
76: "IfIsFree: cannot process type " + m_type);
77: }
78: if (condition) {
79: c.execute_void(n, 2, args);
80: }
81: }
82:
83: /**
84: * Determines which type a specific child of this command has.
85: *
86: * @param a_ind ignored here
87: * @param a_chromNum index of child
88: * @return type of the a_chromNum'th child
89: *
90: * @author Klaus Meffert
91: * @since 3.2
92: */
93: public Class getChildType(IGPProgram a_ind, int a_chromNum) {
94: if (a_chromNum == 0 || a_chromNum == 1) {
95: return m_type;
96: }
97: return CommandGene.VoidClass;
98: }
99: }
|