// =========================================================================================== // CONSTRUCTOR /* * I used this link when I wrote the GA to remember the structure of the program: * http://cstheory.stackexchange.com/questions/14758/tournament-selection-in-genetic-algorithms * selection modifier could be % of pop chosen for tournament selection... depends on selection implementation */ public GA( Game snake, int pop_size ) { // DEFAULT PROPERTY VALUES ProbMutate = (float)0.05; StdDev = (float)0.50; SelectMod = (float)0.10; NumRuns = (int)30; // INITIALIZE VARIABLES SnakeGame = (Game)snake; NeuralNet = snake.GetNN(); Genes = NeuralNet.NumWeights; PopSize = pop_size; Population = new double[PopSize][]; for (int p = 0; p < pop_size; p++) Population[p] = new double[Genes]; Fitnesses = new double[PopSize]; // CREATE AN ARRAY FOR TOURNAMENT SELECTION shuffled = new int[pop_size]; for (int x = 0; x < pop_size; x++) { shuffled[x] = x; } ShuffleArray(shuffled); // SHUFFLE IT ONCE BEFORE USE }
static void Main(string[] args) { //make a game Game game = new Game(20, 10, 3); //make a ga and give it a game GA ga = new GA(game, 50); // SET PROPERTIES ga.NumRuns = 5; ga.ProbMutate = 0.8; ga.StdDev = 1d; ga.SelectMod = 0.30d; //train ga for 1000 iterations ga.TrainFor(1000); //give game correct controller for best chromosome ga.SetUpBestGame(); //display the game while (true) { game.Play(30); Console.ReadKey(); } }