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.*;
013: import org.jgap.gp.*;
014: import org.jgap.gp.impl.*;
015:
016: /**
017: * Look ahead, right and left (in this order) and turns to food in case such was
018: * detected. Does nothing in the other case.<p>
019: * This command is not part of the classic ant problem.
020: *
021: * @author Klaus Meffert
022: * @since 3.01
023: */
024: public class TurnToFood extends AntCommand implements IMutateable {
025: /** String containing the CVS revision. Read out via reflection!*/
026: private final static String CVS_REVISION = "$Revision: 1.2 $";
027:
028: /**
029: * Constructor.
030: *
031: * @param a_conf the configuration to use
032: *
033: * @throws InvalidConfigurationException
034: *
035: * @author Klaus Meffert
036: * @since 3.01
037: */
038: public TurnToFood(final GPConfiguration a_conf)
039: throws InvalidConfigurationException {
040: super (a_conf, 0, CommandGene.VoidClass);
041: }
042:
043: public CommandGene applyMutation(int index, double a_percentage)
044: throws InvalidConfigurationException {
045: CommandGene mutant;
046: if (a_percentage < 0.33d) {
047: mutant = new Right(getGPConfiguration());
048: } else if (a_percentage < 0.67d) {
049: mutant = new Left(getGPConfiguration());
050: } else {
051: mutant = new Move(getGPConfiguration());
052: }
053: return mutant;
054: }
055:
056: public void execute_void(ProgramChromosome a_chrom, int a_n,
057: Object[] a_args) {
058: AntMap map = getMap(a_chrom);
059: int x = map.getPosX();
060: int y = map.getPosY();
061: int orient = map.getOrientation();
062: // Look ahead for food.
063: // --------------------
064: boolean found = false;
065: switch (orient) {
066: case AntMap.O_DOWN:
067: if (y < map.getHeight() - 1) {
068: if (map.getFromMap(x, y + 1) == AntMap.FOOD) {
069: found = true;
070: map.setPosY(y + 1);
071: }
072: }
073: break;
074: case AntMap.O_LEFT:
075: if (x >= 1) {
076: if (map.getFromMap(x - 1, y) == AntMap.FOOD) {
077: found = true;
078: map.setPosX(x - 1);
079: }
080: }
081: break;
082: case AntMap.O_RIGHT:
083: if (x < map.getWidth() - 1) {
084: if (map.getFromMap(x + 1, y) == AntMap.FOOD) {
085: found = true;
086: map.setPosX(x + 1);
087: }
088: }
089: break;
090: case AntMap.O_UP:
091: if (y >= 1) {
092: if (map.getFromMap(x, y - 1) == AntMap.FOOD) {
093: found = true;
094: map.setPosY(y - 1);
095: }
096: }
097: break;
098: }
099: if (!found) {
100: // Look right for food.
101: // --------------------
102: switch (orient) {
103: case AntMap.O_RIGHT:
104: if (y < map.getHeight() - 1) {
105: if (map.getFromMap(x, y + 1) == AntMap.FOOD) {
106: found = true;
107: map.setPosY(y + 1);
108: map.setOrientation(AntMap.O_DOWN);
109: }
110: }
111: break;
112: case AntMap.O_DOWN:
113: if (x >= 1) {
114: if (map.getFromMap(x - 1, y) == AntMap.FOOD) {
115: found = true;
116: map.setPosX(x - 1);
117: map.setOrientation(AntMap.O_LEFT);
118: }
119: }
120: break;
121: case AntMap.O_UP:
122: if (x < map.getWidth() - 1) {
123: if (map.getFromMap(x + 1, y) == AntMap.FOOD) {
124: found = true;
125: map.setPosX(x + 1);
126: map.setOrientation(AntMap.O_RIGHT);
127: }
128: }
129: break;
130: case AntMap.O_LEFT:
131: if (y >= 1) {
132: if (map.getFromMap(x, y - 1) == AntMap.FOOD) {
133: found = true;
134: map.setPosY(y - 1);
135: map.setOrientation(AntMap.O_UP);
136: }
137: }
138: break;
139: }
140: if (!found) {
141: // Look left for food.
142: // -------------------
143: switch (orient) {
144: case AntMap.O_LEFT:
145: if (y < map.getHeight() - 1) {
146: if (map.getFromMap(x, y + 1) == AntMap.FOOD) {
147: found = true;
148: map.setPosY(y + 1);
149: map.setOrientation(AntMap.O_DOWN);
150: }
151: }
152: break;
153: case AntMap.O_UP:
154: if (x >= 1) {
155: if (map.getFromMap(x - 1, y) == AntMap.FOOD) {
156: found = true;
157: map.setPosX(x - 1);
158: map.setOrientation(AntMap.O_LEFT);
159: }
160: }
161: break;
162: case AntMap.O_DOWN:
163: if (x < map.getWidth() - 1) {
164: if (map.getFromMap(x + 1, y) == AntMap.FOOD) {
165: found = true;
166: map.setPosX(x + 1);
167: map.setOrientation(AntMap.O_RIGHT);
168: }
169: }
170: break;
171: case AntMap.O_RIGHT:
172: if (y >= 1) {
173: if (map.getFromMap(x, y - 1) == AntMap.FOOD) {
174: found = true;
175: map.setPosY(y - 1);
176: map.setOrientation(AntMap.O_UP);
177: }
178: }
179: break;
180: }
181: }
182: }
183: if (found) {
184: map.IncrementMoveCounter();
185: }
186: }
187:
188: public String toString() {
189: return "turn-to-food";
190: }
191: }
|