internal virtual GPNode CreateTreeOfType(IEvolutionState state, int thread, GPInitializer initializer, int functionset, int type, int size, IMersenneTwister mt) { //System.out.PrintLn("" + functionset + " " + type + " " + size); var choice = RandomChoice.PickFromDistribution(ROOT_D[functionset][type][size], ROOT_D[functionset][type][size][0], mt.NextDouble()); var node = ROOT_D[functionset][type][size][choice].Node.LightClone(); node.ResetNode(state, thread); // give ERCs a chance to randomize //System.out.PrintLn("Size: " + size + "Rooted: " + node); if (node.Children.Length == 0 && size != 1) // uh oh { Console.Out.WriteLine("Size: " + size + " Node: " + node); for (var x = 0; x < ROOT_D[functionset][type][size].Length; x++) { Console.Out.WriteLine("" + x + (ROOT_D[functionset][type][size][x].Node) + " " + ROOT_D[functionset][type][size][x].Prob); } } if (size > 1) { // nonterminal FillNodeWithChildren(state, thread, initializer, functionset, node, ROOT_D[functionset][type][size][choice].Node, 0, size - 1, mt); } return(node); }
private static int[] Select(IList <int[]> permutations, int size) { var total = 0; long denominator = 1; int[] current; var quantity = new int[permutations.Count]; for (var counter1 = 0; counter1 < permutations.Count; counter1++) { current = permutations[counter1]; long residue = size; // Quick internal calculations for (var counter2 = 0; counter2 < current.Length; counter2++) { residue -= current[counter2]; denominator *= Fact(current[counter2]); } quantity[counter1] = (int)(Fact(size - 1) / (denominator * Fact(residue))); total += quantity[counter1]; } var prob = new double[quantity.Length]; // quantities found... now build array for probabilities for (var counter1 = 0; counter1 < quantity.Length; counter1++) { prob[counter1] = quantity[counter1] / (double)total; // I don't think we need to check for negative values here -- Sean } RandomChoice.OrganizeDistribution(prob); var selection = RandomChoice.PickFromDistribution(prob, 0.0, 7); return(permutations[selection]); }
public virtual int PickSize(IEvolutionState state, int thread, int functionset, int type) { if (UseTrueDistribution) { return(RandomChoice.PickFromDistribution(TrueSizes[functionset][type], state.Random[thread].NextDouble())); } return(base.PickSize(state, thread)); }
/// <summary> /// Assuming that either ResetMinSize and ResetMaxSize, or SizeDistribution, is defined, /// picks a random size from ResetMinSize...ResetMaxSize inclusive, or randomly /// from SizeDistribution. /// </summary> public virtual int PickSize(IEvolutionState state, int thread) { if (SizeDistribution != null) { // pick from distribution return(RandomChoice.PickFromDistribution(SizeDistribution, state.Random[thread].NextDouble())); } // pick from ResetMinSize...ResetMaxSize return(state.Random[thread].NextInt(ResetMaxSize - ResetMinSize + 1) + ResetMinSize); }
public override int Produce(int subpop, IEvolutionState state, int thread) { // pick a coin toss if (state.Random[thread].NextBoolean(Gets_N_Percent)) { // over -- SortedFitUnder.length to SortedPop.length return(SortedPop[SortedFitUnder.Length + RandomChoice.PickFromDistribution(SortedFitOver, state.Random[thread].NextDouble())]); } // under -- 0 to SortedFitUnder.length return(SortedPop[RandomChoice.PickFromDistribution(SortedFitUnder, state.Random[thread].NextDouble())]); }
/// <summary> /// Assuming that either minSize and maxSize, or sizeDistribution, is defined, /// picks a random size from minSize...maxSize inclusive, or randomly /// from sizeDistribution. /// </summary> public virtual int PickSize(IEvolutionState state, int thread) { if (MinSize > 0) { // pick from minSize...maxSize return(state.Random[thread].NextInt(MaxSize - MinSize + 1) + MinSize); } if (SizeDistribution != null) { // pick from distribution return(RandomChoice.PickFromDistribution(SizeDistribution, state.Random[thread].NextFloat(), CHECK_BOUNDARY) + 1); } throw new ApplicationException("Neither minSize nor sizeDistribution is defined in GPNodeBuilder"); }
internal virtual void FillNodeWithChildren(IEvolutionState state, int thread, GPInitializer initializer, int functionset, GPNode parent, GPNode parentc, int pickchild, int outof, IMersenneTwister mt) { if (pickchild == parent.Children.Length - 1) { parent.Children[pickchild] = CreateTreeOfType(state, thread, initializer, functionset, parent.Constraints(initializer).ChildTypes[pickchild].Type, outof, mt); } else { var size = RandomChoice.PickFromDistribution(CHILD_D[functionset][IntForNode(parentc)][outof][pickchild], mt.NextDouble()); parent.Children[pickchild] = CreateTreeOfType(state, thread, initializer, functionset, parent.Constraints(initializer).ChildTypes[pickchild].Type, size, mt); FillNodeWithChildren(state, thread, initializer, functionset, parent, parentc, pickchild + 1, outof - size, mt); } parent.Children[pickchild].Parent = parent; parent.Children[pickchild].ArgPosition = (sbyte)pickchild; }
/// <summary> /// A private function which recursively returns a GROW tree to NewRootedTree(...) /// </summary> private GPNode Ptc1(IEvolutionState state, int current, GPType type, int thread, IGPNodeParent parent, int argPosition, GPFunctionSet funcs, IPTCFunctionSet pfuncs, double[] nonterminalSelectProbs) { // ptc1 can mess up if there are no available terminals for a given type. If this occurs, // and we find ourselves unable to pick a terminal when we want to do so, we will issue a warning, // and pick a nonterminal, violating the PTC1 size and depth contracts. This can lead to pathological situations // where the system will continue to go on and on unable to stop because it can't pick a terminal, // resulting in running out of memory or some such. But there are cases where we'd want to let // this work itself out. var triedTerminals = false; var t = type.Type; var terminals = funcs.Terminals[t]; var nonterminals = funcs.Nonterminals[t]; var nodes = funcs.Nodes[t]; if (nodes.Length == 0) { ErrorAboutNoNodeWithType(type, state); // total failure } // Now pick if we're at max depth // OR if we're below p_y // OR if there are NO nonterminals! // [first set triedTerminals] // AND if there are available terminals if (((current + 1 >= MaxDepth) || !(state.Random[thread].NextBoolean(nonterminalSelectProbs[t])) || WarnAboutNonterminal(nonterminals.Length == 0, type, false, state)) && (triedTerminals = true) && terminals.Length != 0) { var n = terminals[RandomChoice.PickFromDistribution(pfuncs.TerminalProbabilities(t), state.Random[thread].NextDouble())].LightClone(); n.ResetNode(state, thread); // give ERCs a chance to randomize n.ArgPosition = (sbyte)argPosition; n.Parent = parent; return(n); } // above p_y, pick a nonterminal by q_ny probabilities else { if (triedTerminals) { WarnAboutNoTerminalWithType(type, false, state); // we tried terminals and we're here because there were none! } var n = nonterminals[RandomChoice.PickFromDistribution(pfuncs.NonterminalProbabilities(t), state.Random[thread].NextDouble())].LightClone(); n.ResetNode(state, thread); // give ERCs a chance to randomize n.ArgPosition = (sbyte)argPosition; n.Parent = parent; // Populate the node... var childtypes = n.Constraints((GPInitializer)state.Initializer).ChildTypes; for (var x = 0; x < childtypes.Length; x++) { n.Children[x] = Ptc1(state, current + 1, childtypes[x], thread, n, x, funcs, pfuncs, nonterminalSelectProbs); } return(n); } }
/// <summary> /// Picks a random source from an array of sources, with their /// probabilities normalized and summed as follows: For example, /// if four /// breeding source probabilities are {0.3, 0.2, 0.1, 0.4}, then /// they should get normalized and summed by the outside owners /// as: {0.3, 0.5, 0.6, 1.0}. /// </summary> public static int PickRandom(IBreedingSource[] sources, double prob) { return(RandomChoice.PickFromDistribution(sources, sources[0], prob)); }
public override GPNode NewRootedTree(IEvolutionState state, GPType type, int thread, IGPNodeParent parent, GPFunctionSet funcs, int argPosition, int requestedSize) { // ptc2 can mess up if there are no available terminals for a given type. If this occurs, // and we find ourselves unable to pick a terminal when we want to do so, we will issue a warning, // and pick a nonterminal, violating the ptc2 size and depth contracts. This can lead to pathological situations // where the system will continue to go on and on unable to stop because it can't pick a terminal, // resulting in running out of memory or some such. But there are cases where we'd want to let // this work itself out. var triedTerminals = false; if (!(funcs is IPTCFunctionSet)) { state.Output.Fatal("Set " + funcs.Name + " is not of the class ec.gp.build.IPTCFunctionSet, and so cannot be used with PTC Nodebuilders."); } var pfuncs = (IPTCFunctionSet)funcs; // pick a size from the distribution if (requestedSize == NOSIZEGIVEN) { requestedSize = PickSize(state, thread); } GPNode root; var t = type.Type; var terminals = funcs.Terminals[t]; var nonterminals = funcs.Nonterminals[t]; var nodes = funcs.Nodes[t]; if (nodes.Length == 0) { ErrorAboutNoNodeWithType(type, state); // total failure } // return a terminal // Now pick a terminal if our size is 1 // OR if there are NO nonterminals! // [first set triedTerminals] // AND if there are available terminals if ((requestedSize == 1 || WarnAboutNonterminal(nonterminals.Length == 0, type, false, state)) && (triedTerminals = true) && terminals.Length != 0) { root = terminals[RandomChoice.PickFromDistribution(pfuncs.TerminalProbabilities(t), state.Random[thread].NextDouble())].LightClone(); root.ResetNode(state, thread); // give ERCs a chance to randomize root.ArgPosition = (sbyte)argPosition; root.Parent = parent; } // return a nonterminal-rooted tree else { if (triedTerminals) { WarnAboutNoTerminalWithType(type, false, state); // we tried terminals and we're here because there were none! } // pick a nonterminal root = nonterminals[RandomChoice.PickFromDistribution(pfuncs.NonterminalProbabilities(t), state.Random[thread].NextDouble())].LightClone(); root.ResetNode(state, thread); // give ERCs a chance to randomize root.ArgPosition = (sbyte)argPosition; root.Parent = parent; // set the depth, size, and enqueuing, and reset the random dequeue s_size = 0; // pretty critical! var s = 1; var initializer = ((GPInitializer)state.Initializer); var childtypes = root.Constraints(initializer).ChildTypes; for (var x = 0; x < childtypes.Length; x++) { Enqueue(root, x, 1); /* depth 1 */ } while (s_size > 0) { triedTerminals = false; RandomDequeue(state, thread); type = DequeueNode.Constraints(initializer).ChildTypes[DequeueArgpos]; var y = type.Type; terminals = funcs.Terminals[y]; nonterminals = funcs.Nonterminals[y]; nodes = funcs.Nodes[y]; if (nodes.Length == 0) { ErrorAboutNoNodeWithType(type, state); // total failure } // pick a terminal // if we need no nonterminal nodes // OR if we're at max depth and must pick a terminal // OR if there are NO nonterminals! // [first set triedTerminals] // AND if there are available terminals if ((s_size + s >= requestedSize || DequeueDepth == MaxDepth || WarnAboutNonterminal(nonterminals.Length == 0, type, false, state)) && (triedTerminals = true) && terminals.Length != 0) { var n = terminals[RandomChoice.PickFromDistribution(pfuncs.TerminalProbabilities(y), state.Random[thread].NextDouble())].LightClone(); DequeueNode.Children[DequeueArgpos] = n; n.ResetNode(state, thread); // give ERCs a chance to randomize n.ArgPosition = (sbyte)DequeueArgpos; n.Parent = DequeueNode; } // pick a nonterminal and enqueue its children else { if (triedTerminals) { WarnAboutNoTerminalWithType(type, false, state); // we tried terminals and we're here because there were none! } var n = nonterminals[RandomChoice.PickFromDistribution(pfuncs.NonterminalProbabilities(y), state.Random[thread].NextDouble())].LightClone(); DequeueNode.Children[DequeueArgpos] = n; n.ResetNode(state, thread); // give ERCs a chance to randomize n.ArgPosition = (sbyte)DequeueArgpos; n.Parent = DequeueNode; childtypes = n.Constraints(initializer).ChildTypes; for (var x = 0; x < childtypes.Length; x++) { Enqueue(n, x, DequeueDepth + 1); } } s++; } } return(root); }
public override int Produce(int subpop, IEvolutionState state, int thread) { // Pick and return an individual from the population return(RandomChoice.PickFromDistribution(Fitnesses, state.Random[thread].NextDouble())); }