1 10 package org.jgap.gp.impl; 11 12 import java.io.*; 13 import org.jgap.*; 14 import org.jgap.gp.*; 15 16 22 public class BranchTypingCross 23 extends CrossMethod implements Serializable, Comparable { 24 25 private final static String CVS_REVISION = "$Revision: 1.11 $"; 26 27 public BranchTypingCross(GPConfiguration a_config) { 28 super(a_config); 29 } 30 31 43 public IGPProgram[] operate(final IGPProgram i1, 44 final IGPProgram i2) { 45 try { 46 50 int[] sizes = new int[i1.size()]; 51 int totalSize = 0; 52 for (int i = 0; i < i1.size(); i++) { 53 sizes[i] = i1.getChromosome(i).getSize(0); 56 totalSize += sizes[i]; 57 } 58 59 int nodeNum = getConfiguration().getRandomGenerator().nextInt( 60 totalSize); 61 int chromosomeNum; 64 for (chromosomeNum = 0; chromosomeNum < i1.size(); chromosomeNum++) { 65 nodeNum -= sizes[chromosomeNum]; 66 if (nodeNum < 0) 67 break; 68 } 69 ProgramChromosome[] newChromosomes = doCross( 72 i1.getChromosome(chromosomeNum), 73 i2.getChromosome(chromosomeNum)); 74 81 IGPProgram[] newIndividuals = { 82 new GPProgram(i1), new GPProgram(i1)}; for (int i = 0; i < i1.size(); i++) 85 if (i != chromosomeNum) { 86 newIndividuals[0].setChromosome(i, i1.getChromosome(i)); 87 newIndividuals[1].setChromosome(i, i2.getChromosome(i)); 88 } 89 else { 90 newIndividuals[0].setChromosome(i, newChromosomes[0]); 91 newIndividuals[1].setChromosome(i, newChromosomes[1]); 92 } 93 return newIndividuals; 94 } catch (InvalidConfigurationException iex) { 95 return null; 96 } 97 } 98 99 120 protected ProgramChromosome[] doCross(ProgramChromosome c0, 121 ProgramChromosome c1) 122 throws InvalidConfigurationException { 123 ProgramChromosome[] c = { 124 c0, c1}; 125 int p0; 128 RandomGenerator random = getConfiguration().getRandomGenerator(); 129 if (random.nextFloat() < getConfiguration().getFunctionProb()) { 130 int nf = c0.numFunctions(); 132 if (nf == 0) { 133 return c; 135 } 136 p0 = c0.getFunction(random.nextInt(nf)); 137 } 138 else { 139 p0 = c0.getTerminal(random.nextInt(c0.numTerminals())); 142 145 CommandGene command = c0.getNode(p0); 146 if (IMutateable.class.isInstance(command)) { 147 IMutateable term = (IMutateable) command; 148 command = term.applyMutation(0, 0.5d); 149 if (command != null) { 150 if (c0.getCommandOfClass(0, command.getClass()) >=0) { 153 c0.setGene(p0, command); 154 } 155 } 156 } 157 } 158 int p1; 161 CommandGene nodeP0 = c0.getNode(p0); 162 Class type_ = nodeP0.getReturnType(); 163 int subType = nodeP0.getSubReturnType(); 164 if (random.nextFloat() < getConfiguration().getFunctionProb()) { 165 int nf = c1.numFunctions(type_, subType); 167 if (nf == 0) { 168 return c; 171 } 172 p1 = c1.getFunction(random.nextInt(nf), type_, subType); 173 } 174 else { 175 int nt = c1.numTerminals(type_, subType); 178 if (nt == 0) { 179 return c; 182 } 183 p1 = c1.getTerminal(random.nextInt(c1.numTerminals(type_, subType)), type_, subType); 184 187 CommandGene command = c1.getNode(p1); 188 if (IMutateable.class.isInstance(command)) { 189 IMutateable term = (IMutateable) command; 190 command = term.applyMutation(0, 0.5d); 191 if (command != null) { 192 if (c0.getCommandOfClass(0, command.getClass()) >=0) { 195 c1.setGene(p1, command); 196 } 197 } 198 } 199 } 200 int s0 = c0.getSize(p0); int s1 = c1.getSize(p1); int d0 = c0.getDepth(p0); int d1 = c1.getDepth(p1); int c0s = c0.getSize(0); int c1s = c1.getSize(0); 207 if (d0 - 1 + s1 > getConfiguration().getMaxCrossoverDepth() 210 || c0s - p0 - s0 < 0 211 || p0 + s1 + c0s - p0 - s0>= c0.getFunctions().length) { 212 c[0] = c1; 215 } 216 else { 217 c[0] = new ProgramChromosome(getConfiguration(), c0.getFunctions().length, c[0].getFunctionSet(), 219 c[0].getArgTypes(), 220 c0.getIndividual()); 221 System.arraycopy(c0.getFunctions(), 0, c[0].getFunctions(), 0, p0); 222 System.arraycopy(c1.getFunctions(), p1, c[0].getFunctions(), p0, s1); 223 System.arraycopy(c0.getFunctions(), p0 + s0, c[0].getFunctions(), 224 p0 + s1, c0s - p0 - s0); 225 c[0].redepth(); 226 } 227 if (d1 - 1 + s0 > getConfiguration().getMaxCrossoverDepth() 230 || c1s - p1 - s1 < 0 231 || p1 + s0 + c1s - p1 - s1 >= c1.getFunctions().length) { 232 c[1] = c0; 235 } 236 else { 237 c[1] = new ProgramChromosome(getConfiguration(), c1.getFunctions().length, c[1].getFunctionSet(), 239 c[1].getArgTypes(), 240 c1.getIndividual()); 241 System.arraycopy(c1.getFunctions(), 0, c[1].getFunctions(), 0, p1); 242 System.arraycopy(c0.getFunctions(), p0, c[1].getFunctions(), p1, s0); 243 System.arraycopy(c1.getFunctions(), p1 + s1, c[1].getFunctions(), 244 p1 + s0, c1s - p1 - s1); 245 c[1].redepth(); 246 } 247 return c; 248 } 249 250 259 public int compareTo(Object a_other) { 260 BranchTypingCross other = (BranchTypingCross) a_other; 261 if (other == null) { 262 return 1; 263 } 264 return 0; 265 } 266 267 277 public boolean equals(Object a_other) { 278 try { 279 BranchTypingCross other = (BranchTypingCross) a_other; 280 if (other == null) { 281 return false; 282 } 283 else { 284 return true; 285 } 286 } catch (ClassCastException cex) { 287 return false; 288 } 289 } 290 } 291 | Popular Tags |