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; }
/// <summary> /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Update(GameTime gameTime) { float deltaTime = (float)gameTime.ElapsedGameTime.TotalSeconds; // TODO: Add your update logic here var kstate = Keyboard.GetState(); var mstate = Mouse.GetState(); if (currentGameState != GameState.MainMenu) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.OemTilde)) { currentGameState = GameState.MainMenu; } generationComplete = true; foreach (Booster b in boosterList) { if (b.GetGameScore() == 0) { generationComplete = false; double[] outputs = ANN.CalculateOutputs(b, ANN.CalculateHiddenValues(b)); if ((((currentGameState == GameState.NeuralNet) && (outputs[0] >= 0.5)) || ((currentGameState == GameState.PlayerControlled) && kstate.IsKeyDown(Keys.Up))) && b.GetFuel() > 0) { b.ChangeVelocity(new Vector2((float)(mainRocketThrust * Math.Sin(b.GetBoosterRotation()) * b.GetBoosterMass() * deltaTime), (float)(gravity * b.GetBoosterMass() - (mainRocketThrust * Math.Cos(b.GetBoosterRotation()) * b.GetBoosterMass())) * deltaTime)); b.ChangeFuel(fuelConsumption * deltaTime); } else if ((b.GetBoosterPosition().Y + (b.GetBoosterTexture().Height / 2)) < landingPosition.Y) { b.ChangeVelocity(new Vector2(0f, gravity * b.GetBoosterMass() * deltaTime)); } if ((((currentGameState == GameState.NeuralNet) && (outputs[1] >= 0.5)) || ((currentGameState == GameState.PlayerControlled) && kstate.IsKeyDown(Keys.Left))) && b.GetMonopropellant() > 0 && ((b.GetBoosterPosition().Y + (b.GetBoosterTexture().Height / 2)) < landingPosition.Y)) { angularAcceleration = (1 / 2 * gravity * b.GetBoosterMass() * Math.Sin(b.GetBoosterRotation()) - (directionalThrust * b.GetBoosterMass())); angularAcceleration = angularAcceleration / (0.66 * b.GetBoosterMass() * 10); b.ChangeAngVelocity(angularAcceleration * deltaTime); b.ChangeMono(monoConsumption * deltaTime); } else if ((((currentGameState == GameState.NeuralNet) && (outputs[2] >= 0.5)) || ((currentGameState == GameState.PlayerControlled) && kstate.IsKeyDown(Keys.Right))) && b.GetMonopropellant() > 0 && ((b.GetBoosterPosition().Y + (b.GetBoosterTexture().Height / 2)) < landingPosition.Y)) { angularAcceleration = (1 / 2 * gravity * b.GetBoosterMass() * Math.Sin(b.GetBoosterRotation()) + (directionalThrust * b.GetBoosterMass())); angularAcceleration = angularAcceleration / (0.66 * b.GetBoosterMass() * 10); b.ChangeAngVelocity(angularAcceleration * deltaTime); b.ChangeMono(monoConsumption * deltaTime); } else { angularAcceleration = (1 / 2 * gravity * b.GetBoosterMass() * Math.Sin(b.GetBoosterRotation())); angularAcceleration = angularAcceleration / (0.66 * b.GetBoosterMass() * 10); b.ChangeAngVelocityMult((float)0.99); b.ChangeAngVelocity(angularAcceleration * deltaTime); } if (b.GetFuel() < 0) { b.ChangeFuel(Math.Abs(b.GetFuel())); } if (b.GetMonopropellant() < 0) { b.ChangeMono(Math.Abs(b.GetMonopropellant())); } b.ChangeRotation(b.GetAngVelocity() * deltaTime); b.ChangePosition(new Vector2(b.GetBoosterVelocity().X *deltaTime, b.GetBoosterVelocity().Y *deltaTime)); if ((b.GetBoosterPosition().Y + (b.GetBoosterTexture().Height / 2)) >= landingPosition.Y) { b.SetPositionY(landingPosition.Y - (b.GetBoosterTexture().Height / 2)); if ((b.GetBoosterVelocity().Y < 30f) && (b.GetBoosterVelocity().X < 30f) && (b.GetBoosterPosition().X > (landingPosition.X - landingTexture.Width / 2 + 20)) && (b.GetBoosterPosition().X < (landingPosition.X + landingTexture.Width / 2 - 20)) && Math.Abs(b.GetBoosterRotation()) < 0.1f) { successes++; b.ChangeTint(Color.LightGreen); } b.ChangeGameScore(-Math.Abs(b.GetBoosterPosition().X - landingPosition.X)); b.ChangeGameScore(-b.GetBoosterVelocity().Y); b.ChangeGameScore(-Math.Abs(b.GetBoosterVelocity().X)); b.ChangeGameScore(-Math.Abs((float)b.GetBoosterRotation() * 10)); b.ChangeGameScore(-((float)initParams[5] - b.GetFuel())); b.ChangeGameScore(-((float)initParams[4] - b.GetMonopropellant())); b.SetGameScore(1000 / Math.Abs(b.GetGameScore())); b.SetVelocityX(0f); } } } if (generationComplete == true && kstate.IsKeyDown(Keys.Enter)) { Reset(); } else if (generationComplete == true) { if (currentGameState == GameState.NeuralNet) { float closestBoosterDist = 10000; successes = 0; bestGameScoreLastGen = 0; stagnantCtr++; foreach (Booster b in boosterList) { if (Math.Abs(b.GetBoosterPosition().X - landingPosition.X) < closestBoosterDist) { closestBoosterDist = Math.Abs(b.GetBoosterPosition().X - landingPosition.X); } if (b.GetGameScore() > bestGameScoreLastGen) { bestGameScoreLastGen = b.GetGameScore(); } if (b.GetGameScore() > highestGameScore) { stagnantCtr = 0; highestGameScore = b.GetGameScore(); } } using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"D:\Programs\SpaceX Neural Net\neuralnetstats.txt", true)) { file.WriteLine(closestBoosterDist + "," + bestGameScoreLastGen + "," + highestGameScore + "," + successes); } if (generation > 1) { for (int i = 0; i < eliteIndividuals; i++) { Debug.Print("Elite individual " + (i + 1) + " landed the next round with a position of " + boosterList[i].GetBoosterPosition().X + ", a y-velocity of " + boosterList[i].GetBoosterVelocity().Y + ", a rotation of " + boosterList[i].GetBoosterRotation() + ", with " + boosterList[i].GetFuel() + " fuel left and " + boosterList[i].GetMonopropellant() + " mono left, with a score of " + boosterList[i].GetGameScore() + "and weights1 of " + boosterList[i].GetWeights1()[0, 0] + ", " + boosterList[i].GetWeights1()[0, 1] + ", " + boosterList[i].GetWeights1()[0, 2] + ", " + boosterList[i].GetWeights1()[0, 3] + ", " + boosterList[i].GetWeights1()[0, 4]); } } generation++; Reset(); } } } else { if (btnPlayer.isClicked) { 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]); } btnPlayer.Update(mstate); if (btnNeural.isClicked) { currentGameState = GameState.NeuralNet; boosterList.Clear(); for (int i = 0; i < populationSize; i++) { boosterList.AddBooster(Content.Load <Texture2D>("booster"), (Vector2)initParams[0], (Vector2)initParams[1], (float)initParams[2], (float)initParams[3], (float)initParams[4], (float)initParams[5]); } } btnNeural.Update(mstate); if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { Exit(); } } base.Update(gameTime); }