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 IfFoodAheadRight 23 extends AntCommand { 24 25 private final static String CVS_REVISION = "$Revision: 1.3 $"; 26 27 private int m_lookAheadFields; 28 29 38 public IfFoodAheadRight(final GPConfiguration a_conf) 39 throws InvalidConfigurationException { 40 this(a_conf, 1); 41 } 42 43 54 public IfFoodAheadRight(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 IfFoodAheadLeft mutant = new IfFoodAheadLeft(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 || x < m_lookAheadFields) { 76 cell = AntMap.EMPTY; 77 } 78 else { 79 cell = map.getFromMap(x - m_lookAheadFields, y + m_lookAheadFields); 80 } 81 break; 82 case AntMap.O_LEFT: 83 if (x < m_lookAheadFields || y < m_lookAheadFields) { 84 cell = AntMap.EMPTY; 85 } 86 else { 87 cell = map.getFromMap(x - m_lookAheadFields, y - m_lookAheadFields); 88 } 89 break; 90 case AntMap.O_RIGHT: 91 if (x >= map.getWidth() - m_lookAheadFields || 92 y >= map.getHeight() - m_lookAheadFields) { 93 cell = AntMap.EMPTY; 94 } 95 else { 96 cell = map.getFromMap(x + m_lookAheadFields, y + m_lookAheadFields); 97 } 98 break; 99 case AntMap.O_UP: 100 if (y < m_lookAheadFields || x >= map.getWidth() - m_lookAheadFields) { 101 cell = AntMap.EMPTY; 102 } 103 else { 104 cell = map.getFromMap(x + m_lookAheadFields, y - m_lookAheadFields); 105 } 106 break; 107 } 108 if (cell == AntMap.ERROR) { 109 throw new IllegalStateException ("IfFoodAheadRight: illegal cell content"); 110 } 111 if (cell == AntMap.FOOD) { 112 a_chrom.execute_void(a_n, 0, a_args); 113 } 114 else { 115 a_chrom.execute_void(a_n, 1, a_args); 116 } 117 } 118 119 public String toString() { 120 if (m_lookAheadFields == 1) { 121 return "if-food-ahead-right (&1) else (&2)"; 122 } 123 else { 124 return "if-food-ahead-right(" + m_lookAheadFields + ") (&1) else (&2)"; 125 } 126 } 127 } 128 | Popular Tags |