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 licencing 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.anttrail;
011:
012: import org.jgap.gp.impl.*;
013: import org.jgap.*;
014:
015: /**
016: * Move the ant. Allows to specify how many times in a row a move will be
017: * executed. The classic ant problem uses 1 as parameter value.
018: *
019: * @author Klaus Meffert
020: * @since 3.01
021: */
022: public class Move extends AntCommand {
023: /** String containing the CVS revision. Read out via reflection!*/
024: private final static String CVS_REVISION = "$Revision: 1.3 $";
025:
026: private int m_moves;
027:
028: /**
029: * Standard constructor for classic ant problem.
030: *
031: * @param a_conf the configuration to use
032: * @throws InvalidConfigurationException
033: *
034: * @author Klaus Meffert
035: * @since 3.01
036: */
037: public Move(final GPConfiguration a_conf)
038: throws InvalidConfigurationException {
039: this (a_conf, 1);
040: }
041:
042: /**
043: * Allows to move more than one time in a row.
044: *
045: * @param a_conf the configuration to use
046: * @param a_moves number of moves to execute in a row
047: *
048: * @throws InvalidConfigurationException
049: *
050: * @author Klaus Meffert
051: * @since 3.01
052: */
053: public Move(final GPConfiguration a_conf, int a_moves)
054: throws InvalidConfigurationException {
055: super (a_conf);
056: m_moves = a_moves;
057: }
058:
059: public void execute_void(ProgramChromosome a_chrom, int a_n,
060: Object[] a_args) {
061: AntMap map = getMap(a_chrom);
062: int x = map.getPosX();
063: int y = map.getPosY();
064: int orient = map.getOrientation();
065: for (int i = 0; i < m_moves; i++) {
066: switch (orient) {
067: case AntMap.O_DOWN:
068: y++;
069: if (y >= map.getHeight()) {
070: throw new IllegalStateException(
071: "y bigger than height");
072: }
073: map.setPosY(y);
074: break;
075: case AntMap.O_LEFT:
076: x--;
077: if (x < 0) {
078: throw new IllegalStateException("x smaller zero");
079: }
080: map.setPosX(x);
081: break;
082: case AntMap.O_RIGHT:
083: x++;
084: if (x >= map.getWidth()) {
085: throw new IllegalStateException(
086: "x bigger than width");
087: }
088: map.setPosX(x);
089: break;
090: case AntMap.O_UP:
091: y--;
092: if (y < 0) {
093: throw new IllegalStateException("y smaller zero");
094: }
095: map.setPosY(y);
096: break;
097: default:
098: throw new IllegalStateException("Illegal orientation");
099: }
100: map.IncrementMoveCounter();
101: }
102: }
103:
104: public String toString() {
105: if (m_moves == 1) {
106: return "move";
107: } else {
108: return "move" + m_moves;
109: }
110: }
111: }
|