Chromosome Breed(Chromosome a, Chromosome b) { bool mutateBet = Program.RNG.Next(500) == 0; bool mutateScalar = Program.RNG.Next(500) == 0; Chromosome child = new Chromosome(m_countMethods[Program.RNG.Next(m_countMethods.Length)], Program.RNG.Next(2) == 0 ? a.CountScalar : b.CountScalar, Program.RNG.Next(2) == 0 ? a.BetSize : b.BetSize); child.Fitness = TestFitness(child); return child; }
List<Chromosome> GeneratePopulation(int count) { List<Chromosome> ret = new List<Chromosome>(); for (int i = 0; i < count; i++) { Chromosome chromosome = new Chromosome(m_countMethods[Program.RNG.Next(m_countMethods.Length)], 2, /* Program.RNG.Next(1, 21), /* Count Scalar */ 5+i /* Bet size */ ); chromosome.Fitness = TestFitness(chromosome); Console.WriteLine(i); ret.Add(chromosome); } return ret; }
double TestFitness(Chromosome chromosome) { DateTime startTime = DateTime.Now; BlackjackGame game = new BlackjackGame(true, 6, chromosome.CountMethod, 0); game.AddPlayer(new BasicStrategyAIEntity(chromosome.CountScalar, BANKROLL, chromosome.BetSize, 9999)); BasicStrategyAIEntity entity = game.RunGame(10000, BANKROLL * 100); TimeSpan timeTaken = DateTime.Now - startTime; double fitness = (double)entity.Balance;// *Math.Max(10000 - (float)timeTaken.TotalMilliseconds, 0); chromosome.TimeAchieved = timeTaken.TotalMilliseconds; chromosome.BalanceAchieved = entity.Balance; if (fitness > m_highestSeenFitness) { m_highestSeenFitness = fitness; m_fittestEntityResult = entity; m_fittestChromosome = chromosome; } return fitness; }
Chromosome GetFittest(Chromosome a, Chromosome b) { double aFitness = TestFitness(a); double bFitness = TestFitness(b); return aFitness > bFitness ? a : b; }