1 10 package org.jgap.gp.function; 11 12 import org.jgap.*; 13 import org.jgap.gp.*; 14 import org.apache.commons.lang.builder.*; 15 import org.jgap.gp.impl.*; 16 17 23 public class ForLoop 24 extends CommandGene { 25 26 private final static String CVS_REVISION = "$Revision: 1.13 $"; 27 28 private static String INTERNAL_COUNTER_STORAGE = "FORLOOPSTORAGE_INT"; 29 30 private Class m_typeVar; 31 32 private int m_startIndex; 33 34 private int m_endIndex; 35 36 private int m_increment; 37 38 private int m_maxLoop; 39 40 private String m_memory_name_int; 41 42 private String m_varName; 43 44 55 public ForLoop(final GPConfiguration a_conf, Class a_typeVar, int a_maxLoop) 56 throws InvalidConfigurationException { 57 this(a_conf, a_typeVar, 0, a_maxLoop); 58 } 59 60 72 public ForLoop(final GPConfiguration a_conf, Class a_typeVar, 73 int a_startIndex, int a_maxLoop) 74 throws InvalidConfigurationException { 75 this(a_conf, a_typeVar, a_startIndex, a_maxLoop, "i"); 76 } 77 78 public ForLoop(final GPConfiguration a_conf, Class a_typeVar, 79 int a_startIndex, int a_maxLoop, String a_varName) 80 throws InvalidConfigurationException { 81 super(a_conf, 2, CommandGene.VoidClass); 82 m_typeVar = a_typeVar; 83 m_maxLoop = a_maxLoop; 84 m_startIndex = a_startIndex; 85 m_endIndex = -1; 86 m_increment = 1; 87 m_varName = a_varName; 88 init(); 89 } 90 91 106 public ForLoop(final GPConfiguration a_conf, Class a_typeVar, 107 int a_startIndex, int a_endIndex, int a_increment, 108 String a_varName) 109 throws InvalidConfigurationException { 110 this(a_conf, a_typeVar, a_startIndex, a_endIndex, a_increment, a_varName, 0, 111 0); 112 } 113 114 public ForLoop(final GPConfiguration a_conf, Class a_typeVar, 115 int a_startIndex, int a_endIndex, int a_increment, 116 String a_varName, int a_subReturnType, int a_subChildType) 117 throws InvalidConfigurationException { 118 super(a_conf, 1, CommandGene.VoidClass, a_subReturnType, a_subChildType); 119 m_typeVar = a_typeVar; 120 m_increment = a_increment; 121 m_startIndex = a_startIndex; 122 m_endIndex = a_endIndex; 123 m_varName = a_varName; 124 init(); 125 } 126 127 protected void init() { 128 super.init(); 129 m_memory_name_int = INTERNAL_COUNTER_STORAGE; 132 m_memory_name_int += m_varName; 133 m_memory_name_int += getGPConfiguration().getRandomGenerator().nextDouble(); 134 } 135 136 public String toString() { 137 if (m_endIndex == -1) { 138 return "for(int i=" + m_startIndex + ";i<&1;i++) { &2 }"; 139 } 140 else { 141 String incrString; 142 if (m_increment == 1) { 143 incrString = m_varName + "++"; 144 } 145 else { 146 incrString = m_varName + "=" + m_varName + "+1"; 147 } 148 return "for(int " + m_varName + "=" + m_startIndex + ";" + m_varName + 149 "<" + m_endIndex + ";" + 150 incrString + ") { &1 }"; 151 } 152 } 153 154 160 public String getName() { 161 return "ForLoop"; 162 } 163 164 public void execute_void(ProgramChromosome c, int n, Object [] args) { 165 int x; 168 if (m_endIndex == -1) { 169 if (m_typeVar == CommandGene.IntegerClass) { 170 x = c.execute_int(n, 0, args); 171 } 172 else if (m_typeVar == CommandGene.LongClass) { 173 x = (int) c.execute_long(n, 0, args); 174 } 175 else if (m_typeVar == CommandGene.DoubleClass) { 176 x = (int) Math.round(c.execute_double(n, 0, args)); 177 } 178 else if (m_typeVar == CommandGene.FloatClass) { 179 x = (int) Math.round(c.execute_float(n, 0, args)); 180 } 181 else { 182 throw new RuntimeException ("Type " 183 + m_typeVar 184 + " not supported by ForLoop"); 185 } 186 if (x > m_maxLoop) { 187 x = m_maxLoop; 188 } 189 for (int i = m_startIndex; i < x; i++) { 192 c.execute_void(n, 1, args); 193 } 194 } 195 else { 196 for (int i = m_startIndex; i < m_endIndex; i = i + m_increment) { 199 getGPConfiguration().storeInMemory(ForLoop.INTERNAL_COUNTER_STORAGE, 202 new Integer (i)); 203 c.execute_void(n, 0, args); 204 } 205 } 206 } 207 208 public boolean isValid(ProgramChromosome a_program) { 209 return true; 210 } 211 212 public Class getChildType(IGPProgram a_ind, int a_chromNum) { 213 if (m_endIndex == -1) { 214 if (a_chromNum == 0) { 216 return m_typeVar; 219 } 220 else { 221 return CommandGene.VoidClass; 224 } 225 } 226 else { 227 return CommandGene.VoidClass; 229 } 230 } 231 232 241 public int compareTo(Object a_other) { 242 if (a_other == null) { 243 return 1; 244 } 245 else { 246 ForLoop other = (ForLoop) a_other; 247 return new CompareToBuilder() 248 .append(m_typeVar, other.m_typeVar) 249 .append(m_maxLoop, other.m_maxLoop) 250 .toComparison(); 251 } 252 } 253 254 263 public boolean equals(Object a_other) { 264 if (a_other == null) { 265 return false; 266 } 267 else { 268 try { 269 ForLoop other = (ForLoop) a_other; 270 return new EqualsBuilder() 271 .append(m_typeVar, other.m_typeVar) 272 .append(m_maxLoop, other.m_maxLoop) 273 .isEquals(); 274 } catch (ClassCastException cex) { 275 return false; 276 } 277 } 278 } 279 280 287 public String getCounterMemoryName() { 288 return m_memory_name_int; 289 } 290 } 291 | Popular Tags |