Beispiel #1
0
        private static double[][] TrainXOR(int epochs, FastCyclicNetwork network)
        {
            double[][] inputs = new double[][] {
                new double[] { 0, 0 },
                new double[] { 1, 0 },
                new double[] { 0, 1 },
                new double[] { 1, 1 }
            };
            double[][] outputs = new double[][] {
                new double[] { 0 },
                new double[] { 1 },
                new double[] { 1 },
                new double[] { 0 }
            };

            for (int i = 0; i < epochs; i++)
            {
                network.Train(inputs[i % inputs.Length], outputs[i % outputs.Length]);
            }
            return(inputs);
        }
Beispiel #2
0
        /// <summary>
        /// Evaluate the provided IBlackBox against the XOR problem domain and return its fitness score.
        /// </summary>
        public FitnessInfo Evaluate(IBlackBox box)
        {
            double       fitness = 0;
            double       output;
            double       pass      = 1.0;
            ISignalArray inputArr  = box.InputSignalArray;
            ISignalArray outputArr = box.OutputSignalArray;

            _evalCount++;

            // Train the network
            FastCyclicNetwork net = (FastCyclicNetwork)box;

            //----- Test 0,0
            box.ResetState();

            // Set the input values
            inputArr[0] = 0.0;
            inputArr[1] = 0.0;

            // Activate the black box.
            box.Activate();
            if (!box.IsStateValid)
            {   // Any black box that gets itself into an invalid state is unlikely to be
                // any good, so lets just bail out here.
                return(FitnessInfo.Zero);
            }

            // Read output signal.
            output = Math.Min(1, Math.Max(0, outputArr[0]));
            var output0 = output;

            Debug.Assert(output >= 0.0, "Unexpected negative output.");

            // Calculate this test case's contribution to the overall fitness score.
            //fitness += 1.0 - output; // Use this line to punish absolute error instead of squared error.
            fitness += 1.0 - (output * output);
            if (output > 0.5)
            {
                pass = 0.0;
            }

            //----- Test 1,1
            // Reset any black box state from the previous test case.
            box.ResetState();

            // Set the input values
            inputArr[0] = 1.0;
            inputArr[1] = 1.0;

            // Activate the black box.
            box.Activate();
            if (!box.IsStateValid)
            {   // Any black box that gets itself into an invalid state is unlikely to be
                // any good, so lets just bail out here.
                return(FitnessInfo.Zero);
            }

            // Read output signal.
            output = Math.Min(1, Math.Max(0, outputArr[0]));
            var output1 = output;

            Debug.Assert(output >= 0.0, "Unexpected negative output.");

            // Calculate this test case's contribution to the overall fitness score.
            //fitness += 1.0 - output; // Use this line to punish absolute error instead of squared error.
            fitness += 1.0 - (output * output);
            if (output > 0.5)
            {
                pass = 0.0;
            }

            //----- Test 0,1
            // Reset any black box state from the previous test case.
            box.ResetState();

            // Set the input values
            inputArr[0] = 0.0;
            inputArr[1] = 1.0;

            // Activate the black box.
            box.Activate();
            if (!box.IsStateValid)
            {   // Any black box that gets itself into an invalid state is unlikely to be
                // any good, so lets just bail out here.
                return(FitnessInfo.Zero);
            }

            // Read output signal.
            output = Math.Min(1, Math.Max(0, outputArr[0]));
            var output2 = output;

            Debug.Assert(output >= 0.0, "Unexpected negative output.");

            // Calculate this test case's contribution to the overall fitness score.
            // fitness += output; // Use this line to punish absolute error instead of squared error.
            fitness += 1.0 - ((1.0 - output) * (1.0 - output));
            if (output <= 0.5)
            {
                pass = 0.0;
            }

            //----- Test 1,0
            // Reset any black box state from the previous test case.
            box.ResetState();

            // Set the input values
            inputArr[0] = 1.0;
            inputArr[1] = 0.0;

            // Activate the black box.
            box.Activate();
            if (!box.IsStateValid)
            {   // Any black box that gets itself into an invalid state is unlikely to be
                // any good, so lets just bail out here.
                return(FitnessInfo.Zero);
            }

            // Read output signal.
            output = Math.Min(1, Math.Max(0, outputArr[0]));
            var output3 = output;

            Debug.Assert(output >= 0.0, "Unexpected negative output.");

            // Calculate this test case's contribution to the overall fitness score.
            // fitness += output; // Use this line to punish absolute error instead of squared error.
            fitness += 1.0 - ((1.0 - output) * (1.0 - output));
            if (output <= 0.5)
            {
                pass = 0.0;
            }

            // If all four outputs were correct, that is, all four were on the correct side of the
            // threshold level - then we add 10 to the fitness.
            fitness += pass * 10.0;

            if (fitness > 3.6)
            {
                Console.WriteLine("Before Nodes={5} Connections={6} [0,0]={0:N3} [1,1]={1:N3} [0,1]={2:N3} [1,0]={3:N3} fitness={4:N3}", output0, output1, output2, output3, fitness, net.HiddenCount, net.ConnectionCount);
            }
            fitness = 0;

            net.BackpropLearningRate = 0.1;
            net.Momentum             = 0.9;
            double[][] exampleInputs = new double[][] {
                new double[] { 0, 0 },
                new double[] { 0, 1 },
                new double[] { 1, 0 },
                new double[] { 1, 1 }
            };
            double[][] exampleOutputs = new double[][] {
                new double[] { 0 },
                new double[] { 1 },
                new double[] { 1 },
                new double[] { 0 }
            };
            net.RandomizeWeights();
            for (int i = 0; i < 4000; i++)
            {
                net.Train(exampleInputs[i % 4], exampleOutputs[i % 4]);
            }

            //----- Test 0,0
            box.ResetState();

            // Set the input values
            inputArr[0] = 0.0;
            inputArr[1] = 0.0;

            // Activate the black box.
            box.Activate();
            if (!box.IsStateValid)
            {   // Any black box that gets itself into an invalid state is unlikely to be
                // any good, so lets just bail out here.
                return(FitnessInfo.Zero);
            }

            // Read output signal.
            output  = Math.Min(1, Math.Max(0, outputArr[0]));
            output0 = output;
            Debug.Assert(output >= 0.0, "Unexpected negative output.");

            // Calculate this test case's contribution to the overall fitness score.
            //fitness += 1.0 - output; // Use this line to punish absolute error instead of squared error.
            fitness += 1.0 - (output * output);
            if (output > 0.5)
            {
                pass = 0.0;
            }

            //----- Test 1,1
            // Reset any black box state from the previous test case.
            box.ResetState();

            // Set the input values
            inputArr[0] = 1.0;
            inputArr[1] = 1.0;

            // Activate the black box.
            box.Activate();
            if (!box.IsStateValid)
            {   // Any black box that gets itself into an invalid state is unlikely to be
                // any good, so lets just bail out here.
                return(FitnessInfo.Zero);
            }

            // Read output signal.
            output  = Math.Min(1, Math.Max(0, outputArr[0]));
            output1 = output;
            Debug.Assert(output >= 0.0, "Unexpected negative output.");

            // Calculate this test case's contribution to the overall fitness score.
            //fitness += 1.0 - output; // Use this line to punish absolute error instead of squared error.
            fitness += 1.0 - (output * output);
            if (output > 0.5)
            {
                pass = 0.0;
            }

            //----- Test 0,1
            // Reset any black box state from the previous test case.
            box.ResetState();

            // Set the input values
            inputArr[0] = 0.0;
            inputArr[1] = 1.0;

            // Activate the black box.
            box.Activate();
            if (!box.IsStateValid)
            {   // Any black box that gets itself into an invalid state is unlikely to be
                // any good, so lets just bail out here.
                return(FitnessInfo.Zero);
            }

            // Read output signal.
            output  = Math.Min(1, Math.Max(0, outputArr[0]));
            output2 = output;
            Debug.Assert(output >= 0.0, "Unexpected negative output.");

            // Calculate this test case's contribution to the overall fitness score.
            // fitness += output; // Use this line to punish absolute error instead of squared error.
            fitness += 1.0 - ((1.0 - output) * (1.0 - output));
            if (output <= 0.5)
            {
                pass = 0.0;
            }

            //----- Test 1,0
            // Reset any black box state from the previous test case.
            box.ResetState();

            // Set the input values
            inputArr[0] = 1.0;
            inputArr[1] = 0.0;

            // Activate the black box.
            box.Activate();
            if (!box.IsStateValid)
            {   // Any black box that gets itself into an invalid state is unlikely to be
                // any good, so lets just bail out here.
                return(FitnessInfo.Zero);
            }

            // Read output signal.
            output  = Math.Min(1, Math.Max(0, outputArr[0]));
            output3 = output;
            Debug.Assert(output >= 0.0, "Unexpected negative output.");

            // Calculate this test case's contribution to the overall fitness score.
            // fitness += output; // Use this line to punish absolute error instead of squared error.
            fitness += 1.0 - ((1.0 - output) * (1.0 - output));
            if (output <= 0.5)
            {
                pass = 0.0;
            }

            // If all four outputs were correct, that is, all four were on the correct side of the
            // threshold level - then we add 10 to the fitness.
            fitness += pass * 10.0;


            if (fitness >= StopFitness)
            {
                _stopConditionSatisfied = true;
            }


            if (fitness > 3.6)
            {
                Console.WriteLine("After Nodes={5} Connections={6} [0,0]={0:N3} [1,1]={1:N3} [0,1]={2:N3} [1,0]={3:N3} fitness={4:N3}", output0, output1, output2, output3, fitness, net.HiddenCount, net.ConnectionCount);
            }

            return(new FitnessInfo(fitness, fitness));
        }