static void Main(string[] args)
        {
            FeedforwardNetwork network = new FeedforwardNetwork();

            network.AddLayer(new FeedforwardLayer(2));
            network.AddLayer(new FeedforwardLayer(3));
            network.AddLayer(new FeedforwardLayer(1));
            network.Reset();

            // train the neural network
            TrainingSetNeuralGeneticAlgorithm train = new TrainingSetNeuralGeneticAlgorithm(
                network, true, XOR_INPUT, XOR_IDEAL, 5000, 0.1, 0.25);

            int epoch = 1;

            do
            {
                train.Iteration();
                Console.WriteLine("Epoch #" + epoch + " Error:" + train.Error);
                epoch++;
            } while ((epoch < 5000) && (train.Error > 0.001));

            network = train.Network;

            // test the neural network
            Console.WriteLine("Neural Network Results:");
            for (int i = 0; i < XOR_IDEAL.Length; i++)
            {
                double [] actual = network.ComputeOutputs(XOR_INPUT[i]);
                Console.WriteLine(XOR_INPUT[i][0] + "," + XOR_INPUT[i][1]
                                  + ", actual=" + actual[0] + ",ideal=" + XOR_IDEAL[i][0]);
            }
        }
        public TicTacToeGenetic(FeedforwardNetwork network,
                                bool reset, int populationSize,
                                double mutationPercent, double percentToMate,
                                Type opponent)
        {
            this.setOpponent(opponent);
            this.MutationPercent  = mutationPercent;
            this.MatingPopulation = percentToMate * 2;
            this.PopulationSize   = populationSize;
            this.PercentToMate    = percentToMate;

            this.Chromosomes = new TicTacToeChromosome[this.PopulationSize];
            for (int i = 0; i < this.Chromosomes.Length; i++)
            {
                FeedforwardNetwork chromosomeNetwork = (FeedforwardNetwork)network
                                                       .Clone();
                if (reset)
                {
                    chromosomeNetwork.Reset();
                }

                TicTacToeChromosome c = new TicTacToeChromosome(this,
                                                                chromosomeNetwork);
                c.UpdateGenes();
                SetChromosome(i, c);
            }
            SortChromosomes();
        }
        /// <summary>
        /// Construct a genetic algorithm for a neural network that uses training sets.
        /// </summary>
        /// <param name="network">The neural network.</param>
        /// <param name="reset">Should each neural network be reset to random values.</param>
        /// <param name="input">The input training set.</param>
        /// <param name="ideal">The ideal values for the input training set.</param>
        /// <param name="populationSize">The initial population size.</param>
        /// <param name="mutationPercent">The mutation percentage.</param>
        /// <param name="percentToMate">The percentage of the population allowed to mate.</param>
        public TrainingSetNeuralGeneticAlgorithm(FeedforwardNetwork network,
                                                 bool reset, double[][] input,
                                                 double[][] ideal, int populationSize,
                                                 double mutationPercent, double percentToMate)
        {
            this.MutationPercent  = mutationPercent;
            this.MatingPopulation = percentToMate * 2;
            this.PopulationSize   = populationSize;
            this.PercentToMate    = percentToMate;

            this.input = input;
            this.ideal = ideal;

            this.Chromosomes = new TrainingSetNeuralChromosome[this.PopulationSize];
            for (int i = 0; i < this.Chromosomes.Length; i++)
            {
                FeedforwardNetwork chromosomeNetwork = (FeedforwardNetwork)network
                                                       .Clone();
                if (reset)
                {
                    chromosomeNetwork.Reset();
                }

                TrainingSetNeuralChromosome c = new TrainingSetNeuralChromosome(
                    this, chromosomeNetwork);
                c.UpdateGenes();
                SetChromosome(i, c);
            }
            SortChromosomes();
        }
Example #4
0
        /// <summary>
        /// Create, train and use a neural network for XOR.
        /// </summary>
        /// <param name="args">Not used</param>
        static void Main(string[] args)
        {
            FeedforwardNetwork network = new FeedforwardNetwork();

            network.AddLayer(new FeedforwardLayer(2));
            network.AddLayer(new FeedforwardLayer(3));
            network.AddLayer(new FeedforwardLayer(1));
            network.Reset();

            // train the neural network
            Train train = new HeatonResearchNeural.Feedforward.Train.Backpropagation.Backpropagation(network, XOR_INPUT, XOR_IDEAL,
                                                                                                     0.7, 0.9);

            int epoch = 1;

            do
            {
                train.Iteration();
                Console.WriteLine("Epoch #" + epoch + " Error:" + train.Error);
                epoch++;
            } while ((epoch < 5000) && (train.Error > 0.001));

            // test the neural network
            Console.WriteLine("Neural Network Results:");
            for (int i = 0; i < XOR_IDEAL.Length; i++)
            {
                double[] actual = network.ComputeOutputs(XOR_INPUT[i]);
                Console.WriteLine(XOR_INPUT[i][0] + "," + XOR_INPUT[i][1]
                                  + ", actual=" + actual[0] + ",ideal=" + XOR_IDEAL[i][0]);
            }
        }
Example #5
0
        public static FeedforwardNetwork createNetwork()
        {
            FeedforwardNetwork network = new FeedforwardNetwork();

            network.AddLayer(new FeedforwardLayer(9));
            network
            .AddLayer(new FeedforwardLayer(NeuralTicTacToe.NEURONS_HIDDEN_1));
            if (NeuralTicTacToe.NEURONS_HIDDEN_2 > 0)
            {
                network.AddLayer(new FeedforwardLayer(
                                     NeuralTicTacToe.NEURONS_HIDDEN_2));
            }
            network.AddLayer(new FeedforwardLayer(1));
            network.Reset();
            return(network);
        }
Example #6
0
        public static FeedforwardNetwork CreateNetwork()
        {
            ActivationFunction threshold = new ActivationSigmoid();
            FeedforwardNetwork network   = new FeedforwardNetwork();

            network.AddLayer(new FeedforwardLayer(threshold, Config.INPUT_SIZE));
            network.AddLayer(new FeedforwardLayer(threshold,
                                                  Config.NEURONS_HIDDEN_1));
            if (Config.NEURONS_HIDDEN_2 > 0)
            {
                network.AddLayer(new FeedforwardLayer(threshold,
                                                      Config.NEURONS_HIDDEN_2));
            }
            network.AddLayer(new FeedforwardLayer(threshold, Config.OUTPUT_SIZE));
            network.Reset();
            return(network);
        }
Example #7
0
        public TicTacToeGenetic(FeedforwardNetwork network,
                                bool reset, int populationSize,
                                double mutationPercent, double percentToMate,
                                Type opponent)
        {
            this.setOpponent(opponent);
            this.MutationPercent  = mutationPercent;
            this.MatingPopulation = percentToMate * 2;
            this.PopulationSize   = populationSize;
            this.PercentToMate    = percentToMate;

            this.Chromosomes = new TicTacToeChromosome[this.PopulationSize];
            var rangePartitioner = Partitioner.Create(0, this.Chromosomes.Length);

            //Parallel.ForEach(rangePartitioner, (range, loopState) =>
            {
                for (int i = 0; i < Chromosomes.Length; i++)
                {
                    Console.WriteLine("step: " + i);
                    FeedforwardNetwork chromosomeNetwork = (FeedforwardNetwork)network
                                                           .Clone();
                    if (reset)
                    {
                        chromosomeNetwork.Reset();
                    }

                    TicTacToeChromosome c = new TicTacToeChromosome(this,
                                                                    chromosomeNetwork);
                    c.UpdateGenes();
                    lock (_locker)
                    {
                        SetChromosome(i, c);
                    }
                }
            };
            Console.WriteLine("PreEpoch # Error:" + getScore());
            SortChromosomes();
        }
Example #8
0
        static void Main(string[] args)
        {
            input = GetInput();
            label = GetLabel();

            FeedforwardNetwork network = new FeedforwardNetwork();

            network.AddLayer(new FeedforwardLayer(4));
            network.AddLayer(new FeedforwardLayer(5));
            network.AddLayer(new FeedforwardLayer(1));
            network.Reset();

            // train the neural network
            TrainingSetNeuralGeneticAlgorithm train = new TrainingSetNeuralGeneticAlgorithm(
                network, false, input, label, 5000, 0.1, 0.25);

            int epoch = 1;

            do
            {
                train.Iteration();
                Console.WriteLine("Epoch #" + epoch + " Error:" + train.Error);
                epoch++;
            } while ((epoch < 5000) && (train.Error > 0.001));

            network = train.Network;

            // test the neural network
            Console.WriteLine("Neural Network Results:");
            for (int i = 0; i < label.Length; i++)
            {
                double[] actual = network.ComputeOutputs(input[i]);
                Console.WriteLine(input[i][0] + "," + input[i][1]
                                  + ", actual=" + actual[0] + ",ideal=" + label[i][0]);
            }
        }
Example #9
0
        private void Entrenar(int red, int pre)
        {
            string[] patrones = new string[ejemplos];

            switch (red)
            {
            case 1:
                for (int i = 0; i < ejemplos; i++)
                {
                    if (pre == 0)
                    {
                        patrones[i] = pre1[i, 1];
                    }
                    else
                    {
                        patrones[i] = pos1[i, 1];
                    }
                }
                break;

            case 2:
                for (int i = 0; i < ejemplos; i++)
                {
                    if (pre == 0)
                    {
                        patrones[i] = pre2[i, 1];
                    }
                    else
                    {
                        patrones[i] = pos2[i, 1];
                    }
                }
                break;

            case 3:
                for (int i = 0; i < ejemplos; i++)
                {
                    if (pre == 0)
                    {
                        patrones[i] = pre3[i, 1];
                    }
                    else
                    {
                        patrones[i] = pos3[i, 1];
                    }
                }
                break;

            case 4:
                for (int i = 0; i < ejemplos; i++)
                {
                    if (pre == 0)
                    {
                        patrones[i] = pre4[i, 1];
                    }
                    else
                    {
                        patrones[i] = pos4[i, 1];
                    }
                }
                break;

            case 5:
                for (int i = 0; i < ejemplos; i++)
                {
                    if (pre == 0)
                    {
                        patrones[i] = pre5[i, 1];
                    }
                    else
                    {
                        patrones[i] = pos5[i, 1];
                    }
                }
                break;
            }

            Double[][] entrada = new double[ejemplos + 3][];
            double[]   matriz  = new double[30];
            double[]   matriz2 = new double[30];
            double[]   matriz3 = new double[30];
            //Creamos las matrices de los patrones
            for (int i = 0; i < ejemplos; i++)
            {
                double[] matrix = new double[30];
                for (int o = 0; o < 30; o++)
                {
                    matrix[o] = double.Parse(patrones[i][o].ToString());
                }
                entrada[i] = matrix;
            }
            //Creamos 3 matrices orientativas
            bool control = true;

            for (int i = 0; i < 30; i++)
            {
                matriz3[i] = 0;
                if (control)
                {
                    matriz[i]  = 1;
                    matriz2[i] = 0;
                    control    = false;
                }
                else
                {
                    matriz[i]  = 0;
                    matriz2[i] = 1;
                    control    = true;
                }
            }
            entrada[ejemplos]     = matriz;
            entrada[ejemplos + 1] = matriz2;
            entrada[ejemplos + 2] = matriz3;
            //Creamos la matriz ideal
            Double[][] ideal = new double[ejemplos + 3][];
            for (int i = 0; i < ejemplos; i++)
            {
                ideal[i] = new double[] { 1 }
            }
            ;
            for (int i = ejemplos; i < ejemplos + 3; i++)
            {
                ideal[i] = new double[] { 0 }
            }
            ;

            //COMENZAMOS A CREAR LA RED NEURONAL!!!!
            FeedforwardNetwork neurosis = new FeedforwardNetwork();

            neurosis.AddLayer(new FeedforwardLayer(30));
            neurosis.AddLayer(new FeedforwardLayer(30));
            neurosis.AddLayer(new FeedforwardLayer(1));
            neurosis.Reset();
            Train entrenador = new HeatonResearchNeural.Feedforward.Train.Backpropagation.Backpropagation
                                   (neurosis, entrada, ideal, 0.09, 0.9);

            for (int e = 0; e < 1000; e++)
            {
                Console.WriteLine("Error: " + entrenador.Error);
                entrenador.Iteration();
                if (entrenador.Error < 0.012)
                {
                    break;
                }
            }
            redes[red - 1, pre] = neurosis;
        }