// \param numberOfInstructions how many instructions exist for the instructionset? public void sample(double[] propabilityVector, ref uint[] resultInstructions, uint instructionsetCount, int lengthOfProgram, Random random) { // select instruction, look for a children for the node, iterate lengthOfProgram = lengthOfProgram == -1 ? propabilityVector.Length : lengthOfProgram; SparseArrayProgramDistributionTreeElement iterationElement = root; // if the iteration tree element is null we choose an random instruction, // because the tree has no preference or knowledge of the instruction distribution for (int i = 0; i < lengthOfProgram; i++) { // * calculate absolute propability sum double propabilityMassForCurrentSparseArrayProgramDistributionTreeElement = iterationElement != null?iterationElement.getPropabilityMass(instructionsetCount) : instructionsetCount; double propabilityForInstructionByIndex = propabilityVector[i]; double absolutePropabilitySum = propabilityMassForCurrentSparseArrayProgramDistributionTreeElement * propabilityForInstructionByIndex; uint selectedInstruction; // * select instructions if (iterationElement != null) { selectedInstruction = iterationElement.sampleInstructionBasedOnAbsolutePropability(absolutePropabilitySum, instructionsetCount, random); } else { // fast way to choose a random instruction selectedInstruction = (uint)absolutePropabilitySum; } // * store instruction resultInstructions[i] = selectedInstruction; // * choose next tree element if (iterationElement != null) { iterationElement = iterationElement.getChildrenElementByInstruction(selectedInstruction); } } }
public void addProgram(uint[] instructions) { double defaultPropabilityOfShadowedInstruction = 1.0; // just a name for an otherwise magical value SparseArrayProgramDistributionTreeElement treeElementForCurrentInstruction = root; if (root == null) { root = new SparseArrayProgramDistributionTreeElement(defaultPropabilityOfShadowedInstruction); treeElementForCurrentInstruction = root; } foreach (uint iterationInstruction in instructions) { if (!treeElementForCurrentInstruction.isInstructionKnown(iterationInstruction)) { treeElementForCurrentInstruction.appendInstruction(iterationInstruction, defaultPropabilityOfShadowedInstruction, new SparseArrayProgramDistributionTreeElement(defaultPropabilityOfShadowedInstruction)); } treeElementForCurrentInstruction = treeElementForCurrentInstruction.getChildrenElementByInstruction(iterationInstruction); } }