Exemple #1
0
        public double[] GetNetOutput(NEATNetwork net, double[][] input, IEvolutionState state)
        {
            double[] output;
            int      netDepth = net.MaxDepth();

            net.LoadSensors(input[0]);
            for (int relax = 0; relax < netDepth; relax++)
            {
                net.Activate(state);
            }

            output = net.GetOutputResults();

            net.Flush();


            return(output);
        }
Exemple #2
0
        public virtual void Evaluate(IEvolutionState state, Individual ind, int subpopulation, int threadnum)
        {
            if (ind.Evaluated)
            {
                return;
            }

            if (!(ind is NEATIndividual))
            {
                state.Output.Fatal("Whoa! It's not a NEATIndividual!!!", null);
            }

            NEATIndividual neatInd = (NEATIndividual)ind;

            if (!(neatInd.Fitness is SimpleFitness))
            {
                state.Output.Fatal("Whoa! It's not a SimpleFitness!!!", null);
            }

            //The four possible input combinations to xor
            //The first number is for biasing
            double[][] input =
            {
                new [] { 1.0, 0.0, 0.0 }, // output 0
                new [] { 1.0, 0.0, 1.0 }, //        1
                new [] { 1.0, 1.0, 0.0 }, //        1
                new [] { 1.0, 1.0, 1.0 }, //        0
            };

            double[] output      = new double[4];
            double[] expectedOut = { 0.0, 1.0, 1.0, 0.0 };

            NEATNetwork net = neatInd.CreateNetwork();

            int netDepth = net.MaxDepth();

            // Load and activate the network on each input
            for (int i = 0; i < input.Length; i++)
            {
                net.LoadSensors(input[i]);

                for (int relax = 0; relax < netDepth; relax++)
                {
                    net.Activate(state);
                }

                // only have one output, so let's get it
                output[i] = net.GetOutputResults()[0];

                net.Flush();
            }


            // calculate fitness

            double errorSum = 0;

            for (int i = 0; i < output.Length; i++)
            {
                errorSum += Math.Abs(output[i] - expectedOut[i]);
            }

            double fitness = (4.0 - errorSum) * (4.0 - errorSum);

            // this is from the original code for counting as ideal
            bool ideal = true;

            for (int i = 0; i < output.Length; i++)
            {
                if (Math.Abs(output[i] - expectedOut[i]) > 0.5)
                {
                    ideal = false;
                    break;
                }
            }

            ((SimpleFitness)neatInd.Fitness).SetFitness(state, fitness, ideal);
            neatInd.Evaluated = true;
        }