Ejemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="GenerationEvaluationActor{TTargetAlgorithm, TInstance, TResult}"/> class.
        /// </summary>
        /// <param name="targetAlgorithmFactory">The <see cref="ITargetAlgorithmFactory{TTargetAlgorithm,TInstance,TResult}"/>.</param>
        /// <param name="runEvaluator">The <see cref="IRunEvaluator{TInstance,TResult}"/>.</param>
        /// <param name="configuration">The configuration.</param>
        /// <param name="resultStorageActor">The <see cref="IActorRef"/> to the result storage actor.</param>
        /// <param name="parameterTree">The parameter tree.</param>
        /// <param name="customGrayBoxMethods">The <see cref="ICustomGrayBoxMethods{TResult}"/>.</param>
        public GenerationEvaluationActor(
            ITargetAlgorithmFactory <TTargetAlgorithm, TInstance, TResult> targetAlgorithmFactory,
            IRunEvaluator <TInstance, TResult> runEvaluator,
            AlgorithmTunerConfiguration configuration,
            IActorRef resultStorageActor,
            ParameterTree parameterTree,
            ICustomGrayBoxMethods <TResult> customGrayBoxMethods)
        {
            this._targetAlgorithmFactory = targetAlgorithmFactory ?? throw new ArgumentNullException(nameof(targetAlgorithmFactory));
            this._runEvaluator           = runEvaluator ?? throw new ArgumentNullException(nameof(runEvaluator));
            this._configuration          = configuration ?? throw new ArgumentNullException(nameof(configuration));
            this._resultStorageActor     = resultStorageActor ?? throw new ArgumentNullException(nameof(resultStorageActor));
            this._parameterTree          = parameterTree ?? throw new ArgumentNullException(nameof(parameterTree));

            // No need to check for null. Might be null by purpose.
            this._customGrayBoxMethods = customGrayBoxMethods;

            // If Akka.Cluster gets used, watch for disconnecting cluster members to make evaluation rollbacks possible.
            if (Context.System.HasExtension <Cluster>())
            {
                Cluster.Get(Context.System).Subscribe(this.Self, typeof(ClusterEvent.UnreachableMember));
            }

            this.Become(this.WaitingForWorkers);
            this.Self.Tell(new CheckWorkers());
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SortingGenerationEvaluationStrategy{TInstance,TResult}"/> class.
        /// </summary>
        /// <param name="runEvaluator">The <see cref="IRunEvaluator{TInstance,TResult}"/> for sorting genomes.</param>
        /// <param name="genomes">The genomes for evaluation. Do not need to be distinct.</param>
        /// <param name="instances">The instances for evaluation.</param>
        /// <param name="generation">The generation number.</param>
        /// <param name="useGrayBoxInGeneration">Boolean indicating whether to use gray box tuning in current generation.</param>
        public SortingGenerationEvaluationStrategy(
            IRunEvaluator <TInstance, TResult> runEvaluator,
            IEnumerable <ImmutableGenome> genomes,
            IEnumerable <TInstance> instances,
            int generation,
            bool useGrayBoxInGeneration)
        {
            this._runEvaluator = runEvaluator ?? throw new ArgumentNullException(nameof(runEvaluator));

            if (genomes == null)
            {
                throw new ArgumentNullException(nameof(genomes));
            }

            this._genomes = genomes.ToList();

            if (instances == null)
            {
                throw new ArgumentNullException(nameof(instances));
            }

            this._generation             = generation;
            this._useGrayBoxInGeneration = useGrayBoxInGeneration;

            this._genomeToGenomeStats = this._genomes.Distinct(ImmutableGenome.GenomeComparer).ToDictionary(
                g => g,
                g => new GenomeStats <TInstance, TResult>(g, Enumerable.Empty <TInstance>(), instances),
                ImmutableGenome.GenomeComparer);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MiniTournamentActor{TTargetAlgorithm, TInstance, TResult}" /> class.
        /// </summary>
        /// <param name="targetAlgorithmFactory">Produces configured target algorithms to run with the genomes.</param>
        /// <param name="runEvaluator">Object for evaluating target algorithm runs.</param>
        /// <param name="configuration">Algorithm tuner configuration parameters.</param>
        /// <param name="resultStorageActor">
        /// Actor which is responsible for storing all evaluation results that have
        /// been observed so far.
        /// </param>
        /// <param name="parameterTree">Specifies parameters and their relationships.</param>
        /// <param name="tournamentSelector">Actor which provides this actor with evaluation tasks.</param>
        public MiniTournamentActor(
            ITargetAlgorithmFactory <TTargetAlgorithm, TInstance, TResult> targetAlgorithmFactory,
            IRunEvaluator <TResult> runEvaluator,
            AlgorithmTunerConfiguration configuration,
            IActorRef resultStorageActor,
            ParameterTree parameterTree,
            IActorRef tournamentSelector)
            : base(runEvaluator)
        {
            if (tournamentSelector == null)
            {
                throw new ArgumentNullException(nameof(tournamentSelector));
            }

            this._targetAlgorithmFactory =
                targetAlgorithmFactory ?? throw new ArgumentNullException(nameof(targetAlgorithmFactory));
            this._configuration      = configuration ?? throw new ArgumentNullException(nameof(configuration));
            this._resultStorageActor =
                resultStorageActor ?? throw new ArgumentNullException(nameof(resultStorageActor));
            this._parameterTree = parameterTree ?? throw new ArgumentNullException(nameof(parameterTree));

            // Start in waiting for instances state.
            this.WaitForInstances();

            // Finally, find out if there is something to do already.
            tournamentSelector.Tell(new InstancesRequest());
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MiniTournamentManager{I, R}"/> class.
        /// </summary>
        /// <param name="participants">The tournament's participants. Do not need to be distinct.</param>
        /// <param name="instances">The tournament's instances.</param>
        /// <param name="miniTournamentId">The mini tournament id.</param>
        /// <param name="generationId">The generation id.</param>
        /// <param name="runEvaluator">The <see cref="IRunEvaluator{TInstance,TResult}"/>.</param>
        /// <param name="enableRacing">Boolean indicating whether racing is enabled.</param>
        /// <param name="desiredNumberOfWinners">The desired number of winners.</param>
        public MiniTournamentManager(
            IReadOnlyList <ImmutableGenome> participants,
            IEnumerable <TInstance> instances,
            int miniTournamentId,
            int generationId,
            IRunEvaluator <TInstance, TResult> runEvaluator,
            bool enableRacing,
            int desiredNumberOfWinners)
        {
            this.Participants = participants ?? throw new ArgumentNullException(nameof(participants));

            if (instances == null)
            {
                throw new ArgumentNullException(nameof(instances));
            }

            this.MiniTournamentId = miniTournamentId;
            this._generationId    = generationId;

            this._runEvaluator           = runEvaluator ?? throw new ArgumentNullException(nameof(runEvaluator));
            this._enableRacing           = enableRacing;
            this._desiredNumberOfWinners = desiredNumberOfWinners;

            this._genomeToGenomeTournamentKey =
                this.Participants.Distinct(ImmutableGenome.GenomeComparer).ToDictionary(
                    genome => genome,
                    genome => new GenomeTournamentKey(genome, this.MiniTournamentId),
                    ImmutableGenome.GenomeComparer);

            this._genomeToGenomeStats = this._genomeToGenomeTournamentKey.Keys.ToDictionary(
                genome => genome,
                genome => new GenomeStats <TInstance, TResult>(genome, Enumerable.Empty <TInstance>(), instances),
                ImmutableGenome.GenomeComparer)
                                        .ToImmutableDictionary(ImmutableGenome.GenomeComparer);
        }
        /// <summary>
        /// Validates the parameters:
        /// * Null checks for every one of them.
        /// * Checks that the number of training instances provided is sufficient considering the given configuration.
        /// * Checks that configuration about whether to use run time tuning
        /// and the usage of the run time tuning result interface fit together
        /// * Checks that the parameter tree contains parameters.
        /// * Checks that those parameters' identifiers are unique.
        /// </summary>
        /// <param name="targetAlgorithmFactory">
        /// Produces configured instances of the target algorithm to tune.
        /// </param>
        /// <param name="runEvaluator">
        /// Object for evaluating target algorithm runs.
        /// </param>
        /// <param name="trainingInstances">
        /// The set of instances used for tuning.
        /// </param>
        /// <param name="parameterTree">
        /// The parameter tree.
        /// </param>
        /// <param name="configuration">
        /// Algorithm tuner configuration parameters.
        /// </param>
        /// <param name="genomeBuilder">
        /// The genome builder.
        /// </param>
        private static void ValidateParameters(
            ITargetAlgorithmFactory <TTargetAlgorithm, TInstance, TResult> targetAlgorithmFactory,
            IRunEvaluator <TResult> runEvaluator,
            IEnumerable <TInstance> trainingInstances,
            ParameterTree parameterTree,
            AlgorithmTunerConfiguration configuration,
            GenomeBuilder genomeBuilder)
        {
            // Check argument for nulls.
            if (targetAlgorithmFactory == null)
            {
                throw new ArgumentNullException(nameof(targetAlgorithmFactory), "You must specify a target algorithm factory.");
            }

            if (runEvaluator == null)
            {
                throw new ArgumentNullException(nameof(runEvaluator), "You must specify a run evaluator.");
            }

            if (trainingInstances == null)
            {
                throw new ArgumentNullException(nameof(trainingInstances), "You must specify a list of training instances.");
            }

            if (parameterTree == null)
            {
                throw new ArgumentNullException(nameof(parameterTree), "You must specify a parameter tree.");
            }

            if (configuration == null)
            {
                throw new ArgumentNullException(nameof(configuration), "You must specify an algorithm tuner configuration.");
            }

            if (genomeBuilder == null)
            {
                throw new ArgumentNullException(nameof(genomeBuilder), "You must specify a genome builder.");
            }

            // Check enough training instances have been provided.
            var numInstances = trainingInstances.Count();

            if (numInstances < configuration.EndNumInstances)
            {
                throw new ArgumentException(
                          $"In the end, {configuration.EndNumInstances} training instances should be used, but only {numInstances} have been provided.",
                          nameof(trainingInstances));
            }

            // Check that the parameter tree is valid.
            if (!parameterTree.ContainsParameters())
            {
                throw new ArgumentException("Specified parameter tree without parameters.", nameof(parameterTree));
            }

            if (!parameterTree.IdentifiersAreUnique())
            {
                throw new ArgumentException("Specified parameter tree contained duplicate identifiers.", nameof(parameterTree));
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="GenomeSorter{TInstance,TResult}"/> class.
        /// </summary>
        /// <param name="runEvaluator">Object for evaluating target algorithm runs.</param>
        public GenomeSorter(IRunEvaluator <TResult> runEvaluator)
            : base(runEvaluator)
        {
            this.WaitingForEvaluators();

            // Directly check if we have enough evaluators to switch to ready state.
            this.Self.Tell(new CheckWorkers());
        }
Ejemplo n.º 7
0
 /// <summary>
 /// Initializes a new instance of the <see cref="AlgorithmTuner{TTargetAlgorithm,TInstance,TResult}"/> class.
 /// </summary>
 /// <param name="targetAlgorithmFactory">
 /// The target algorithm factory.
 /// </param>
 /// <param name="runEvaluator">
 /// The run evaluator.
 /// </param>
 /// <param name="trainingInstances">
 /// The training instances.
 /// </param>
 /// <param name="parameterTree">
 /// The parameter tree.
 /// </param>
 /// <param name="configuration">
 /// The configuration.
 /// </param>
 /// <param name="genomeBuilder">
 /// The genome builder.
 /// </param>
 public AlgorithmTuner(
     ITargetAlgorithmFactory <TTargetAlgorithm, TInstance, TResult> targetAlgorithmFactory,
     IRunEvaluator <TResult> runEvaluator,
     IEnumerable <TInstance> trainingInstances,
     ParameterTree parameterTree,
     AlgorithmTunerConfiguration configuration,
     GenomeBuilder genomeBuilder)
     : base(targetAlgorithmFactory, runEvaluator, trainingInstances, parameterTree, configuration, genomeBuilder)
 {
 }
        /// <summary>
        /// Initializes a new instance of the <see cref="TournamentSelector{TTargetAlgorithm, TInstance, TResult}" /> class.
        /// </summary>
        /// <param name="targetAlgorithmFactory">Produces configured target algorithms to run with the genomes.</param>
        /// <param name="runEvaluator">Object for evaluating target algorithm runs.</param>
        /// <param name="configuration">Algorithm tuner configuration parameters.</param>
        /// <param name="resultStorageActor">
        /// Actor which is responsible for storing all evaluation results that have
        /// been observed so far.
        /// </param>
        /// <param name="parameterTree">Specifies parameters and their relationships.</param>
        public TournamentSelector(
            ITargetAlgorithmFactory <TTargetAlgorithm, TInstance, TResult> targetAlgorithmFactory,
            IRunEvaluator <TResult> runEvaluator,
            AlgorithmTunerConfiguration configuration,
            IActorRef resultStorageActor,
            ParameterTree parameterTree)
        {
            // Verify parameters.
            if (targetAlgorithmFactory == null)
            {
                throw new ArgumentNullException("targetAlgorithmFactory");
            }

            if (runEvaluator == null)
            {
                throw new ArgumentNullException("runEvaluator");
            }

            if (configuration == null)
            {
                throw new ArgumentNullException("configuration");
            }

            if (resultStorageActor == null)
            {
                throw new ArgumentNullException("resultStorageActor");
            }

            if (parameterTree == null)
            {
                throw new ArgumentNullException("parameterTree");
            }

            // Use them to set fields.
            this._targetAlgorithmFactory = targetAlgorithmFactory;
            this._runEvaluator           = runEvaluator;
            this._configuration          = configuration;
            this._resultStorageActor     = resultStorageActor;
            this._parameterTree          = parameterTree;

            this._selectionProgress = new TournamentSelectionProgress <TInstance, TResult>(this._configuration);

            // If Akka.Cluster gets used, watch for disconnecting cluster members
            // to make tournament rollbacks possible.
            if (Context.System.HasExtension <Cluster>())
            {
                Cluster.Get(Context.System).Subscribe(this.Self, typeof(ClusterEvent.UnreachableMember));
            }

            // Start in waiting for workers state.
            this.WaitingForWorkers();
            // And directly check if we have enough workers to switch to ready state.
            this.Self.Tell(new CheckWorkers());
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MiniTournamentManagerTest"/> class.
        /// </summary>
        public MiniTournamentManagerTest()
        {
            this._instances    = Enumerable.Range(0, 5).Select(i => new TestInstance($"{i}")).ToList();
            this._participants = this.CreateGenomesDescendingByAge(8, 0).ToList();
            this._globalQueue  = new SimplePriorityQueue <GenomeTournamentKey, double>();
            this._runEvaluator = new KeepSuggestedOrder <TestInstance, ContinuousResult>();

            this._defaultManager = new MiniTournamentManager <TestInstance, ContinuousResult>(
                this._participants,
                this._instances,
                42,
                0,
                this._runEvaluator,
                false,
                1);
        }
 /// <summary>
 /// Creates a <see cref="TournamentSelector{TTargetAlgorithm, TInstance, TResult}"/>.
 /// </summary>
 /// <typeparam name="TTargetAlgorithm">Algorithm to execute.</typeparam>
 /// <typeparam name="TInstance">Type of instances the algorithm takes.</typeparam>
 /// <typeparam name="TResult">Type of result the algorithm returns.</typeparam>
 /// <param name="system">The <see cref="ActorSystem"/> to add the actor to.</param>
 /// <param name="targetAlgorithmFactory">Specifies how to create an algorithm instance.</param>
 /// <param name="runEvaluator">Specifies how runs should be compared.</param>
 /// <returns>The created <see cref="TournamentSelector{TTargetAlgorithm, TInstance, TResult}"/>.</returns>
 private IActorRef CreateTournamentSelector <TTargetAlgorithm, TInstance, TResult>(
     ActorSystem system,
     ITargetAlgorithmFactory <TTargetAlgorithm, TInstance, TResult> targetAlgorithmFactory,
     IRunEvaluator <TResult> runEvaluator)
     where TTargetAlgorithm : ITargetAlgorithm <TInstance, TResult> where TInstance : InstanceBase where TResult : ResultBase <TResult>, new()
 {
     return(system.ActorOf(
                Props.Create(
                    () => new TournamentSelector <TTargetAlgorithm, TInstance, TResult>(
                        targetAlgorithmFactory,
                        runEvaluator,
                        this._configuration,
                        this._resultStorageActor,
                        this._parameterTree)),
                AkkaNames.TournamentSelector));
 }
Ejemplo n.º 11
0
 /// <summary>
 /// Initializes a new instance of the <see cref="AlgorithmTuner{TTargetAlgorithm,TInstance,TResult}"/> class.
 /// </summary>
 /// <param name="targetAlgorithmFactory">
 /// The target algorithm factory.
 /// </param>
 /// <param name="runEvaluator">
 /// The run evaluator.
 /// </param>
 /// <param name="trainingInstances">
 /// The training instances.
 /// </param>
 /// <param name="parameterTree">
 /// The parameter tree.
 /// </param>
 /// <param name="configuration">
 /// The configuration.
 /// </param>
 /// <param name="customGrayBoxMethods">The <see cref="ICustomGrayBoxMethods{TResult}"/>, if any. Default is null.</param>
 public AlgorithmTuner(
     ITargetAlgorithmFactory <TTargetAlgorithm, TInstance, TResult> targetAlgorithmFactory,
     IRunEvaluator <TInstance, TResult> runEvaluator,
     IEnumerable <TInstance> trainingInstances,
     ParameterTree parameterTree,
     AlgorithmTunerConfiguration configuration,
     ICustomGrayBoxMethods <TResult> customGrayBoxMethods = null)
     : this(
         targetAlgorithmFactory,
         runEvaluator,
         trainingInstances,
         parameterTree,
         configuration,
         new GenomeBuilder(parameterTree, configuration),
         customGrayBoxMethods)
 {
 }
        /// <summary>
        /// Creates all required messages to send the results of a <see cref="SelectCommand{TInstance}"/>.
        /// </summary>
        /// <param name="runEvaluator">Object for evaluating target algorithm runs.</param>
        /// <returns>The created messages.</returns>
        public object CreateSelectionResultMessage(IRunEvaluator <TResult> runEvaluator)
        {
            // Sort all tournament winners to get global winners.
            // assumption: all mini tournaments of a specific generation were held on the same subset of instances.
            var tournamentWinnerResults = this._miniTournamentResults
                                          .SelectMany(msg => msg.WinnerResults)
                                          .ToDictionary(keyValuePair => keyValuePair.Key, keyValuePair => keyValuePair.Value as IEnumerable <TResult>);

            var orderedTournamentWinners = runEvaluator.Sort(tournamentWinnerResults).ToList();
            var generationBest           = orderedTournamentWinners.First();

            return(new SelectionResultMessage <TResult>(
                       orderedTournamentWinners.ToImmutableList(),
                       this.GenomeToTournamentResults,
                       generationBest,
                       tournamentWinnerResults[generationBest]));
        }
        /// <summary>
        /// Creates the different <see cref="IPopulationUpdateStrategy{TInstance,TResult}"/>s that may be used to
        /// update the population.
        /// </summary>
        /// <param name="targetAlgorithmFactory">
        /// Produces configured instances of the target algorithm to tune.
        /// </param>
        /// <param name="runEvaluator">
        /// Object for evaluating target algorithm runs.
        /// </param>
        /// <returns>The created <see cref="IPopulationUpdateStrategy{TInstance,TResult}"/>s.</returns>
        private IEnumerable <IPopulationUpdateStrategy <TInstance, TResult> > CreatePopulationUpdateStrategies(
            ITargetAlgorithmFactory <TTargetAlgorithm, TInstance, TResult> targetAlgorithmFactory,
            IRunEvaluator <TResult> runEvaluator)
        {
            var tournamentSelector = this._targetAlgorithmRunActors.ActorOf(
                Props.Create(
                    () => new TournamentSelector <TTargetAlgorithm, TInstance, TResult>(
                        targetAlgorithmFactory,
                        runEvaluator,
                        this._configuration,
                        this._targetRunResultStorage,
                        this._parameterTree)),
                AkkaNames.TournamentSelector);

            yield return(new GgaStrategy <TInstance, TResult>(
                             this._configuration,
                             this._parameterTree,
                             this._genomeBuilder,
                             tournamentSelector,
                             this.GeneticEngineering));

            if (this._configuration.ContinuousOptimizationMethod == ContinuousOptimizationMethod.Jade)
            {
                yield return(new DifferentialEvolutionStrategy <TInstance, TResult>(
                                 this._configuration,
                                 this._parameterTree,
                                 this._genomeBuilder,
                                 this._genomeSorter,
                                 this._targetRunResultStorage));
            }

            if (this._configuration.ContinuousOptimizationMethod == ContinuousOptimizationMethod.CmaEs)
            {
                yield return(CovarianceMatrixAdaptationInformationFlowSwitch.CreateCovarianceMatrixAdaptationStrategy <TInstance, TResult>(
                                 this._configuration,
                                 this._parameterTree,
                                 this._genomeBuilder,
                                 this._genomeSorter,
                                 this._targetRunResultStorage));
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="AlgorithmTuner{TTargetAlgorithm,TInstance,TResult,TModelLearner,TPredictorModel,TSamplingStrategy}"/> class.
        /// </summary>
        /// <param name="targetAlgorithmFactory">
        /// Produces configured instances of the algorithm to tune.
        /// </param>
        /// <param name="runEvaluator">
        /// Object for evaluating target algorithm runs.
        /// </param>
        /// <param name="trainingInstances">
        /// The set of instances used for tuning.
        /// </param>
        /// <param name="parameterTree">
        /// Provides the tunable parameters.
        /// </param>
        /// <param name="configuration">
        /// Algorithm tuner configuration parameters.
        /// </param>
        /// <param name="genomeBuilder">
        /// Responsible for creation, modification and crossover of genomes.
        /// Needs to be compatible with the given parameter tree and configuration.
        /// This parameter can be left out if an ordinary <see cref="GenomeBuilder"/> should be used.
        /// </param>
        public AlgorithmTuner(
            ITargetAlgorithmFactory <TTargetAlgorithm, TInstance, TResult> targetAlgorithmFactory,
            IRunEvaluator <TResult> runEvaluator,
            IEnumerable <TInstance> trainingInstances,
            ParameterTree parameterTree,
            AlgorithmTunerConfiguration configuration,
            GenomeBuilder genomeBuilder)
        {
            ValidateParameters(targetAlgorithmFactory, runEvaluator, trainingInstances, parameterTree, configuration, genomeBuilder);
            LoggingHelper.WriteLine(
                VerbosityLevel.Info,
                $"Tuning target algorithm with {parameterTree.GetParameters().Count()} parameters.");

            this._configuration = configuration;
            this._runEvaluator  = runEvaluator;

            this.GeneticEngineering = new GeneticEngineering <TModelLearner, TPredictorModel, TSamplingStrategy>(parameterTree, this._configuration);
            this.IncumbentQuality   = new List <double>();

            this._trainingInstances = trainingInstances.ToList();
            this._instanceSelector  = new InstanceSelector <TInstance>(this._trainingInstances, configuration);
            this._parameterTree     = parameterTree;
            this._genomeBuilder     = genomeBuilder;

            this._targetAlgorithmRunActors = ActorSystem.Create(AkkaNames.ActorSystemName, configuration.AkkaConfiguration);
            this._targetRunResultStorage   = this._targetAlgorithmRunActors.ActorOf(
                Props.Create(() => new ResultStorageActor <TInstance, TResult>()),
                AkkaNames.ResultStorageActor);
            this._genomeSorter = this._targetAlgorithmRunActors.ActorOf(
                Props.Create(() => new GenomeSorter <TInstance, TResult>(runEvaluator)),
                AkkaNames.GenomeSorter);

            this._logWriter = new LogWriter <TInstance, TResult>(parameterTree, configuration);

            this._populationUpdateStrategyManager = new PopulationUpdateStrategyManager <TInstance, TResult>(
                this.CreatePopulationUpdateStrategies(targetAlgorithmFactory, runEvaluator).ToList(),
                configuration);
        }
Ejemplo n.º 15
0
        /// <summary>
        /// Initializes a new instance of the <see cref="GrayBoxSimulation{TTargetAlgorithm, TInstance, TResult}" /> class.
        /// </summary>
        /// <param name="configuration">The <see cref="AlgorithmTunerConfiguration"/>.</param>
        /// <param name="targetAlgorithmFactory">The <see cref="ITargetAlgorithmFactory{TTargetAlgorithm, TInstance, TResult}"/>.</param>
        /// <param name="customGrayBoxMethods">The <see cref="ICustomGrayBoxMethods{TResult}"/>.</param>
        /// <param name="runEvaluator">The <see cref="IRunEvaluator{TInstance, TResult}"/>.</param>
        /// <param name="parameterTree">The <see cref="ParameterTree"/>.</param>
        public GrayBoxSimulation(
            AlgorithmTunerConfiguration configuration,
            ITargetAlgorithmFactory <TTargetAlgorithm, TInstance, TResult> targetAlgorithmFactory,
            ICustomGrayBoxMethods <TResult> customGrayBoxMethods,
            IRunEvaluator <TInstance, TResult> runEvaluator,
            ParameterTree parameterTree)
        {
            ProcessUtils.SetDefaultCultureInfo(CultureInfo.InvariantCulture);

            this._configuration          = configuration ?? throw new ArgumentNullException(nameof(configuration));
            this._customGrayBoxMethods   = customGrayBoxMethods ?? throw new ArgumentNullException(nameof(customGrayBoxMethods));
            this._runEvaluator           = runEvaluator ?? throw new ArgumentNullException(nameof(runEvaluator));
            this._targetAlgorithmFactory = targetAlgorithmFactory ?? throw new ArgumentNullException(nameof(targetAlgorithmFactory));
            this._parameterTree          = parameterTree ?? throw new ArgumentNullException(nameof(parameterTree));

            this._logFileDirectory = new DirectoryInfo(Path.Combine(this._configuration.DataRecordDirectoryPath, "GrayBoxSimulationLogFiles"));
            Directory.CreateDirectory(this._logFileDirectory.FullName);

            LoggingHelper.Configure(
                Path.Combine(this._logFileDirectory.FullName, $"consoleOutput_GrayBoxSimulation_{ProcessUtils.GetCurrentProcessId()}.log"));
            LoggingHelper.ChangeConsoleLoggingLevel(configuration.Verbosity);
            LoggingHelper.WriteLine(VerbosityLevel.Info, "Reading in and preprocessing data for gray box simulation.");

            if (!GrayBoxUtils.TryToReadDataRecordsFromDirectory(
                    targetAlgorithmFactory,
                    configuration.DataRecordDirectoryPath,
                    0,
                    configuration.Generations - 1,
                    out this._allDataRecords))
            {
                throw new ArgumentException($"Cannot read data records from {configuration.DataRecordDirectoryPath}!");
            }

            this.ReadGenerationCompositionFiles();
            this.CreatePredictionDictionaryAndDataDictionary();
        }
Ejemplo n.º 16
0
 /// <summary>
 /// Initializes a new instance of the <see cref="GenomeEvaluationDelegatorBase{TInstance, TResult}"/> class.
 /// </summary>
 /// <param name="runEvaluator">Object for evaluating target algorithm runs.</param>
 protected GenomeEvaluationDelegatorBase(IRunEvaluator <TResult> runEvaluator)
 {
     this._runEvaluator = runEvaluator ?? throw new ArgumentNullException(nameof(runEvaluator));
 }
 /// <summary>
 /// Sets the <see cref="IRunEvaluator{TestResult}"/> to provide to the
 /// <see cref="TournamentSelector{NoOperation, TestInstance, EmptyResult}"/> constructor. Default is an
 /// evaluator that doesn't reorder the genomes at all.
 /// </summary>
 /// <param name="runEvaluator">The run evaluator to provide to the min tournament actor constructor.
 /// </param>
 /// <returns>The <see cref="TournamentSelectorPropsBuilder"/> in its new state.</returns>
 public TournamentSelectorPropsBuilder SetRunEvaluator(IRunEvaluator <TestResult> runEvaluator)
 {
     this._runEvaluator = runEvaluator;
     return(this);
 }
Ejemplo n.º 18
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MiniTournamentGenerationEvaluationStrategy{TInstance,TResult}"/> class.
        /// </summary>
        /// <param name="runEvaluator">The <see cref="IRunEvaluator{TInstance,TResult}"/> for sorting genomes.</param>
        /// <param name="genomes">The genomes for evaluation. Do not need to be distinct.</param>
        /// <param name="instances">The instances for evaluation.</param>
        /// <param name="generation">The generation number.</param>
        /// <param name="configuration">The algorithm tuner configuration.</param>
        /// <param name="useGrayBoxInGeneration">Boolean indicating whether to use gray box tuning in current generation.</param>
        /// <param name="useSingleTournamentWithSingleWinnerInGeneration">Boolean indicating whether to use only a single tournament with single winner in current generation.</param>
        public MiniTournamentGenerationEvaluationStrategy(
            IRunEvaluator <TInstance, TResult> runEvaluator,
            IEnumerable <ImmutableGenome> genomes,
            IEnumerable <TInstance> instances,
            int generation,
            AlgorithmTunerConfiguration configuration,
            bool useGrayBoxInGeneration,
            bool useSingleTournamentWithSingleWinnerInGeneration)
        {
            this._runEvaluator = runEvaluator ?? throw new ArgumentNullException(nameof(runEvaluator));

            if (genomes == null)
            {
                throw new ArgumentNullException(nameof(genomes));
            }

            this._genomes = genomes.ToList();

            if (instances == null)
            {
                throw new ArgumentNullException(nameof(instances));
            }

            this._generation = generation;

            if (configuration == null)
            {
                throw new ArgumentNullException(nameof(configuration));
            }

            this._useGrayBoxInGeneration = useGrayBoxInGeneration;

            if (useSingleTournamentWithSingleWinnerInGeneration)
            {
                this._tournamentManagers = new List <MiniTournamentManager <TInstance, TResult> >
                {
                    new MiniTournamentManager <TInstance, TResult>(
                        this._genomes,
                        instances,
                        0,
                        this._generation,
                        this._runEvaluator,
                        configuration.EnableRacing,
                        1),
                };
            }
            else
            {
                var counter = 0;
                this._tournamentManagers
                    = Randomizer.Instance.SplitIntoRandomBalancedSubsets(this._genomes, configuration.MaximumMiniTournamentSize)
                      .Select(
                          participants =>
                {
                    var participantList = participants.ToList();
                    return(new MiniTournamentManager <TInstance, TResult>(
                               participantList,
                               instances,
                               counter++,
                               this._generation,
                               this._runEvaluator,
                               configuration.EnableRacing,
                               MiniTournamentGenerationEvaluationStrategy <TInstance, TResult> .GetDesiredNumberOfWinners(
                                   configuration,
                                   participantList)));
                }).ToList();
            }

            // The generation evaluation actor requeues all required evaluations.
            this._priorityQueue = new SimplePriorityQueue <GenomeTournamentKey, double>();
        }