Free cookie consent management tool by TermsFeed Policy Generator

Changeset 15338


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

Location:
branches/EfficientGlobalOptimization/HeuristicLab.Algorithms.EGO
Files:
2 added
12 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
  • branches/EfficientGlobalOptimization/HeuristicLab.Algorithms.EGO/EfficientGlobalOptimizationAlgorithm.cs

    r15064 r15338  
    4545    public override bool SupportsPause => true;
    4646    public override Type ProblemType => typeof(SingleObjectiveBasicProblem<IEncoding>);
    47     public new SingleObjectiveBasicProblem<IEncoding> Problem
    48     {
     47    public new SingleObjectiveBasicProblem<IEncoding> Problem {
    4948      get { return (SingleObjectiveBasicProblem<IEncoding>)base.Problem; }
    5049      set { base.Problem = value; }
     
    130129
    131130    #region ResultsProperties
    132     private double ResultsBestQuality
    133     {
     131    private double ResultsBestQuality {
    134132      get { return ((DoubleValue)Results[BestQualityResultName].Value).Value; }
    135133      set { ((DoubleValue)Results[BestQualityResultName].Value).Value = value; }
    136134    }
    137     private RealVector ResultsBestSolution
    138     {
     135    private RealVector ResultsBestSolution {
    139136      get { return (RealVector)Results[BestSolutionResultName].Value; }
    140137      set { Results[BestSolutionResultName].Value = value; }
    141138    }
    142     private int ResultsEvaluations
    143     {
     139    private int ResultsEvaluations {
    144140      get { return ((IntValue)Results[EvaluatedSoultionsResultName].Value).Value; }
    145141      set { ((IntValue)Results[EvaluatedSoultionsResultName].Value).Value = value; }
    146142    }
    147     private int ResultsIterations
    148     {
     143    private int ResultsIterations {
    149144      get { return ((IntValue)Results[IterationsResultName].Value).Value; }
    150145      set { ((IntValue)Results[IterationsResultName].Value).Value = value; }
     
    154149    private DataRow ResultsQualitiesWorst => ResultsQualities.Rows[WorstQualitiesRowResultName];
    155150    private DataRow ResultsQualitiesIteration => ResultsQualities.Rows[CurrentQualitiesRowResultName];
    156     private IRegressionSolution ResultsModel
    157     {
     151    private IRegressionSolution ResultsModel {
    158152      get { return (IRegressionSolution)Results[RegressionSolutionResultName].Value; }
    159153      set { Results[RegressionSolutionResultName].Value = value; }
     
    252246            Samples.Add(Evaluate(t));
    253247            cancellationToken.ThrowIfCancellationRequested();
    254           }
    255           finally {
     248          } finally {
    256249            Analyze();
    257250          }
     
    271264          }
    272265
    273         }
    274         finally {
     266        } finally {
    275267          Analyze();
    276268        }
     
    348340
    349341      while (solution == null && i++ < 100) {
    350         var results = EgoUtilities.SyncRunSubAlgorithm(RegressionAlgorithm, Random.Next(int.MaxValue));
     342        var results = EgoUtilities.SyncRunSubAlgorithm(RegressionAlgorithm, Random.Next(int.MaxValue), cancellationToken);
    351343        solution = results.Select(x => x.Value).OfType<IRegressionSolution>().SingleOrDefault();
    352344        cancellationToken.ThrowIfCancellationRequested();
     
    371363            solution = sol;
    372364          }
    373         }
    374         catch (ArgumentException) { }
     365        } catch (ArgumentException) { }
    375366      }
    376367
     
    405396      for (var i = 0; i < InfillOptimizationRestarts; i++) {
    406397        //optimize
    407         var res = EgoUtilities.SyncRunSubAlgorithm(InfillOptimizationAlgorithm, Random.Next(int.MaxValue));
     398        var res = EgoUtilities.SyncRunSubAlgorithm(InfillOptimizationAlgorithm, Random.Next(int.MaxValue), cancellationToken);
    408399        cancellationToken.ThrowIfCancellationRequested();
    409400        //extract results
  • branches/EfficientGlobalOptimization/HeuristicLab.Algorithms.EGO/EgoUtilities.cs

    r15064 r15338  
    2424using System.Linq;
    2525using System.Threading;
    26 using HeuristicLab.Common;
    2726using HeuristicLab.Core;
    2827using HeuristicLab.Data;
     
    6463
    6564    //Sub-ALgorithms
    66     public static ResultCollection SyncRunSubAlgorithm(IAlgorithm alg, int random) {
    67 
     65    public static ResultCollection SyncRunSubAlgorithm(IAlgorithm alg, int random, CancellationToken cancellation) {
    6866      if (alg.Parameters.ContainsKey("SetSeedRandomly") && alg.Parameters.ContainsKey("Seed")) {
    6967        var setSeed = alg.Parameters["SetSeedRandomly"].ActualValue as BoolValue;
     
    7270        setSeed.Value = false;
    7371        seed.Value = random;
    74 
    7572      }
    76 
    77 
    78       EventWaitHandle trigger = new AutoResetEvent(false);
    79       Exception ex = null;
    80       EventHandler<EventArgs<Exception>> exhandler = (sender, e) => { ex = e.Value; trigger.Set(); };
    81       EventHandler stoppedHandler = (sender, e) => trigger.Set();
    82 
    83       alg.ExceptionOccurred -= exhandler; //avoid double attaching in case of pause
    84       alg.ExceptionOccurred += exhandler;
    85       alg.Stopped -= stoppedHandler;
    86       alg.Stopped += stoppedHandler;
    87       alg.Paused -= stoppedHandler;
    88       alg.Paused += stoppedHandler;
    89 
    9073      if (alg.ExecutionState != ExecutionState.Paused) alg.Prepare();
    91       alg.Start();
    92       trigger.WaitOne();
    93       alg.ExceptionOccurred -= exhandler;
    94       alg.Stopped -= stoppedHandler;
    95       if (ex != null) throw ex;
     74      alg.Start(cancellation);
    9675      return alg.Results;
    9776    }
    9877
    9978    //RegressionModel extensions
    100     public const double DuplicateResolution = 0.0001;
     79    private const double DuplicateResolution = 0.0001;
    10180    public static Dataset GetDataSet(IReadOnlyList<Tuple<RealVector, double>> samples, bool removeDuplicates) {
    10281      if (removeDuplicates) samples = RemoveDuplicates(samples); //TODO duplicate removal leads to incorrect uncertainty values in models
  • branches/EfficientGlobalOptimization/HeuristicLab.Algorithms.EGO/HeuristicLab.Algorithms.EGO-3.4.csproj

    r15064 r15338  
    118118    <Compile Include="InfillCriteria\InfillCriterionBase.cs" />
    119119    <Compile Include="InfillCriteria\ExpectedImprovement.cs" />
     120    <Compile Include="Interfaces\IResultsOperator.cs" />
     121    <Compile Include="Interfaces\ICancellableOperator.cs" />
    120122    <Compile Include="Interfaces\IInitialSampling.cs" />
    121123    <Compile Include="Interfaces\ISurrogateAlgorithm.cs" />
  • branches/EfficientGlobalOptimization/HeuristicLab.Algorithms.EGO/InfillCriteria/NeighbourDistance.cs

    r15064 r15338  
    3636  public class NeighbourDistance : InfillCriterionBase {
    3737
    38     private IVantagePointTree<IEnumerable<double>> Points;
     38    private VantagePointTree<IEnumerable<double>> Points;
    3939    #region Constructors, Serialization and Cloning
    4040    [StorableConstructor]
    4141    protected NeighbourDistance(bool deserializing) : base(deserializing) { }
    42 
    43     protected NeighbourDistance(NeighbourDistance original, Cloner cloner) : base(original, cloner) {
    44     }
     42    protected NeighbourDistance(NeighbourDistance original, Cloner cloner) : base(original, cloner) { }
    4543    public NeighbourDistance() { }
    46     public override IDeepCloneable Clone(Cloner cloner) {
    47       return new NeighbourDistance(this, cloner);
    48     }
     44    public override IDeepCloneable Clone(Cloner cloner) { return new NeighbourDistance(this, cloner); }
    4945    #endregion
    5046
     
    5551      Points.Search(vector, 1, out neighbours, out distances);
    5652      return distances[0];
    57       //return RegressionSolution.ProblemData.AllIndices.Min(i => DistanceToSample(vector, i));
    5853    }
    5954
    6055    public override void Initialize() {
    6156      Points = CreateTree();
    62     }
    63 
    64     private double DistanceToSample(RealVector vector, int i) {
    65       throw new NotImplementedException();
    6657    }
    6758
  • branches/EfficientGlobalOptimization/HeuristicLab.Algorithms.EGO/Operators/CorrelationAnalyzer.cs

    r15064 r15338  
    3636  [Item("CorrelationAnalyzer", "Analyzes the correlation between perdictions and actual fitness values")]
    3737  [StorableClass]
    38   public class CorrelationAnalyzer : SingleSuccessorOperator, IAnalyzer {
     38  public class CorrelationAnalyzer : SingleSuccessorOperator, IAnalyzer, IResultsOperator {
    3939    public override bool CanChangeName => true;
    4040    public bool EnabledByDefault => false;
  • branches/EfficientGlobalOptimization/HeuristicLab.Algorithms.EGO/Operators/EvaluatedSolutionsAnalyzer.cs

    r15064 r15338  
    3838  [Item("EvaluatedSolutionsAnalyzer", "Displays the evaluated Solutions for one or two dimensional problems")]
    3939  [StorableClass]
    40   public class EvaluatedSolutionsAnalyzer : SingleSuccessorOperator, IAnalyzer {
     40  public class EvaluatedSolutionsAnalyzer : SingleSuccessorOperator, IAnalyzer, IResultsOperator {
    4141    public override bool CanChangeName => true;
    4242    public bool EnabledByDefault => false;
  • branches/EfficientGlobalOptimization/HeuristicLab.Algorithms.EGO/Operators/FitnessClusteringAnalyzer.cs

    r15064 r15338  
    2222using System;
    2323using System.Linq;
     24using System.Threading;
    2425using HeuristicLab.Algorithms.DataAnalysis;
    2526using HeuristicLab.Analysis;
     
    3233using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    3334using HeuristicLab.Problems.DataAnalysis;
    34 using HeuristicLab.Problems.SurrogateProblem;
    3535
    3636namespace HeuristicLab.Algorithms.EGO {
    3737  [Item("FitnessClusteringAnalyzer", "Analyzes the correlation between perdictions and actual fitness values")]
    3838  [StorableClass]
    39   public class FitnessClusteringAnalyzer : SingleSuccessorOperator, IAnalyzer, IStochasticOperator {
     39  public class FitnessClusteringAnalyzer : SingleSuccessorOperator, IAnalyzer, IStochasticOperator, IResultsOperator {
    4040    public override bool CanChangeName => true;
    4141    public bool EnabledByDefault => false;
     
    9595        Problem = prob
    9696      };
    97       tsne.ClassesParameter.Value.Value = "cluster";
    98       var res = EgoUtilities.SyncRunSubAlgorithm(tsne, random.Next());
     97      tsne.ClassesNameParameter.Value = tsne.ClassesNameParameter.ValidValues.FirstOrDefault(x => x.Value.Equals("cluster"));
     98      var res = EgoUtilities.SyncRunSubAlgorithm(tsne, random.Next(), CancellationToken.None);
    9999      return res.Select(r => r.Value).OfType<ScatterPlot>().First();
    100100    }
     
    106106      pd.TrainingPartition.Start = 0;
    107107      pd.TrainingPartition.End = dataset.Rows;
    108 
    109       return ObservationWeightedKMeans.CreateKMeansSolution(pd, GetWeights(dataset), KParameter.Value.Value, 1000, random);
     108      return KMeansClustering.CreateKMeansSolution(pd, KParameter.Value.Value, 1);
    110109    }
    111110    private double[] GetWeights(ModifiableDataset dataset) {
  • branches/EfficientGlobalOptimization/HeuristicLab.Algorithms.EGO/Operators/InfillSolver.cs

    r15064 r15338  
    2121
    2222using System;
     23using System.Threading;
    2324using HeuristicLab.Common;
    2425using HeuristicLab.Core;
     
    3637  [Item("InfillSolver", "A RealVectorCreator that creates candidates by optimizing an infill-subproblem")]
    3738  [StorableClass]
    38   public class InfillSolver : RealVectorCreator {
     39  public class InfillSolver : RealVectorCreator, ICancellableOperator {
    3940
    4041    public ILookupParameter<IAlgorithm> InfillOptimizationAlgorithmParamter => (ILookupParameter<IAlgorithm>)Parameters["InfillAlgorithm"];
     
    4445    public IFixedValueParameter<DoubleValue> DuplicateCutoffParameter => (IFixedValueParameter<DoubleValue>)Parameters["Duplicates Cutoff"];
    4546    public ILookupParameter<DoubleMatrix> InfillBoundsParameter => (ILookupParameter<DoubleMatrix>)Parameters["InfillBounds"];
     47
     48    public CancellationToken Cancellation { get; set; }
    4649
    4750    [StorableConstructor]
     
    7780    }
    7881
    79     public static RealVector OptimizeInfillProblem(IAlgorithm algorithm, IRegressionSolution model, bool maximization, DoubleMatrix bounds, int length, IRandom random) {
     82    private RealVector OptimizeInfillProblem(IAlgorithm algorithm, IRegressionSolution model, bool maximization, DoubleMatrix bounds, int length, IRandom random) {
    8083      var infillProblem = algorithm.Problem as InfillProblem;
    8184      if (infillProblem == null) throw new ArgumentException("The algortihm has no InfillProblem to solve");
     
    8386      infillProblem.Encoding.Bounds = bounds;
    8487      infillProblem.Initialize(model, maximization);
    85       var res = EgoUtilities.SyncRunSubAlgorithm(algorithm, random.Next(int.MaxValue));
     88      var res = EgoUtilities.SyncRunSubAlgorithm(algorithm, random.Next(int.MaxValue), Cancellation);
    8689      var v = res[InfillProblem.BestInfillSolutionResultName].Value as RealVector;
    8790      algorithm.Runs.Clear();
     
    104107
    105108
     109
    106110  }
    107111}
  • branches/EfficientGlobalOptimization/HeuristicLab.Algorithms.EGO/Operators/ModelBuilder.cs

    r15064 r15338  
    2222using System;
    2323using System.Linq;
     24using System.Threading;
    2425using HeuristicLab.Algorithms.DataAnalysis;
    2526using HeuristicLab.Common;
     
    3132using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    3233using HeuristicLab.Problems.DataAnalysis;
     34using HeuristicLab.Problems.SurrogateProblem;
    3335
    3436namespace HeuristicLab.Algorithms.EGO {
     
    3840  [Item("ModelBuilder", "Builds a model from a dataset and a given RegressionAlgorithm")]
    3941  [StorableClass]
    40   public class ModelBuilder : InstrumentedOperator, IStochasticOperator {
     42  public class ModelBuilder : InstrumentedOperator, IStochasticOperator, ICancellableOperator {
    4143    public override bool CanChangeName => true;
     44    public CancellationToken Cancellation { get; set; }
    4245
     46    #region Parameter properties
    4347    public ILookupParameter<IDataAnalysisAlgorithm<IRegressionProblem>> RegressionAlgorithmParameter => (ILookupParameter<IDataAnalysisAlgorithm<IRegressionProblem>>)Parameters["RegressionAlgorithm"];
    4448    public ILookupParameter<IRegressionSolution> ModelParameter => (ILookupParameter<IRegressionSolution>)Parameters["Model"];
     
    4751    public ILookupParameter<IntValue> MaxModelSizeParameter => (ILookupParameter<IntValue>)Parameters["Maximal Model Size"];
    4852    public ILookupParameter<DoubleMatrix> InfillBoundsParameter => (ILookupParameter<DoubleMatrix>)Parameters["InfillBounds"];
     53    #endregion
    4954
    5055    [StorableConstructor]
     
    9398    }
    9499
    95     private static IRegressionSolution BuildModel(IRandom random, IDataAnalysisAlgorithm<IRegressionProblem> regressionAlgorithm, IDataset dataset, IRegressionSolution oldSolution) {
     100    private IRegressionSolution BuildModel(IRandom random, IDataAnalysisAlgorithm<IRegressionProblem> regressionAlgorithm, IDataset dataset, IRegressionSolution oldSolution) {
    96101      //var dataset = EgoUtilities.GetDataSet(dataSamples, RemoveDuplicates);
    97102      var problemdata = new RegressionProblemData(dataset, dataset.VariableNames.Where(x => !x.Equals("output")), "output");
     
    108113
    109114      while (solution == null && i++ < 100) {
    110         var results = EgoUtilities.SyncRunSubAlgorithm(regressionAlgorithm, random.Next(int.MaxValue));
     115        var results = EgoUtilities.SyncRunSubAlgorithm(regressionAlgorithm, random.Next(int.MaxValue), Cancellation);
    111116        solution = results.Select(x => x.Value).OfType<IRegressionSolution>().SingleOrDefault();
    112117      }
    113118
    114       //try creating a model with old hyperparameters and new dataset;
    115       var gp = regressionAlgorithm as GaussianProcessRegression;
    116       var oldmodel = oldSolution as GaussianProcessRegressionSolution;
    117       if (gp != null && oldmodel != null) {
    118         var mean = (IMeanFunction)oldmodel.Model.MeanFunction.Clone();
    119         var cov = (ICovarianceFunction)oldmodel.Model.CovarianceFunction.Clone();
    120         try {
    121           var model = new GaussianProcessModel(problemdata.Dataset, problemdata.TargetVariable,
    122             problemdata.AllowedInputVariables, problemdata.TrainingIndices, new[] { 0.0 }, mean, cov);
    123           model.FixParameters();
    124           var sol = new GaussianProcessRegressionSolution(model, problemdata);
    125           if (solution == null || solution.TrainingMeanSquaredError > sol.TrainingMeanSquaredError) {
    126             solution = sol;
    127           }
    128         }
    129         catch (ArgumentException) { }
    130       }
     119      if (regressionAlgorithm is GaussianProcessRegression && oldSolution != null)
     120        solution = SanitizeGaussianProcess(oldSolution as GaussianProcessRegressionSolution, solution as GaussianProcessRegressionSolution, Cancellation);
     121
     122      if (regressionAlgorithm is M5RegressionTree && oldSolution != null)
     123        solution = SanitizeM5Regression(oldSolution.Model as M5Model, solution, random, Cancellation);
     124
    131125
    132126      regressionAlgorithm.Runs.Clear();
     
    134128
    135129    }
     130
     131    private static IRegressionSolution SanitizeM5Regression(M5Model oldmodel, IRegressionSolution newSolution, IRandom random, CancellationToken cancellation) {
     132      var problemdata = newSolution.ProblemData;
     133      oldmodel.UpdateLeafModels(problemdata, problemdata.AllIndices, random, cancellation);
     134      var oldSolution = oldmodel.CreateRegressionSolution(problemdata);
     135      var magicDecision = newSolution.TrainingRSquared < oldSolution.TrainingRSquared - 0.05;
     136      return magicDecision ? newSolution : oldmodel.CreateRegressionSolution(problemdata);
     137    }
     138
     139    //try creating a model with old hyperparameters and new dataset;
     140    private static IRegressionSolution SanitizeGaussianProcess(GaussianProcessRegressionSolution oldmodel, GaussianProcessRegressionSolution newSolution, CancellationToken cancellation) {
     141      var problemdata = newSolution.ProblemData;
     142      var mean = (IMeanFunction)oldmodel.Model.MeanFunction.Clone();
     143      var cov = (ICovarianceFunction)oldmodel.Model.CovarianceFunction.Clone();
     144      try {
     145        var model = new GaussianProcessModel(problemdata.Dataset, problemdata.TargetVariable, problemdata.AllowedInputVariables, problemdata.TrainingIndices, new[] { 0.0 }, mean, cov);
     146        cancellation.ThrowIfCancellationRequested();
     147        model.FixParameters();
     148        var sol = new GaussianProcessRegressionSolution(model, problemdata);
     149        if (newSolution.TrainingMeanSquaredError > sol.TrainingMeanSquaredError) {
     150          newSolution = sol;
     151        }
     152      } catch (ArgumentException) { }
     153      return newSolution;
     154    }
     155
    136156  }
    137157}
  • branches/EfficientGlobalOptimization/HeuristicLab.Algorithms.EGO/Operators/ModelQualityAnalyzer.cs

    r15064 r15338  
    3535  [Item("ModelQualityAnalyzer", "Collects RealVectors into a modifiablbe dataset")]
    3636  [StorableClass]
    37   public class ModelQualityAnalyzer : SingleSuccessorOperator, IAnalyzer {
     37  public class ModelQualityAnalyzer : SingleSuccessorOperator, IAnalyzer, IResultsOperator {
    3838    public override bool CanChangeName => true;
    3939    public bool EnabledByDefault => false;
  • branches/EfficientGlobalOptimization/HeuristicLab.Algorithms.EGO/Operators/VariableVariabilityAnalyzer.cs

    r15064 r15338  
    3535  [Item("VariableVariabilityAnalyzer", "Analyzes the correlation between perdictions and actual fitness values")]
    3636  [StorableClass]
    37   public class VariableVariabilityAnalyzer : SingleSuccessorOperator, IAnalyzer {
     37  public class VariableVariabilityAnalyzer : SingleSuccessorOperator, IAnalyzer, IResultsOperator {
    3838    public override bool CanChangeName => true;
    3939    public bool EnabledByDefault => false;
Note: See TracChangeset for help on using the changeset viewer.