private GeneticAlgorithm(int depth, int size, int populationSize, ISolutionDefinition solutionDefinition) { _solutionDefinition = solutionDefinition; _depth = depth; _size = size; _populationSize = populationSize; _crossOverCount = (int)Math.Truncate(populationSize * 0.6); _mutationCount = populationSize - _eliteChildren - _crossOverCount; _solutions = new Solution[populationSize]; _nextSolutions = new Solution[populationSize]; for (int i = 0; i < _populationSize; i++) { _nextSolutions[i] = new Solution(_depth, _size); } }
public static Solution[] FindBestSolutions( int runTime, int populationCount, int depth, int solutionSize, ISolutionDefinition solutionDefinition, Solution[] initialSolution) { var stopwatch = new Stopwatch(); stopwatch.Start(); var ga = new GeneticAlgorithm(depth, solutionSize, populationCount, solutionDefinition); ga.AddInitial(initialSolution); while (stopwatch.ElapsedMilliseconds < runTime) { ga.CreateNextGeneration(1 - (double)stopwatch.ElapsedMilliseconds / (double)runTime); } return(ga.GetSortedResult()); }