Example #1
0
        // \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);
                }
            }
        }