Free cookie consent management tool by TermsFeed Policy Generator

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

#2524: made BasicAlgorithm pausable

Location:
branches/PausableBasicAlgorithm/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3
Files:
2 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    }
Note: See TracChangeset for help on using the changeset viewer.