public void LoadTrainedSnake() { snake = new BotSnake(true); SnakeBotData data = SaveLoad.LoadSnakeBot(); snake.LoadSnakeData(data); }
public static void SaveSnakeBot(BotSnake snake) { BinaryFormatter bf = new BinaryFormatter(); FileStream stream = new FileStream(Application.LocalUserAppDataPath + "\\config.snakeBotData", FileMode.Create); SnakeBotData data = new SnakeBotData(snake); bf.Serialize(stream, data); stream.Close(); }
//construct public SnakePopulation(int size, double mutationRate = 0.07) //start with mutation rate of 1% { Snakes = new BotSnake [size]; for (int i = 0; i < Snakes.Length; ++i) { Snakes [i] = new BotSnake(); } GlobalBestSnake = Snakes [0].Clone(); PopulationMutationRate = mutationRate; }
public void InitEnemySnake() { if (Configerator.instance.ActiveLevel.EnemySnakeEnabled) { enemySnake = new BotSnake(false); enemySnake.CurrentFoodUnit = snake.CurrentFoodUnit; SnakeBotData data = SaveLoad.LoadSnakeBot(); enemySnake.LoadSnakeData(data); } }
public void CreateNextGeneration() { BotSnake [] NextGen = new BotSnake [Snakes.Length]; NextGen [0] = GlobalBestSnake.Clone(); if (PopulationMutationRate > 0.01) { PopulationMutationRate -= 0.01; } /* * //half mutation rate every 10 generations so that search space is searched more in the begining and we start convergence toward optimum later * if (generationCounter % 10 == 0 && PopulationMutationRate > 0.015) PopulationMutationRate *= 0.5; * * //if after 40 generations snakes are still small, increse mutationRate so that we escape bad local optimum * if (generationCounter > 40 && GlobalBestSnake.Length < 8) * { * PopulationMutationRate *= 10; * generationCounter = 0; * } */ for (int i = 1; i < NextGen.Length; ++i) { BotSnake firstPartner = SelectSnake(); BotSnake child; if (MersenneTwister.Randoms.NextDouble() < crossoverProbability) { BotSnake secondPartner = SelectSnake(); child = firstPartner.Crossover(secondPartner); } else { child = firstPartner.Clone(); } child.Mutate(PopulationMutationRate); NextGen [i] = child; } Snakes = (BotSnake [])NextGen.Clone(); //update/reset population params currentGenerationNo++; generationCounter++; currentBest = 4; PopulationSumOfFitness = 0; //globalBestFitness = 0; numOfDeadSnakes = 0; CurrentBestSnakeIdx = 0; }
public override void DoStep() { // run genethic algorithm if (gen < maxGen) { UpdateAlive(); if (Done()) { Items.Item.allItems.Clear(); GeneticAlgorithm(); WorldRenderer.UpdateGenerationLabel(gen); } } else // if all generations were run, save the best snake from all species if (gen == maxGen) { BotSnake bestSnake = Species[bestSpeciesIdx].GlobalBestSnake; SaveLoad.SaveSnakeBot(bestSnake); } }
private static void RenderSnake() { if (Configerator.instance.GameType == Configerator.Game.bot) { BotWorld botWorld = (BotWorld)instance.World; instance.snakeLabel.Text = "Best Snake Idx: " + botWorld.Species [0].CurrentBestSnakeIdx.ToString(); // COMENT THIS NOT TO RENDER JUST BEST CURRENT SNAKE BotSnake snake = botWorld.Species [0].Snakes [botWorld.Species [0].CurrentBestSnakeIdx]; RenderPiece(snake.HeadPosition, Brushes.Red); foreach (Vector2 part in snake.BodyParts) { RenderPiece(part, Brushes.White); } if (snake.CurrentFoodUnit != null) { RenderPiece(snake.CurrentFoodUnit.Location(), Brushes.Yellow); } // UNCOMMENT THIS TO RENDER ALL SNAKES //int idx = 0; //foreach (BotSnake snake in botWorld.Species [0].Snakes) //{ // if (snake.isDead || idx++ > 0) // { // continue; // } // RenderPiece(snake.HeadPosition, Brushes.Red); // foreach (Vector2 part in snake.BodyParts) // { // RenderPiece(part, Brushes.White); // } // RenderPiece(snake.CurrentFoodUnit.Location(), Brushes.Yellow); //} } else if (Configerator.instance.GameType == Configerator.Game.test) { TestBotWorld testBotWorld = (TestBotWorld)instance.World; instance.snakeLabel.Text = "Testing evolved snake"; BotSnake snake = testBotWorld.snake; RenderPiece(snake.HeadPosition, Brushes.Red); foreach (Vector2 part in snake.BodyParts) { RenderPiece(part, Brushes.White); } if (snake.CurrentFoodUnit != null) { RenderPiece(snake.CurrentFoodUnit.Location(), Brushes.Yellow); } } else { Snake snake = instance.World.snake; RenderPiece(snake.HeadPosition, Brushes.Red); foreach (Vector2 part in snake.BodyParts) { RenderPiece(part, Brushes.White); } if (instance.World.enemySnake != null) { BotSnake enemySnake = instance.World.enemySnake; RenderPiece(enemySnake.HeadPosition, Brushes.Red); foreach (Vector2 part in enemySnake.BodyParts) { RenderPiece(part, Brushes.White); } } } }
public SnakeBotData(BotSnake snake) { snake.SaveSnakeData(ref whi, ref whh, ref who); }