1 10 package org.jgap.gp.impl; 11 12 import java.io.*; 13 import java.util.*; 14 import org.jgap.*; 15 import org.jgap.gp.function.*; 16 import org.jgap.gp.*; 17 import org.jgap.util.*; 18 19 25 public class GPProgram 26 extends GPProgramBase implements Serializable, Comparable , ICloneable { 27 28 private final static String CVS_REVISION = "$Revision: 1.10 $"; 29 30 33 private ProgramChromosome[] m_chromosomes; 34 35 56 public GPProgram(GPConfiguration a_conf, Class [] a_types, 57 Class [][] a_argTypes, CommandGene[][] a_nodeSets, 58 int[] a_minDepths, int[] a_maxDepths, int a_maxNodes) 59 throws InvalidConfigurationException { 60 super(a_conf); 61 m_chromosomes = new ProgramChromosome[a_types.length]; 62 setTypes(a_types); 63 setArgTypes(a_argTypes); 64 setNodeSets(a_nodeSets); 65 setMaxDepths(a_maxDepths); 66 setMinDepths(a_minDepths); 67 setMaxNodes(a_maxNodes); 68 } 69 70 79 public GPProgram(IGPProgram a_prog) 80 throws InvalidConfigurationException { 81 super(a_prog); 82 m_chromosomes = new ProgramChromosome[getTypes().length]; 83 } 84 85 95 public GPProgram(GPConfiguration a_conf, int a_numChromosomes) 96 throws InvalidConfigurationException { 97 super(a_conf); 98 m_chromosomes = new ProgramChromosome[a_numChromosomes]; 99 } 100 101 108 public ProgramChromosome getChromosome(int a_index) { 109 return m_chromosomes[a_index]; 110 } 111 112 121 public void setChromosome(int a_index, ProgramChromosome a_chrom) { 122 m_chromosomes[a_index] = a_chrom; 123 } 124 125 public void growOrFull(int a_depth, boolean a_grow, int a_maxNodes, 126 boolean[] a_fullModeAllowed) { 127 GPConfiguration conf = getGPConfiguration(); 128 int size = m_chromosomes.length; 129 for (int i = 0; i < size; i++) { 130 try { 131 m_chromosomes[i] = new ProgramChromosome(conf, a_maxNodes, this); 134 } catch (InvalidConfigurationException iex) { 135 throw new RuntimeException (iex); 136 } 137 m_chromosomes[i].setArgTypes(getArgTypes()[i]); 138 int len = getNodeSets()[i].length; 142 for (int j = 0; j < len; j++) { 143 if (getNodeSets()[i][j] instanceof ADF) { 144 ( (ADF) getNodeSets()[i][j]).setReturnType( 145 getTypes()[ ( (ADF) getNodeSets()[i][j]).getChromosomeNum()]); 146 } 147 } 148 } 149 int depth; 150 for (int i = 0; i < size; i++) { 151 if (getMaxDepths() != null && a_depth > getMaxDepths()[i]) { 154 depth = getMaxDepths()[i]; 155 } 156 else { 157 if (getMinDepths() != null && a_depth < getMinDepths()[i]) { 158 depth = getMinDepths()[i]; 159 } 160 else { 161 depth = a_depth; 162 } 163 } 164 if (a_grow || !a_fullModeAllowed[i]) { 167 m_chromosomes[i].growOrFull(i, depth, getType(i), getArgType(i), 168 getNodeSet(i), true); 169 } 170 else { 171 m_chromosomes[i].growOrFull(i, depth, getType(i), getArgType(i), 172 getNodeSet(i), false); 173 } 174 } 175 if (getGPConfiguration().isUseProgramCache()) { 176 GPProgramInfo pcInfo = getGPConfiguration().readProgramCache(this); 179 if (pcInfo == null) { 180 181 pcInfo = getGPConfiguration().putToProgramCache(this); 182 } 183 else { 184 setFitnessValue(pcInfo.getFitnessValue()); 185 } 186 } 187 } 188 189 195 public int size() { 196 return m_chromosomes.length; 197 } 198 199 208 public String toString(int a_startNode) { 209 if (a_startNode < 0) { 210 return ""; 211 } 212 StringBuffer sb = new StringBuffer (); 213 for (int i = 0; i < m_chromosomes.length; i++) { 214 if (i > 0) { 215 sb.append(" ==> "); 216 } 217 sb.append(m_chromosomes[i].toString(a_startNode)); 218 } 219 return sb.toString(); 220 } 221 222 230 public String toStringNorm(int a_startNode) { 231 if (a_startNode < 0) { 232 return ""; 233 } 234 StringBuffer sb = new StringBuffer (); 235 for (int i = 0; i < m_chromosomes.length; i++) { 236 if (i > 0) { 237 sb.append(" ==> "); 238 } 239 sb.append(m_chromosomes[i].toStringNorm(a_startNode)); 240 } 241 return sb.toString(); 242 } 243 244 254 public int execute_int(int a_chromosomeNum, Object [] a_args) { 255 m_chromosomes[a_chromosomeNum].setIndividual(this); 256 return m_chromosomes[a_chromosomeNum].execute_int(a_args); 257 } 258 259 269 public float execute_float(int a_chromosomeNum, Object [] a_args) { 270 m_chromosomes[a_chromosomeNum].setIndividual(this); 271 return m_chromosomes[a_chromosomeNum].execute_float(a_args); 272 } 273 274 284 public double execute_double(int a_chromosomeNum, Object [] a_args) { 285 m_chromosomes[a_chromosomeNum].setIndividual(this); 286 return m_chromosomes[a_chromosomeNum].execute_double(a_args); 287 } 288 289 299 public Object execute_object(int a_chromosomeNum, Object [] a_args) { 300 m_chromosomes[a_chromosomeNum].setIndividual(this); 301 return m_chromosomes[a_chromosomeNum].execute_object(a_args); 302 } 303 304 313 public void execute_void(int a_chromosomeNum, Object [] a_args) { 314 m_chromosomes[a_chromosomeNum].setIndividual(this); 315 m_chromosomes[a_chromosomeNum].execute_void(a_args); 316 } 317 318 328 public int getCommandOfClass(int a_chromosomeNum, Class a_class) { 329 for (int i = a_chromosomeNum; i < m_chromosomes.length; i++) { 330 int j = m_chromosomes[i].getCommandOfClass(0, a_class); 331 if (j >= 0) { 332 return j; 333 } 334 } 335 return -1; 336 } 337 338 349 public int compareTo(Object a_other) { 350 if (a_other == null) { 354 return 1; 355 } 356 int size = size(); 357 GPProgram other = (GPProgram) a_other; 358 ProgramChromosome[] otherChroms = other.m_chromosomes; 359 if (other.size() != size) { 363 return size() - other.size(); 364 } 365 Arrays.sort(m_chromosomes); 370 Arrays.sort(otherChroms); 371 for (int i = 0; i < size; i++) { 372 int comparison = m_chromosomes[i].compareTo(otherChroms[i]); 373 if (comparison != 0) { 374 return comparison; 375 } 376 } 377 return 0; 380 } 381 382 388 public Object clone() { 389 try { 390 int[] minDepthsClone; 393 if (getMinDepths() != null) { 394 minDepthsClone = (int[]) getMinDepths().clone(); 395 } 396 else { 397 minDepthsClone = null; 398 } 399 int[] maxDepthsClone; 400 if (getMaxDepths() != null) { 401 maxDepthsClone = (int[]) getMaxDepths().clone(); 402 } 403 else { 404 maxDepthsClone = null; 405 } 406 GPProgram result = new GPProgram(getGPConfiguration(), 407 (Class []) getTypes().clone(), 408 (Class [][]) getArgTypes().clone(), 409 (CommandGene[][]) getNodeSets().clone(), 410 minDepthsClone, 411 maxDepthsClone, 412 getMaxNodes()); 413 result.setFitnessValue(getFitnessValueDirectly()); 414 Object appData = getApplicationData(); 417 if (appData != null) { 418 ICloneHandler cloner = getGPConfiguration().getJGAPFactory().getCloneHandlerFor(appData,null); 419 if (cloner != null) { 420 result.setApplicationData(cloner.perform(appData,null,null)); 421 } 422 else { 423 result.setApplicationData(appData); 424 } 425 } 426 for (int i = 0; i < m_chromosomes.length; i++) { 427 if (m_chromosomes[i] == null) { 428 break; 429 } 430 result.m_chromosomes[i] = (ProgramChromosome) m_chromosomes[i].clone(); 431 } 432 return result; 433 } catch (Exception ex) { 434 throw new CloneException(ex); 435 } 436 } 437 } 438 | Popular Tags |