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 to the right then execute <child 0> else execute <child 1>.
018: *
019: * @author Klaus Meffert
020: * @since 3.01
021: */
022: public class IfFoodAheadRight extends AntCommand {
023: /** String containing the CVS revision. Read out via reflection!*/
024: private final static String CVS_REVISION = "$Revision: 1.4 $";
025:
026: private int m_lookAheadFields;
027:
028: /**
029: * Looks ahead 1 field to the right.
030: *
031: * @param a_conf the configuration to use
032: * @throws InvalidConfigurationException
033: *
034: * @author Klaus Meffert
035: * @since 3.01
036: */
037: public IfFoodAheadRight(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 to the right.
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 IfFoodAheadRight(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 CommandGene applyMutation(int index, double a_percentage)
060: throws InvalidConfigurationException {
061: IfFoodAheadLeft mutant = new IfFoodAheadLeft(
062: getGPConfiguration(), m_lookAheadFields);
063: return mutant;
064: }
065:
066: public void execute_void(ProgramChromosome a_chrom, int a_n,
067: Object[] a_args) {
068: AntMap map = getMap(a_chrom);
069: int x = map.getPosX();
070: int y = map.getPosY();
071: int orient = map.getOrientation();
072: int cell = AntMap.ERROR;
073: switch (orient) {
074: case AntMap.O_DOWN:
075: if (y >= map.getHeight() - m_lookAheadFields
076: || x < m_lookAheadFields) {
077: cell = AntMap.EMPTY;
078: } else {
079: cell = map.getFromMap(x - m_lookAheadFields, y
080: + m_lookAheadFields);
081: }
082: break;
083: case AntMap.O_LEFT:
084: if (x < m_lookAheadFields || y < m_lookAheadFields) {
085: cell = AntMap.EMPTY;
086: } else {
087: cell = map.getFromMap(x - m_lookAheadFields, y
088: - m_lookAheadFields);
089: }
090: break;
091: case AntMap.O_RIGHT:
092: if (x >= map.getWidth() - m_lookAheadFields
093: || y >= map.getHeight() - m_lookAheadFields) {
094: cell = AntMap.EMPTY;
095: } else {
096: cell = map.getFromMap(x + m_lookAheadFields, y
097: + m_lookAheadFields);
098: }
099: break;
100: case AntMap.O_UP:
101: if (y < m_lookAheadFields
102: || x >= map.getWidth() - m_lookAheadFields) {
103: cell = AntMap.EMPTY;
104: } else {
105: cell = map.getFromMap(x + m_lookAheadFields, y
106: - m_lookAheadFields);
107: }
108: break;
109: }
110: if (cell == AntMap.ERROR) {
111: throw new IllegalStateException(
112: "IfFoodAheadRight: illegal cell content");
113: }
114: if (cell == AntMap.FOOD) {
115: a_chrom.execute_void(a_n, 0, a_args);
116: } else {
117: a_chrom.execute_void(a_n, 1, a_args);
118: }
119: }
120:
121: public String toString() {
122: if (m_lookAheadFields == 1) {
123: return "if-food-ahead-right (&1) else (&2)";
124: } else {
125: return "if-food-ahead-right(" + m_lookAheadFields
126: + ") (&1) else (&2)";
127: }
128: }
129: }
|