public CoverageAwareRuleCreator(CFSBESeedFinder seedSelector, RuleAntecedentHyperRectangleConverter boxConverter, NonIntersectingRectangleCreator boxCreator, HyperRectangleCoverageComputer coverageComputer, AntecedentCreator antecedentCreator, IConsequentCreator consequentCreator, HyperRectangleIntersector hyperRectangleIntersector, int targetNumberOfInstancesToCover, Dataset dataset, bool runExpensiveSanityChecks)
 {
     _seedSelector      = seedSelector;
     _boxConverter      = boxConverter;
     _boxCreator        = boxCreator;
     _coverageComputer  = coverageComputer;
     _antecedentCreator = antecedentCreator;
     _consequentCreator = consequentCreator;
     _targetNumberOfInstancesToCover = targetNumberOfInstancesToCover;
     _rectangleIntersector           = hyperRectangleIntersector;
     _dataset = dataset;
     _runExpensiveSanityChecks = runExpensiveSanityChecks;
 }
Exemple #2
0
        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);
        }