Exemple #1
0
        //This is the default test for the neural network to confirm it is performing as expected
        public string getTestDataString(NN n)
        {
            StringBuilder outStringBuilder = new StringBuilder();
            outStringBuilder.AppendLine("Test Neural Network");
            outStringBuilder.AppendLine("-------------------");

            //Print the input data
            outStringBuilder.AppendLine("Input Data");
            outStringBuilder.AppendFormat("Number of Inputs: {0}\n", n.NumberOfInputs);
            outStringBuilder.AppendFormat("Number of Outputs: {0}\n", n.NumberOfOutputs);
            outStringBuilder.AppendFormat("Number of Hidden Layers: {0}\n", n.NumberOfHiddenLayers);
            outStringBuilder.AppendLine("Number of Nodes Per Hidden Layer: ");
            Array.ForEach(n.NumberOfNodesPerHiddenLayer, x => outStringBuilder.AppendFormat("{0} ", x));
            outStringBuilder.AppendLine();

            //Print the calculated data
            outStringBuilder.AppendLine("-------------------");
            outStringBuilder.AppendLine("General Calculated Metadata");
            outStringBuilder.AppendFormat("Total Number of Layers: {0}\n", n.NumberOfLayers);
            outStringBuilder.AppendFormat("Total Number of Nodes: {0}\n", n.NumberOfNodes);
            outStringBuilder.AppendFormat("Total Number of Hidden Nodes: {0}\n", n.NumberOfHiddenNodes);
            outStringBuilder.AppendLine();

            //Print the layer data
            outStringBuilder.AppendLine("-------------------");
            outStringBuilder.AppendLine("Specific Calculated Metadata Per Layer");
            outStringBuilder.AppendLine("Number of Weights Per Layer: ");
            Array.ForEach(n.WeightsPerLayer, x => outStringBuilder.AppendFormat("{0} ", x));
            outStringBuilder.AppendLine();
            outStringBuilder.AppendLine("Number of Inputs Per Layer: ");
            Array.ForEach(n.InputsPerLayer, x => outStringBuilder.AppendFormat("{0} ", x));
            outStringBuilder.AppendLine();
            outStringBuilder.AppendLine("Number of Nodes Per Layer: ");
            Array.ForEach(n.NodesPerLayer, x => outStringBuilder.AppendFormat("{0} ", x));
            outStringBuilder.AppendLine();
            outStringBuilder.AppendLine("Weight Start Index Per Layer: ");
            Array.ForEach(n.WeightStartIndexPerLayer, x => outStringBuilder.AppendFormat("{0} ", x));
            outStringBuilder.AppendLine();
            outStringBuilder.AppendLine("Weight Start Index Per Node Per Layer: ");
            for (int i = 0; i < n.WeightStartIndexPerLayerPerNode.Length; i++)
            {
                Array.ForEach(n.WeightStartIndexPerLayerPerNode[i], x => outStringBuilder.AppendFormat("{0} ", x));
                outStringBuilder.AppendLine();
            }
            outStringBuilder.AppendLine();
            outStringBuilder.AppendLine();

            //Print the detailed data
            outStringBuilder.AppendLine("-------------------");
            outStringBuilder.AppendLine("Detailed Data");
            outStringBuilder.AppendFormat("Number of Weights: {0}\n", n.Weights.Length);
            outStringBuilder.AppendLine("Weights: ");
            Array.ForEach(n.Weights, x => outStringBuilder.AppendFormat("{0}\n", x));
            outStringBuilder.AppendLine();

            //Return the results string
            return outStringBuilder.ToString();
        }
Exemple #2
0
        public List<NN> BreedPickEach(Tuple<int, int>[] couples, KeyValuePair<int, double>[] sortedFitnessList)
        {
            //Create a list of new neural networks for the next generation
            List<NN> newPopulation = new List<NN>();

            //Place the elites in the population
            for (int i = 0; i < numberOfElites; i++)
                for (int j = 0; j < numOfCopiesOfElites; j++)
                    newPopulation.Add(population[sortedFitnessList[i].Key]);

            //Filling the remainder of the population
            int k = 0;
            while (newPopulation.Count < population.Count)
            {
                //If we randomly are above the crossover rate
                if (Util.randNumGen.NextDouble() > crossoverRate)
                {
                    //Add the first parent
                    newPopulation.Add(population[couples[k].Item1]);
                    //If we have room for another add the second parent
                    if (!(newPopulation.Count <= population.Count))
                        newPopulation.Add(population[couples[k].Item2]);
                }
                //If we are below the crossover rate
                else
                {
                    //Create a new neural network with the same structure as the rest of the population
                    NN child1 = new NN(networkStructure[0], networkStructure[networkStructure.Length - 1], hiddenLayerStructure.Length, hiddenLayerStructure);

                    //Create a new neural network with the same structure as the rest of the population
                    NN child2 = new NN(networkStructure[0], networkStructure[networkStructure.Length - 1], hiddenLayerStructure.Length, hiddenLayerStructure);

                    //Add all genes from each parent up to the crossover point to the two different children
                    int i;
                    for (i = 0; i < population[k].Weights.Length; i++)
                    {
                        //Randomly stitch one of the parents genes onto each child
                        if (Util.randNumGen.NextDouble() > 0.5)
                        {
                            child1.Weights[i] = population[couples[k].Item1].Weights[i];
                            child2.Weights[i] = population[couples[k].Item2].Weights[i];
                        }
                        else
                        {
                            child1.Weights[i] = population[couples[k].Item2].Weights[i];
                            child2.Weights[i] = population[couples[k].Item1].Weights[i];
                        }
                    }

                    //Add the first child to the new population
                    newPopulation.Add(child1);
                    //If we have room for more, add the second child to the population
                    if (!(newPopulation.Count <= population.Count))
                        newPopulation.Add(child2);
                }

                //Confirm that the iteration variable does not exceed the array size from which it is pulling values
                k = (k + 1) % couples.Length;
            }

            //Return the new population
            return newPopulation;
        }
Exemple #3
0
        //This function tests the serial functionality of the Neural Network object.
        //This can be useful if a particularly great neural network is consturcted and should be saved/loaded.
        private bool testSerialization(NN n)
        {
            //Save the neural network created when this object was built
            n.SaveNNToFile("Test_NN.dat");
            //Load a new neural network from that file
            NN loadedNN = NN.LoadNNFromFile("Test_NN.dat");

            //Using the custom equals function, test to see if the networks are equal then show the results.
            if (loadedNN.Equals(n))
                return true;
            return false;
        }
Exemple #4
0
        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            //Initialize a test network
            testNN = new NN(4, 2, 1, new int[] { 6 });

            //Build a test population
            pop = new NNPopulation(simulationPopulationSize, neuralNetworkStructure, typeof(NNAntSimulation));

            //Output the default object test results
            ///richTextBox_simpleOut.Text = getTestDataString();

            //Fill the simThread with a default value (not running)
            simThread = new Thread(new ThreadStart(runGeneticSimulation));
            //No stop is request at the beginning of execution
            requestStop = false;

            ///label_totalIterations.Text = "0 Total Iterations, Population Size: " + pop.StartPopulationSize;

            ///comboBox_BreedingType.SelectedIndex = 0;

            prevKeyboardState = Keyboard.GetState();
            prevMouseState = Mouse.GetState();
            prevGamePadState = GamePad.GetState(PlayerIndex.One);

            basicEffect = new BasicEffect(graphics.GraphicsDevice);
            basicEffect.VertexColorEnabled = true;
            basicEffect.Projection = Matrix.CreateOrthographicOffCenter
               (0, graphics.GraphicsDevice.Viewport.Width,     // left, right
                graphics.GraphicsDevice.Viewport.Height, 0,    // bottom, top
                0, 1);                                         // near, far plane

            Util.genericTextFont = Content.Load<SpriteFont>("GenericFont");

            base.Initialize();
        }