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.anttrail;
011:
012: import org.jgap.*;
013: import org.jgap.gp.*;
014: import org.jgap.gp.impl.*;
015:
016: /**
017: * If food-ahead then execute <child 0> else execute <child 1>.
018: *
019: * @author Klaus Meffert
020: * @since 3.01
021: */
022: public class IfFoodAheadElse extends AntCommand {
023: /** String containing the CVS revision. Read out via reflection!*/
024: private final static String CVS_REVISION = "$Revision: 1.7 $";
025:
026: private int m_lookAheadFields;
027:
028: /**
029: * Standard constructor for the classic ant problem. Looks ahead 1 field.
030: *
031: * @param a_conf the configuration to use
032: * @throws InvalidConfigurationException
033: *
034: * @author Klaus Meffert
035: * @since 3.01
036: */
037: public IfFoodAheadElse(final GPConfiguration a_conf)
038: throws InvalidConfigurationException {
039: this (a_conf, 1);
040: }
041:
042: /**
043: * Allows to specify how many fields to look ahead.
044: *
045: * @param a_conf the configuration to use
046: * @param a_lookAheadFields look ahead n fields
047: *
048: * @throws InvalidConfigurationException
049: *
050: * @author Klaus Meffert
051: * @since 3.01
052: */
053: public IfFoodAheadElse(final GPConfiguration a_conf,
054: int a_lookAheadFields) throws InvalidConfigurationException {
055: super (a_conf, 2, CommandGene.VoidClass);
056: m_lookAheadFields = a_lookAheadFields;
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: int cell = AntMap.ERROR;
066: switch (orient) {
067: case AntMap.O_DOWN:
068: if (y >= map.getHeight() - m_lookAheadFields) {
069: cell = AntMap.EMPTY;
070: } else {
071: cell = map.getFromMap(x, y + m_lookAheadFields);
072: }
073: break;
074: case AntMap.O_LEFT:
075: if (x < m_lookAheadFields) {
076: cell = AntMap.EMPTY;
077: } else {
078: cell = map.getFromMap(x - m_lookAheadFields, y);
079: }
080: break;
081: case AntMap.O_RIGHT:
082: if (x >= map.getWidth() - m_lookAheadFields) {
083: cell = AntMap.EMPTY;
084: } else {
085: cell = map.getFromMap(x + m_lookAheadFields, y);
086: }
087: break;
088: case AntMap.O_UP:
089: if (y < m_lookAheadFields) {
090: cell = AntMap.EMPTY;
091: } else {
092: cell = map.getFromMap(x, y - m_lookAheadFields);
093: }
094: break;
095: }
096: if (cell == AntMap.ERROR) {
097: throw new IllegalStateException(
098: "IfFoodAheadElse: illegal cell content");
099: }
100: if (cell == AntMap.FOOD) {
101: a_chrom.execute_void(a_n, 0, a_args);
102: } else {
103: a_chrom.execute_void(a_n, 1, a_args);
104: }
105: }
106:
107: public String toString() {
108: if (m_lookAheadFields == 1) {
109: return "if-food (&1) else (&2)";
110: } else {
111: return "if-food(" + m_lookAheadFields + ") (&1) else (&2)";
112: }
113: }
114: }
|