Changeset 15338 for branches/EfficientGlobalOptimization/HeuristicLab.Algorithms.EGO/AdaptiveSamplingAlgorithm.cs
- Timestamp:
- 08/22/17 15:01:46 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/EfficientGlobalOptimization/HeuristicLab.Algorithms.EGO/AdaptiveSamplingAlgorithm.cs
r15064 r15338 2 2 using System.Collections.Generic; 3 3 using System.Linq; 4 using System.Threading; 4 5 using HeuristicLab.Algorithms.DataAnalysis; 5 6 using HeuristicLab.Analysis; … … 49 50 public IValueParameter<Algorithm> InfillOptimizationAlgorithmParameter => Parameters[InfillOptimizationAlgorithmParameterName] as IValueParameter<Algorithm>; 50 51 public IValueParameter<IDataAnalysisAlgorithm<IRegressionProblem>> RegressionAlgorithmParameter => Parameters[RegressionAlgorithmParameterName] as IValueParameter<IDataAnalysisAlgorithm<IRegressionProblem>>; 51 p rivateIFixedValueParameter<MultiAnalyzer> AnalyzerParameter => (IFixedValueParameter<MultiAnalyzer>)Parameters[AnalyzerParameterName];52 public IFixedValueParameter<MultiAnalyzer> AnalyzerParameter => (IFixedValueParameter<MultiAnalyzer>)Parameters[AnalyzerParameterName]; 52 53 public IFixedValueParameter<BoolValue> RemoveDuplicatesParameter => (IFixedValueParameter<BoolValue>)Parameters[RemoveDuplicatesParamterName]; 53 54 public IFixedValueParameter<IntValue> MaxModelSizeParameter => (IFixedValueParameter<IntValue>)Parameters[MaxModelSizeParameterName]; … … 65 66 public bool RemoveDuplicates => RemoveDuplicatesParameter.Value.Value; 66 67 public int MaxModelSize => MaxModelSizeParameter.Value.Value; 67 #endregion68 69 #region Helper Properties70 68 private SolutionsCreator SolutionsCreator => OperatorGraph.Iterate().OfType<SolutionsCreator>().First(); 71 69 #endregion 72 70 71 72 73 #region Operators 74 75 [Storable] 76 private OperatorList Operators; 77 73 78 #region Preconfigured Analyzers 74 79 [Storable] … … 102 107 [Storable] 103 108 private SolutionsCreator infillSolutionsCreator; 109 #endregion 104 110 #endregion 105 111 … … 123 129 modelAnalyzer = cloner.Clone(original.modelAnalyzer); 124 130 fitnessClusterAnalyzer = cloner.Clone(original.fitnessClusterAnalyzer); 131 132 Operators = cloner.Clone(original.Operators); 125 133 Initialize(); 126 134 } … … 163 171 164 172 #region Create operators 173 Operators = new OperatorList(); 174 165 175 var randomCreator = new RandomCreator(); 176 Operators.Add(randomCreator); 166 177 var variableCreator = new VariableCreator { Name = "Initialize Variables" }; 178 Operators.Add(variableCreator); 167 179 var resultsCollector = new ResultsCollector(); 180 Operators.Add(resultsCollector); 168 181 var solutionCreator = new SolutionsCreator { Name = "Create Initial Solutions" }; 182 Operators.Add(solutionCreator); 169 183 var analyzerPlaceholder = new Placeholder { Name = "Analyzer (Placeholder)" }; 184 Operators.Add(analyzerPlaceholder); 170 185 var evaluationsCounter = new IntCounter { Name = "Increment EvaluatedSolutions" }; 186 Operators.Add(evaluationsCounter); 171 187 var solutionsCollectorProcessor = new UniformSubScopesProcessor { Name = "Collect Samples to Dataset" }; 188 Operators.Add(solutionsCollectorProcessor); 172 189 var phaseChecker = new ConditionalBranch { Name = "Inital Samples evaluated?" }; 190 Operators.Add(phaseChecker); 173 191 var initialPhaseChecker = new Comparator { Name = "Compare Initial Samples to evaluated Solutions" }; 192 Operators.Add(initialPhaseChecker); 174 193 var modelbuilder = new ModelBuilder { Name = "Build Model" }; 194 Operators.Add(modelbuilder); 195 175 196 sampleCollector = new SampleCollector { Name = "Collect Sample" }; 197 Operators.Add(sampleCollector); 176 198 infillSolutionsCreator = new SolutionsCreator { Name = "Create Adaptive Solutions" }; 199 Operators.Add(infillSolutionsCreator); 177 200 infillSolver = new InfillSolver(); 201 Operators.Add(infillSolver); 202 178 203 var subScopesRemover = new SubScopesRemover(); 204 Operators.Add(subScopesRemover); 179 205 var iterationsCounter = new IntCounter { Name = "Increment Iterations" }; 206 Operators.Add(iterationsCounter); 180 207 var terminationOperator = new TerminationOperator(); 208 Operators.Add(terminationOperator); 181 209 #endregion 182 210 … … 218 246 modelbuilder.ModelParameter.ActualName = modelVariableName; 219 247 modelbuilder.RegressionAlgorithmParameter.ActualName = RegressionAlgorithmParameter.Name; 220 ParameterizeStochasticOperator(modelbuilder); 248 221 249 modelbuilder.Successor = infillSolutionsCreator; 222 250 modelbuilder.MaxModelSizeParameter.ActualName = MaxModelSizeParameter.Name; … … 265 293 iterationsCounter.Successor = terminationOperator; 266 294 267 268 269 295 terminationOperator.TerminatorParameter.ActualName = TerminatorParameter.Name; 270 296 terminationOperator.ContinueBranch = phaseChecker; 271 272 297 #endregion 273 298 274 299 #region Create analyzers 275 300 singleObjectiveQualityAnalyzer = new BestAverageWorstQualityAnalyzer(); 301 Operators.Add(singleObjectiveQualityAnalyzer); 302 276 303 correlationAnalyzer = new CorrelationAnalyzer(); 277 304 correlationAnalyzer.ModelParameter.ActualName = modelVariableName; 305 Operators.Add(correlationAnalyzer); 278 306 279 307 sampleAnalyzer = new EvaluatedSolutionsAnalyzer(); 280 308 sampleAnalyzer.PhaseParameter.ActualName = initialPhaseVariableName; 309 Operators.Add(sampleAnalyzer); 281 310 282 311 modelAnalyzer = new ModelQualityAnalyzer(); 283 312 modelAnalyzer.ModelParameter.ActualName = modelVariableName; 313 Operators.Add(modelAnalyzer); 284 314 285 315 variabilityAnalyzer = new VariableVariabilityAnalyzer(); 286 316 variabilityAnalyzer.DatasetParameter.ActualName = datasetVariableName; 287 317 variabilityAnalyzer.InitialEvaluationsParameter.ActualName = InitialEvaluationsParameter.Name; 318 Operators.Add(variabilityAnalyzer); 288 319 289 320 fitnessClusterAnalyzer = new FitnessClusteringAnalyzer(); 290 321 fitnessClusterAnalyzer.DatasetParameter.ActualName = datasetVariableName; 322 Operators.Add(fitnessClusterAnalyzer); 291 323 #endregion 292 324 293 325 #region Create terminators 294 326 295 evaluationsTerminator = new ComparisonTerminator<IntValue>("EvaluatedSolutions", ComparisonType.Less, 296 new IntValue(1000)) { Name = "Evaluated solutions." };327 evaluationsTerminator = new ComparisonTerminator<IntValue>("EvaluatedSolutions", ComparisonType.Less, new IntValue(1000)) { Name = "Evaluated solutions." }; 328 Operators.Add(evaluationsTerminator); 297 329 qualityTerminator = new SingleObjectiveQualityTerminator { Name = "Quality" }; 330 Operators.Add(qualityTerminator); 298 331 executionTimeTerminator = new ExecutionTimeTerminator(this, new TimeSpanValue(TimeSpan.FromMinutes(5))); 299 332 Operators.Add(executionTimeTerminator); 300 333 #endregion 301 334 … … 303 336 UpdateAnalyzers(); 304 337 ParameterizeAnalyzers(); 338 ParameterizeStochasticOperators(Operators); 305 339 UpdateTerminators(); 306 340 #endregion … … 308 342 Initialize(); 309 343 } 310 #endregion311 344 312 345 private void Initialize() { 313 346 if (SingleObjectiveProblem != null) { 314 SingleObjectiveProblem.Evaluator.QualityParameter.ActualNameChanged += 315 Evaluator_QualityParameter_ActualNameChanged; 347 SingleObjectiveProblem.Evaluator.QualityParameter.ActualNameChanged += Evaluator_QualityParameter_ActualNameChanged; 316 348 } 317 318 singleObjectiveQualityAnalyzer.CurrentBestQualityParameter.NameChanged += 319 QualityAnalyzer_CurrentBestQualityParameter_NameChanged; 349 singleObjectiveQualityAnalyzer.CurrentBestQualityParameter.NameChanged += QualityAnalyzer_CurrentBestQualityParameter_NameChanged; 320 350 RegisterParameterEvents(); 351 } 352 #endregion 353 354 public override void Start(CancellationToken cancellationToken) { 355 ParameterizeProblemSpecificOperators(); 356 ((InfillProblem)InfillOptimizationAlgorithm.Problem).InfillCriterion = InfillCriterion; 357 ParameterizeCancellableOperators(cancellationToken); 358 base.Start(cancellationToken); 359 } 360 public override void Stop() { 361 if (InfillOptimizationAlgorithm.ExecutionState == ExecutionState.Started || InfillOptimizationAlgorithm.ExecutionState == ExecutionState.Paused) 362 InfillOptimizationAlgorithm.Stop(); 363 if (RegressionAlgorithm.ExecutionState == ExecutionState.Started || RegressionAlgorithm.ExecutionState == ExecutionState.Paused) 364 RegressionAlgorithm.Stop(); 365 base.Stop(); 366 } 367 public override void Pause() { 368 if (InfillOptimizationAlgorithm.ExecutionState == ExecutionState.Started) InfillOptimizationAlgorithm.Pause(); 369 if (RegressionAlgorithm.ExecutionState == ExecutionState.Started) RegressionAlgorithm.Pause(); 370 base.Pause(); 321 371 } 322 372 … … 328 378 } 329 379 private void ParameterizeAnalyzers() { 330 //TODO how to guarantee ResultsParameters and QualityParameters are really there? This seems hardcoded 380 foreach (var op in Operators.OfType<IResultsOperator>()) { 381 op.ResultsParameter.ActualName = "Results"; 382 op.ResultsParameter.Hidden = true; 383 } 384 331 385 singleObjectiveQualityAnalyzer.ResultsParameter.ActualName = "Results"; 332 386 singleObjectiveQualityAnalyzer.ResultsParameter.Hidden = true; 333 387 singleObjectiveQualityAnalyzer.QualityParameter.Depth = 1; 334 correlationAnalyzer.ResultsParameter.ActualName = "Results";335 correlationAnalyzer.ResultsParameter.Hidden = true;336 388 correlationAnalyzer.QualityParameter.Depth = 1; 337 389 correlationAnalyzer.RealVectorParameter.Depth = 1; 338 sampleAnalyzer.ResultsParameter.ActualName = "Results";339 sampleAnalyzer.ResultsParameter.Hidden = true;340 390 sampleAnalyzer.QualityParameter.Depth = 1; 341 391 sampleAnalyzer.RealVectorParameter.Depth = 1; 342 modelAnalyzer.ResultsParameter.ActualName = "Results";343 modelAnalyzer.ResultsParameter.Hidden = true;344 fitnessClusterAnalyzer.ResultsParameter.ActualName = "Results";345 fitnessClusterAnalyzer.ResultsParameter.Hidden = true;346 347 392 348 393 if (SingleObjectiveProblem == null) return; … … 362 407 SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name; 363 408 infillSolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name; 364 } 365 private void ParameterizeStochasticOperator(IOperator @operator) { 366 var stochasticOperator = @operator as IStochasticOperator; 367 if (stochasticOperator == null) return; 368 stochasticOperator.RandomParameter.ActualName = "Random"; 369 stochasticOperator.RandomParameter.Hidden = true; 409 ParameterizeStochasticOperators(new[] { SolutionsCreator, infillSolutionsCreator }); 410 } 411 private static void ParameterizeStochasticOperators(IEnumerable<IItem> operators) { 412 foreach (var op in operators.OfType<IStochasticOperator>()) { 413 op.RandomParameter.ActualName = "Random"; 414 op.RandomParameter.Hidden = true; 415 } 370 416 } 371 417 private void ParameterizeProblemSpecificOperators() { … … 374 420 var rcreator = Problem.SolutionCreator as IRealVectorCreator; 375 421 if (rcreator == null) throw new ArgumentException("Adaptive sampling requires real vectors as solution candidates"); 422 376 423 infillSolver.LengthParameter.ActualName = rcreator.LengthParameter.ActualName; 377 424 infillSolver.BoundsParameter.ActualName = rcreator.BoundsParameter.ActualName; 378 // if (rcreator.LengthParameter.Value != null)379 425 infillSolver.LengthParameter.Value = rcreator.LengthParameter.Value; 380 // if (rcreator.BoundsParameter.Value != null)381 426 infillSolver.BoundsParameter.Value = rcreator.BoundsParameter.Value; 382 427 infillSolver.RealVectorParameter.ActualName = rcreator.RealVectorParameter.ActualName; … … 388 433 sampleAnalyzer.RealVectorParameter.ActualName = rcreator.RealVectorParameter.ActualName; 389 434 } 390 #endregion 391 392 public override void Start() { 393 ParameterizeProblemSpecificOperators(); 394 ((InfillProblem)InfillOptimizationAlgorithm.Problem).InfillCriterion = InfillCriterion; 395 base.Start(); 396 } 397 public override void Stop() { 398 if (InfillOptimizationAlgorithm.ExecutionState == ExecutionState.Started || InfillOptimizationAlgorithm.ExecutionState == ExecutionState.Paused) 399 InfillOptimizationAlgorithm.Stop(); 400 if (RegressionAlgorithm.ExecutionState == ExecutionState.Started || RegressionAlgorithm.ExecutionState == ExecutionState.Paused) 401 RegressionAlgorithm.Stop(); 402 base.Stop(); 403 } 404 public override void Pause() { 405 if (InfillOptimizationAlgorithm.ExecutionState == ExecutionState.Started) InfillOptimizationAlgorithm.Pause(); 406 if (RegressionAlgorithm.ExecutionState == ExecutionState.Started) RegressionAlgorithm.Pause(); 407 base.Pause(); 408 } 409 410 #region events 435 private void ParameterizeCancellableOperators(CancellationToken cancellation) { 436 foreach (var op in Operators.OfType<ICancellableOperator>()) 437 op.Cancellation = cancellation; 438 } 439 440 private void UnParameterizeCancellableOperators(CancellationToken cancellation) { 441 foreach (var op in Operators.OfType<ICancellableOperator>()) 442 op.Cancellation = CancellationToken.None; 443 } 444 #endregion 445 446 #region Events 411 447 private void RegisterParameterEvents() { 412 448 InfillOptimizationAlgorithmParameter.ValueChanged += OnInfillAlgorithmChanged; … … 428 464 InfillOptimizationAlgorithm.ProblemChanged += OnInfillProblemChanged; 429 465 } 466 430 467 protected override void OnProblemChanged() { 431 468 base.OnProblemChanged(); 432 ParameterizeStochasticOperator(Problem.SolutionCreator); 433 foreach (var @operator in Problem.Operators.OfType<IOperator>()) 434 ParameterizeStochasticOperator(@operator); 435 436 //TODO ParameterizeIterationBasedOperators(); 437 438 ParameterizeSolutionsCreators(); 439 ParameterizeAnalyzers(); 440 ParameterizeTerminators(); 441 442 if (SingleObjectiveProblem != null) 443 SingleObjectiveProblem.Evaluator.QualityParameter.ActualNameChanged += 444 Evaluator_QualityParameter_ActualNameChanged; 445 469 ParameterizeStochasticOperators(Problem.Operators); 446 470 UpdateAnalyzers(); 447 471 UpdateTerminators(); 472 ParameterizeSolutionsCreators(); 473 if (SingleObjectiveProblem != null) 474 SingleObjectiveProblem.Evaluator.QualityParameter.ActualNameChanged += Evaluator_QualityParameter_ActualNameChanged; 448 475 } 449 476 protected override void RegisterProblemEvents() { … … 460 487 protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) { 461 488 base.Problem_SolutionCreatorChanged(sender, e); 462 ParameterizeStochasticOperator(Problem.SolutionCreator); 463 464 if (SingleObjectiveProblem != null) SingleObjectiveProblem.Evaluator.QualityParameter.ActualNameChanged += Evaluator_QualityParameter_ActualNameChanged; 465 489 ParameterizeStochasticOperators(Problem.SolutionCreator.ToEnumerable()); 490 SingleObjectiveProblem.Evaluator.QualityParameter.ActualNameChanged += Evaluator_QualityParameter_ActualNameChanged; 466 491 ParameterizeSolutionsCreators(); 467 492 ParameterizeAnalyzers(); … … 469 494 protected override void Problem_EvaluatorChanged(object sender, EventArgs e) { 470 495 base.Problem_EvaluatorChanged(sender, e); 471 foreach (var @operator in Problem.Operators.OfType<IOperator>()) 472 ParameterizeStochasticOperator(@operator); 473 474 UpdateAnalyzers(); 475 496 ParameterizeStochasticOperators(Problem.Operators); 476 497 ParameterizeSolutionsCreators(); 477 498 } 478 499 protected override void Problem_OperatorsChanged(object sender, EventArgs e) { 479 500 base.Problem_OperatorsChanged(sender, e); 480 //TODO ParameterizeIterationBasedOperators();501 UpdateAnalyzers(); 481 502 UpdateTerminators(); 482 503 } 483 504 private void MaximizationParameter_ValueChanged(object sender, EventArgs e) { 505 ParameterizeAnalyzers(); 484 506 ParameterizeTerminators(); 485 507 } … … 490 512 ParameterizeTerminators(); 491 513 } 514 492 515 #endregion 493 516 … … 495 518 private void UpdateAnalyzers() { 496 519 Analyzer.Operators.Clear(); 497 if (Problem == null) return; 498 if (SingleObjectiveProblem != null) { 499 Analyzer.Operators.Add(singleObjectiveQualityAnalyzer, singleObjectiveQualityAnalyzer.EnabledByDefault); 500 Analyzer.Operators.Add(correlationAnalyzer, correlationAnalyzer.EnabledByDefault); 501 Analyzer.Operators.Add(sampleAnalyzer, sampleAnalyzer.EnabledByDefault); 502 Analyzer.Operators.Add(modelAnalyzer, modelAnalyzer.EnabledByDefault); 503 Analyzer.Operators.Add(variabilityAnalyzer, variabilityAnalyzer.EnabledByDefault); 504 Analyzer.Operators.Add(fitnessClusterAnalyzer, fitnessClusterAnalyzer.EnabledByDefault); 505 } 506 foreach (var analyzer in Problem.Operators.OfType<IAnalyzer>()) 520 if (SingleObjectiveProblem == null) return; 521 foreach (var analyzer in Operators.OfType<IAnalyzer>().Concat(SingleObjectiveProblem.Operators.OfType<IAnalyzer>())) 507 522 Analyzer.Operators.Add(analyzer, analyzer.EnabledByDefault); 523 ParameterizeAnalyzers(); 508 524 } 509 525 private void UpdateTerminators() { … … 519 535 } 520 536 }; 521 if (Problem != null) { 522 if (SingleObjectiveProblem != null) 523 newTerminators.Add(qualityTerminator, 524 Terminator.Operators.Contains(qualityTerminator) && Terminator.Operators.ItemChecked(qualityTerminator)); 537 if (SingleObjectiveProblem != null) { 538 newTerminators.Add(qualityTerminator, Terminator.Operators.Contains(qualityTerminator) && Terminator.Operators.ItemChecked(qualityTerminator)); 525 539 foreach (var terminator in Problem.Operators.OfType<ITerminator>()) 526 newTerminators.Add(terminator, 527 !Terminator.Operators.Contains(terminator) || Terminator.Operators.ItemChecked(terminator)); 540 newTerminators.Add(terminator, !Terminator.Operators.Contains(terminator) || Terminator.Operators.ItemChecked(terminator)); 528 541 } 529 530 542 Terminator.Operators.Clear(); 531 543 foreach (var newTerminator in newTerminators) 532 544 Terminator.Operators.Add(newTerminator.Key, newTerminator.Value); 533 }534 #endregion535 545 ParameterizeTerminators(); 546 } 547 #endregion 536 548 } 537 549
Note: See TracChangeset
for help on using the changeset viewer.