private GeneticAlgorithm PrepareGeneticAlgorithm(string[][] board) { double[][] parsedBoard = this.ParseBoardToDouble(board); var chromosome = new TicTacToeChromosome(parsedBoard); var population = new Population(50, 100, chromosome); var fitness = new TicTacToeFitness { Minimum = BOARD_SIZE }; var selection = new EliteSelection(); var crossover = new UniformCrossover(0.5f); var mutation = new UniformMutation(); var termination = new OrTermination(new ITermination[] { new FitnessStagnationTermination(3000), new FitnessThresholdTermination(4), new FitnessThresholdTermination(3), new FitnessThresholdTermination(2) }); var geneticAlgorithm = new GeneticAlgorithm(population, fitness, selection, crossover, mutation) { Termination = termination }; #if DEBUG geneticAlgorithm.GenerationRan += (sender, e) => { var bestChromosome = geneticAlgorithm.BestChromosome as TicTacToeChromosome; Console.WriteLine($"Generation { geneticAlgorithm.GenerationsNumber }: Position ({ bestChromosome.Position.X }, { bestChromosome.Position.Y }) = { bestChromosome.Fitness }"); }; #endif return(geneticAlgorithm); }
/// <summary> /// Main method which takes all the required data and components and plays TicTacToe. /// </summary> public override void PrepareNextAction() { if (!this.CanMakeMove()) { return; } if (this.IsBotConnectedToGame()) { string[][] board = this.BuildBoardMatrix(); GeneticAlgorithm geneticAlgorithm = this.PrepareGeneticAlgorithm(board); // Start genetic algorithm ale let it find best solution. geneticAlgorithm.Start(); TicTacToeChromosome chromosome = geneticAlgorithm.BestChromosome as TicTacToeChromosome; Point boardCell = chromosome.Position; IEnumerable <IWindowControl> buttons = this.FindButtons(); this.PressButton(buttons, boardCell); } else { throw new IndexOutOfRangeException("Bot is not connected to any running process."); } }