public EvolutionEngine(int maximumNumberOfGenerations, FitnessEvaluator fitnessEvaluator, PopulationMutator populationMutator, IFittestIdentifier fittestIdentifier) { if (maximumNumberOfGenerations <= 0) { throw new ArgumentOutOfRangeException(nameof(maximumNumberOfGenerations)); } _maximumNumberOfGenerations = maximumNumberOfGenerations; _fitnessEvaluator = fitnessEvaluator; _populationMutator = populationMutator; _fittestIdentifier = fittestIdentifier; }
public IMeal Solve(IEnumerable <DietaryReferenceIntake> maxWeight, IEnumerable <Food> items, int iterationCount) { MaxWeight = maxWeight; Items = items; IterationCount = iterationCount; var population = PopulationInitializer.InitializePopulation(MaxWeight, items); for (int i = 0; i < IterationCount; i++) { if (IsDebug) { Console.WriteLine($"{population.GetFittest().TotalCostSum:F2}"); } population.RemoveNotFit(); PopulationCrossoverer.CrossoverPopulation(population); PopulationMutator.MutatePopulation(population); population.MergeNewGeneration(); } return(population.GetFittest()); }
public static int Run(ProgramSettings settings) { ThreadPool.SetMaxThreads(Environment.ProcessorCount, Environment.ProcessorCount); PrintSettings(settings); (var trainDataset, var testDataset) = DatasetLoader.LoadDatasets( trainDataFilename: settings.TrainDataPath, trainLabelsFilename: settings.TrainLabelsPath, testDataFilename: settings.TestDataPath, testLabelsFilename: settings.TestLabelsPath, classificationType: settings.ClassificationType); Console.WriteLine(); PrintTrainDatasetInformation(trainDataset); Console.WriteLine(); var hyperRectangleCoverageComputer = new HyperRectangleCoverageComputer(dataset: trainDataset); var featureTestIntervalConveter = new FeatureTestDimensionIntervalConverter(trainDataset); var ruleAntecedentHyperRectangleConverter = new RuleAntecedentHyperRectangleConverter(featureTestIntervalConveter); var seedSelector = new CFSBESeedFinder( ruleConverter: ruleAntecedentHyperRectangleConverter, coverageComputer: hyperRectangleCoverageComputer); var antecedentCreator = new AntecedentCreator(ruleAntecedentHyperRectangleConverter: ruleAntecedentHyperRectangleConverter); var consequentCreator = settings.ClassificationType switch { ClassificationType.SingleLabel => (IConsequentCreator) new SingleLabelConsequentCreator(dataset: trainDataset), ClassificationType.MultiLabel => (IConsequentCreator) new MultiLabelConsequentCreator(dataset: trainDataset, threshold: settings.RuleConsequentThreshold), _ => throw CommonExceptions.UnknownClassificationType, }; var hyperRectangleIntersector = new HyperRectangleIntersector(trainDataset); var nonIntersectingHyperRectangleCreator = new NonIntersectingRectangleCreator(hyperRectangleIntersector); var ruleCreator = new CoverageAwareRuleCreator( seedSelector: seedSelector, boxConverter: ruleAntecedentHyperRectangleConverter, boxCreator: nonIntersectingHyperRectangleCreator, coverageComputer: hyperRectangleCoverageComputer, antecedentCreator: antecedentCreator, consequentCreator: consequentCreator, hyperRectangleIntersector: hyperRectangleIntersector, targetNumberOfInstancesToCover: settings.TargetNumberOfInstancesToCoverDuringRuleCreationg, runExpensiveSanityChecks: settings.SkipExpensiveSanityChecks); var individualMutationChooser = BiasedOptionChooser <IndividualMutationType> .Create( new Dictionary <IndividualMutationType, int>() { [IndividualMutationType.AddRule] = settings.IndividualMutationAddRuleWeight, [IndividualMutationType.ModifyRule] = settings.IndividualMutationModifyRuleWeight, [IndividualMutationType.RemoveRule] = settings.IndividualMutationRemoveRuleWeight }); var ruleSwappingindividualMutator = new RuleSwappingIndividualMutator( mutationChooser: individualMutationChooser, ruleCreator: ruleCreator); var populationMutator = new PopulationMutator( individualMutator: ruleSwappingindividualMutator, mutantsPerGeneration: settings.MutantsPerGeneration, maximumFailedAttemptsPerGeneration: settings.MaximumFailedMutationAttemptsPerGeneration); var trainMetrics = IMetricParser.ParseMetrics( dataset: trainDataset, metricsNames: settings.MetricNames, classificationType: settings.ClassificationType); var trainFitnessEvaluator = new FitnessEvaluator(trainMetrics); var testMetrics = IMetricParser.ParseMetrics( dataset: testDataset, metricsNames: settings.MetricNames, classificationType: settings.ClassificationType); var testFitnessEvaluator = new FitnessEvaluator(testMetrics); var fittestIdentifier = IFittestIdentifierParser.Parse( name: settings.SelectionAlgorithm, fittestCount: settings.PopulationSize); var individualCreator = new SingleRuleIndividualCreator(ruleCreator: ruleCreator); var initialPopulation = CreateInitialPopulation( individualCreator: individualCreator, settings: settings); var modelSerializer = new ModelSerializer(); var populationFitnessSerializer = new PopulationFitnessSerializer( trainFitnessEvaluator: trainFitnessEvaluator, testFitnessEvaluator: testFitnessEvaluator); var trainPredictionsSerializer = new PredictionsSerializer(dataset: trainDataset); var testPredictionsSerializer = new PredictionsSerializer(dataset: testDataset); var persistentOutputManager = new PersistentOutputManager( outputDirectory: settings.OutputDirectory, saveModels: settings.SaveModels, saveTrainPredictions: settings.SaveTrainPredictions, saveTestPredictions: settings.SaveTestPredictions, modelSerializer: modelSerializer, populationFitnessSerializer: populationFitnessSerializer, trainPredictionsSerializer: trainPredictionsSerializer, testPredictionsSerializer: testPredictionsSerializer); //var consistencyChecker = new RuleConsistencyChecker( // ruleAntecedentHyperRectangleConverterconverter: ruleAntecedentHyperRectangleConverter, // hyperRectangleIntersector: hyperRectangleIntersector); var evolutionEngine = new EvolutionEngine( maximumNumberOfGenerations: settings.MaximumGenerations, fitnessEvaluator: trainFitnessEvaluator, populationMutator: populationMutator, fittestIdentifier: fittestIdentifier); var lastGenerationSummary = evolutionEngine.Run(initialPopulation); if (lastGenerationSummary.GenerationNumber == settings.MaximumGenerations) { Console.WriteLine($"Evolution cycle stopped. Reason: maximum number of generations reached."); } else { Console.WriteLine($"Evolution cycle stopped. Reason: maximum number of generations reached."); } persistentOutputManager.SaveWhatMustBeSaved(population: lastGenerationSummary.Population); Console.WriteLine("Done."); return(0); }