Exemplo n.º 1
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()
        {
            // TODO: Add your initialization logic here
            //initParams = new object[] { new Vector2(graphics.PreferredBackBufferWidth / 2, graphics.PreferredBackBufferHeight / 2), new Vector2(0f, 0f), 0f, 1f, 4f, 10f };
            Random rnd = new Random();

            //initParams = 1. Position, 2. Velocity, 3. Rotation, 4. Mass, 5. Monopropellant, 6. Fuel
            //initParams = new object[] { new Vector2(graphics.PreferredBackBufferWidth / 3, graphics.PreferredBackBufferHeight / 3), new Vector2(0f, 0f), 0f, 10f, 4f, 10f };
            initParams  = new object[] { new Vector2(rnd.Next(100, graphics.PreferredBackBufferWidth - 100), rnd.Next(100, graphics.PreferredBackBufferHeight / 2)), new Vector2((float)rnd.NextDouble() * 200 - 100, (float)rnd.NextDouble() * 100 - 50), (float)(rnd.NextDouble() * 0.4 - 0.2), 10f, 4f, 10f };
            boosterList = new BoosterList();
            //rnd.Next(Content.Load<Texture2D>("landing pad").Width / 2, graphics.PreferredBackBufferWidth - (Content.Load<Texture2D>("landing pad").Width / 2))
            landingPosition     = new Vector2(rnd.Next(Content.Load <Texture2D>("landing pad").Width / 2, graphics.PreferredBackBufferWidth - (Content.Load <Texture2D>("landing pad").Width / 2)), graphics.PreferredBackBufferHeight - 200);
            mainRocketThrust    = 15f;
            directionalThrust   = 5f;
            angularAcceleration = 0f;
            gravity             = 10f;
            fuelConsumption     = -0.8f;
            monoConsumption     = -0.2f;
            generationComplete  = false;

            populationSize    = 500;
            eliteIndividuals  = 10;
            numNewIndividuals = 20;
            numEliteChildren  = 30;
            crossoverRate     = 0.6f;
            crossover1Section = new int[] { 4, 7, 0, 7 };
            crossover2Section = new int[] { 4, 7, 0, 2 };
            mutationRate      = 0.4f;

            base.Initialize();
        }
Exemplo n.º 2
0
        private static Booster TournamentSelection(Random rnd, BoosterList currentGen, int tournamentSize)
        {
            BoosterList selection = new BoosterList();

            for (int i = 0; i < tournamentSize; i++)
            {
                int index = rnd.Next(0, currentGen.Count());
                selection.Add(currentGen[index]);
            }

            selection.Sort((a, b) => b.GetGameScore().CompareTo(a.GetGameScore()));
            return(selection[0]);
        }
Exemplo n.º 3
0
        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;
        }
Exemplo n.º 4
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);
        }
Exemplo n.º 5
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);
        }
Exemplo n.º 6
0
        private static Booster RouletteSelection(Random rnd, BoosterList currentGen)
        {
            float sumOfFitness = 0;
            int   fixedPoint;
            float partialSum = 0;

            foreach (Booster b in currentGen)
            {
                sumOfFitness += b.GetGameScore();
            }

            fixedPoint = rnd.Next((int)sumOfFitness);

            foreach (Booster b in currentGen)
            {
                partialSum += b.GetGameScore();
                if (partialSum > fixedPoint)
                {
                    return(b);
                }
            }

            return(currentGen[0]);
        }