/// <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()); }
/// <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> /// Builds a OPTANO Algorithm Tuner instance that tunes by process runtime. /// </summary> /// <param name="config"> /// The configuration. /// </param> /// <param name="trainingInstanceFolder"> /// The path to the folder containing training instances. /// </param> /// <param name="testInstanceFolder"> /// The path to the folder containing test instances. /// </param> /// <param name="basicCommand"> /// The basic command to the target algorithm as it should be executed by the /// command line. The path to the instance file and the parameters will be set by replacing /// <see cref="CommandExecutorBase{TResult}.InstanceReplacement"/> and /// <see cref="CommandExecutorBase{TResult}.ParameterReplacement"/>. /// </param> /// <param name="pathToParameterTree"> /// The path to a parameter tree defined via XML. /// </param> /// <param name="factorParK"> /// The factor for the penalization of the average runtime. /// </param> /// <returns> /// The built OPTANO Algorithm Tuner instance. /// </returns> private static AlgorithmTuner <TimeMeasuringExecutor, InstanceFile, RuntimeResult> BuildRuntimeTuner( AlgorithmTunerConfiguration config, string trainingInstanceFolder, string testInstanceFolder, string basicCommand, string pathToParameterTree, int factorParK) { IRunEvaluator <InstanceFile, RuntimeResult> runEvaluator; if (factorParK == 0) { runEvaluator = new SortByUnpenalizedRuntime <InstanceFile>(config.CpuTimeout); } else { runEvaluator = new SortByPenalizedRuntime <InstanceFile>(factorParK, config.CpuTimeout); } var tuner = new AlgorithmTuner <TimeMeasuringExecutor, InstanceFile, RuntimeResult>( targetAlgorithmFactory: new TimeMeasuringExecutorFactory(basicCommand, config.CpuTimeout), runEvaluator: runEvaluator, trainingInstances: ExtractInstances(trainingInstanceFolder), parameterTree: ParameterTreeConverter.ConvertToParameterTree(pathToParameterTree), configuration: config); if (testInstanceFolder != null) { tuner.SetTestInstances(ExtractInstances(testInstanceFolder)); } return(tuner); }
/// <summary> /// Initializes a new instance of the <see cref="DifferentialEvolutionStrategy{TInstance, TResult}"/> class. /// </summary> /// <param name="configuration">Options used for this instance.</param> /// <param name="parameterTree">Provides the tunable parameters.</param> /// <param name="genomeBuilder">Responsible for creation, modification and crossover of genomes. /// Needs to be compatible with the given parameter tree and configuration.</param> /// <param name="genomeSorter"> /// An <see cref="IActorRef" /> to a <see cref="GenomeSorter{TInstance,TResult}" />. /// </param> /// <param name="targetRunResultStorage"> /// An <see cref="IActorRef" /> to a <see cref="ResultStorageActor{TInstance,TResult}" /> /// which knows about all executed target algorithm runs and their results. /// </param> public DifferentialEvolutionStrategy( AlgorithmTunerConfiguration configuration, ParameterTree parameterTree, GenomeBuilder genomeBuilder, IActorRef genomeSorter, IActorRef targetRunResultStorage) : base(configuration, parameterTree, targetRunResultStorage, new GenomeSearchPointSorter <TInstance>(genomeSorter)) { this._strategyConfiguration = this.Configuration.ExtractDetailedConfiguration <DifferentialEvolutionStrategyConfiguration>( DifferentialEvolutionStrategyArgumentParser.Identifier); if (this._strategyConfiguration.FocusOnIncumbent) { this._informationFlow = new LocalDifferentialEvolutionInformationFlow( this._strategyConfiguration, parameterTree, genomeBuilder); } else { this._informationFlow = new GlobalDifferentialEvolutionInformationFlow( this._strategyConfiguration, parameterTree, genomeBuilder); } this._searchPointFactory = (vector, parent) => new GenomeSearchPoint(vector, parent, genomeBuilder); this._differentialEvolutionRunner = this.CreateDifferentialEvolutionRunner( this._strategyConfiguration.DifferentialEvolutionConfiguration.InitialMeanMutationFactor, this._strategyConfiguration.DifferentialEvolutionConfiguration.InitialMeanCrossoverRate); }
/// <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()); }
/// <summary> /// Creates a <see cref="CovarianceMatrixAdaptationStrategyBase{TSearchPoint, TInstance, TResult}"/> depending /// on the <see cref="CovarianceMatrixAdaptationStrategyConfiguration"/>. /// </summary> /// <typeparam name="TInstance"> /// The instance type to use. /// </typeparam> /// <typeparam name="TResult"> /// The result for an individual evaluation. /// </typeparam> /// <param name="configuration">Options to use.</param> /// <param name="parameterTree">Provides the tunable parameters.</param> /// <param name="genomeBuilder">Responsible for creation, modification and crossover of genomes. /// Needs to be compatible with the given parameter tree and configuration.</param> /// <param name="genomeSorter"> /// An <see cref="IActorRef" /> to a <see cref="GenomeSorter{TInstance,TResult}" />. /// </param> /// <param name="targetRunResultStorage"> /// An <see cref="IActorRef" /> to a <see cref="ResultStorageActor{TInstance,TResult}" /> /// which knows about all executed target algorithm runs and their results. /// </param> /// <returns> /// The created <see cref="CovarianceMatrixAdaptationStrategyBase{TSearchPoint, TInstance, TResult}"/> instance. /// </returns> public static IPopulationUpdateStrategy <TInstance, TResult> CreateCovarianceMatrixAdaptationStrategy <TInstance, TResult>( AlgorithmTunerConfiguration configuration, ParameterTree parameterTree, GenomeBuilder genomeBuilder, IActorRef genomeSorter, IActorRef targetRunResultStorage) where TInstance : InstanceBase where TResult : ResultBase <TResult>, new() { if (StrategyShouldFocusOnIncumbent(configuration)) { return(new LocalCovarianceMatrixAdaptationStrategy <TInstance, TResult>( configuration, parameterTree, genomeBuilder, genomeSorter, targetRunResultStorage)); } else { return(new GlobalCovarianceMatrixAdaptationStrategy <TInstance, TResult>( configuration, parameterTree, genomeBuilder, genomeSorter, targetRunResultStorage)); } }
private static AlgorithmTuner <BbobRunner, InstanceFile, ContinuousResult, TLearnerModel, TPredictorModel, TSamplingStrategy> BuildBbobRunner( AlgorithmTunerConfiguration configuration, string trainingInstanceFolder, string testInstanceFolder, BbobRunnerConfiguration bbobRunnerConfig) { var requiredInstances = (int)Math.Max(configuration.StartNumInstances, configuration.EndNumInstances); var random = new Random(bbobRunnerConfig.InstanceSeed); var tuner = new AlgorithmTuner <BbobRunner, InstanceFile, ContinuousResult, TLearnerModel, TPredictorModel, TSamplingStrategy>( targetAlgorithmFactory: new BbobRunnerFactory( bbobRunnerConfig.PythonBin, bbobRunnerConfig.PathToExecutable, bbobRunnerConfig.FunctionId), runEvaluator: new SortByValue <InstanceFile>(@ascending: true), trainingInstances: BbobUtils.CreateInstancesFilesAndReturnAsList(trainingInstanceFolder, requiredInstances, random), parameterTree: BbobUtils.CreateParameterTree(bbobRunnerConfig.Dimensions), configuration: configuration); if (!string.IsNullOrWhiteSpace(testInstanceFolder)) { tuner.SetTestInstances(BbobUtils.CreateInstancesFilesAndReturnAsList(testInstanceFolder, requiredInstances, random)); } return(tuner); }
public void HasTerminatedConsidersGenerationsInPhase() { // Do not use int.MaxValue generations for the strategy. this._configuration = new AlgorithmTunerConfiguration.AlgorithmTunerConfigurationBuilder() .SetMaximumNumberGgaGenerations(20) .BuildWithFallback(this._configuration); var population = new Population(this._configuration); population.AddGenome(this._genomeBuilder.CreateRandomGenome(age: 1), true); var strategy = this.CreateTestStrategy(); strategy.Initialize(population, this.CreateIncumbentGenomeWrapper(), null); // First phase: One generation less than needed. for (int generation = 0; generation < this._configuration.MaximumNumberGgaGenerations - 1; generation++) { strategy.PerformIteration(generation, this._singleTestInstance); } Assert.False(strategy.HasTerminated(), "Should not have terminated yet."); // Start a new phase. strategy.Initialize(population, this.CreateIncumbentGenomeWrapper(), null); for (int generation = 0; generation < this._configuration.MaximumNumberGgaGenerations - 1; generation++) { strategy.PerformIteration(generation, this._singleTestInstance); } Assert.False(strategy.HasTerminated(), "Should not have terminated yet."); strategy.PerformIteration(this._configuration.MaximumNumberGgaGenerations - 1, this._singleTestInstance); Assert.True(strategy.HasTerminated(), "Should have terminated."); }
/// <summary> /// Builds an instance of the <see cref="AlgorithmTuner{TTargetAlorithm,TInstance,TResult}" /> class for tuning Gurobi. /// </summary> /// <param name="configuration">The <see cref="AlgorithmTunerConfiguration" /> to use.</param> /// <param name="pathToTrainingInstanceFolder">The path to the folder containing training instances.</param> /// <param name="pathToTestInstanceFolder">The path to test instance folder.</param> /// <param name="gurobiConfigBuilder">The gurobi configuration builder.</param> /// <returns> /// The built instance. /// </returns> public static AlgorithmTuner <GurobiRunner, InstanceSeedFile, GurobiResult> BuildGurobiRunner( AlgorithmTunerConfiguration configuration, string pathToTrainingInstanceFolder, string pathToTestInstanceFolder, GurobiRunnerConfiguration.GurobiRunnerConfigBuilder gurobiConfigBuilder) { var gurobiConfig = gurobiConfigBuilder.Build(configuration.CpuTimeout); var tuner = new AlgorithmTuner <GurobiRunner, InstanceSeedFile, GurobiResult>( targetAlgorithmFactory: new GurobiRunnerFactory(gurobiConfig), runEvaluator: new GurobiRunEvaluator(), trainingInstances: GurobiUtils.CreateInstances(pathToTrainingInstanceFolder, gurobiConfig.NumberOfSeeds, gurobiConfig.RngSeed), parameterTree: GurobiUtils.CreateParameterTree(), configuration: configuration); try { if (!string.IsNullOrWhiteSpace(pathToTestInstanceFolder)) { var testInstances = GurobiUtils.CreateInstances(pathToTestInstanceFolder, gurobiConfig.NumberOfSeeds, gurobiConfig.RngSeed); tuner.SetTestInstances(testInstances); } } catch { } return(tuner); }
public Status( int generation, Population population, AlgorithmTunerConfiguration configuration, GeneticEngineering <TLearnerModel, TPredictorModel, TSamplingStrategy> geneticEngineering, int currentUpdateStrategyIndex, List <double> incumbentQuality, IncumbentGenomeWrapper <TResult> incumbentGenomeWrapper, List <GenerationInformation> informationHistory, TimeSpan elapsedTime, ImmutableGenome defaultGenome = null) { this.Generation = generation; this.Population = population ?? throw new ArgumentNullException(nameof(population)); this.Configuration = configuration ?? throw new ArgumentNullException(nameof(configuration)); this.GeneticEngineering = geneticEngineering ?? throw new ArgumentNullException(nameof(geneticEngineering)); this.CurrentUpdateStrategyIndex = currentUpdateStrategyIndex; this.IncumbentQuality = incumbentQuality ?? throw new ArgumentNullException(nameof(incumbentQuality)); this.IncumbentGenomeWrapper = incumbentGenomeWrapper ?? throw new ArgumentNullException(nameof(incumbentGenomeWrapper)); this.InformationHistory = informationHistory ?? throw new ArgumentNullException(nameof(informationHistory)); this.ElapsedTime = elapsedTime; this.DefaultGenome = defaultGenome; }
/// <summary> /// Initializes a new instance of the <see cref="GrayBoxHandler{TInstance, TResult}" /> class. /// </summary> /// <param name="configuration">The <see cref="AlgorithmTunerConfiguration"/>.</param> /// <param name="grayBoxTargetAlgorithm">The gray box target algorithm.</param> /// <param name="actorId">The actor ID.</param> /// <param name="tunerDataRecord">The tuner data record.</param> /// <param name="useGrayBoxInCurrentEvaluation">Boolean indicating whether to use gray box tuning in current evaluation.</param> /// <param name="customGrayBoxMethods">The <see cref="ICustomGrayBoxMethods{TResult}"/>.</param> /// <param name="grayBoxRandomForest">The gray box random forest.</param> public GrayBoxHandler( AlgorithmTunerConfiguration configuration, IGrayBoxTargetAlgorithm <TInstance, TResult> grayBoxTargetAlgorithm, int actorId, TunerDataRecord <TResult> tunerDataRecord, bool useGrayBoxInCurrentEvaluation, ICustomGrayBoxMethods <TResult> customGrayBoxMethods, ClassificationForestModel grayBoxRandomForest) { this._configuration = configuration ?? throw new ArgumentNullException(nameof(configuration)); this._grayBoxTargetAlgorithm = grayBoxTargetAlgorithm ?? throw new ArgumentNullException(nameof(grayBoxTargetAlgorithm)); this._actorId = actorId; this._tunerDataRecord = tunerDataRecord ?? throw new ArgumentNullException(nameof(tunerDataRecord)); if (useGrayBoxInCurrentEvaluation) { this._customGrayBoxMethods = customGrayBoxMethods ?? throw new ArgumentNullException(nameof(customGrayBoxMethods)); this._grayBoxRandomForest = grayBoxRandomForest ?? throw new ArgumentNullException(nameof(grayBoxRandomForest)); this._useGrayBoxInCurrentEvaluation = true; } this._grayBoxTargetAlgorithm.OnNewDataRecord += this.HandleDataRecordUpdate; lock (this._lock) { this._listOfDataRecords = new List <DataRecord <TResult> >(); } }
/// <summary> /// Writes the provided <see cref="AlgorithmTunerConfiguration"/> into a status file stored at the provided path. /// The generation will be set to 0 and the population and run results will be empty. /// </summary> /// <param name="config">The <see cref="AlgorithmTunerConfiguration"/> to write to the status file.</param> /// <param name="pathToStatusFile">Path to which the status file should be written.</param> private void WriteConfigurationToStatusFile(AlgorithmTunerConfiguration config, string pathToStatusFile) { // Create status objects with correct configuration. var status = new Status <TestInstance, TestResult, StandardRandomForestLearner <ReuseOldTreesStrategy>, GenomePredictionForestModel <GenomePredictionTree>, ReuseOldTreesStrategy>( generation: 0, population: new Population(config), configuration: config, geneticEngineering: this.DummyGeneticEngineering, currentUpdateStrategyIndex: 0, // dummy data... incumbentQuality: new List <double>(), incumbentGenomeWrapper: new IncumbentGenomeWrapper <TestResult>() { IncumbentGenome = new Genome(), IncumbentGeneration = 0, IncumbentInstanceResults = new List <TestResult>().ToImmutableList(), }, informationHistory: new List <GenerationInformation>(0), elapsedTime: TimeSpan.Zero); status.SetRunResults( new Dictionary <ImmutableGenome, ImmutableDictionary <TestInstance, TestResult> >().ToImmutableDictionary()); var ggaStatus = new GgaStatus(new Population(config), 0, 0, new Dictionary <Genome, List <GenomeTournamentResult> >()); // Write them to file. status.WriteToFile(pathToStatusFile); this._pathToGgaStatusFile = Path.Combine(Path.GetDirectoryName(pathToStatusFile), GgaStatus.FileName); ggaStatus.WriteToFile(this._pathToGgaStatusFile); }
/// <summary> /// Initializes a new instance of the <see cref="MiniTournamentIncumbentEvaluator{TInstance,TResult}"/> class. /// </summary> /// <param name="generationEvaluationActor">The generation evaluation actor.</param> /// <param name="configuration">The configuration.</param> public MiniTournamentIncumbentEvaluator( IActorRef generationEvaluationActor, AlgorithmTunerConfiguration configuration) { this._generationEvaluationActor = generationEvaluationActor ?? throw new ArgumentNullException(nameof(generationEvaluationActor)); this._configuration = configuration ?? throw new ArgumentNullException(nameof(configuration)); }
public void CancellationWorks() { var gurobiEnvironment = new GRBEnv(); var runnerConfiguration = new GurobiRunnerConfiguration.GurobiRunnerConfigBuilder().Build(TimeSpan.FromSeconds(1)); var tunerConfiguration = new AlgorithmTunerConfiguration(); // Note, that this cancellation token source is never used in GurobiRunner.Run(). var cancellationTokenSource = new CancellationTokenSource(500); var timer = new Stopwatch(); timer.Start(); var gurobiRunner = new GurobiRunner(gurobiEnvironment, runnerConfiguration, tunerConfiguration); var runner = gurobiRunner.Run( new InstanceSeedFile(GurobiRunnerTests.PathToTestInstance, GurobiRunnerTests.TestInstanceSeed), cancellationTokenSource.Token); runner.Wait(); timer.Stop(); var result = runner.Result; result.IsCancelled.ShouldBeTrue(); timer.Elapsed.ShouldBeGreaterThan(TimeSpan.FromMilliseconds(1000)); timer.Elapsed.ShouldBeLessThan(TimeSpan.FromMilliseconds(1900)); gurobiEnvironment.Dispose(); }
/// <summary> /// Initializes a new instance of the <see cref="EvaluationActor{TTargetAlgorithm, TInstance, TResult}" /> class. /// </summary> /// <param name="targetAlgorithmFactory">The target algorithm factory.</param> /// <param name="configuration">The algorithm tuner configuration.</param> /// <param name="parameterTree">The parameter tree.</param> /// <param name="customGrayBoxMethods">The <see cref="ICustomGrayBoxMethods{TResult}"/>.</param> /// <param name="generationEvaluationActor">The generation evaluation actor.</param> public EvaluationActor( ITargetAlgorithmFactory <TTargetAlgorithm, TInstance, TResult> targetAlgorithmFactory, AlgorithmTunerConfiguration configuration, ParameterTree parameterTree, ICustomGrayBoxMethods <TResult> customGrayBoxMethods, IActorRef generationEvaluationActor) { LoggingHelper.WriteLine(VerbosityLevel.Info, $"Starting new evaluation actor! Address: {this.Self}"); this._targetAlgorithmFactory = targetAlgorithmFactory ?? throw new ArgumentNullException(nameof(targetAlgorithmFactory)); this._configuration = configuration ?? throw new ArgumentNullException(nameof(configuration)); this._parameterTree = parameterTree ?? throw new ArgumentNullException(nameof(parameterTree)); if (generationEvaluationActor == null) { throw new ArgumentNullException(nameof(generationEvaluationActor)); } // No need to check for null. Might be null by purpose. this._customGrayBoxMethods = customGrayBoxMethods; this._id = Interlocked.Increment(ref EvaluationActor <TTargetAlgorithm, TInstance, TResult> .idCounter); this.Become(this.Ready); generationEvaluationActor.Tell(new HelloWorld()); }
ReuseOldTreesStrategy> CreateStatus( int generation, Population population, AlgorithmTunerConfiguration configuration, int currentUpdateStrategyIndex, List <GenerationInformation> informationHistory) { if (this.DummyEngineering == null) { this.DummyEngineering = new GeneticEngineering <GenomePredictionRandomForest <ReuseOldTreesStrategy>, GenomePredictionForestModel <GenomePredictionTree>, ReuseOldTreesStrategy>(this.DummyTree, configuration); } return(new Status <TestInstance, TestResult, GenomePredictionRandomForest <ReuseOldTreesStrategy>, GenomePredictionForestModel <GenomePredictionTree>, ReuseOldTreesStrategy>( generation, population, configuration, this.DummyEngineering, currentUpdateStrategyIndex, new List <double>(), new IncumbentGenomeWrapper <TestResult>() { IncumbentGenome = new Genome(), IncumbentGeneration = 0, IncumbentInstanceResults = new List <TestResult>().ToImmutableList(), }, informationHistory, elapsedTime: TimeSpan.Zero)); }
/// <summary> /// Determines whether the <see cref="CovarianceMatrixAdaptationStrategyBase{TSearchPoint,TInstance,TResult}"/> /// is supposed to focus on the incumbent according to the provided <see cref="AlgorithmTunerConfiguration"/>. /// </summary> /// <param name="configuration">The <see cref="AlgorithmTunerConfiguration"/> to check.</param> /// <returns> /// Whether the <see cref="CovarianceMatrixAdaptationStrategyBase{TSearchPoint,TInstance,TResult}"/> /// is supposed to focus on the incumbent. /// </returns> private static bool StrategyShouldFocusOnIncumbent(AlgorithmTunerConfiguration configuration) { var strategyConfiguration = configuration .ExtractDetailedConfiguration <CovarianceMatrixAdaptationStrategyConfiguration>( CovarianceMatrixAdaptationStrategyArgumentParser.Identifier); return(strategyConfiguration.FocusOnIncumbent); }
/// <summary> /// Initializes a new instance of the <see cref="PopulationUpdateStrategyManager{TInstance, TResult}"/> class. /// </summary> /// <param name="populationUpdateStrategies">All possible <see cref="IPopulationUpdateStrategy{TInstance,TResult}"/>s.</param> /// <param name="configuration">Algorithm tuner configuration parameters.</param> public PopulationUpdateStrategyManager( List <IPopulationUpdateStrategy <TInstance, TResult> > populationUpdateStrategies, AlgorithmTunerConfiguration configuration) { this._populationUpdateStrategies = populationUpdateStrategies; this.CurrentUpdateStrategyIndex = 0; this.BasePopulation = new Population(configuration); }
/// <summary> /// Initializes a new instance of the <see cref="InstanceSelector{TInstance}"/> class. /// </summary> /// <param name="instances"> /// All available instances. /// </param> /// <param name="configuration"> /// Algorithm tuner configuration containing parameters needed by the selector, /// e.g. goal generation. /// </param> public InstanceSelector(IEnumerable <TInstance> instances, AlgorithmTunerConfiguration configuration) { this._instances = new List <TInstance>(instances).AsReadOnly(); this._startNumInstances = configuration.StartNumInstances; this._endNumInstances = configuration.EndNumInstances; this._goalGeneration = configuration.GoalGeneration; this._linearIncrease = (double)(configuration.EndNumInstances - configuration.StartNumInstances) / configuration.GoalGeneration; }
/// <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> /// Simulates a tuner run for the specified number of generations and stores results in a new <see cref="TrainingDataWrapper"/>. /// </summary> /// <param name="tree"><see cref="ParameterTree"/> to base genomes on.</param> /// <param name="encoder">Strategy to convert genomes to double arrays.</param> /// <param name="genomeCount">Number of genomes to add to result per generation.</param> /// <param name="generations">Number of generations to simulate.</param> /// <param name="config"><see cref="AlgorithmTunerConfiguration"/>, required to generate new genomes.</param> /// <returns>The created <see cref="TrainingDataWrapper"/>.</returns> public static TrainingDataWrapper GenerateTrainingData( ParameterTree tree, IBulkGenomeTransformation encoder, int genomeCount, int generations, AlgorithmTunerConfiguration config) { var result = new TrainingDataWrapper( new Dictionary <Genome, List <GenomeTournamentRank> >(Genome.GenomeComparer), generations - 1); // Start with correct number of random genomes. var randomGenomes = TestDataUtils.GenerateGenomes(tree, config, genomeCount); // Then simulate the correct number of generations. for (var currentGen = 0; currentGen < generations; currentGen++) { var fitness = TestDataUtils.EvaluateTargetFunction(encoder, randomGenomes); // add result for every genome for (var genomeIndex = 0; genomeIndex < genomeCount; genomeIndex++) { var currentGenome = randomGenomes[genomeIndex]; if (!result.TournamentResults.ContainsKey(currentGenome)) { result.TournamentResults[currentGenome] = new List <GenomeTournamentRank>(); } var tournamentResult = new GenomeTournamentRank() { GenerationId = currentGen, TournamentId = currentGen, TournamentRank = fitness[genomeIndex], }; result.TournamentResults[currentGenome].Add(tournamentResult); } // swap out some genomes var replaceCount = (int)Math.Ceiling(0.3 * genomeCount); var indiciesToReplace = Randomizer.Instance.ChooseRandomSubset( Enumerable.Range(0, genomeCount), replaceCount); var newGenomes = TestDataUtils.GenerateGenomes(tree, config, replaceCount); var replacementIndex = 0; foreach (var indexToReplace in indiciesToReplace) { randomGenomes[indexToReplace] = newGenomes[replacementIndex++]; } } return(result); }
/// <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()); }
/// <summary> /// Initializes a new instance of the <see cref="Population" /> class. /// Copy constructor for <see cref="Population"/> class. /// </summary> /// <param name="original">The <see cref="Population"/> object to copy.</param> public Population(Population original) { if (original == null) { throw new ArgumentNullException(nameof(original)); } this._competitive = original._competitive.Select(genome => new Genome(genome)).ToList(); this._nonCompetitive = original._nonCompetitive.Select(genome => new Genome(genome)).ToList(); this._configuration = original._configuration; }
/// <summary> /// I don't know a good way to use 'TestInitialize' flag with test-specific parameters. /// </summary> /// <param name="tree">The <see cref="ParameterTree"/> to use.</param> /// <param name="config">The <see cref="AlgorithmTunerConfiguration"/> to use.</param> private void InitializeAndPopulateProperties(ParameterTree tree, AlgorithmTunerConfiguration config) { this.CurrentTree = tree; this.CurrentConfig = config; this.EngineeringInstance = new GeneticEngineering <StandardRandomForestLearner <ReuseOldTreesStrategy>, GenomePredictionForestModel <GenomePredictionTree>, ReuseOldTreesStrategy>( tree, config); this.InitializeTrainingDataAndCurrentPopulation(); }
public void LogFinishedGenerationThrowsForUnknownDirectory() { // Set non existing log file path, then log finished generation. this._configuration = new AlgorithmTunerConfiguration.AlgorithmTunerConfigurationBuilder() .SetLogFilePath("foo/bar.txt") .Build(maximumNumberParallelEvaluations: 1); this._genomeBuilder = new GenomeBuilder(this._parameterTree, this._configuration); this._writer = new LogWriter <InstanceFile, RuntimeResult>(this._parameterTree, this._configuration); Assert.Throws <DirectoryNotFoundException>( () => this._writer.LogFinishedGeneration(0, 1, this._genomeBuilder.CreateRandomGenome(0), LogWriterTest.EmptyResults)); }
/// <summary> /// Initializes a new instance of the /// <see cref="CovarianceMatrixAdaptationStrategyBase{TSearchPoint, TInstance, TResult}"/> class. /// </summary> /// <param name="configuration">Options used for this instance.</param> /// <param name="parameterTree">Provides the tunable parameters.</param> /// <param name="genomeSorter"> /// An <see cref="IActorRef" /> to a <see cref="GenomeSorter{TInstance,TResult}" />. /// </param> /// <param name="targetRunResultStorage"> /// An <see cref="IActorRef" /> to a <see cref="ResultStorageActor{TInstance,TResult}" /> /// which knows about all executed target algorithm runs and their results. /// </param> protected CovarianceMatrixAdaptationStrategyBase( AlgorithmTunerConfiguration configuration, ParameterTree parameterTree, IActorRef genomeSorter, IActorRef targetRunResultStorage) : base(configuration, parameterTree, targetRunResultStorage, new RepairedGenomeSearchPointSorter <TSearchPoint, TInstance>(genomeSorter)) { this.StrategyConfiguration = this.Configuration.ExtractDetailedConfiguration <CovarianceMatrixAdaptationStrategyConfiguration>( CovarianceMatrixAdaptationStrategyArgumentParser.Identifier); }
/// <summary> /// Creates a <see cref="Optano.Algorithm.Tuner.Genomes.GenomeBuilder"/> which forbids using "3" for <see cref="FreeParameterName"/>. /// </summary> /// <param name="configuration"> /// <see cref="AlgorithmTunerConfiguration"/> for the <see cref="Optano.Algorithm.Tuner.Genomes.GenomeBuilder"/>. /// </param> /// <returns>The created <see cref="Optano.Algorithm.Tuner.Genomes.GenomeBuilder"/>.</returns> private ConfigurableGenomeBuilder CreateGenomeBuilderWithForbiddenValue(AlgorithmTunerConfiguration configuration) { Func <Genome, bool> invalidityFunction = candidate => !object.Equals( candidate.GetGeneValue(GenomeAssistedSorterBaseTest <TSearchPoint> .FreeParameterName).GetValue(), 3); return(new ConfigurableGenomeBuilder( this.ParameterTree, invalidityFunction, configuration.MutationRate)); }
/// <summary> /// Initializes a new instance of the <see cref="ContinuousOptimizationStrategyBase{TSearchPoint, TInstance, TResult}"/> class. /// </summary> /// <param name="configuration">Options used for this instance.</param> /// <param name="parameterTree">Provides the tunable parameters.</param> /// <param name="targetRunResultStorage"> /// An <see cref="IActorRef" /> to a <see cref="ResultStorageActor{TInstance,TResult}" /> /// which knows about all executed target algorithm runs and their results. /// </param> /// <param name="searchPointSorter"> /// A <see cref="ISearchPointSorter{TSearchPoint}"/> which evaluates /// <typeparamref name="TSearchPoint"/>s via a <see cref="GenerationEvaluationActor{TTargetAlgorithm,TInstance,TResult}"/>. /// </param> /// <exception cref="ArgumentNullException"> /// configuration /// or /// parameterTree /// or /// targetRunResultStorage /// or /// searchPointSorter. /// </exception> protected ContinuousOptimizationStrategyBase( AlgorithmTunerConfiguration configuration, ParameterTree parameterTree, IActorRef targetRunResultStorage, GenomeAssistedSorterBase <TSearchPoint, TInstance, TResult> searchPointSorter) { this.Configuration = configuration ?? throw new ArgumentNullException(nameof(configuration)); this.ParameterTree = parameterTree ?? throw new ArgumentNullException(nameof(parameterTree)); this.TargetRunResultStorage = targetRunResultStorage ?? throw new ArgumentNullException(nameof(targetRunResultStorage)); this.SearchPointSorter = searchPointSorter ?? throw new ArgumentNullException(nameof(searchPointSorter)); }
/// <summary> /// Initializes a new instance of the <see cref="ParameterConfigurationSpaceGenomeBuilder"/> class. /// </summary> /// <param name="parameterTree">The parameters' structure.</param> /// <param name="parameterSpecification"> /// A specification defining forbidden parameter combinations and activity conditions. /// </param> /// <param name="configuration">Configuration parameters.</param> public ParameterConfigurationSpaceGenomeBuilder( ParameterTree parameterTree, ParameterConfigurationSpaceSpecification parameterSpecification, AlgorithmTunerConfiguration configuration) : base(parameterTree, configuration) { this._parameterTree = parameterTree; this._parameterSpecification = parameterSpecification ?? throw new ArgumentNullException(nameof(parameterSpecification)); this._identifierToTreeNode = this._parameterTree.GetParameters().ToDictionary(node => node.Identifier, node => node); }
/// <summary> /// Initializes a new instance of the <see cref="LogWriterTest"/> class. /// </summary> public LogWriterTest() { this._configuration = new AlgorithmTunerConfiguration.AlgorithmTunerConfigurationBuilder() .SetGenerations(24) .SetGoalGeneration(17) .SetLogFilePath(LogWriterTest.LogFilePath) .Build(maximumNumberParallelEvaluations: 1); this._genomeBuilder = new GenomeBuilder(this._parameterTree, this._configuration); Randomizer.Reset(); Randomizer.Configure(0); this._writer = new LogWriter <InstanceFile, RuntimeResult>(this._parameterTree, this._configuration); }