Free cookie consent management tool by TermsFeed Policy Generator

Changeset 13378


Ignore:
Timestamp:
11/24/15 17:46:18 (9 years ago)
Author:
jkarder
Message:

#2524: made BasicAlgorithm pausable

Location:
branches/PausableBasicAlgorithm
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/PausableBasicAlgorithm/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/HillClimber.cs

    r13173 r13378  
    4848
    4949    private const string IterationsParameterName = "Iterations";
     50    private const string BestQualityResultName = "Best quality";
     51    private const string IterationsResultName = "Iterations";
     52
     53    public override bool Pausable { get { return true; } }
    5054
    5155    public override Type ProblemType {
     
    6670    }
    6771
     72    #region ResultsProperties
     73    private double ResultsBestQuality {
     74      get { return ((DoubleValue)Results[BestQualityResultName].Value).Value; }
     75      set { ((DoubleValue)Results[BestQualityResultName].Value).Value = value; }
     76    }
     77    private int ResultsIterations {
     78      get { return ((IntValue)Results[IterationsResultName].Value).Value; }
     79      set { ((IntValue)Results[IterationsResultName].Value).Value = value; }
     80    }
     81    #endregion
     82
    6883    [StorableConstructor]
    6984    protected HillClimber(bool deserializing) : base(deserializing) { }
     
    8095      Parameters.Add(new FixedValueParameter<IntValue>(IterationsParameterName, "", new IntValue(100)));
    8196    }
     97    protected override void Initialize(CancellationToken cancellationToken) {
     98      Results.Add(new Result(BestQualityResultName, new DoubleValue(double.NaN)));
     99      Results.Add(new Result(IterationsResultName, new IntValue(0)));
     100      base.Initialize(cancellationToken);
     101    }
    82102    protected override void Run(CancellationToken cancellationToken) {
    83       var BestQuality = new DoubleValue(double.NaN);
    84       Results.Add(new Result("Best quality", BestQuality));
    85       for (int iteration = 0; iteration < Iterations; iteration++) {
     103      while (ResultsIterations < Iterations) {
     104        cancellationToken.ThrowIfCancellationRequested();
     105
    86106        var solution = new BinaryVector(Problem.Length);
    87107        for (int i = 0; i < solution.Length; i++) {
     
    92112
    93113        fitness = ImproveToLocalOptimum(Problem, solution, fitness, random);
    94         if (double.IsNaN(BestQuality.Value) || Problem.IsBetter(fitness, BestQuality.Value)) {
    95           BestQuality.Value = fitness;
     114        if (double.IsNaN(ResultsBestQuality) || Problem.IsBetter(fitness, ResultsBestQuality)) {
     115          ResultsBestQuality = fitness;
    96116        }
     117
     118        ResultsIterations++;
    97119      }
    98120    }
  • branches/PausableBasicAlgorithm/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/ParameterlessPopulationPyramid.cs

    r13173 r13378  
    213213    }
    214214
    215     protected override void Run(CancellationToken cancellationToken) {
     215    protected override void Initialize(CancellationToken cancellationToken) {
    216216      // Set up the algorithm
    217217      if (SetSeedRandomly) Seed = new System.Random().Next();
     
    242242      Results.Add(new Result("Stored Solutions", table));
    243243
     244      base.Initialize(cancellationToken);
     245    }
     246
     247    protected override void Run(CancellationToken cancellationToken) {
    244248      // Loop until iteration limit reached or canceled.
    245       for (ResultsIterations = 0; ResultsIterations < MaximumIterations; ResultsIterations++) {
    246         double fitness = double.NaN;
    247 
    248         try {
    249           fitness = iterate();
    250           cancellationToken.ThrowIfCancellationRequested();
    251         } finally {
    252           ResultsEvaluations = tracker.Evaluations;
    253           ResultsBestSolution = new BinaryVector(tracker.BestSolution);
    254           ResultsBestQuality = tracker.BestQuality;
    255           ResultsBestFoundOnEvaluation = tracker.BestFoundOnEvaluation;
    256           ResultsQualitiesBest.Values.Add(tracker.BestQuality);
    257           ResultsQualitiesIteration.Values.Add(fitness);
    258           ResultsLevels.Values.Add(pyramid.Count);
    259           ResultsSolutions.Values.Add(seen.Count);
    260         }
     249      while (ResultsIterations < MaximumIterations) {
     250        cancellationToken.ThrowIfCancellationRequested();
     251        double fitness = iterate();
     252
     253        ResultsEvaluations = tracker.Evaluations;
     254        ResultsBestSolution = new BinaryVector(tracker.BestSolution);
     255        ResultsBestQuality = tracker.BestQuality;
     256        ResultsBestFoundOnEvaluation = tracker.BestFoundOnEvaluation;
     257        ResultsQualitiesBest.Values.Add(tracker.BestQuality);
     258        ResultsQualitiesIteration.Values.Add(fitness);
     259        ResultsLevels.Values.Add(pyramid.Count);
     260        ResultsSolutions.Values.Add(seen.Count);
     261
     262        ResultsIterations++;
    261263      }
    262264    }
  • branches/PausableBasicAlgorithm/HeuristicLab.Optimization.Views/3.3/BasicAlgorithmView.cs

    r11790 r13378  
    3434    }
    3535
    36     protected override void SetEnabledStateOfControls() {
    37       base.SetEnabledStateOfControls();
    38       pauseButton.Enabled = false;
    39     }
    40 
    4136    protected override void SetEnabledStateOfExecutableButtons() {
    4237      base.SetEnabledStateOfExecutableButtons();
    43       pauseButton.Enabled = false;
     38      if (Content != null)
     39        pauseButton.Enabled &= Content.Pausable;
    4440    }
    4541  }
  • branches/PausableBasicAlgorithm/HeuristicLab.Optimization/3.3/Algorithms/BasicAlgorithm.cs

    r11878 r13378  
    2424using System.Threading.Tasks;
    2525using HeuristicLab.Common;
     26using HeuristicLab.Core;
    2627using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2728
     
    3031  public abstract class BasicAlgorithm : Algorithm, IStorableContent {
    3132    public string Filename { get; set; }
     33    public virtual bool Pausable { get { return false; } }
    3234
    3335    [Storable]
     
    5860      base.Prepare();
    5961      results.Clear();
     62      initialized = false;
    6063      OnPrepared();
    6164    }
     
    6467      base.Start();
    6568      CancellationTokenSource = new CancellationTokenSource();
    66 
     69      pausePending = false;
    6770      OnStarted();
    6871      Task task = Task.Factory.StartNew(Run, cancellationTokenSource.Token, cancellationTokenSource.Token);
     
    8083        CancellationTokenSource.Dispose();
    8184        CancellationTokenSource = null;
    82         OnStopped();
     85        if (pausePending) OnPaused();
     86        else OnStopped();
    8387      });
    8488    }
    8589
     90    private bool pausePending;
    8691    public override void Pause() {
    87       throw new NotSupportedException("Pause is not supported in basic algorithms.");
     92      if (!Pausable)
     93        throw new NotSupportedException("Pause is not supported by this algorithm.");
     94
     95      base.Pause();
     96      pausePending = Pausable;
     97      cancellationTokenSource.Cancel();
    8898    }
    8999
     
    92102      // alternatively check the IsCancellationRequested property of the cancellation token
    93103      base.Stop();
    94       CancellationTokenSource.Cancel();
     104      if (ExecutionState == ExecutionState.Paused) OnStopped();
     105      else CancellationTokenSource.Cancel();
    95106    }
    96107
     
    105116      timer.Start();
    106117      try {
     118        if (!initialized)
     119          Initialize(cancellationToken);
     120        initialized = true;
    107121        Run(cancellationToken);
    108122      } finally {
     
    113127    }
    114128
     129    private bool initialized;
     130    protected virtual void Initialize(CancellationToken cancellationToken) { }
    115131    protected abstract void Run(CancellationToken cancellationToken);
    116132
Note: See TracChangeset for help on using the changeset viewer.