public void Start() { GeneFactory.Initialize(_config.Genes); var chromosome = new Chromosome(false, GeneFactory.Config); //todo: should use return value var cost = _fitness.Evaluate(chromosome); LogProvider.GenerationsLogger.Info(Termination); var best = (Chromosome)((SharpeMaximizer)_fitness).Best; var info = $"Algorithm: {_config.AlgorithmTypeName}, Fitness: {chromosome.Fitness}, {_fitness.Name}: " + $"{cost.ToString("F")}, {best.ToKeyValueString()}"; LogProvider.GenerationsLogger.Info(info); }
public void Start() { if (_executor == null) { throw new Exception("Executor was not initialized"); } //create the population IList <IChromosome> list = new List <IChromosome>(); GeneFactory.Initialize(_config.Genes); for (int i = 0; i < _config.PopulationSize; i++) { //first chromosome always use actuals. For others decide by config var isActual = i == 0 || _config.UseActualGenesForWholeGeneration; list.Add(new Chromosome(isActual, GeneFactory.Config)); } int max = _config.PopulationSizeMaximum < _config.PopulationSize ? _config.PopulationSize * 2 : _config.PopulationSizeMaximum; _population = new PreloadPopulation(_config.PopulationSize, max, list); _population.GenerationStrategy = new PerformanceGenerationStrategy(); //create the GA itself var ga = new GeneticAlgorithm(_population, _fitness, new TournamentSelection(), _config.OnePointCrossover ? new OnePointCrossover() : new TwoPointCrossover(), new UniformMutation(true)); //subscribe to events ga.GenerationRan += GenerationRan; ga.TerminationReached += TerminationReached; ga.TaskExecutor = _executor; ga.Termination = new OrTermination(new FitnessStagnationTermination(_config.StagnationGenerations), new GenerationNumberTermination(_config.Generations)); ga.Reinsertion = new ElitistReinsertion(); ga.MutationProbability = _config.MutationProbability; ga.CrossoverProbability = _config.CrossoverProbability; //run the GA ga.Start(); }