1 10 package examples.gp.anttrail; 11 12 import org.jgap.*; 13 import org.jgap.gp.*; 14 import org.jgap.gp.impl.*; 15 16 24 public class TurnToFood 25 extends AntCommand implements IMutateable { 26 27 private final static String CVS_REVISION = "$Revision: 1.2 $"; 28 29 39 public TurnToFood(final GPConfiguration a_conf) 40 throws InvalidConfigurationException { 41 super(a_conf, 0, CommandGene.VoidClass); 42 } 43 44 public CommandGene applyMutation(int index, double a_percentage) 45 throws InvalidConfigurationException { 46 CommandGene mutant; 47 if (a_percentage < 0.33d) { 48 mutant = new Right(getGPConfiguration()); 49 } 50 else if (a_percentage < 0.67d) { 51 mutant = new Left(getGPConfiguration()); 52 } 53 else { 54 mutant = new Move(getGPConfiguration()); 55 } 56 return mutant; 57 } 58 59 public void execute_void(ProgramChromosome a_chrom, int a_n, Object [] a_args) { 60 AntMap map = getMap(a_chrom); 61 int x = map.getPosX(); 62 int y = map.getPosY(); 63 int orient = map.getOrientation(); 64 boolean found = false; 67 switch (orient) { 68 case AntMap.O_DOWN: 69 if (y < map.getHeight() - 1) { 70 if (map.getFromMap(x, y + 1) == AntMap.FOOD) { 71 found = true; 72 map.setPosY(y + 1); 73 } 74 } 75 break; 76 case AntMap.O_LEFT: 77 if (x >= 1) { 78 if (map.getFromMap(x-1, y ) == AntMap.FOOD) { 79 found = true; 80 map.setPosX(x - 1); 81 } 82 } 83 break; 84 case AntMap.O_RIGHT: 85 if (x < map.getWidth() - 1) { 86 if (map.getFromMap(x+1, y ) == AntMap.FOOD) { 87 found = true; 88 map.setPosX(x + 1); 89 } 90 } 91 break; 92 case AntMap.O_UP: 93 if (y >= 1) { 94 if (map.getFromMap(x, y - 1) == AntMap.FOOD) { 95 found = true; 96 map.setPosY(y - 1); 97 } 98 } 99 break; 100 } 101 if (!found) { 102 switch (orient) { 105 case AntMap.O_RIGHT: 106 if (y < map.getHeight() - 1) { 107 if (map.getFromMap(x, y + 1) == AntMap.FOOD) { 108 found = true; 109 map.setPosY(y + 1); 110 map.setOrientation(AntMap.O_DOWN); 111 } 112 } 113 break; 114 case AntMap.O_DOWN: 115 if (x >= 1) { 116 if (map.getFromMap(x-1, y ) == AntMap.FOOD) { 117 found = true; 118 map.setPosX(x - 1); 119 map.setOrientation(AntMap.O_LEFT); 120 } 121 } 122 break; 123 case AntMap.O_UP: 124 if (x < map.getWidth() - 1) { 125 if (map.getFromMap(x+1, y ) == AntMap.FOOD) { 126 found = true; 127 map.setPosX(x + 1); 128 map.setOrientation(AntMap.O_RIGHT); 129 } 130 } 131 break; 132 case AntMap.O_LEFT: 133 if (y >= 1) { 134 if (map.getFromMap(x, y - 1) == AntMap.FOOD) { 135 found = true; 136 map.setPosY(y - 1); 137 map.setOrientation(AntMap.O_UP); 138 } 139 } 140 break; 141 } 142 if (!found) { 143 switch (orient) { 146 case AntMap.O_LEFT: 147 if (y < map.getHeight() - 1) { 148 if (map.getFromMap(x, y + 1) == AntMap.FOOD) { 149 found = true; 150 map.setPosY(y + 1); 151 map.setOrientation(AntMap.O_DOWN); 152 } 153 } 154 break; 155 case AntMap.O_UP: 156 if (x >= 1) { 157 if (map.getFromMap(x-1, y ) == AntMap.FOOD) { 158 found = true; 159 map.setPosX(x - 1); 160 map.setOrientation(AntMap.O_LEFT); 161 } 162 } 163 break; 164 case AntMap.O_DOWN: 165 if (x < map.getWidth() - 1) { 166 if (map.getFromMap(x+1, y ) == AntMap.FOOD) { 167 found = true; 168 map.setPosX(x + 1); 169 map.setOrientation(AntMap.O_RIGHT); 170 } 171 } 172 break; 173 case AntMap.O_RIGHT: 174 if (y >= 1) { 175 if (map.getFromMap(x, y - 1) == AntMap.FOOD) { 176 found = true; 177 map.setPosY(y - 1); 178 map.setOrientation(AntMap.O_UP); 179 } 180 } 181 break; 182 } 183 } 184 } 185 if (found) { 186 map.IncrementMoveCounter(); 187 } 188 } 189 190 public String toString() { 191 return "turn-to-food"; 192 } 193 } 194 | Popular Tags |