Beispiel #1
0
        public MainForm()
        {
            InitializeComponent();

            //Initialize a test network
            testNN = new NN(4, 2, 1, new int[] { 6 });

            //Build a test population
            pop = new NNPopulation(Params.simulationPopulationSize, Params.neuralNetworkStructure);

            //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 = 1;

            numIterations = 0;

            avgScoreList = new List<double>();
            movAvgScoreList = new List<double>();
            maxScoreList = new List<double>();
            movMaxScoreList = new List<double>();

            Series mAvgSeries = new Series("Average Moving Average");
            Series avgSeries = new Series("Average");
            Series mMaxSeries = new Series("Max Moving Average");
            Series maxSeries = new Series("Max");

            mAvgSeries.ChartType = SeriesChartType.Spline;
            avgSeries.ChartType = SeriesChartType.Line;
            mMaxSeries.ChartType = SeriesChartType.Spline;
            maxSeries.ChartType = SeriesChartType.Line;

            chart.Series.Add(mAvgSeries);
            chart.Series.Add(avgSeries);
            chart.Series.Add(mMaxSeries);
            chart.Series.Add(maxSeries);
        }
Beispiel #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 < Params.numberOfElites; i++)
                for (int j = 0; j < Params.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() > Params.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;
        }