/// <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> /// Initializes a new instance of the <see cref="ConfigurableGenomeBuilder"/> class. /// </summary> /// <param name="parameterTree">The parameters' structure. /// All genes created by this builder should comply with it.</param> /// <param name="isValidFunction">The function that checks whether or not a genome is valid.</param> /// <param name="mutationRate">Probability that a certain parameter is mutated when mutating a genome.</param> public ConfigurableGenomeBuilder( ParameterTree parameterTree, Func <Genome, bool> isValidFunction, double mutationRate) : this(parameterTree, isValidFunction, null, mutationRate) { }
/// <summary> /// Configures the current target algorithm and gray box handler. /// </summary> /// <param name="targetAlgorithmFactory">The <see cref="ITargetAlgorithmFactory{TTargetAlgorithm,TInstance,TResult}"/>.</param> /// <param name="parameterTree">The <see cref="ParameterTree"/>.</param> /// <param name="indexOfDesiredPostTuningRun">The index of the desired post tuning run.</param> /// <param name="postTuningGenomeInstancePair">The post tuning genome instance pair.</param> private void ConfigureTargetAlgorithmAndGrayBoxHandler( ITargetAlgorithmFactory <TGrayBoxTargetAlgorithm, TInstance, TResult> targetAlgorithmFactory, ParameterTree parameterTree, int indexOfDesiredPostTuningRun, GenomeInstancePairStringRepresentation postTuningGenomeInstancePair) { var genomeDoubleRepresentation = GenomeDoubleRepresentation.GetGenomeDoubleRepresentationFromGenomeIdentifierStringRepresentation(postTuningGenomeInstancePair.Genome); var genomeTransformation = new GenomeTransformation <CategoricalBinaryEncoding>(parameterTree); var genome = genomeTransformation.ConvertBack(genomeDoubleRepresentation); var runnerDictionary = genome.GetFilteredGenes(parameterTree); this._configuredTargetAlgorithm = targetAlgorithmFactory.ConfigureTargetAlgorithm(runnerDictionary); // Set generation to -1, since this is a post tuning run. var tunerDataRecord = new TunerDataRecord <TResult>( NetworkUtils.GetFullyQualifiedDomainName(), -1, 0, postTuningGenomeInstancePair.Instance, double.NaN, genomeTransformation.GetConverterColumnHeader(), genomeDoubleRepresentation, null); this._grayBoxHandler = new GrayBoxHandler <TInstance, TResult>( this._tunerConfiguration, this._configuredTargetAlgorithm, indexOfDesiredPostTuningRun, tunerDataRecord, false, null, null); }
/// <summary> /// Initializes a new instance of the <see cref="ContinuizedGenomeSearchPoint"/> class. /// </summary> /// <param name="values"> /// The real-valued point to base this on. /// This is the internal representation, not the one in the parameter space. /// <para>Use <see cref="CreateFromGenome"/> if starting from search space.</para> /// </param> /// <param name="parameterTree">Specifies the parameters.</param> /// <param name="genomeBuilder">Responsible for checking validity and repairing.</param> /// <param name="lowerBounds">The lower bounds by dimension.</param> /// <param name="upperBounds">The upper bounds by dimension.</param> public ContinuizedGenomeSearchPoint( Vector <double> values, ParameterTree parameterTree, GenomeBuilder genomeBuilder, double[] lowerBounds, double[] upperBounds) : base(values, lowerBounds, upperBounds) { if (parameterTree == null) { throw new ArgumentNullException(nameof(parameterTree)); } if (genomeBuilder == null) { throw new ArgumentNullException(nameof(genomeBuilder)); } var transformator = new TolerantGenomeTransformation(parameterTree); var geneValues = transformator.RoundToValidValues(this.MapIntoBounds().ToArray()); var genome = transformator.ConvertBack(geneValues); // Remember whether there was no direct mapping to a valid genome. this.IsRepaired = false; if (!genomeBuilder.IsGenomeValid(genome)) { genomeBuilder.MakeGenomeValid(genome); this.IsRepaired = true; } this.Genome = new ImmutableGenome(genome); }
/// <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)); } }
public void CheckIfTreeCanBeDeserialized() { var root = new AndNode(); root.AddChild(new ValueNode <int>("a", new IntegerDomain())); root.AddChild(new ValueNode <int>("b", new IntegerDomain())); var tree = new ParameterTree(root); var serializer = new Hyperion.Serializer(); var treeStream = new MemoryStream(); serializer.Serialize(tree, treeStream); var streamCopy = new MemoryStream(treeStream.GetBuffer()); var restoredTree = serializer.Deserialize <ParameterTree>(streamCopy); Assert.NotNull(restoredTree); var originalNodes = tree.GetParameters().ToList(); var restoredNodes = restoredTree.GetParameters().ToList(); Assert.Equal(originalNodes.Count, restoredNodes.Count); for (var i = 0; i < originalNodes.Count; i++) { var expectedNode = originalNodes[i]; var restoredNode = restoredNodes[i]; Assert.True(expectedNode.Identifier == restoredNode.Identifier, "Nodes were deserialized in wrong order."); Assert.Equal(expectedNode.Domain.DomainSize, restoredNode.Domain.DomainSize); } }
/// <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); }
public void TestFindActiveIdentifiers() { // Build up a tree with OR node as root, child a (OR node) for value 0, child b (value node) for value 1. // Create the nodes. IParameterTreeNode a = new OrNode <int>("a", new CategoricalDomain <int>(new List <int> { 2, 5 })); IParameterTreeNode b = new ValueNode <int>("b", new IntegerDomain()); var rootDecision = new OrNode <int>("or", new CategoricalDomain <int>(new List <int> { 0, 1 })); // Create connections. rootDecision.AddChild(0, a); rootDecision.AddChild(1, b); var parameterTree = new ParameterTree(rootDecision); // Set value for roort node s.t. only node a should be active. var values = new Dictionary <string, IAllele>(3) { { "a", new Allele <int>(2) }, { "b", new Allele <int>(7) }, { "or", new Allele <int>(0) }, }; var activeIdentifiers = parameterTree.FindActiveIdentifiers(values.ToImmutableDictionary()).ToList(); var expectedActiveIdentifiers = new List <string> { "or", "a" }; Assert.True( TestUtils.SetsAreEquivalent(activeIdentifiers, expectedActiveIdentifiers), $"Active identifiers should be {TestUtils.PrintList(expectedActiveIdentifiers)}, but are {TestUtils.PrintList(activeIdentifiers)}."); }
public void RootIsSetCorrectly() { var root = new AndNode(); var tree = new ParameterTree(root); Assert.Equal(root, tree.Root); }
/// <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()); }
public void GetFilteredGenesHandlesOrNodesCorrectly() { // Build up a tree with OR node as root, child a (OR node) for value 0, child b (value node) for value 1. // Create the nodes. IParameterTreeNode a = new OrNode <int>("a", new CategoricalDomain <int>(new List <int> { 2, 5 })); IParameterTreeNode b = new ValueNode <int>("b", new IntegerDomain()); OrNode <int> rootDecision = new OrNode <int>("or", new CategoricalDomain <int>(new List <int> { 0, 1 })); // Create connections. rootDecision.AddChild(0, a); rootDecision.AddChild(1, b); var parameterTree = new ParameterTree(rootDecision); this._genome.SetGene("a", new Allele <int>(2)); this._genome.SetGene("b", new Allele <int>(7)); this._genome.SetGene("or", new Allele <int>(0)); var filteredGenes = this._genome.GetFilteredGenes(parameterTree); var expectedFilteredGenes = new List <string> { "or", "a" }; Assert.True( TestUtils.SetsAreEquivalent(filteredGenes.Keys, expectedFilteredGenes), $"Filtered genes should be {TestUtils.PrintList(expectedFilteredGenes)}, but are {TestUtils.PrintList(filteredGenes.Keys)}."); }
/// <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="ParameterTree"/> consisting of a single node with categorical domain. /// </summary> /// <typeparam name="T">Type of the categorical domain.</typeparam> /// <param name="domain">The categorical domain.</param> /// <returns>The created <see cref="ParameterTree"/>.</returns> private ParameterTree SingleCategoryTree <T>(CategoricalDomain <T> domain) { var root = new ValueNode <T>("CategoricalFeature", domain); var tree = new ParameterTree(root); return(tree); }
/// <summary> /// Initializes a new instance of the <see cref="ContinuizedGenomeSearchPointTest"/> class. /// </summary> public ContinuizedGenomeSearchPointTest() { Randomizer.Configure(); this._parameterTree = this.CreateParameterTree(); this._genomeBuilder = new GenomeBuilder( this._parameterTree, new AlgorithmTunerConfiguration.AlgorithmTunerConfigurationBuilder().SetEnableRacing(true).Build(1)); }
/// <summary> /// Initializes a new instance of the <see cref="LocalDifferentialEvolutionInformationFlow"/> class. /// </summary> /// <param name="strategyConfiguration"> /// Options used for the <see cref="DifferentialEvolutionStrategy{TInstance,TResult}"/>. /// </param> /// <param name="parameterTree">Provides the tunable parameters.</param> /// <param name="genomeBuilder">Responsible for creation, modification and crossover of genomes.</param> public LocalDifferentialEvolutionInformationFlow( DifferentialEvolutionStrategyConfiguration strategyConfiguration, ParameterTree parameterTree, GenomeBuilder genomeBuilder) { this._strategyConfiguration = strategyConfiguration ?? throw new ArgumentNullException(nameof(strategyConfiguration)); this._parameterTree = parameterTree ?? throw new ArgumentNullException(nameof(parameterTree)); this._genomeBuilder = genomeBuilder ?? throw new ArgumentNullException(nameof(genomeBuilder)); }
/// <summary> /// Initializes a new instance of the <see cref="GenomeSearchPointConverter"/> class. /// </summary> /// <param name="parameterTree">Specification of all parameters.</param> /// <param name="minimumDomainSize">Minimum size of an integer domain to be handled as continuous.</param> internal GenomeSearchPointConverter(ParameterTree parameterTree, int minimumDomainSize) { if (parameterTree == null) { throw new ArgumentNullException(nameof(parameterTree)); } this._realValuedParameters = ExtractContinuousParameters(parameterTree, minimumDomainSize); }
/// <summary> /// Extracts all parameters from the provided <see cref="ParameterTree"/> which can be considered as /// continuous. /// </summary> /// <param name="parameterTree">The <see cref="ParameterTree"/>.</param> /// <param name="minimumDomainSize">Minimum size of an integer domain to be considered continuous.</param> /// <returns> /// All parameters from the provided <see cref="ParameterTree"/> which can be considered as continuous, /// orderd by <see cref="ParameterNodeComparer"/>. /// </returns> internal static ImmutableList <IParameterNode> ExtractContinuousParameters( ParameterTree parameterTree, int minimumDomainSize) { return(parameterTree .GetParameters(ParameterNodeComparer) .Where(parameter => ParameterIsConsideredContinuous(parameter, minimumDomainSize)) .ToImmutableList()); }
internal void Read(ParameterTree tree) { Name = tree.Get <string>("name"); Position = new Vector3( tree.Get <float>("posx"), tree.Get <float>("posy"), tree.Get <float>("posz") ); }
/// <summary> /// Generates training data. /// </summary> /// <param name="tree"><see cref="ParameterTree"/> to base genomes on.</param> /// <returns>The generated <see cref="TrainingDataWrapper"/>.</returns> private TrainingDataWrapper GetDefaultTrainingData(ParameterTree tree) { return(TestDataUtils.GenerateTrainingData( tree, this.EngineeringInstance.GenomeTransformator, GeneticEngineeringTest.PopsizeHalf, 1, this.CurrentConfig)); }
/// <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) { }
internal void Read(ParameterTree tree) { HingeYMin = tree.Get <float>("hinge_ymin", -180); HingeYMax = tree.Get <float>("hinge_ymax", 180); HingeZMin = tree.Get <float>("hinge_zmin", -180); HingeZMax = tree.Get <float>("hinge_zmax", 180); Radius = tree.Get <float>("coli_r"); Weight = tree.Get <float>("weight", 1); InertialCancel = tree.Get <float>("inertial_cancel"); }
/// <summary> /// Creates a tree containing two integer parameter nodes. /// </summary> /// <returns>The created <see cref="ParameterTree"/>.</returns> protected virtual ParameterTree GetDefaultParameterTree() { var root = new ValueNode <int>("a", new IntegerDomain(-5, 5)); root.SetChild(new ValueNode <int>("b", new IntegerDomain(0, 10))); var tree = new ParameterTree(root); return(tree); }
/// <summary> /// Initializes a new instance of the <see cref="PartialGenomeSearchPointTest"/> class. /// </summary> public PartialGenomeSearchPointTest() { Randomizer.Configure(); this._parameterTree = this.CreateParameterTree(); this._genomeBuilder = new GenomeBuilder( this._parameterTree, new AlgorithmTunerConfiguration.AlgorithmTunerConfigurationBuilder().SetEnableRacing(true).Build(1)); this._genomeSearchPointConverter = new GenomeSearchPointConverter(this._parameterTree, this._minimumDomainSize); this._baseGenome = new ImmutableGenome(this._genomeBuilder.CreateRandomGenome(age: 0)); }
/// <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> /// 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(); }
/// <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> /// 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> /// Creates a <see cref="GenomeSearchPoint"/> which is based on a <see cref="Genome"/>, but uses /// random values for the real-valued components. /// </summary> /// <param name="genome"> /// The <see cref="Genome"/> to base the <see cref="GenomeSearchPoint"/> on. /// Components considered as real-valued are ignored. /// </param> /// <param name="parameterTree">Specification of all parameters.</param> /// <param name="minimumDomainSize">Minimum size of an integer domain to be handled as continuous.</param> /// <param name="genomeBuilder"> /// Responsible for validity checking of <see cref="Genome"/>s. /// Needs to be compatible with <paramref name="parameterTree"/>. /// </param> /// <returns>The created <see cref="GenomeSearchPoint"/>.</returns> public static GenomeSearchPoint BaseRandomPointOnGenome( Genome genome, ParameterTree parameterTree, int minimumDomainSize, GenomeBuilder genomeBuilder) { var converter = new GenomeSearchPointConverter(parameterTree, minimumDomainSize); var values = converter.RandomlyCreateRealValuedParameterValues(); return(new GenomeSearchPoint(values, converter, new ImmutableGenome(genome), genomeBuilder)); }
/// <summary> /// Initializes a new instance of the <see cref="ConfigurableGenomeBuilder"/> class. /// </summary> /// <param name="parameterTree">The parameters' structure. /// All genes created by this builder should comply with it.</param> /// <param name="isValidFunction">The function that checks whether or not a genome is valid.</param> /// <param name="makeValidFunction">The function which repairs a genome.</param> /// <param name="mutationRate">Probability that a certain parameter is mutated when mutating a genome.</param> public ConfigurableGenomeBuilder( ParameterTree parameterTree, Func <Genome, bool> isValidFunction, Action <Genome> makeValidFunction, double mutationRate) : base( parameterTree, new AlgorithmTunerConfiguration.AlgorithmTunerConfigurationBuilder().SetMutationRate(mutationRate).Build(1)) { this._isValidFunction = isValidFunction; this._makeValidFunction = makeValidFunction; }