[15064] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Linq;
|
---|
[15338] | 4 | using System.Threading;
|
---|
[17332] | 5 | using HEAL.Attic;
|
---|
[15064] | 6 | using HeuristicLab.Algorithms.DataAnalysis;
|
---|
| 7 | using HeuristicLab.Analysis;
|
---|
| 8 | using HeuristicLab.Common;
|
---|
| 9 | using HeuristicLab.Core;
|
---|
| 10 | using HeuristicLab.Data;
|
---|
[15343] | 11 | using HeuristicLab.Encodings.IntegerVectorEncoding;
|
---|
[15064] | 12 | using HeuristicLab.Operators;
|
---|
| 13 | using HeuristicLab.Optimization;
|
---|
| 14 | using HeuristicLab.Optimization.Operators;
|
---|
| 15 | using HeuristicLab.Parameters;
|
---|
| 16 | using HeuristicLab.Problems.DataAnalysis;
|
---|
| 17 | using HeuristicLab.Random;
|
---|
| 18 |
|
---|
| 19 | namespace HeuristicLab.Algorithms.EGO {
|
---|
[15343] | 20 | [Item("Discrete Adaptive Sampling Algorithm (D-EGO)", "A discrete adaptive sampling algorithm.")]
|
---|
[15064] | 21 | [Creatable(CreatableAttribute.Categories.SingleSolutionAlgorithms, Priority = 150)]
|
---|
[17332] | 22 | [StorableType("39e06191-714c-44e0-8b57-a5a64f072371")]
|
---|
| 23 | public class DiscreteAdaptiveSamplingAlgorithm : HeuristicOptimizationEngineAlgorithm, IStorableContent {
|
---|
[15064] | 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";
|
---|
[15976] | 42 | private const string InitialDatasetParameterName = "SampleSet";
|
---|
[15064] | 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>>;
|
---|
[15338] | 53 | public IFixedValueParameter<MultiAnalyzer> AnalyzerParameter => (IFixedValueParameter<MultiAnalyzer>)Parameters[AnalyzerParameterName];
|
---|
[15064] | 54 | public IFixedValueParameter<BoolValue> RemoveDuplicatesParameter => (IFixedValueParameter<BoolValue>)Parameters[RemoveDuplicatesParamterName];
|
---|
| 55 | public IFixedValueParameter<IntValue> MaxModelSizeParameter => (IFixedValueParameter<IntValue>)Parameters[MaxModelSizeParameterName];
|
---|
[15976] | 56 | public IValueParameter<IDataset> InitialDatasetParameter => (IValueParameter<IDataset>)Parameters[InitialDatasetParameterName];
|
---|
[15064] | 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();
|
---|
[15976] | 71 | public IDataset InitialDataset => InitialDatasetParameter.Value;
|
---|
[15064] | 72 | #endregion
|
---|
| 73 |
|
---|
[15338] | 74 |
|
---|
| 75 |
|
---|
| 76 | #region Operators
|
---|
| 77 |
|
---|
| 78 | [Storable]
|
---|
| 79 | private OperatorList Operators;
|
---|
| 80 |
|
---|
[15064] | 81 | #region Preconfigured Analyzers
|
---|
| 82 | [Storable]
|
---|
| 83 | private BestAverageWorstQualityAnalyzer singleObjectiveQualityAnalyzer;
|
---|
| 84 | [Storable]
|
---|
[15343] | 85 | private DiscreteCorrelationAnalyzer correlationAnalyzer;
|
---|
[15064] | 86 | [Storable]
|
---|
[15343] | 87 | private DiscreteEvaluatedSolutionsAnalyzer sampleAnalyzer;
|
---|
[15064] | 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]
|
---|
[15343] | 107 | private DiscreteSampleCollector sampleCollector;
|
---|
[15064] | 108 | [Storable]
|
---|
[15343] | 109 | private DiscreteInfillSolver infillSolver;
|
---|
[15064] | 110 | [Storable]
|
---|
| 111 | private SolutionsCreator infillSolutionsCreator;
|
---|
| 112 | #endregion
|
---|
[15338] | 113 | #endregion
|
---|
[15064] | 114 |
|
---|
| 115 | #region Constructors
|
---|
| 116 | [StorableConstructor]
|
---|
[17332] | 117 | protected DiscreteAdaptiveSamplingAlgorithm(StorableConstructorFlag deserializing) : base(deserializing) { }
|
---|
[15064] | 118 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 119 | private void AfterDeserialization() {
|
---|
| 120 | Initialize();
|
---|
| 121 | }
|
---|
[15343] | 122 | protected DiscreteAdaptiveSamplingAlgorithm(DiscreteAdaptiveSamplingAlgorithm original, Cloner cloner) : base(original, cloner) {
|
---|
[15064] | 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);
|
---|
[15338] | 134 |
|
---|
| 135 | Operators = cloner.Clone(original.Operators);
|
---|
[15064] | 136 | Initialize();
|
---|
| 137 | }
|
---|
| 138 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[15343] | 139 | return new DiscreteAdaptiveSamplingAlgorithm(this, cloner);
|
---|
[15064] | 140 | }
|
---|
[15343] | 141 | public DiscreteAdaptiveSamplingAlgorithm() {
|
---|
[15064] | 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 | };
|
---|
[15343] | 153 | var cmaes = new OffspringSelectionGeneticAlgorithm.OffspringSelectionGeneticAlgorithm {
|
---|
| 154 | MaximumGenerations = new IntValue(300),
|
---|
| 155 | PopulationSize = new IntValue(50),
|
---|
| 156 | Problem = new DiscreteInfillProblem()
|
---|
[15064] | 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)));
|
---|
[15976] | 173 | Parameters.Add(new ValueParameter<IDataset>(InitialDatasetParameterName, "The initial dataset that should be built upon.", new ModifiableDataset()));
|
---|
[15064] | 174 | #endregion
|
---|
| 175 |
|
---|
| 176 | #region Create operators
|
---|
[15338] | 177 | Operators = new OperatorList();
|
---|
| 178 |
|
---|
[15064] | 179 | var randomCreator = new RandomCreator();
|
---|
[15338] | 180 | Operators.Add(randomCreator);
|
---|
[15064] | 181 | var variableCreator = new VariableCreator { Name = "Initialize Variables" };
|
---|
[15338] | 182 | Operators.Add(variableCreator);
|
---|
[15064] | 183 | var resultsCollector = new ResultsCollector();
|
---|
[15338] | 184 | Operators.Add(resultsCollector);
|
---|
[15064] | 185 | var solutionCreator = new SolutionsCreator { Name = "Create Initial Solutions" };
|
---|
[15338] | 186 | Operators.Add(solutionCreator);
|
---|
[15064] | 187 | var analyzerPlaceholder = new Placeholder { Name = "Analyzer (Placeholder)" };
|
---|
[15338] | 188 | Operators.Add(analyzerPlaceholder);
|
---|
[15064] | 189 | var evaluationsCounter = new IntCounter { Name = "Increment EvaluatedSolutions" };
|
---|
[15338] | 190 | Operators.Add(evaluationsCounter);
|
---|
[15064] | 191 | var solutionsCollectorProcessor = new UniformSubScopesProcessor { Name = "Collect Samples to Dataset" };
|
---|
[15338] | 192 | Operators.Add(solutionsCollectorProcessor);
|
---|
[15064] | 193 | var phaseChecker = new ConditionalBranch { Name = "Inital Samples evaluated?" };
|
---|
[15338] | 194 | Operators.Add(phaseChecker);
|
---|
[15064] | 195 | var initialPhaseChecker = new Comparator { Name = "Compare Initial Samples to evaluated Solutions" };
|
---|
[15338] | 196 | Operators.Add(initialPhaseChecker);
|
---|
[15064] | 197 | var modelbuilder = new ModelBuilder { Name = "Build Model" };
|
---|
[15338] | 198 | Operators.Add(modelbuilder);
|
---|
| 199 |
|
---|
[15343] | 200 | sampleCollector = new DiscreteSampleCollector { Name = "Collect Sample" };
|
---|
[15338] | 201 | Operators.Add(sampleCollector);
|
---|
[15064] | 202 | infillSolutionsCreator = new SolutionsCreator { Name = "Create Adaptive Solutions" };
|
---|
[15338] | 203 | Operators.Add(infillSolutionsCreator);
|
---|
[15343] | 204 | infillSolver = new DiscreteInfillSolver();
|
---|
[15338] | 205 | Operators.Add(infillSolver);
|
---|
| 206 |
|
---|
[15064] | 207 | var subScopesRemover = new SubScopesRemover();
|
---|
[15338] | 208 | Operators.Add(subScopesRemover);
|
---|
[15064] | 209 | var iterationsCounter = new IntCounter { Name = "Increment Iterations" };
|
---|
[15338] | 210 | Operators.Add(iterationsCounter);
|
---|
[15064] | 211 | var terminationOperator = new TerminationOperator();
|
---|
[15338] | 212 | Operators.Add(terminationOperator);
|
---|
[15064] | 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)));
|
---|
[15343] | 236 | variableCreator.CollectedValues.Add(new ValueParameter<IntMatrix>(infillBoundsVariableName));
|
---|
[15064] | 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;
|
---|
[15338] | 252 |
|
---|
[15064] | 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();
|
---|
[15338] | 305 | Operators.Add(singleObjectiveQualityAnalyzer);
|
---|
| 306 |
|
---|
[15343] | 307 | correlationAnalyzer = new DiscreteCorrelationAnalyzer();
|
---|
[15064] | 308 | correlationAnalyzer.ModelParameter.ActualName = modelVariableName;
|
---|
[15338] | 309 | Operators.Add(correlationAnalyzer);
|
---|
[15064] | 310 |
|
---|
[15343] | 311 | sampleAnalyzer = new DiscreteEvaluatedSolutionsAnalyzer();
|
---|
[15064] | 312 | sampleAnalyzer.PhaseParameter.ActualName = initialPhaseVariableName;
|
---|
[15338] | 313 | Operators.Add(sampleAnalyzer);
|
---|
[15064] | 314 |
|
---|
| 315 | modelAnalyzer = new ModelQualityAnalyzer();
|
---|
| 316 | modelAnalyzer.ModelParameter.ActualName = modelVariableName;
|
---|
[15338] | 317 | Operators.Add(modelAnalyzer);
|
---|
[15064] | 318 |
|
---|
| 319 | variabilityAnalyzer = new VariableVariabilityAnalyzer();
|
---|
| 320 | variabilityAnalyzer.DatasetParameter.ActualName = datasetVariableName;
|
---|
| 321 | variabilityAnalyzer.InitialEvaluationsParameter.ActualName = InitialEvaluationsParameter.Name;
|
---|
[15338] | 322 | Operators.Add(variabilityAnalyzer);
|
---|
[15064] | 323 |
|
---|
| 324 | fitnessClusterAnalyzer = new FitnessClusteringAnalyzer();
|
---|
| 325 | fitnessClusterAnalyzer.DatasetParameter.ActualName = datasetVariableName;
|
---|
[15338] | 326 | Operators.Add(fitnessClusterAnalyzer);
|
---|
[15064] | 327 | #endregion
|
---|
| 328 |
|
---|
| 329 | #region Create terminators
|
---|
| 330 |
|
---|
[15338] | 331 | evaluationsTerminator = new ComparisonTerminator<IntValue>("EvaluatedSolutions", ComparisonType.Less, new IntValue(1000)) { Name = "Evaluated solutions." };
|
---|
| 332 | Operators.Add(evaluationsTerminator);
|
---|
[15064] | 333 | qualityTerminator = new SingleObjectiveQualityTerminator { Name = "Quality" };
|
---|
[15338] | 334 | Operators.Add(qualityTerminator);
|
---|
[15064] | 335 | executionTimeTerminator = new ExecutionTimeTerminator(this, new TimeSpanValue(TimeSpan.FromMinutes(5)));
|
---|
[15338] | 336 | Operators.Add(executionTimeTerminator);
|
---|
[15064] | 337 | #endregion
|
---|
| 338 |
|
---|
| 339 | #region Parameterize
|
---|
| 340 | UpdateAnalyzers();
|
---|
| 341 | ParameterizeAnalyzers();
|
---|
[15338] | 342 | ParameterizeStochasticOperators(Operators);
|
---|
[15064] | 343 | UpdateTerminators();
|
---|
| 344 | #endregion
|
---|
| 345 |
|
---|
| 346 | Initialize();
|
---|
| 347 | }
|
---|
| 348 |
|
---|
| 349 | private void Initialize() {
|
---|
| 350 | if (SingleObjectiveProblem != null) {
|
---|
[15338] | 351 | SingleObjectiveProblem.Evaluator.QualityParameter.ActualNameChanged += Evaluator_QualityParameter_ActualNameChanged;
|
---|
[15064] | 352 | }
|
---|
[15338] | 353 | singleObjectiveQualityAnalyzer.CurrentBestQualityParameter.NameChanged += QualityAnalyzer_CurrentBestQualityParameter_NameChanged;
|
---|
[15064] | 354 | RegisterParameterEvents();
|
---|
| 355 | }
|
---|
[15338] | 356 | #endregion
|
---|
[15064] | 357 |
|
---|
[15338] | 358 | public override void Start(CancellationToken cancellationToken) {
|
---|
| 359 | ParameterizeProblemSpecificOperators();
|
---|
[15343] | 360 | ((DiscreteInfillProblem)InfillOptimizationAlgorithm.Problem).InfillCriterion = InfillCriterion;
|
---|
[15338] | 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 |
|
---|
[15064] | 377 | #region Parameterization
|
---|
| 378 | private void ParameterizeTerminators() {
|
---|
| 379 | if (SingleObjectiveProblem != null)
|
---|
| 380 | qualityTerminator.Parameterize(singleObjectiveQualityAnalyzer.CurrentBestQualityParameter,
|
---|
| 381 | SingleObjectiveProblem);
|
---|
| 382 | }
|
---|
| 383 | private void ParameterizeAnalyzers() {
|
---|
[15338] | 384 | foreach (var op in Operators.OfType<IResultsOperator>()) {
|
---|
| 385 | op.ResultsParameter.ActualName = "Results";
|
---|
| 386 | op.ResultsParameter.Hidden = true;
|
---|
| 387 | }
|
---|
| 388 |
|
---|
[15064] | 389 | singleObjectiveQualityAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
| 390 | singleObjectiveQualityAnalyzer.ResultsParameter.Hidden = true;
|
---|
| 391 | singleObjectiveQualityAnalyzer.QualityParameter.Depth = 1;
|
---|
| 392 | correlationAnalyzer.QualityParameter.Depth = 1;
|
---|
[15343] | 393 | correlationAnalyzer.IntegerVectorParameter.Depth = 1;
|
---|
[15064] | 394 | sampleAnalyzer.QualityParameter.Depth = 1;
|
---|
[15343] | 395 | sampleAnalyzer.IntegerVectorParameter.Depth = 1;
|
---|
[15064] | 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;
|
---|
[15338] | 413 | ParameterizeStochasticOperators(new[] { SolutionsCreator, infillSolutionsCreator });
|
---|
[15064] | 414 | }
|
---|
[15338] | 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 | }
|
---|
[15064] | 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;
|
---|
[15343] | 424 | var rcreator = Problem.SolutionCreator as IIntegerVectorCreator;
|
---|
| 425 | if (rcreator == null) throw new ArgumentException("Discrete Adaptive sampling requires integer vectors as solution candidates");
|
---|
[15338] | 426 |
|
---|
[15064] | 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;
|
---|
[15343] | 431 | infillSolver.IntegerVectorParameter.ActualName = rcreator.IntegerVectorParameter.ActualName;
|
---|
[15064] | 432 |
|
---|
[15343] | 433 | sampleCollector.IntegerVectorParameter.ActualName = rcreator.IntegerVectorParameter.ActualName;
|
---|
[15064] | 434 | sampleCollector.QualityParameter.ActualName = SingleObjectiveProblem.Evaluator.QualityParameter.ActualName;
|
---|
| 435 |
|
---|
[15343] | 436 | correlationAnalyzer.IntegerVectorParameter.ActualName = rcreator.IntegerVectorParameter.ActualName;
|
---|
| 437 | sampleAnalyzer.IntegerVectorParameter.ActualName = rcreator.IntegerVectorParameter.ActualName;
|
---|
[15064] | 438 | }
|
---|
[15338] | 439 | private void ParameterizeCancellableOperators(CancellationToken cancellation) {
|
---|
| 440 | foreach (var op in Operators.OfType<ICancellableOperator>())
|
---|
| 441 | op.Cancellation = cancellation;
|
---|
| 442 | }
|
---|
[15064] | 443 |
|
---|
[15338] | 444 | private void UnParameterizeCancellableOperators(CancellationToken cancellation) {
|
---|
| 445 | foreach (var op in Operators.OfType<ICancellableOperator>())
|
---|
| 446 | op.Cancellation = CancellationToken.None;
|
---|
[15064] | 447 | }
|
---|
[15338] | 448 | #endregion
|
---|
[15064] | 449 |
|
---|
[15338] | 450 | #region Events
|
---|
[15064] | 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) {
|
---|
[15343] | 457 | ((DiscreteInfillProblem)InfillOptimizationAlgorithm.Problem).InfillCriterion = InfillCriterion;
|
---|
[15064] | 458 | }
|
---|
| 459 | private void OnInfillAlgorithmChanged(object sender, EventArgs e) {
|
---|
[15343] | 460 | InfillOptimizationAlgorithm.Problem = new DiscreteInfillProblem { InfillCriterion = InfillCriterion };
|
---|
[15064] | 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;
|
---|
[15343] | 467 | InfillOptimizationAlgorithm.Problem = new DiscreteInfillProblem { InfillCriterion = InfillCriterion };
|
---|
[15064] | 468 | InfillOptimizationAlgorithm.ProblemChanged += OnInfillProblemChanged;
|
---|
| 469 | }
|
---|
[15338] | 470 |
|
---|
[15064] | 471 | protected override void OnProblemChanged() {
|
---|
| 472 | base.OnProblemChanged();
|
---|
[15338] | 473 | ParameterizeStochasticOperators(Problem.Operators);
|
---|
| 474 | UpdateAnalyzers();
|
---|
| 475 | UpdateTerminators();
|
---|
[15064] | 476 | ParameterizeSolutionsCreators();
|
---|
| 477 | if (SingleObjectiveProblem != null)
|
---|
[15338] | 478 | SingleObjectiveProblem.Evaluator.QualityParameter.ActualNameChanged += Evaluator_QualityParameter_ActualNameChanged;
|
---|
[15064] | 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);
|
---|
[15338] | 493 | ParameterizeStochasticOperators(Problem.SolutionCreator.ToEnumerable());
|
---|
[17337] | 494 | var qParameter = SingleObjectiveProblem?.Evaluator?.QualityParameter;
|
---|
| 495 | if(qParameter!= null)
|
---|
| 496 | qParameter.ActualNameChanged += Evaluator_QualityParameter_ActualNameChanged;
|
---|
[15064] | 497 | ParameterizeSolutionsCreators();
|
---|
| 498 | ParameterizeAnalyzers();
|
---|
| 499 | }
|
---|
| 500 | protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
|
---|
| 501 | base.Problem_EvaluatorChanged(sender, e);
|
---|
[15338] | 502 | ParameterizeStochasticOperators(Problem.Operators);
|
---|
[15064] | 503 | ParameterizeSolutionsCreators();
|
---|
| 504 | }
|
---|
| 505 | protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
|
---|
| 506 | base.Problem_OperatorsChanged(sender, e);
|
---|
[15338] | 507 | UpdateAnalyzers();
|
---|
[15064] | 508 | UpdateTerminators();
|
---|
| 509 | }
|
---|
| 510 | private void MaximizationParameter_ValueChanged(object sender, EventArgs e) {
|
---|
[15338] | 511 | ParameterizeAnalyzers();
|
---|
[15064] | 512 | ParameterizeTerminators();
|
---|
| 513 | }
|
---|
| 514 | private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
| 515 | ParameterizeAnalyzers();
|
---|
| 516 | }
|
---|
| 517 | private void QualityAnalyzer_CurrentBestQualityParameter_NameChanged(object sender, EventArgs e) {
|
---|
| 518 | ParameterizeTerminators();
|
---|
| 519 | }
|
---|
[15338] | 520 |
|
---|
[15064] | 521 | #endregion
|
---|
| 522 |
|
---|
| 523 | #region Updates
|
---|
| 524 | private void UpdateAnalyzers() {
|
---|
| 525 | Analyzer.Operators.Clear();
|
---|
[15338] | 526 | if (SingleObjectiveProblem == null) return;
|
---|
| 527 | foreach (var analyzer in Operators.OfType<IAnalyzer>().Concat(SingleObjectiveProblem.Operators.OfType<IAnalyzer>()))
|
---|
[15064] | 528 | Analyzer.Operators.Add(analyzer, analyzer.EnabledByDefault);
|
---|
[15338] | 529 | ParameterizeAnalyzers();
|
---|
[15064] | 530 | }
|
---|
| 531 | private void UpdateTerminators() {
|
---|
| 532 | var newTerminators = new Dictionary<ITerminator, bool> {
|
---|
| 533 | {
|
---|
| 534 | evaluationsTerminator,
|
---|
| 535 | !Terminator.Operators.Contains(evaluationsTerminator) ||
|
---|
| 536 | Terminator.Operators.ItemChecked(evaluationsTerminator)
|
---|
| 537 | }, {
|
---|
| 538 | executionTimeTerminator,
|
---|
| 539 | Terminator.Operators.Contains(executionTimeTerminator) &&
|
---|
| 540 | Terminator.Operators.ItemChecked(executionTimeTerminator)
|
---|
| 541 | }
|
---|
| 542 | };
|
---|
[15338] | 543 | if (SingleObjectiveProblem != null) {
|
---|
| 544 | newTerminators.Add(qualityTerminator, Terminator.Operators.Contains(qualityTerminator) && Terminator.Operators.ItemChecked(qualityTerminator));
|
---|
[15064] | 545 | foreach (var terminator in Problem.Operators.OfType<ITerminator>())
|
---|
[15338] | 546 | newTerminators.Add(terminator, !Terminator.Operators.Contains(terminator) || Terminator.Operators.ItemChecked(terminator));
|
---|
[15064] | 547 | }
|
---|
| 548 | Terminator.Operators.Clear();
|
---|
| 549 | foreach (var newTerminator in newTerminators)
|
---|
| 550 | Terminator.Operators.Add(newTerminator.Key, newTerminator.Value);
|
---|
[15338] | 551 | ParameterizeTerminators();
|
---|
[15064] | 552 | }
|
---|
| 553 | #endregion
|
---|
| 554 | }
|
---|
| 555 |
|
---|
| 556 | }
|
---|