Free cookie consent management tool by TermsFeed Policy Generator

source: branches/EfficientGlobalOptimization/HeuristicLab.Algorithms.EGO/DiscreteEGO/DiscreteAdaptiveSamplingAlgorithm.cs @ 16101

Last change on this file since 16101 was 15976, checked in by jkarder, 7 years ago

#2745: worked on EGO implementation

  • added parameter for initial sample set (only for D-EGO)
  • fixed project references and output paths
  • minor changes
File size: 29.7 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Threading;
5using HeuristicLab.Algorithms.DataAnalysis;
6using HeuristicLab.Analysis;
7using HeuristicLab.Common;
8using HeuristicLab.Core;
9using HeuristicLab.Data;
10using HeuristicLab.Encodings.IntegerVectorEncoding;
11using HeuristicLab.Operators;
12using HeuristicLab.Optimization;
13using HeuristicLab.Optimization.Operators;
14using HeuristicLab.Parameters;
15using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
16using HeuristicLab.Problems.DataAnalysis;
17using HeuristicLab.Random;
18
19namespace HeuristicLab.Algorithms.EGO {
20  [Item("Discrete Adaptive Sampling Algorithm (D-EGO)", "A discrete adaptive sampling algorithm.")]
21  [Creatable(CreatableAttribute.Categories.SingleSolutionAlgorithms, Priority = 150)]
22  [StorableClass]
23  public class DiscreteAdaptiveSamplingAlgorithm : HeuristicOptimizationEngineAlgorithm, IStorableContent {
24    public string Filename { get; set; }
25
26    #region Problem Properties
27    public override Type ProblemType => typeof(IHeuristicOptimizationProblem);
28    private ISingleObjectiveHeuristicOptimizationProblem SingleObjectiveProblem => Problem as ISingleObjectiveHeuristicOptimizationProblem;
29    #endregion
30
31    #region Parameter Names
32    private const string InfillCriterionParameterName = "InfillCriterion";
33    private const string InitialEvaluationsParameterName = "Initial Evaluations";
34    private const string TerminatorParameterName = "Terminator";
35    private const string SeedParameterName = "Seed";
36    private const string SetSeedRandomlyParameterName = "SetSeedRandomly";
37    private const string RegressionAlgorithmParameterName = "RegressionAlgorithm";
38    private const string InfillOptimizationAlgorithmParameterName = "InfillOptimizationAlgorithm";
39    private const string AnalyzerParameterName = "Analyzers";
40    private const string RemoveDuplicatesParamterName = "PerturbDuplicates";
41    private const string MaxModelSizeParameterName = "Maximal Model Size";
42    private const string InitialDatasetParameterName = "SampleSet";
43    #endregion
44
45    #region Parameter Properties
46    public IFixedValueParameter<IntValue> SeedParameter => (IFixedValueParameter<IntValue>)Parameters[SeedParameterName];
47    public IFixedValueParameter<BoolValue> SetSeedRandomlyParameter => (IFixedValueParameter<BoolValue>)Parameters[SetSeedRandomlyParameterName];
48    public IFixedValueParameter<MultiTerminator> TerminatorParameter => (IFixedValueParameter<MultiTerminator>)Parameters[TerminatorParameterName];
49    public IFixedValueParameter<IntValue> InitialEvaluationsParameter => (IFixedValueParameter<IntValue>)Parameters[InitialEvaluationsParameterName];
50    public IConstrainedValueParameter<IInfillCriterion> InfillCriterionParameter => Parameters[InfillCriterionParameterName] as IConstrainedValueParameter<IInfillCriterion>;
51    public IValueParameter<Algorithm> InfillOptimizationAlgorithmParameter => Parameters[InfillOptimizationAlgorithmParameterName] as IValueParameter<Algorithm>;
52    public IValueParameter<IDataAnalysisAlgorithm<IRegressionProblem>> RegressionAlgorithmParameter => Parameters[RegressionAlgorithmParameterName] as IValueParameter<IDataAnalysisAlgorithm<IRegressionProblem>>;
53    public IFixedValueParameter<MultiAnalyzer> AnalyzerParameter => (IFixedValueParameter<MultiAnalyzer>)Parameters[AnalyzerParameterName];
54    public IFixedValueParameter<BoolValue> RemoveDuplicatesParameter => (IFixedValueParameter<BoolValue>)Parameters[RemoveDuplicatesParamterName];
55    public IFixedValueParameter<IntValue> MaxModelSizeParameter => (IFixedValueParameter<IntValue>)Parameters[MaxModelSizeParameterName];
56    public IValueParameter<IDataset> InitialDatasetParameter => (IValueParameter<IDataset>)Parameters[InitialDatasetParameterName];
57    #endregion
58
59    #region Properties
60    public int Seed => SeedParameter.Value.Value;
61    public bool SetSeedRandomly => SetSeedRandomlyParameter.Value.Value;
62    public MultiTerminator Terminator => TerminatorParameter.Value;
63    public MultiAnalyzer Analyzer => AnalyzerParameter.Value;
64    public int InitialEvaluations => InitialEvaluationsParameter.Value.Value;
65    public IInfillCriterion InfillCriterion => InfillCriterionParameter.Value;
66    public Algorithm InfillOptimizationAlgorithm => InfillOptimizationAlgorithmParameter.Value;
67    public IDataAnalysisAlgorithm<IRegressionProblem> RegressionAlgorithm => RegressionAlgorithmParameter.Value;
68    public bool RemoveDuplicates => RemoveDuplicatesParameter.Value.Value;
69    public int MaxModelSize => MaxModelSizeParameter.Value.Value;
70    private SolutionsCreator SolutionsCreator => OperatorGraph.Iterate().OfType<SolutionsCreator>().First();
71    public IDataset InitialDataset => InitialDatasetParameter.Value;
72    #endregion
73
74
75
76    #region Operators
77
78    [Storable]
79    private OperatorList Operators;
80
81    #region Preconfigured Analyzers
82    [Storable]
83    private BestAverageWorstQualityAnalyzer singleObjectiveQualityAnalyzer;
84    [Storable]
85    private DiscreteCorrelationAnalyzer correlationAnalyzer;
86    [Storable]
87    private DiscreteEvaluatedSolutionsAnalyzer sampleAnalyzer;
88    [Storable]
89    private ModelQualityAnalyzer modelAnalyzer;
90    [Storable]
91    private VariableVariabilityAnalyzer variabilityAnalyzer;
92    [Storable]
93    private FitnessClusteringAnalyzer fitnessClusterAnalyzer;
94    #endregion
95
96    #region Preconfigured Terminators
97    [Storable]
98    private ComparisonTerminator<IntValue> evaluationsTerminator;
99    [Storable]
100    private ExecutionTimeTerminator executionTimeTerminator;
101    [Storable]
102    private SingleObjectiveQualityTerminator qualityTerminator;
103    #endregion
104
105    #region Problem-dependent Operators
106    [Storable]
107    private DiscreteSampleCollector sampleCollector;
108    [Storable]
109    private DiscreteInfillSolver infillSolver;
110    [Storable]
111    private SolutionsCreator infillSolutionsCreator;
112    #endregion
113    #endregion
114
115    #region Constructors
116    [StorableConstructor]
117    protected DiscreteAdaptiveSamplingAlgorithm(bool deserializing) : base(deserializing) { }
118    [StorableHook(HookType.AfterDeserialization)]
119    private void AfterDeserialization() {
120      Initialize();
121    }
122    protected DiscreteAdaptiveSamplingAlgorithm(DiscreteAdaptiveSamplingAlgorithm original, Cloner cloner) : base(original, cloner) {
123      singleObjectiveQualityAnalyzer = cloner.Clone(original.singleObjectiveQualityAnalyzer);
124      evaluationsTerminator = cloner.Clone(original.evaluationsTerminator);
125      qualityTerminator = cloner.Clone(original.qualityTerminator);
126      executionTimeTerminator = cloner.Clone(original.executionTimeTerminator);
127      sampleCollector = cloner.Clone(original.sampleCollector);
128      infillSolver = cloner.Clone(original.infillSolver);
129      infillSolutionsCreator = cloner.Clone(original.infillSolutionsCreator);
130      correlationAnalyzer = cloner.Clone(original.correlationAnalyzer);
131      sampleAnalyzer = cloner.Clone(original.sampleAnalyzer);
132      modelAnalyzer = cloner.Clone(original.modelAnalyzer);
133      fitnessClusterAnalyzer = cloner.Clone(original.fitnessClusterAnalyzer);
134
135      Operators = cloner.Clone(original.Operators);
136      Initialize();
137    }
138    public override IDeepCloneable Clone(Cloner cloner) {
139      return new DiscreteAdaptiveSamplingAlgorithm(this, cloner);
140    }
141    public DiscreteAdaptiveSamplingAlgorithm() {
142
143      #region Add parameters
144      var criteria = new ItemSet<IInfillCriterion> {
145        new AugmentedExpectedImprovement(),
146        new ExpectedImprovement(),
147        new ExpectedQuality(),
148        new ExpectedQuantileImprovement(),
149        new MinimalQuantileCriterium(),
150        new NeighbourDistance(),
151        new PluginExpectedImprovement()
152      };
153      var cmaes = new OffspringSelectionGeneticAlgorithm.OffspringSelectionGeneticAlgorithm {
154        MaximumGenerations = new IntValue(300),
155        PopulationSize = new IntValue(50),
156        Problem = new DiscreteInfillProblem()
157      };
158      var model = new GaussianProcessRegression {
159        Problem = new RegressionProblem()
160      };
161      model.CovarianceFunctionParameter.Value = new CovarianceRationalQuadraticIso();
162
163      Parameters.Add(new FixedValueParameter<IntValue>(SeedParameterName, "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
164      Parameters.Add(new FixedValueParameter<BoolValue>(SetSeedRandomlyParameterName, "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
165      Parameters.Add(new FixedValueParameter<MultiTerminator>("Terminator", "The termination criteria that defines if the algorithm should continue or stop.", new MultiTerminator()));
166      Parameters.Add(new FixedValueParameter<MultiAnalyzer>(AnalyzerParameterName, "The operator used to analyze the solutions each iteration.", new MultiAnalyzer()));
167      Parameters.Add(new FixedValueParameter<IntValue>(InitialEvaluationsParameterName, "The number of random solutions that are evaluated (in parallel) per iteration.", new IntValue(100)));
168      Parameters.Add(new ConstrainedValueParameter<IInfillCriterion>(InfillCriterionParameterName, "Decision what value should decide the next sample", criteria, criteria.OfType<ExpectedImprovement>().Single()));
169      Parameters.Add(new ValueParameter<IDataAnalysisAlgorithm<IRegressionProblem>>(RegressionAlgorithmParameterName, "The model used to approximate the problem", model));
170      Parameters.Add(new ValueParameter<Algorithm>(InfillOptimizationAlgorithmParameterName, "The algorithm used to solve the expected improvement subproblem", cmaes));
171      Parameters.Add(new FixedValueParameter<BoolValue>(RemoveDuplicatesParamterName, "Whether (almost) duplicate points in the dataset should be perturbed before evaluation (this greatly increases numerical stability for some model types)", new BoolValue(true)));
172      Parameters.Add(new FixedValueParameter<IntValue>("Maximal Model Size", "The maximum number of sample points used to build the model", new IntValue(-1)));
173      Parameters.Add(new ValueParameter<IDataset>(InitialDatasetParameterName, "The initial dataset that should be built upon.", new ModifiableDataset()));
174      #endregion
175
176      #region Create operators
177      Operators = new OperatorList();
178
179      var randomCreator = new RandomCreator();
180      Operators.Add(randomCreator);
181      var variableCreator = new VariableCreator { Name = "Initialize Variables" };
182      Operators.Add(variableCreator);
183      var resultsCollector = new ResultsCollector();
184      Operators.Add(resultsCollector);
185      var solutionCreator = new SolutionsCreator { Name = "Create Initial Solutions" };
186      Operators.Add(solutionCreator);
187      var analyzerPlaceholder = new Placeholder { Name = "Analyzer (Placeholder)" };
188      Operators.Add(analyzerPlaceholder);
189      var evaluationsCounter = new IntCounter { Name = "Increment EvaluatedSolutions" };
190      Operators.Add(evaluationsCounter);
191      var solutionsCollectorProcessor = new UniformSubScopesProcessor { Name = "Collect Samples to Dataset" };
192      Operators.Add(solutionsCollectorProcessor);
193      var phaseChecker = new ConditionalBranch { Name = "Inital Samples evaluated?" };
194      Operators.Add(phaseChecker);
195      var initialPhaseChecker = new Comparator { Name = "Compare Initial Samples to evaluated Solutions" };
196      Operators.Add(initialPhaseChecker);
197      var modelbuilder = new ModelBuilder { Name = "Build Model" };
198      Operators.Add(modelbuilder);
199
200      sampleCollector = new DiscreteSampleCollector { Name = "Collect Sample" };
201      Operators.Add(sampleCollector);
202      infillSolutionsCreator = new SolutionsCreator { Name = "Create Adaptive Solutions" };
203      Operators.Add(infillSolutionsCreator);
204      infillSolver = new DiscreteInfillSolver();
205      Operators.Add(infillSolver);
206
207      var subScopesRemover = new SubScopesRemover();
208      Operators.Add(subScopesRemover);
209      var iterationsCounter = new IntCounter { Name = "Increment Iterations" };
210      Operators.Add(iterationsCounter);
211      var terminationOperator = new TerminationOperator();
212      Operators.Add(terminationOperator);
213      #endregion
214
215      #region Wire operators
216      const string iterationsVariableName = "Iterations";
217      const string evaluatedSolutionsVariableName = "EvaluatedSolutions";
218      const string datasetVariableName = "SampleSet";
219      const string modelVariableName = "Model";
220      const string initialPhaseVariableName = "InitialPhase";
221      const string infillBoundsVariableName = "InfillBounds";
222
223      OperatorGraph.InitialOperator = randomCreator;
224
225      randomCreator.SeedParameter.Value = null;
226      randomCreator.SeedParameter.ActualName = SeedParameter.Name;
227      randomCreator.SetSeedRandomlyParameter.Value = null;
228      randomCreator.SetSeedRandomlyParameter.ActualName = SetSeedRandomlyParameter.Name;
229      randomCreator.Successor = variableCreator;
230
231      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>(iterationsVariableName, new IntValue(0)));
232      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>(evaluatedSolutionsVariableName, new IntValue(0)));
233      variableCreator.CollectedValues.Add(new ValueParameter<ModifiableDataset>(datasetVariableName, new ModifiableDataset()));
234      variableCreator.CollectedValues.Add(new ValueParameter<IRegressionSolution>(modelVariableName, "The regression solution representing the model for EGO", null));
235      variableCreator.CollectedValues.Add(new FixedValueParameter<BoolValue>(initialPhaseVariableName, new BoolValue(false)));
236      variableCreator.CollectedValues.Add(new ValueParameter<IntMatrix>(infillBoundsVariableName));
237      variableCreator.Successor = resultsCollector;
238
239      resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>(iterationsVariableName, "The current iteration number."));
240      resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>(evaluatedSolutionsVariableName, "The current number of evaluated solutions."));
241      resultsCollector.CollectedValues.Add(new LookupParameter<ModifiableDataset>(datasetVariableName, "A dataset containing all evaluated solutions"));
242      resultsCollector.Successor = phaseChecker;
243
244      phaseChecker.ConditionParameter.ActualName = initialPhaseVariableName;
245      phaseChecker.FalseBranch = solutionCreator;
246      phaseChecker.TrueBranch = modelbuilder;
247
248      #region Adaptive Phase
249      modelbuilder.DatasetParameter.ActualName = datasetVariableName;
250      modelbuilder.ModelParameter.ActualName = modelVariableName;
251      modelbuilder.RegressionAlgorithmParameter.ActualName = RegressionAlgorithmParameter.Name;
252
253      modelbuilder.Successor = infillSolutionsCreator;
254      modelbuilder.MaxModelSizeParameter.ActualName = MaxModelSizeParameter.Name;
255
256      infillSolutionsCreator.SolutionCreatorParameter.Value = infillSolver;
257      infillSolutionsCreator.Successor = evaluationsCounter;
258      infillSolutionsCreator.NumberOfSolutionsParameter.Value = new IntValue(1);
259
260      infillSolver.InfillOptimizationAlgorithmParamter.ActualName = InfillOptimizationAlgorithmParameterName;
261      infillSolver.ModelParameter.ActualName = modelVariableName;
262      infillSolver.RemoveDuplicatesParameter.ActualName = RemoveDuplicatesParameter.Name;
263      infillSolver.InfillBoundsParameter.ActualName = infillBoundsVariableName;
264      #endregion
265
266      #region Initial Phase
267      solutionCreator.NumberOfSolutionsParameter.Value = new IntValue(1);
268      solutionCreator.ParallelParameter.Value.Value = true;
269      solutionCreator.Successor = evaluationsCounter;
270      #endregion
271
272      evaluationsCounter.ValueParameter.ActualName = evaluatedSolutionsVariableName;
273      evaluationsCounter.Increment = null;
274      evaluationsCounter.IncrementParameter.Value = new IntValue(1);
275      evaluationsCounter.Successor = initialPhaseChecker;
276
277      initialPhaseChecker.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
278      initialPhaseChecker.LeftSideParameter.ActualName = evaluatedSolutionsVariableName;
279      initialPhaseChecker.RightSideParameter.ActualName = InitialEvaluationsParameterName;
280      initialPhaseChecker.ResultParameter.ActualName = initialPhaseVariableName;
281      initialPhaseChecker.Successor = analyzerPlaceholder;
282
283      analyzerPlaceholder.OperatorParameter.ActualName = AnalyzerParameter.Name;
284      analyzerPlaceholder.Successor = solutionsCollectorProcessor;
285
286      solutionsCollectorProcessor.Operator = sampleCollector;
287      solutionsCollectorProcessor.Depth.Value = 1;
288      solutionsCollectorProcessor.Successor = subScopesRemover;
289
290      sampleCollector.DatasetParameter.ActualName = datasetVariableName;
291
292      subScopesRemover.RemoveAllSubScopes = true;
293      subScopesRemover.Successor = iterationsCounter;
294
295      iterationsCounter.ValueParameter.ActualName = "Iterations";
296      iterationsCounter.Increment = new IntValue(1);
297      iterationsCounter.Successor = terminationOperator;
298
299      terminationOperator.TerminatorParameter.ActualName = TerminatorParameter.Name;
300      terminationOperator.ContinueBranch = phaseChecker;
301      #endregion
302
303      #region Create analyzers
304      singleObjectiveQualityAnalyzer = new BestAverageWorstQualityAnalyzer();
305      Operators.Add(singleObjectiveQualityAnalyzer);
306
307      correlationAnalyzer = new DiscreteCorrelationAnalyzer();
308      correlationAnalyzer.ModelParameter.ActualName = modelVariableName;
309      Operators.Add(correlationAnalyzer);
310
311      sampleAnalyzer = new DiscreteEvaluatedSolutionsAnalyzer();
312      sampleAnalyzer.PhaseParameter.ActualName = initialPhaseVariableName;
313      Operators.Add(sampleAnalyzer);
314
315      modelAnalyzer = new ModelQualityAnalyzer();
316      modelAnalyzer.ModelParameter.ActualName = modelVariableName;
317      Operators.Add(modelAnalyzer);
318
319      variabilityAnalyzer = new VariableVariabilityAnalyzer();
320      variabilityAnalyzer.DatasetParameter.ActualName = datasetVariableName;
321      variabilityAnalyzer.InitialEvaluationsParameter.ActualName = InitialEvaluationsParameter.Name;
322      Operators.Add(variabilityAnalyzer);
323
324      fitnessClusterAnalyzer = new FitnessClusteringAnalyzer();
325      fitnessClusterAnalyzer.DatasetParameter.ActualName = datasetVariableName;
326      Operators.Add(fitnessClusterAnalyzer);
327      #endregion
328
329      #region Create terminators
330
331      evaluationsTerminator = new ComparisonTerminator<IntValue>("EvaluatedSolutions", ComparisonType.Less, new IntValue(1000)) { Name = "Evaluated solutions." };
332      Operators.Add(evaluationsTerminator);
333      qualityTerminator = new SingleObjectiveQualityTerminator { Name = "Quality" };
334      Operators.Add(qualityTerminator);
335      executionTimeTerminator = new ExecutionTimeTerminator(this, new TimeSpanValue(TimeSpan.FromMinutes(5)));
336      Operators.Add(executionTimeTerminator);
337      #endregion
338
339      #region Parameterize
340      UpdateAnalyzers();
341      ParameterizeAnalyzers();
342      ParameterizeStochasticOperators(Operators);
343      UpdateTerminators();
344      #endregion
345
346      Initialize();
347    }
348
349    private void Initialize() {
350      if (SingleObjectiveProblem != null) {
351        SingleObjectiveProblem.Evaluator.QualityParameter.ActualNameChanged += Evaluator_QualityParameter_ActualNameChanged;
352      }
353      singleObjectiveQualityAnalyzer.CurrentBestQualityParameter.NameChanged += QualityAnalyzer_CurrentBestQualityParameter_NameChanged;
354      RegisterParameterEvents();
355    }
356    #endregion
357
358    public override void Start(CancellationToken cancellationToken) {
359      ParameterizeProblemSpecificOperators();
360      ((DiscreteInfillProblem)InfillOptimizationAlgorithm.Problem).InfillCriterion = InfillCriterion;
361      ParameterizeCancellableOperators(cancellationToken);
362      base.Start(cancellationToken);
363    }
364    public override void Stop() {
365      if (InfillOptimizationAlgorithm.ExecutionState == ExecutionState.Started || InfillOptimizationAlgorithm.ExecutionState == ExecutionState.Paused)
366        InfillOptimizationAlgorithm.Stop();
367      if (RegressionAlgorithm.ExecutionState == ExecutionState.Started || RegressionAlgorithm.ExecutionState == ExecutionState.Paused)
368        RegressionAlgorithm.Stop();
369      base.Stop();
370    }
371    public override void Pause() {
372      if (InfillOptimizationAlgorithm.ExecutionState == ExecutionState.Started) InfillOptimizationAlgorithm.Pause();
373      if (RegressionAlgorithm.ExecutionState == ExecutionState.Started) RegressionAlgorithm.Pause();
374      base.Pause();
375    }
376
377    #region Parameterization
378    private void ParameterizeTerminators() {
379      if (SingleObjectiveProblem != null)
380        qualityTerminator.Parameterize(singleObjectiveQualityAnalyzer.CurrentBestQualityParameter,
381          SingleObjectiveProblem);
382    }
383    private void ParameterizeAnalyzers() {
384      foreach (var op in Operators.OfType<IResultsOperator>()) {
385        op.ResultsParameter.ActualName = "Results";
386        op.ResultsParameter.Hidden = true;
387      }
388
389      singleObjectiveQualityAnalyzer.ResultsParameter.ActualName = "Results";
390      singleObjectiveQualityAnalyzer.ResultsParameter.Hidden = true;
391      singleObjectiveQualityAnalyzer.QualityParameter.Depth = 1;
392      correlationAnalyzer.QualityParameter.Depth = 1;
393      correlationAnalyzer.IntegerVectorParameter.Depth = 1;
394      sampleAnalyzer.QualityParameter.Depth = 1;
395      sampleAnalyzer.IntegerVectorParameter.Depth = 1;
396
397      if (SingleObjectiveProblem == null) return;
398      singleObjectiveQualityAnalyzer.MaximizationParameter.ActualName = SingleObjectiveProblem.MaximizationParameter.Name;
399      singleObjectiveQualityAnalyzer.MaximizationParameter.Hidden = true;
400      singleObjectiveQualityAnalyzer.QualityParameter.ActualName = SingleObjectiveProblem.Evaluator.QualityParameter.ActualName;
401      singleObjectiveQualityAnalyzer.QualityParameter.Hidden = true;
402      singleObjectiveQualityAnalyzer.BestKnownQualityParameter.ActualName = SingleObjectiveProblem.BestKnownQualityParameter.Name;
403      singleObjectiveQualityAnalyzer.BestKnownQualityParameter.Hidden = true;
404      correlationAnalyzer.QualityParameter.ActualName = SingleObjectiveProblem.Evaluator.QualityParameter.ActualName;
405      correlationAnalyzer.QualityParameter.Hidden = true;
406      sampleAnalyzer.QualityParameter.ActualName = SingleObjectiveProblem.Evaluator.QualityParameter.ActualName;
407      sampleAnalyzer.QualityParameter.Hidden = true;
408    }
409    private void ParameterizeSolutionsCreators() {
410      SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
411      SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
412      infillSolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
413      ParameterizeStochasticOperators(new[] { SolutionsCreator, infillSolutionsCreator });
414    }
415    private static void ParameterizeStochasticOperators(IEnumerable<IItem> operators) {
416      foreach (var op in operators.OfType<IStochasticOperator>()) {
417        op.RandomParameter.ActualName = "Random";
418        op.RandomParameter.Hidden = true;
419      }
420    }
421    private void ParameterizeProblemSpecificOperators() {
422      if (SingleObjectiveProblem == null) throw new ArgumentException("Adaptive sampling requires a single objective problem");
423      infillSolver.MaximizationParameter.ActualName = SingleObjectiveProblem.MaximizationParameter.Name;
424      var rcreator = Problem.SolutionCreator as IIntegerVectorCreator;
425      if (rcreator == null) throw new ArgumentException("Discrete Adaptive sampling requires integer vectors as solution candidates");
426
427      infillSolver.LengthParameter.ActualName = rcreator.LengthParameter.ActualName;
428      infillSolver.BoundsParameter.ActualName = rcreator.BoundsParameter.ActualName;
429      infillSolver.LengthParameter.Value = rcreator.LengthParameter.Value;
430      infillSolver.BoundsParameter.Value = rcreator.BoundsParameter.Value;
431      infillSolver.IntegerVectorParameter.ActualName = rcreator.IntegerVectorParameter.ActualName;
432
433      sampleCollector.IntegerVectorParameter.ActualName = rcreator.IntegerVectorParameter.ActualName;
434      sampleCollector.QualityParameter.ActualName = SingleObjectiveProblem.Evaluator.QualityParameter.ActualName;
435
436      correlationAnalyzer.IntegerVectorParameter.ActualName = rcreator.IntegerVectorParameter.ActualName;
437      sampleAnalyzer.IntegerVectorParameter.ActualName = rcreator.IntegerVectorParameter.ActualName;
438    }
439    private void ParameterizeCancellableOperators(CancellationToken cancellation) {
440      foreach (var op in Operators.OfType<ICancellableOperator>())
441        op.Cancellation = cancellation;
442    }
443
444    private void UnParameterizeCancellableOperators(CancellationToken cancellation) {
445      foreach (var op in Operators.OfType<ICancellableOperator>())
446        op.Cancellation = CancellationToken.None;
447    }
448    #endregion
449
450    #region Events
451    private void RegisterParameterEvents() {
452      InfillOptimizationAlgorithmParameter.ValueChanged += OnInfillAlgorithmChanged;
453      InfillOptimizationAlgorithm.ProblemChanged += OnInfillProblemChanged;
454      InfillCriterionParameter.ValueChanged += OnInfillCriterionChanged;
455    }
456    private void OnInfillCriterionChanged(object sender, EventArgs e) {
457      ((DiscreteInfillProblem)InfillOptimizationAlgorithm.Problem).InfillCriterion = InfillCriterion;
458    }
459    private void OnInfillAlgorithmChanged(object sender, EventArgs e) {
460      InfillOptimizationAlgorithm.Problem = new DiscreteInfillProblem { InfillCriterion = InfillCriterion };
461      InfillOptimizationAlgorithm.ProblemChanged -= OnInfillProblemChanged; //avoid double attaching
462      InfillOptimizationAlgorithm.ProblemChanged += OnInfillProblemChanged;
463    }
464    private void OnInfillProblemChanged(object sender, EventArgs e) {
465      //enforce an infill problem for the infill optimization algorithm
466      InfillOptimizationAlgorithm.ProblemChanged -= OnInfillProblemChanged;
467      InfillOptimizationAlgorithm.Problem = new DiscreteInfillProblem { InfillCriterion = InfillCriterion };
468      InfillOptimizationAlgorithm.ProblemChanged += OnInfillProblemChanged;
469    }
470
471    protected override void OnProblemChanged() {
472      base.OnProblemChanged();
473      ParameterizeStochasticOperators(Problem.Operators);
474      UpdateAnalyzers();
475      UpdateTerminators();
476      ParameterizeSolutionsCreators();
477      if (SingleObjectiveProblem != null)
478        SingleObjectiveProblem.Evaluator.QualityParameter.ActualNameChanged += Evaluator_QualityParameter_ActualNameChanged;
479    }
480    protected override void RegisterProblemEvents() {
481      base.RegisterProblemEvents();
482      var maximizationParameter = (IValueParameter<BoolValue>)SingleObjectiveProblem?.MaximizationParameter;
483      if (maximizationParameter != null) maximizationParameter.ValueChanged += MaximizationParameter_ValueChanged;
484
485    }
486    protected override void DeregisterProblemEvents() {
487      var maximizationParameter = (IValueParameter<BoolValue>)SingleObjectiveProblem?.MaximizationParameter;
488      if (maximizationParameter != null) maximizationParameter.ValueChanged -= MaximizationParameter_ValueChanged;
489      base.DeregisterProblemEvents();
490    }
491    protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
492      base.Problem_SolutionCreatorChanged(sender, e);
493      ParameterizeStochasticOperators(Problem.SolutionCreator.ToEnumerable());
494      SingleObjectiveProblem.Evaluator.QualityParameter.ActualNameChanged += Evaluator_QualityParameter_ActualNameChanged;
495      ParameterizeSolutionsCreators();
496      ParameterizeAnalyzers();
497    }
498    protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
499      base.Problem_EvaluatorChanged(sender, e);
500      ParameterizeStochasticOperators(Problem.Operators);
501      ParameterizeSolutionsCreators();
502    }
503    protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
504      base.Problem_OperatorsChanged(sender, e);
505      UpdateAnalyzers();
506      UpdateTerminators();
507    }
508    private void MaximizationParameter_ValueChanged(object sender, EventArgs e) {
509      ParameterizeAnalyzers();
510      ParameterizeTerminators();
511    }
512    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
513      ParameterizeAnalyzers();
514    }
515    private void QualityAnalyzer_CurrentBestQualityParameter_NameChanged(object sender, EventArgs e) {
516      ParameterizeTerminators();
517    }
518
519    #endregion
520
521    #region Updates
522    private void UpdateAnalyzers() {
523      Analyzer.Operators.Clear();
524      if (SingleObjectiveProblem == null) return;
525      foreach (var analyzer in Operators.OfType<IAnalyzer>().Concat(SingleObjectiveProblem.Operators.OfType<IAnalyzer>()))
526        Analyzer.Operators.Add(analyzer, analyzer.EnabledByDefault);
527      ParameterizeAnalyzers();
528    }
529    private void UpdateTerminators() {
530      var newTerminators = new Dictionary<ITerminator, bool> {
531        {
532          evaluationsTerminator,
533          !Terminator.Operators.Contains(evaluationsTerminator) ||
534          Terminator.Operators.ItemChecked(evaluationsTerminator)
535        }, {
536          executionTimeTerminator,
537          Terminator.Operators.Contains(executionTimeTerminator) &&
538          Terminator.Operators.ItemChecked(executionTimeTerminator)
539        }
540      };
541      if (SingleObjectiveProblem != null) {
542        newTerminators.Add(qualityTerminator, Terminator.Operators.Contains(qualityTerminator) && Terminator.Operators.ItemChecked(qualityTerminator));
543        foreach (var terminator in Problem.Operators.OfType<ITerminator>())
544          newTerminators.Add(terminator, !Terminator.Operators.Contains(terminator) || Terminator.Operators.ItemChecked(terminator));
545      }
546      Terminator.Operators.Clear();
547      foreach (var newTerminator in newTerminators)
548        Terminator.Operators.Add(newTerminator.Key, newTerminator.Value);
549      ParameterizeTerminators();
550    }
551    #endregion
552  }
553
554}
Note: See TracBrowser for help on using the repository browser.