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 IfFoodAheadElse 23 extends AntCommand { 24 25 private final static String CVS_REVISION = "$Revision: 1.6 $"; 26 27 private int m_lookAheadFields; 28 29 38 public IfFoodAheadElse(final GPConfiguration a_conf) 39 throws InvalidConfigurationException { 40 this(a_conf, 1); 41 } 42 43 54 public IfFoodAheadElse(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 void execute_void(ProgramChromosome a_chrom, int a_n, Object [] a_args) { 61 AntMap map = getMap(a_chrom); 62 int x = map.getPosX(); 63 int y = map.getPosY(); 64 int orient = map.getOrientation(); 65 int cell = AntMap.ERROR; 66 switch (orient) { 67 case AntMap.O_DOWN: 68 if (y >= map.getHeight() - m_lookAheadFields) { 69 cell = AntMap.EMPTY; 70 } 71 else { 72 cell = map.getFromMap(x, y + m_lookAheadFields); 73 } 74 break; 75 case AntMap.O_LEFT: 76 if (x < m_lookAheadFields) { 77 cell = AntMap.EMPTY; 78 } 79 else { 80 cell = map.getFromMap(x - m_lookAheadFields, y); 81 } 82 break; 83 case AntMap.O_RIGHT: 84 if (x >= map.getWidth() - m_lookAheadFields) { 85 cell = AntMap.EMPTY; 86 } 87 else { 88 cell = map.getFromMap(x + m_lookAheadFields, y); 89 } 90 break; 91 case AntMap.O_UP: 92 if (y < m_lookAheadFields) { 93 cell = AntMap.EMPTY; 94 } 95 else { 96 cell = map.getFromMap(x, y - m_lookAheadFields); 97 } 98 break; 99 } 100 if (cell == AntMap.ERROR) { 101 throw new IllegalStateException ("IfFoodAheadElse: illegal cell content"); 102 } 103 if (cell == AntMap.FOOD) { 104 a_chrom.execute_void(a_n, 0, a_args); 105 } 106 else { 107 a_chrom.execute_void(a_n, 1, a_args); 108 } 109 } 110 111 public String toString() { 112 if (m_lookAheadFields == 1) { 113 return "if-food (&1) else (&2)"; 114 } 115 else { 116 return "if-food(" + m_lookAheadFields + ") (&1) else (&2)"; 117 } 118 } 119 } 120 | Popular Tags |