Ejemplo n.º 1
0
        public double CalculateError(Ann ann, double[][] inputs, double[][] expectedOutputs, bool debugFlag = false)
        {
            double error = 0;

            for (int inputIndex = 0; inputIndex < inputs.Length; inputIndex++)
            {
                double[] currentInputs          = inputs[inputIndex];
                double[] currentExpectedOutputs = expectedOutputs[inputIndex];

                double[] outputs = ann.Execute(currentInputs);

                for (int i = 0; i < currentExpectedOutputs.Length; i++)
                {
                    error += Math.Sqrt(Math.Pow(currentExpectedOutputs[i] - outputs[i], 2));
                }

                for (int i = 0; i < ann.hiddenNeurons.Count + ann.synapses.Count; i++)
                {
                    error += ErrorPerPart;
                }
            }

            return(error);
        }
Ejemplo n.º 2
0
        private static void TestHardcodedXOR()
        {
            NeatLib.Ann ann = new NeatLib.Ann(2, 1, NeatLib.ActivationFunction.Sigmoid);

            ann.hiddenNeurons.Add(new NeatLib.Neuron(0, 0)
            {
                Bias = -10
            });
            ann.hiddenNeurons.Add(new NeatLib.Neuron(0, 1)
            {
                Bias = 30
            });
            ann.outputNeurons[0].Bias = -30;

            ann.synapses.Add(new NeatLib.Synapse(-1, 0, 0, 0)
            {
                Weight = 20
            });
            ann.synapses.Add(new NeatLib.Synapse(-1, 1, 0, 0)
            {
                Weight = 20
            });

            ann.synapses.Add(new NeatLib.Synapse(-1, 0, 0, 1)
            {
                Weight = -20
            });
            ann.synapses.Add(new NeatLib.Synapse(-1, 1, 0, 1)
            {
                Weight = -20
            });

            ann.synapses.Add(new NeatLib.Synapse(0, 0, -2, 0)
            {
                Weight = 20
            });
            ann.synapses.Add(new NeatLib.Synapse(0, 1, -2, 0)
            {
                Weight = 20
            });

            double xor00 = Math.Round(ann.Execute(new double[] { 0, 0 })[0], 2);
            double xor01 = Math.Round(ann.Execute(new double[] { 0, 1 })[0], 2);
            double xor10 = Math.Round(ann.Execute(new double[] { 1, 0 })[0], 2);
            double xor11 = Math.Round(ann.Execute(new double[] { 1, 1 })[0], 2);

            Console.ForegroundColor = ConsoleColor.Gray;
            Console.Write("XOR 0 + 0 = ");
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine(xor00);

            Console.ForegroundColor = ConsoleColor.Gray;
            Console.Write("XOR 0 + 1 = ");
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine(xor01);

            Console.ForegroundColor = ConsoleColor.Gray;
            Console.Write("XOR 1 + 0 = ");
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine(xor10);

            Console.ForegroundColor = ConsoleColor.Gray;
            Console.Write("XOR 1 + 1 = ");
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine(xor11);

            if (xor00 == 0 && xor01 == 1 && xor10 == 1 && xor11 == 0)
            {
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Hardcoded neural network succesfully calculated XOR logic.");
            }
            else
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Hardcoded neural network unsuccesfully calculated XOR logic.");
            }
            Console.ForegroundColor = ConsoleColor.Gray;
            Console.WriteLine();
        }