public static void WriteGeneration(BotVersion botVersion, TetrisGeneration tetrisGeneration) { string path = GetBotVersionGensLogFilePath(botVersion); if (path == "") { Debug.Log("Wrong bot version"); } else { List <TetrisGeneration> currentGenerations = ReadGenerationsList(botVersion, path); if (currentGenerations == null) { currentGenerations = new List <TetrisGeneration>(); } if (tetrisGeneration.genIndex > currentGenerations.Count) { currentGenerations.Add(tetrisGeneration); } else { currentGenerations[tetrisGeneration.genIndex - 1] = tetrisGeneration; } string json = JsonHelper.ToJson(currentGenerations, true); File.WriteAllText(path, json); } }
//The data of the current generation is saved in a log file private void SaveCurrentGeneration() { TetrisGeneration currentGeneration = new TetrisGeneration(); currentGeneration.genIndex = generation; currentGeneration.bestScore = best.GetScore(); currentGeneration.bestWeights = best.GetWeights(); currentGeneration.scoreMean = scoreSum / population.Count; currentGeneration.pieces = best.GetPieces(); currentGeneration.level = best.GetLevel(); currentGeneration.lines = best.GetLines(); LogWriter.WriteGeneration(botVersion, currentGeneration); }
/// <summary> /// Giving a generation, loads that generation from a file and sets its weights to the weights of the bot /// </summary> private void LoadWeightsFromFile() { TetrisGeneration tetrisGeneration = LogWriter.GetGeneration(botVersion, generation - 1); if (tetrisGeneration != null) { holesWeight = tetrisGeneration.bestWeights[0]; bumpinessWeight = tetrisGeneration.bestWeights[1]; linesWeight = tetrisGeneration.bestWeights[2]; linesHolesWeight = tetrisGeneration.bestWeights[3]; if (botVersion == BotVersion.HumanizedBot) { humanizedWeight = tetrisGeneration.bestWeights[4]; } UpdateUIWeights(); UpdateUIGeneration(generation); } }