public void InitializeSimulation() { AlgorithmAndSimulationType.Clear(); for (int i = 0; i < Algorithm.TotalNetworks; ++i) { IWeightedNetwork network = Algorithm.NetworksAndFitness.Select(x => x.Key).ElementAt(i); //AlgorithmAndSimulationType.Add(network, SimulationType.()); } throw new NotImplementedException(); }
private static void RunSnakeGame(KeyValuePair <IWeightedNetwork, SnakeGame> networkAndGame, bool setfitness = true) { SnakeGame currentSnakeGame = networkAndGame.Value; IWeightedNetwork currentNetwork = networkAndGame.Key; double totalFitness = 0.0; const int TOTAL_PLAYS_PER_NETWORK = 10; for (int i = 0; i < TOTAL_PLAYS_PER_NETWORK; ++i) { int iterationsSinceLastFood = 0; int previousSnakeLength = currentSnakeGame.SnakeLength; while (!currentSnakeGame.IsPlayerGameOver()) { double[] input = GetNetworkInput(currentSnakeGame); currentNetwork.SetInput(input); currentNetwork.Propagate(); double[] output = currentNetwork.GetOutput(); double maxNumber = output.Max(); int index = output.ToList().IndexOf(maxNumber); if (currentSnakeGame.Printer.IsPrintingMap) { ConsoleColor previousColor = Console.ForegroundColor; for (int j = 0; j < output.Length; ++j) { if (j == index) { Console.ForegroundColor = ConsoleColor.DarkRed; } Console.SetCursorPosition(0, currentSnakeGame.Map.Height + j + 1); Console.Write($"{Enum.GetValues(typeof(Direction)).GetValue(j)}"); Console.ForegroundColor = previousColor; } } Direction newDirection = Enum.TryParse(index.ToString(), out Direction result) ? result : Direction.None; if (newDirection == Direction.Up && currentSnakeGame.Printer.IsPrintingMap) { } //string stringNumberToParse = (Math.Round(output[0], 0) % 4).ToString(); //string stringNumberToParse = Math.Round(4 / (1 + output[0]), 0).ToString(); //Direction newDirection = Enum.TryParse(stringNumberToParse, out Direction result) // ? result // : Direction.None; currentSnakeGame.SetSnakeDirection(newDirection); currentSnakeGame.NextRound(); if (nextRoundDelay > 0) { Thread.Sleep(nextRoundDelay); } ++iterationsSinceLastFood; if (currentSnakeGame.SnakeLength > previousSnakeLength) { iterationsSinceLastFood = 0; previousSnakeLength = currentSnakeGame.SnakeLength; } if (iterationsSinceLastFood >= FORCE_BREAK_AFTER_ITERATIONS) { break; } } //totalFitness += (currentSnakeGame.SnakeLength - INITIAL_SNAKE_LENGTH) * currentSnakeGame.TotalMoves; totalFitness += ((currentSnakeGame.SnakeLength - INITIAL_SNAKE_LENGTH) * currentSnakeGame.TotalMoves) + currentSnakeGame.TotalMoves; //totalFitness += (currentSnakeGame.SnakeLength - INITIAL_SNAKE_LENGTH); networkAndGame.Value.InitializeGame(); networkAndGame.Value.PlaceSnakeOnMap(); } if (setfitness) { lock (setFitnessLock) { algorithm.SetFitness(currentNetwork, totalFitness / TOTAL_PLAYS_PER_NETWORK); } } }