Exemple #1
0
 static void PrintWeights(FastCyclicNetwork network)
 {
     foreach (var conn in network.ConnectionArray)
     {
         Console.WriteLine("[{0}] -> [{1}]: {2:N4}", conn._srcNeuronIdx, conn._tgtNeuronIdx, conn._weight);
     }
 }
Exemple #2
0
        public static CFastCyclicNetwork Convert(FastCyclicNetwork fcn)
        {
            var cfcn            = new CFastCyclicNetwork();
            var numberOfNeurons = fcn._neuronActivationFnArray.Length;

            // Copy connections
            cfcn.Connections = new List <CConnection>(fcn._connectionArray.Length);
            foreach (var c in fcn._connectionArray)
            {
                var cc = new CConnection
                {
                    ToNeuronId   = c._tgtNeuronIdx,
                    FromNeuronId = c._srcNeuronIdx,
                    Weight       = c._weight
                };
                cfcn.Connections.Add(cc);
            }

            // Copy activation functions
            cfcn.ActivationFunctions = new List <string>(numberOfNeurons);
            foreach (var s in fcn._neuronActivationFnArray)
            {
                if (s == null)
                {
                    cfcn.ActivationFunctions.Add("");
                }
                else
                {
                    cfcn.ActivationFunctions.Add(s.FunctionId);
                }
            }

            // Copy auxiliary arguments
            cfcn.NeuronAuxArgs = new List <List <double> >(numberOfNeurons);
            foreach (var aux in fcn._neuronAuxArgsArray)
            {
                if (aux == null)
                {
                    cfcn.NeuronAuxArgs.Add(new List <double>());
                }
                else
                {
                    cfcn.NeuronAuxArgs.Add(new List <double>(aux));
                }
            }
            cfcn.NeuronCount            = fcn._neuronCount;
            cfcn.InputNeuronCount       = fcn._inputNeuronCount;
            cfcn.OutputNeuronCount      = fcn._outputNeuronCount;
            cfcn.TimestepsPerActivation = fcn._timestepsPerActivation;

            return(cfcn);
        }
Exemple #3
0
        private static void RunXor(FastCyclicNetwork network, double[][] inputs)
        {
            for (int i = 0; i < inputs.Length; i++)
            {
                network.ResetState();

                // Convert the sensors into an input array for the network
                for (int j = 0; j < inputs[i].Length; j++)
                {
                    network.InputSignalArray[j] = inputs[i][j];
                }

                // Activate the network
                network.Activate();

                Console.WriteLine("[{0},{1}] -> {2:N4}", inputs[i][0], inputs[i][1], network.OutputSignalArray[0]);
            }
        }
Exemple #4
0
        public FitnessInfo Evaluate(FastCyclicNetwork phenome)
        {
            EvaluationCount++;

            var fcn = FastCyclicNetworkAdapter.Convert(phenome);

            ProtocolManager.Open();
            var cFitness = ProtocolManager.Client.calculateXorPhenotypeFitness(fcn);
            //ProtocolManager.Close(); adding this line implies a huge performance hit

            var auxFitness = new AuxFitnessInfo[cFitness.AuxFitness.Count];

            for (int i = 0; i < auxFitness.Length; i++)
            {
                auxFitness[i] = new AuxFitnessInfo(cFitness.AuxFitness[i].Name, cFitness.AuxFitness[i].Value);
            }
            var fitness = new FitnessInfo(cFitness.Fitness, auxFitness);

            StopConditionSatisfied = cFitness.StopConditionSatisfied;
            if (StopConditionSatisfied)
            {
                FitnessInfo fi = _xorBlackBoxEvaluator.Evaluate(phenome);
                bool        reallySatisfied = _xorBlackBoxEvaluator.StopConditionSatisfied;
                Debug.Assert((fi._fitness >= 10) == _xorBlackBoxEvaluator.StopConditionSatisfied);
                if (!reallySatisfied)
                {
                    Console.Out.WriteLine("ERROR: really fitness is not calculated (" + fi._fitness + " versus " +
                                          cFitness.Fitness + ")");
                    Console.Out.WriteLine(phenome.ToString());
                    //throw new Exception("Wheih :(");
                }
                else
                {
                    Console.Out.WriteLine("Networks is really good");
                }
            }

            // We do not use the NEAT genome decoder. The NEAT genome decoder would
            // do the same as the following code, but provide us with an IBlackBox object.
            // Because we pass on the object to our Java code, we need to be aware of its
            // underlying structure. The additional layer of abstraction gets in the way.
            return(fitness);
        }
Exemple #5
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);
        }
Exemple #6
0
        public FitnessInfo Evaluate(IBlackBox box)
        {
            EvaluationCount++;

            FastCyclicNetwork network = box as FastCyclicNetwork;

            // Write them out to a file.
            DeepBeliefNetworkBiaserIO.WriteOutputMatrix(network._connectionArray,
                                                        Constants.INPUT_SIZE + 1,
                                                        Constants.OUTPUT_SIZE);

            // Run the python DBN script which will output
            DeepBeliefNetworkBiaserIO.RunPythonScriptMultithreaded();

            Tuple <double, double> fitnessPair = DeepBeliefNetworkBiaserIO.ReadFitness();

            // Set stop flag when max fitness is attained.
            if (!StopConditionSatisfied && fitnessPair.Item1 == MaxFitness)
            {
                StopConditionSatisfied = true;
            }

            return(new FitnessInfo(fitnessPair.Item1, fitnessPair.Item2));
        }
Exemple #7
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));
        }