public int produce( final int min, final int max, final int start, final int subpopulation, final Individual[] inds, final EvolutionState state, final int thread) { // how many individuals should we make? int n = typicalIndsProduced(); if (n < min) n = min; if (n > max) n = max; // should we bother? if (!state.random[thread].nextBoolean(likelihood)) return reproduce( n, start, subpopulation, inds, state, thread, true); // DO produce children from source -- we've not done so already // random select one tree from treelib // int treeIndex = (new Random()).nextInt(state.maxTreelibSize); // GPNode TR = state.treelib[treeIndex]; // create tree: 1 - TR GPNode OneSubTR = (GPNode) (((GPNode[]) fs.nodesByName.get("-"))[0]).lightClone(); OneSubTR.children = new GPNode[2]; RegERC oneNode = new RegERC(); oneNode.constraints = 6; oneNode.children = new GPNode[0]; oneNode.value = 1; OneSubTR.children[0] = oneNode; GPInitializer initializer = ((GPInitializer) state.initializer); for (int q = start; q < n + start; /* no increment */ ) // keep on going until we're filled up { // grab two individuals from our sources if (sources[0] == sources[1]) // grab from the same source sources[0].produce(2, 2, 0, subpopulation, parents, state, thread); else // grab from different sources { sources[0].produce(1, 1, 0, subpopulation, parents, state, thread); sources[1].produce(1, 1, 1, subpopulation, parents, state, thread); } // at this point, parents[] contains our two selected individuals // random select one tree from treelib int treeIndex = (new Random()).nextInt(state.maxTreelibSize); GPNode TR = state.treelib[treeIndex]; OneSubTR.children[1] = (GPNode) TR.clone(); double[] randomSemanticTraining = state.semanticTreelibTraining[treeIndex]; double[] randomSemanticTesting = state.semanticTreelibTesting[treeIndex]; // are our tree values valid? if (tree1 != TREE_UNFIXED && (tree1 < 0 || tree1 >= parents[0].trees.length)) // uh oh state.output.fatal( "GP Crossover Pipeline attempted to fix tree.0 to a value which was out of bounds of the array of the individual's trees. Check the pipeline's fixed tree values -- they may be negative or greater than the number of trees in an individual"); if (tree2 != TREE_UNFIXED && (tree2 < 0 || tree2 >= parents[1].trees.length)) // uh oh state.output.fatal( "GP Crossover Pipeline attempted to fix tree.1 to a value which was out of bounds of the array of the individual's trees. Check the pipeline's fixed tree values -- they may be negative or greater than the number of trees in an individual"); int t1 = 0; int t2 = 0; if (tree1 == TREE_UNFIXED || tree2 == TREE_UNFIXED) { do // pick random trees -- their GPTreeConstraints must be the same { if (tree1 == TREE_UNFIXED) if (parents[0].trees.length > 1) t1 = state.random[thread].nextInt(parents[0].trees.length); else t1 = 0; else t1 = tree1; if (tree2 == TREE_UNFIXED) if (parents[1].trees.length > 1) t2 = state.random[thread].nextInt(parents[1].trees.length); else t2 = 0; else t2 = tree2; } while (parents[0].trees[t1].constraints(initializer) != parents[1].trees[t2].constraints(initializer)); } else { t1 = tree1; t2 = tree2; // make sure the constraints are okay if (parents[0].trees[t1].constraints(initializer) != parents[1].trees[t2].constraints(initializer)) // uh oh state.output.fatal( "GP Crossover Pipeline's two tree choices are both specified by the user -- but their GPTreeConstraints are not the same"); } // number of crossover state.numOfSGX[state.generation][0] += 1; GPIndividual child1 = (GPIndividual) (parents[0].lightClone()); child1.trees[0].semanticTraining = new double[randomSemanticTraining.length]; child1.trees[0].semanticTesting = new double[randomSemanticTesting.length]; GPIndividual child2 = null; if (n - (q - start) >= 2 && !tossSecondParent) { child2 = (GPIndividual) (parents[1].lightClone()); child2.trees[0].semanticTraining = new double[randomSemanticTraining.length]; child2.trees[0].semanticTesting = new double[randomSemanticTesting.length]; } // geometric semantic crossover on training set for (int i = 0; i < parents[0].trees[0].semanticTraining.length; i++) { child1.trees[0].semanticTraining[i] = randomSemanticTraining[i] * parents[0].trees[0].semanticTraining[i] + (1 - randomSemanticTraining[i]) * parents[1].trees[0].semanticTraining[i]; child1.evaluated = false; if (child2 != null) { child2.trees[0].semanticTraining[i] = (1 - randomSemanticTraining[i]) * parents[0].trees[0].semanticTraining[i] + randomSemanticTraining[i] * parents[1].trees[0].semanticTraining[i]; child2.evaluated = false; } } // geometric semantic crossover on testing set for (int i = 0; i < parents[0].trees[0].semanticTesting.length; i++) { child1.trees[0].semanticTesting[i] = randomSemanticTesting[i] * parents[0].trees[0].semanticTesting[i] + (1 - randomSemanticTesting[i]) * parents[1].trees[0].semanticTesting[i]; if (child2 != null) { child2.trees[0].semanticTesting[i] = (1 - randomSemanticTesting[i]) * parents[0].trees[0].semanticTesting[i] + randomSemanticTesting[i] * parents[1].trees[0].semanticTesting[i]; } } // add the individuals to the population inds[q] = child1; q++; if (q < n + start && !tossSecondParent) { inds[q] = child2; q++; } } return n; }
public void setup(final EvolutionState state, final Parameter base) { super.setup(state, base); Parameter def = defaultBase(); Parameter p = base.push(P_NODESELECTOR).push("0"); Parameter d = def.push(P_NODESELECTOR).push("0"); nodeselect1 = (GPNodeSelector) (state.parameters.getInstanceForParameter(p, d, GPNodeSelector.class)); nodeselect1.setup(state, p); p = base.push(P_NODESELECTOR).push("1"); d = def.push(P_NODESELECTOR).push("1"); if (state.parameters.exists(p, d) && state.parameters.getString(p, d).equals(V_SAME)) // can't just copy it this time; the selectors // use internal caches. So we have to clone it no matter what nodeselect2 = (GPNodeSelector) (nodeselect1.clone()); else { nodeselect2 = (GPNodeSelector) (state.parameters.getInstanceForParameter(p, d, GPNodeSelector.class)); nodeselect2.setup(state, p); } numTries = state.parameters.getInt(base.push(P_NUM_TRIES), def.push(P_NUM_TRIES), 1); if (numTries == 0) state.output.fatal( "GPCrossover Pipeline has an invalid number of tries (it must be >= 1).", base.push(P_NUM_TRIES), def.push(P_NUM_TRIES)); maxDepth = state.parameters.getInt(base.push(P_MAXDEPTH), def.push(P_MAXDEPTH), 1); if (maxDepth == 0) state.output.fatal( "GPCrossover Pipeline has an invalid maximum depth (it must be >= 1).", base.push(P_MAXDEPTH), def.push(P_MAXDEPTH)); maxSize = NO_SIZE_LIMIT; if (state.parameters.exists(base.push(P_MAXSIZE), def.push(P_MAXSIZE))) { maxSize = state.parameters.getInt(base.push(P_MAXSIZE), def.push(P_MAXSIZE), 1); if (maxSize < 1) state.output.fatal("Maximum tree size, if defined, must be >= 1"); } tree1 = TREE_UNFIXED; if (state.parameters.exists(base.push(P_TREE).push("" + 0), def.push(P_TREE).push("" + 0))) { tree1 = state.parameters.getInt(base.push(P_TREE).push("" + 0), def.push(P_TREE).push("" + 0), 0); if (tree1 == -1) state.output.fatal("Tree fixed value, if defined, must be >= 0"); } tree2 = TREE_UNFIXED; if (state.parameters.exists(base.push(P_TREE).push("" + 1), def.push(P_TREE).push("" + 1))) { tree2 = state.parameters.getInt(base.push(P_TREE).push("" + 1), def.push(P_TREE).push("" + 1), 0); if (tree2 == -1) state.output.fatal("Tree fixed value, if defined, must be >= 0"); } tossSecondParent = state.parameters.getBoolean(base.push(P_TOSS), def.push(P_TOSS), false); GPInitializer initializer = ((GPInitializer) state.initializer); fs = initializer.treeConstraints[0].functionset; templateTree = (GPNode) (((GPNode[]) fs.nodesByName.get("+"))[0]).lightClone(); GPNode left = (GPNode) (((GPNode[]) fs.nodesByName.get("*"))[0]).lightClone(); GPNode right = (GPNode) (((GPNode[]) fs.nodesByName.get("*"))[0]).lightClone(); // RegERC ercNode1 = (RegERC)((((GPNode[])fs.nodesByName.get("ERC"))[0]).lightClone()); // // generate TR node that will be replated by random tree // GPNode TRNode = new GPNode(); // ercNode.constraints = 6; // ercNode.children = new GPNode[0]; templateTree.children = new GPNode[2]; templateTree.children[0] = left; templateTree.children[1] = right; left.children = new GPNode[2]; left.children[0] = null; left.children[1] = null; right.children = new GPNode[2]; right.children[0] = null; right.children[1] = null; }