Exemplo n.º 1
0
        void engEngine_EvalFitnessForProgramEvent(TreeLanguageEvolute.TreeProgram progProgram, GeneticProgrammingEngine sender)
        {
            progProgram.Fitness = 0;
            Hashtable hsVariables = progProgram.GetVariables();
            Variable  varX        = (Variable)hsVariables["X"];

            for (int nCaseNumber = 0;
                 nCaseNumber < 100;
                 nCaseNumber++)
            {
                // y = 5x^3+x^2+x
                varX.Value = (float)GlobalRandom.m_rndRandom.NextDouble();
                float fExpectedResult = (5 * varX.Value * varX.Value * varX.Value)
                                        + (3 * varX.Value * varX.Value) + varX.Value;
                progProgram.Run();
                float fActualResult = progProgram.Result;
                progProgram.Fitness += Math.Abs(fExpectedResult - fActualResult);
            }

            if (progProgram.Fitness < 0.1F)
            {
                progProgram.Fitness =
                    0.00000001F * progProgram.Count;
            }
        }
Exemplo n.º 2
0
            public override float action(Node[] arrNodes, TreeLanguageEvolute.TreeProgram progOwner)
            {
                Hashtable hsVariables          = progOwner.GetVariables();
                float     fWantedVariable      = arrNodes[0].ReturnValue(progOwner);
                float     fWantedValue         = arrNodes[1].ReturnValue(progOwner);
                uint      nWantedVariableIndex = ((uint)fWantedVariable) % (uint)hsVariables.Count;

                Variable varWantedVariable = progOwner.Variables[nWantedVariableIndex];

                varWantedVariable.Value = fWantedValue;

                return(1);
            }