private void Reset()
        {
            Random rnd = new Random();

            if (currentGameState == GameState.NeuralNet)
            {
                currentlyFlying = populationSize;
                landingPosition = new Vector2(rnd.Next(Content.Load <Texture2D>("landing pad").Width / 2, graphics.PreferredBackBufferWidth - (Content.Load <Texture2D>("landing pad").Width / 2)), graphics.PreferredBackBufferHeight - 200);
                boosterList     = ANN.BreedNewGeneration(boosterList, initParams, eliteIndividuals, crossoverRate, crossover1Section, crossover2Section, mutationRate, numNewIndividuals, numEliteChildren);
            }
            else if (currentGameState == GameState.PlayerControlled)
            {
                boosterList.Clear();
                boosterList.AddBooster(Content.Load <Texture2D>("booster"), (Vector2)initParams[0], (Vector2)initParams[1], (float)initParams[2], (float)initParams[3], (float)initParams[4], (float)initParams[5]);
            }
            generationComplete = false;
        }
Example #2
0
        public static BoosterList BreedNewGeneration(BoosterList currentGen, object[] initParams, int eliteIndividuals, float crossoverRate, int[] crossover1Section, int[] crossover2Section, float mutationRate, int numNewIndividuals, int numEliteChildren)
        {
            Random      rnd           = new Random(DateTime.Now.Millisecond);
            BoosterList newGeneration = new BoosterList();

            currentGen.Sort((a, b) => b.GetGameScore().CompareTo(a.GetGameScore()));
            for (int i = 0; i < eliteIndividuals; i++)
            {
                Booster elite = currentGen[i];
                Debug.Print("Elite individual " + (i + 1) + " landed with a position of " + elite.GetBoosterPosition().X + ", a y-velocity of " + elite.GetBoosterVelocity().Y +
                            ", a rotation of " + elite.GetBoosterRotation() + ", with " + elite.GetFuel() + " fuel left and " + elite.GetMonopropellant() + " mono left, with a score of " + elite.GetGameScore() +
                            "and weights1 of " + elite.GetWeights1()[0, 0] + ", " + elite.GetWeights1()[0, 1] + ", " + elite.GetWeights1()[0, 2] + ", " + elite.GetWeights1()[0, 3] + ", " + elite.GetWeights1()[0, 4]);
                newGeneration.AddBooster(elite.GetBoosterTexture(), (Vector2)initParams[0], (Vector2)initParams[1], (float)initParams[2], (float)initParams[3], (float)initParams[4], (float)initParams[5], elite.GetWeights1(), elite.GetWeights2());
                newGeneration[i].ChangeTint(Color.Blue);
                newGeneration[i].ChangeLayerDepth(0f);
            }

            for (int i = 0; i < (numEliteChildren / eliteIndividuals); i++)
            {
                for (int j = 0; j < eliteIndividuals; j++)
                {
                    newGeneration.AddRange(CreateChildren(rnd, currentGen, newGeneration[j], RouletteSelection(rnd, currentGen), initParams, crossoverRate, crossover1Section, crossover2Section, mutationRate));
                }
            }

            for (int i = eliteIndividuals; i < numEliteChildren; i++)
            {
                newGeneration[i].ChangeTint(Color.Purple);
                newGeneration[i].ChangeLayerDepth(0.1f);
            }

            while (newGeneration.Count() < (currentGen.Count() - numNewIndividuals))
            {
                newGeneration.AddRange(CreateChildren(rnd, currentGen, RouletteSelection(rnd, currentGen), RouletteSelection(rnd, currentGen), initParams, crossoverRate, crossover1Section, crossover2Section, mutationRate));
            }

            while (newGeneration.Count() < currentGen.Count())
            {
                newGeneration.AddBooster(newGeneration[0].GetBoosterTexture(), (Vector2)initParams[0], (Vector2)initParams[1], (float)initParams[2], (float)initParams[3], (float)initParams[4], (float)initParams[5]);
                newGeneration[newGeneration.Count() - 1].ChangeTint(Color.Yellow);
                newGeneration[newGeneration.Count() - 1].ChangeLayerDepth(0.2f);
            }

            return(newGeneration);
        }
Example #3
0
        private static BoosterList CreateChildren(Random rnd, BoosterList currentGen, Booster parent1, Booster parent2, object[] initParams, float crossoverRate, int[] crossover1Section, int[] crossover2Section, float mutationRate)
        {
            BoosterList children = new BoosterList();

            double[,] child1Weights1 = accord.Matrix.Copy(parent1.GetWeights1());
            double[,] child1Weights2 = accord.Matrix.Copy(parent1.GetWeights2());
            double[,] child2Weights1 = accord.Matrix.Copy(parent2.GetWeights1());
            double[,] child2Weights2 = accord.Matrix.Copy(parent2.GetWeights2());
            double[,] temp;

            if (rnd.NextDouble() <= crossoverRate)
            {
                temp = accord.Matrix.Copy(child1Weights1);
                for (int i = crossover1Section[0]; i <= crossover1Section[1]; i++)
                {
                    for (int j = crossover1Section[2]; j <= crossover1Section[3]; j++)
                    {
                        child1Weights1[i, j] = child2Weights1[i, j];
                        child2Weights1[i, j] = temp[i, j];
                    }
                }

                temp = accord.Matrix.Copy(child1Weights2);
                for (int i = 0; i < accord.Matrix.Rows(child1Weights2); i++)
                {
                    for (int j = 0; j < accord.Matrix.Columns(child1Weights2); j++)
                    {
                        child1Weights2[i, j] = child2Weights2[i, j];
                        child2Weights2[i, j] = temp[i, j];
                    }
                }
            }

            child1Weights1 = MutateMatrix(rnd, child1Weights1, mutationRate);
            child1Weights2 = MutateMatrix(rnd, child1Weights2, mutationRate);
            child2Weights1 = MutateMatrix(rnd, child2Weights1, mutationRate);
            child2Weights2 = MutateMatrix(rnd, child2Weights2, mutationRate);

            children.AddBooster(parent1.GetBoosterTexture(), (Vector2)initParams[0], (Vector2)initParams[1], (float)initParams[2], (float)initParams[3], (float)initParams[4], (float)initParams[5], child1Weights1, child1Weights2);
            children.AddBooster(parent2.GetBoosterTexture(), (Vector2)initParams[0], (Vector2)initParams[1], (float)initParams[2], (float)initParams[3], (float)initParams[4], (float)initParams[5], child2Weights1, child2Weights2);

            return(children);
        }