Ejemplo n.º 1
0
        /// <summary>
        /// Simulates the predictions of the gray box random forest.
        /// </summary>
        /// <param name="testData">The test data.</param>
        /// <param name="grayBoxRandomForest">The gray box random forest.</param>
        /// <param name="currentGeneration">The current generation.</param>
        private void SimulatePredictionsOfGrayBoxRandomForest(
            List <DataRecord <TResult> > testData,
            ClassificationForestModel grayBoxRandomForest,
            int currentGeneration)
        {
            for (var currentTimePoint = TimeSpan.Zero;
                 currentTimePoint < this._configuration.CpuTimeout;
                 currentTimePoint += this._configuration.DataRecordUpdateInterval)
            {
                if (currentTimePoint < this._simulationConfiguration.GrayBoxStartTimePoint)
                {
                    continue;
                }

                var currentTestData =
                    GrayBoxSimulation <TTargetAlgorithm, TInstance, TResult> .GetCurrentTestData(testData, currentTimePoint);

                if (!currentTestData.Any())
                {
                    continue;
                }

                var predictions = this.GetPredictionsOfGrayBoxRandomForest(grayBoxRandomForest, currentTestData);
                this.UpdatePredictionDictionaryAndDataDictionary(predictions, currentTestData, currentTimePoint);

                var currentPredictionScores = this.GetPredictionScores(
                    currentGeneration,
                    currentTimePoint,
                    currentTestData);
                this._predictionScoresRecorder.WriteRow(currentPredictionScores.ToStringArray());
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Reads the generation composition files.
        /// </summary>
        private void ReadGenerationCompositionFiles()
        {
            var timer = Stopwatch.StartNew();

            if (!GrayBoxUtils.TryToReadGenerationCompositionFromFile(
                    this._configuration.GenerationInstanceCompositionFile,
                    out var generationInstanceComposition))
            {
                throw new ArgumentException(
                          $"Cannot read generation instance composition from {this._configuration.GenerationInstanceCompositionFile.FullName}!");
            }

            if (!GrayBoxUtils.TryToReadGenerationCompositionFromFile(
                    this._configuration.GenerationGenomeCompositionFile,
                    out var generationGenomeComposition))
            {
                throw new ArgumentException(
                          $"Cannot read generation genome composition from {this._configuration.GenerationGenomeCompositionFile.FullName}!");
            }

            if (generationInstanceComposition.Count != generationGenomeComposition.Count)
            {
                throw new ArgumentException(
                          "The number of generations given by the generation instance composition does not equal the number of generations given by the generation genome composition.");
            }

            Debug.Assert(
                GrayBoxSimulation <TTargetAlgorithm, TInstance, TResult> .ValidateGenomeInstancePairsOfListOfDataRecordsAndGenerationComposition(
                    this._allDataRecords,
                    generationInstanceComposition,
                    generationGenomeComposition),
                "The genome instance pairs, given by the data records, do not contain all genome instance pairs, given by the generation composition.");

            var(convertedGenerationInstanceComposition, instanceStringDictionary) =
                GrayBoxUtils.ConvertGenerationInstanceComposition(
                    generationInstanceComposition,
                    this._targetAlgorithmFactory);

            var(convertedGenerationGenomeComposition, genomeStringDictionary) =
                GrayBoxUtils.ConvertGenerationGenomeComposition(
                    generationGenomeComposition,
                    this._parameterTree);

            this._generationInstanceComposition = convertedGenerationInstanceComposition;
            this._instanceStringDictionary      = instanceStringDictionary;
            this._generationGenomeComposition   = convertedGenerationGenomeComposition;
            this._genomeStringDictionary        = genomeStringDictionary;

            timer.Stop();
            LoggingHelper.WriteLine(
                VerbosityLevel.Info,
                $"Reading in generation composition files took {timer.Elapsed.TotalSeconds} seconds.");
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Evaluates the given mini tournaments.
        /// </summary>
        /// <param name="miniTournaments">The mini tournaments.</param>
        /// <param name="genomeToGenomeStatsPairs">The <see cref="ImmutableGenome"/> to <see cref="GrayBoxSimulationGenomeStatsPair{TInstance,TResult}"/> dictionary.</param>
        private void EvaluateMiniTournaments(
            List <ImmutableList <ImmutableGenome> > miniTournaments,
            Dictionary <ImmutableGenome, GrayBoxSimulationGenomeStatsPair <TInstance, TResult> > genomeToGenomeStatsPairs)
        {
            var partitionOptions = new ParallelOptions {
                MaxDegreeOfParallelism = this._configuration.MaximumNumberParallelThreads
            };
            var rangePartitioner = Partitioner.Create(miniTournaments, true);

            Parallel.ForEach(
                rangePartitioner,
                partitionOptions,
                (miniTournament, loopState) =>
            {
                var currentGenomeStatsPairs =
                    miniTournament.Select(participant => genomeToGenomeStatsPairs[participant]).ToList();

                var blackBoxTournamentRanking = this._runEvaluator
                                                .Sort(currentGenomeStatsPairs.Select(pair => pair.BlackBoxGenomeStats))
                                                .Select(gs => gs.Genome).ToList();

                var desiredNumberOfTournamentWinners =
                    (int)Math.Ceiling(miniTournament.Count * this._configuration.TournamentWinnerPercentage);

                var grayBoxTournamentWinners = this._runEvaluator
                                               .Sort(currentGenomeStatsPairs.Select(pair => pair.GrayBoxGenomeStats))
                                               .Select(gs => gs.Genome).Take(desiredNumberOfTournamentWinners).ToList();

                var(percentageOfTournamentWinnerChanges, adaptedWsCoefficient) =
                    GrayBoxSimulation <TTargetAlgorithm, TInstance, TResult> .GetTournamentStatistics(
                        blackBoxTournamentRanking,
                        grayBoxTournamentWinners);

                this._bagOfPercentageOfTournamentWinnerChangesPerTournament.Add(percentageOfTournamentWinnerChanges);
                this._bagOfAdaptedWsCoefficientsPerTournament.Add(adaptedWsCoefficient);
            });
        }