Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
08/22/17 15:01:46 (7 years ago)
Author:
bwerth
Message:

#2745 fixed bug concerning new Start and StartAsync methods; passed CancellationToken to sub algorithms

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/EfficientGlobalOptimization/HeuristicLab.Algorithms.EGO/AdaptiveSamplingAlgorithm.cs

    r15064 r15338  
    22using System.Collections.Generic;
    33using System.Linq;
     4using System.Threading;
    45using HeuristicLab.Algorithms.DataAnalysis;
    56using HeuristicLab.Analysis;
     
    4950    public IValueParameter<Algorithm> InfillOptimizationAlgorithmParameter => Parameters[InfillOptimizationAlgorithmParameterName] as IValueParameter<Algorithm>;
    5051    public IValueParameter<IDataAnalysisAlgorithm<IRegressionProblem>> RegressionAlgorithmParameter => Parameters[RegressionAlgorithmParameterName] as IValueParameter<IDataAnalysisAlgorithm<IRegressionProblem>>;
    51     private IFixedValueParameter<MultiAnalyzer> AnalyzerParameter => (IFixedValueParameter<MultiAnalyzer>)Parameters[AnalyzerParameterName];
     52    public IFixedValueParameter<MultiAnalyzer> AnalyzerParameter => (IFixedValueParameter<MultiAnalyzer>)Parameters[AnalyzerParameterName];
    5253    public IFixedValueParameter<BoolValue> RemoveDuplicatesParameter => (IFixedValueParameter<BoolValue>)Parameters[RemoveDuplicatesParamterName];
    5354    public IFixedValueParameter<IntValue> MaxModelSizeParameter => (IFixedValueParameter<IntValue>)Parameters[MaxModelSizeParameterName];
     
    6566    public bool RemoveDuplicates => RemoveDuplicatesParameter.Value.Value;
    6667    public int MaxModelSize => MaxModelSizeParameter.Value.Value;
    67     #endregion
    68 
    69     #region Helper Properties
    7068    private SolutionsCreator SolutionsCreator => OperatorGraph.Iterate().OfType<SolutionsCreator>().First();
    7169    #endregion
    7270
     71
     72
     73    #region Operators
     74
     75    [Storable]
     76    private OperatorList Operators;
     77
    7378    #region Preconfigured Analyzers
    7479    [Storable]
     
    102107    [Storable]
    103108    private SolutionsCreator infillSolutionsCreator;
     109    #endregion
    104110    #endregion
    105111
     
    123129      modelAnalyzer = cloner.Clone(original.modelAnalyzer);
    124130      fitnessClusterAnalyzer = cloner.Clone(original.fitnessClusterAnalyzer);
     131
     132      Operators = cloner.Clone(original.Operators);
    125133      Initialize();
    126134    }
     
    163171
    164172      #region Create operators
     173      Operators = new OperatorList();
     174
    165175      var randomCreator = new RandomCreator();
     176      Operators.Add(randomCreator);
    166177      var variableCreator = new VariableCreator { Name = "Initialize Variables" };
     178      Operators.Add(variableCreator);
    167179      var resultsCollector = new ResultsCollector();
     180      Operators.Add(resultsCollector);
    168181      var solutionCreator = new SolutionsCreator { Name = "Create Initial Solutions" };
     182      Operators.Add(solutionCreator);
    169183      var analyzerPlaceholder = new Placeholder { Name = "Analyzer (Placeholder)" };
     184      Operators.Add(analyzerPlaceholder);
    170185      var evaluationsCounter = new IntCounter { Name = "Increment EvaluatedSolutions" };
     186      Operators.Add(evaluationsCounter);
    171187      var solutionsCollectorProcessor = new UniformSubScopesProcessor { Name = "Collect Samples to Dataset" };
     188      Operators.Add(solutionsCollectorProcessor);
    172189      var phaseChecker = new ConditionalBranch { Name = "Inital Samples evaluated?" };
     190      Operators.Add(phaseChecker);
    173191      var initialPhaseChecker = new Comparator { Name = "Compare Initial Samples to evaluated Solutions" };
     192      Operators.Add(initialPhaseChecker);
    174193      var modelbuilder = new ModelBuilder { Name = "Build Model" };
     194      Operators.Add(modelbuilder);
     195
    175196      sampleCollector = new SampleCollector { Name = "Collect Sample" };
     197      Operators.Add(sampleCollector);
    176198      infillSolutionsCreator = new SolutionsCreator { Name = "Create Adaptive Solutions" };
     199      Operators.Add(infillSolutionsCreator);
    177200      infillSolver = new InfillSolver();
     201      Operators.Add(infillSolver);
     202
    178203      var subScopesRemover = new SubScopesRemover();
     204      Operators.Add(subScopesRemover);
    179205      var iterationsCounter = new IntCounter { Name = "Increment Iterations" };
     206      Operators.Add(iterationsCounter);
    180207      var terminationOperator = new TerminationOperator();
     208      Operators.Add(terminationOperator);
    181209      #endregion
    182210
     
    218246      modelbuilder.ModelParameter.ActualName = modelVariableName;
    219247      modelbuilder.RegressionAlgorithmParameter.ActualName = RegressionAlgorithmParameter.Name;
    220       ParameterizeStochasticOperator(modelbuilder);
     248
    221249      modelbuilder.Successor = infillSolutionsCreator;
    222250      modelbuilder.MaxModelSizeParameter.ActualName = MaxModelSizeParameter.Name;
     
    265293      iterationsCounter.Successor = terminationOperator;
    266294
    267 
    268 
    269295      terminationOperator.TerminatorParameter.ActualName = TerminatorParameter.Name;
    270296      terminationOperator.ContinueBranch = phaseChecker;
    271 
    272297      #endregion
    273298
    274299      #region Create analyzers
    275300      singleObjectiveQualityAnalyzer = new BestAverageWorstQualityAnalyzer();
     301      Operators.Add(singleObjectiveQualityAnalyzer);
     302
    276303      correlationAnalyzer = new CorrelationAnalyzer();
    277304      correlationAnalyzer.ModelParameter.ActualName = modelVariableName;
     305      Operators.Add(correlationAnalyzer);
    278306
    279307      sampleAnalyzer = new EvaluatedSolutionsAnalyzer();
    280308      sampleAnalyzer.PhaseParameter.ActualName = initialPhaseVariableName;
     309      Operators.Add(sampleAnalyzer);
    281310
    282311      modelAnalyzer = new ModelQualityAnalyzer();
    283312      modelAnalyzer.ModelParameter.ActualName = modelVariableName;
     313      Operators.Add(modelAnalyzer);
    284314
    285315      variabilityAnalyzer = new VariableVariabilityAnalyzer();
    286316      variabilityAnalyzer.DatasetParameter.ActualName = datasetVariableName;
    287317      variabilityAnalyzer.InitialEvaluationsParameter.ActualName = InitialEvaluationsParameter.Name;
     318      Operators.Add(variabilityAnalyzer);
    288319
    289320      fitnessClusterAnalyzer = new FitnessClusteringAnalyzer();
    290321      fitnessClusterAnalyzer.DatasetParameter.ActualName = datasetVariableName;
     322      Operators.Add(fitnessClusterAnalyzer);
    291323      #endregion
    292324
    293325      #region Create terminators
    294326
    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);
    297329      qualityTerminator = new SingleObjectiveQualityTerminator { Name = "Quality" };
     330      Operators.Add(qualityTerminator);
    298331      executionTimeTerminator = new ExecutionTimeTerminator(this, new TimeSpanValue(TimeSpan.FromMinutes(5)));
    299 
     332      Operators.Add(executionTimeTerminator);
    300333      #endregion
    301334
     
    303336      UpdateAnalyzers();
    304337      ParameterizeAnalyzers();
     338      ParameterizeStochasticOperators(Operators);
    305339      UpdateTerminators();
    306340      #endregion
     
    308342      Initialize();
    309343    }
    310     #endregion
    311344
    312345    private void Initialize() {
    313346      if (SingleObjectiveProblem != null) {
    314         SingleObjectiveProblem.Evaluator.QualityParameter.ActualNameChanged +=
    315          Evaluator_QualityParameter_ActualNameChanged;
     347        SingleObjectiveProblem.Evaluator.QualityParameter.ActualNameChanged += Evaluator_QualityParameter_ActualNameChanged;
    316348      }
    317 
    318       singleObjectiveQualityAnalyzer.CurrentBestQualityParameter.NameChanged +=
    319         QualityAnalyzer_CurrentBestQualityParameter_NameChanged;
     349      singleObjectiveQualityAnalyzer.CurrentBestQualityParameter.NameChanged += QualityAnalyzer_CurrentBestQualityParameter_NameChanged;
    320350      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();
    321371    }
    322372
     
    328378    }
    329379    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
    331385      singleObjectiveQualityAnalyzer.ResultsParameter.ActualName = "Results";
    332386      singleObjectiveQualityAnalyzer.ResultsParameter.Hidden = true;
    333387      singleObjectiveQualityAnalyzer.QualityParameter.Depth = 1;
    334       correlationAnalyzer.ResultsParameter.ActualName = "Results";
    335       correlationAnalyzer.ResultsParameter.Hidden = true;
    336388      correlationAnalyzer.QualityParameter.Depth = 1;
    337389      correlationAnalyzer.RealVectorParameter.Depth = 1;
    338       sampleAnalyzer.ResultsParameter.ActualName = "Results";
    339       sampleAnalyzer.ResultsParameter.Hidden = true;
    340390      sampleAnalyzer.QualityParameter.Depth = 1;
    341391      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 
    347392
    348393      if (SingleObjectiveProblem == null) return;
     
    362407      SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
    363408      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      }
    370416    }
    371417    private void ParameterizeProblemSpecificOperators() {
     
    374420      var rcreator = Problem.SolutionCreator as IRealVectorCreator;
    375421      if (rcreator == null) throw new ArgumentException("Adaptive sampling requires real vectors as solution candidates");
     422
    376423      infillSolver.LengthParameter.ActualName = rcreator.LengthParameter.ActualName;
    377424      infillSolver.BoundsParameter.ActualName = rcreator.BoundsParameter.ActualName;
    378       // if (rcreator.LengthParameter.Value != null)
    379425      infillSolver.LengthParameter.Value = rcreator.LengthParameter.Value;
    380       // if (rcreator.BoundsParameter.Value != null)
    381426      infillSolver.BoundsParameter.Value = rcreator.BoundsParameter.Value;
    382427      infillSolver.RealVectorParameter.ActualName = rcreator.RealVectorParameter.ActualName;
     
    388433      sampleAnalyzer.RealVectorParameter.ActualName = rcreator.RealVectorParameter.ActualName;
    389434    }
    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
    411447    private void RegisterParameterEvents() {
    412448      InfillOptimizationAlgorithmParameter.ValueChanged += OnInfillAlgorithmChanged;
     
    428464      InfillOptimizationAlgorithm.ProblemChanged += OnInfillProblemChanged;
    429465    }
     466
    430467    protected override void OnProblemChanged() {
    431468      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);
    446470      UpdateAnalyzers();
    447471      UpdateTerminators();
     472      ParameterizeSolutionsCreators();
     473      if (SingleObjectiveProblem != null)
     474        SingleObjectiveProblem.Evaluator.QualityParameter.ActualNameChanged += Evaluator_QualityParameter_ActualNameChanged;
    448475    }
    449476    protected override void RegisterProblemEvents() {
     
    460487    protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
    461488      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;
    466491      ParameterizeSolutionsCreators();
    467492      ParameterizeAnalyzers();
     
    469494    protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
    470495      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);
    476497      ParameterizeSolutionsCreators();
    477498    }
    478499    protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
    479500      base.Problem_OperatorsChanged(sender, e);
    480       //TODO ParameterizeIterationBasedOperators();
     501      UpdateAnalyzers();
    481502      UpdateTerminators();
    482503    }
    483504    private void MaximizationParameter_ValueChanged(object sender, EventArgs e) {
     505      ParameterizeAnalyzers();
    484506      ParameterizeTerminators();
    485507    }
     
    490512      ParameterizeTerminators();
    491513    }
     514
    492515    #endregion
    493516
     
    495518    private void UpdateAnalyzers() {
    496519      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>()))
    507522        Analyzer.Operators.Add(analyzer, analyzer.EnabledByDefault);
     523      ParameterizeAnalyzers();
    508524    }
    509525    private void UpdateTerminators() {
     
    519535        }
    520536      };
    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));
    525539        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));
    528541      }
    529 
    530542      Terminator.Operators.Clear();
    531543      foreach (var newTerminator in newTerminators)
    532544        Terminator.Operators.Add(newTerminator.Key, newTerminator.Value);
    533     }
    534     #endregion
    535 
     545      ParameterizeTerminators();
     546    }
     547    #endregion
    536548  }
    537549
Note: See TracChangeset for help on using the changeset viewer.