1 10 package examples.gp.anttrail; 11 12 import org.jgap.*; 13 import org.jgap.gp.*; 14 import org.jgap.gp.impl.*; 15 16 22 public class IfFoodAheadLeft 23 extends AntCommand implements IMutateable { 24 25 private final static String CVS_REVISION = "$Revision: 1.3 $"; 26 27 private int m_lookAheadFields; 28 29 38 public IfFoodAheadLeft(final GPConfiguration a_conf) 39 throws InvalidConfigurationException { 40 this(a_conf, 1); 41 } 42 43 54 public IfFoodAheadLeft(final GPConfiguration a_conf, int a_lookAheadFields) 55 throws InvalidConfigurationException { 56 super(a_conf, 2, CommandGene.VoidClass); 57 m_lookAheadFields = a_lookAheadFields; 58 } 59 60 public CommandGene applyMutation(int index, double a_percentage) 61 throws InvalidConfigurationException { 62 IfFoodAheadRight mutant = new IfFoodAheadRight(getGPConfiguration(), 63 m_lookAheadFields); 64 return mutant; 65 } 66 67 public void execute_void(ProgramChromosome a_chrom, int a_n, Object [] a_args) { 68 AntMap map = getMap(a_chrom); 69 int x = map.getPosX(); 70 int y = map.getPosY(); 71 int orient = map.getOrientation(); 72 int cell = AntMap.ERROR; 73 switch (orient) { 74 case AntMap.O_DOWN: 75 if (y >= map.getHeight() - m_lookAheadFields || 76 x >= map.getWidth() - m_lookAheadFields) { 77 cell = AntMap.EMPTY; 78 } 79 else { 80 cell = map.getFromMap(x + m_lookAheadFields, y + m_lookAheadFields); 81 } 82 break; 83 case AntMap.O_LEFT: 84 if (x < m_lookAheadFields || y >= map.getHeight() - m_lookAheadFields) { 85 cell = AntMap.EMPTY; 86 } 87 else { 88 cell = map.getFromMap(x - m_lookAheadFields, y + m_lookAheadFields); 89 } 90 break; 91 case AntMap.O_RIGHT: 92 if (x >= map.getWidth() - m_lookAheadFields || 93 y < m_lookAheadFields) { 94 cell = AntMap.EMPTY; 95 } 96 else { 97 cell = map.getFromMap(x + m_lookAheadFields, y - m_lookAheadFields); 98 } 99 break; 100 case AntMap.O_UP: 101 if (y < m_lookAheadFields || x < m_lookAheadFields) { 102 cell = AntMap.EMPTY; 103 } 104 else { 105 cell = map.getFromMap(x - m_lookAheadFields, y - m_lookAheadFields); 106 } 107 break; 108 } 109 if (cell == AntMap.ERROR) { 110 throw new IllegalStateException ("IfFoodAheadLeft: illegal cell content"); 111 } 112 if (cell == AntMap.FOOD) { 113 a_chrom.execute_void(a_n, 0, a_args); 114 } 115 else { 116 a_chrom.execute_void(a_n, 1, a_args); 117 } 118 } 119 120 public String toString() { 121 if (m_lookAheadFields == 1) { 122 return "if-food-ahead-left (&1) else (&2)"; 123 } 124 else { 125 return "if-food-ahead-left(" + m_lookAheadFields + ") (&1) else (&2)"; 126 } 127 } 128 } 129 | Popular Tags |