Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
12/22/16 10:07:46 (8 years ago)
Author:
jkarder
Message:

#2524: made BasicAlgorithm pausable

Location:
trunk/sources/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/HillClimber.cs

    r14185 r14517  
    4848
    4949    private const string IterationsParameterName = "Iterations";
     50    private const string BestQualityResultName = "Best quality";
     51    private const string IterationsResultName = "Iterations";
    5052
    5153    public override Type ProblemType {
     
    6668    }
    6769
     70    #region ResultsProperties
     71    private double ResultsBestQuality {
     72      get { return ((DoubleValue)Results[BestQualityResultName].Value).Value; }
     73      set { ((DoubleValue)Results[BestQualityResultName].Value).Value = value; }
     74    }
     75    private int ResultsIterations {
     76      get { return ((IntValue)Results[IterationsResultName].Value).Value; }
     77      set { ((IntValue)Results[IterationsResultName].Value).Value = value; }
     78    }
     79    #endregion
     80
    6881    [StorableConstructor]
    6982    protected HillClimber(bool deserializing) : base(deserializing) { }
     
    7790    public HillClimber()
    7891      : base() {
     92      pausable = true;
    7993      random = new MersenneTwister();
    8094      Parameters.Add(new FixedValueParameter<IntValue>(IterationsParameterName, "", new IntValue(100)));
    8195    }
     96
     97    [StorableHook(HookType.AfterDeserialization)]
     98    private void AfterDeserialization() {
     99      // BackwardsCompatibility3.3
     100      #region Backwards compatible code, remove with 3.4
     101      pausable = true;
     102      #endregion
     103    }
     104
     105    protected override void Initialize(CancellationToken cancellationToken) {
     106      Results.Add(new Result(BestQualityResultName, new DoubleValue(double.NaN)));
     107      Results.Add(new Result(IterationsResultName, new IntValue(0)));
     108      base.Initialize(cancellationToken);
     109    }
    82110    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++) {
     111      while (ResultsIterations < Iterations) {
     112        cancellationToken.ThrowIfCancellationRequested();
     113
    86114        var solution = new BinaryVector(Problem.Length);
    87115        for (int i = 0; i < solution.Length; i++) {
     
    92120
    93121        fitness = ImproveToLocalOptimum(Problem, solution, fitness, random);
    94         if (double.IsNaN(BestQuality.Value) || Problem.IsBetter(fitness, BestQuality.Value)) {
    95           BestQuality.Value = fitness;
     122        if (double.IsNaN(ResultsBestQuality) || Problem.IsBetter(fitness, ResultsBestQuality)) {
     123          ResultsBestQuality = fitness;
    96124        }
     125
     126        ResultsIterations++;
    97127      }
    98128    }
  • trunk/sources/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/ParameterlessPopulationPyramid.cs

    r14185 r14517  
    165165
    166166    public ParameterlessPopulationPyramid() {
     167      pausable = true;
    167168      Parameters.Add(new FixedValueParameter<IntValue>(MaximumIterationsParameterName, "", new IntValue(Int32.MaxValue)));
    168169      Parameters.Add(new FixedValueParameter<IntValue>(MaximumEvaluationsParameterName, "", new IntValue(Int32.MaxValue)));
     
    170171      Parameters.Add(new FixedValueParameter<IntValue>(SeedParameterName, "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
    171172      Parameters.Add(new FixedValueParameter<BoolValue>(SetSeedRandomlyParameterName, "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
     173    }
     174
     175    [StorableHook(HookType.AfterDeserialization)]
     176    private void AfterDeserialization() {
     177      // BackwardsCompatibility3.3
     178      #region Backwards compatible code, remove with 3.4
     179      pausable = true;
     180      #endregion
    172181    }
    173182
     
    213222    }
    214223
    215     protected override void Run(CancellationToken cancellationToken) {
     224    protected override void Initialize(CancellationToken cancellationToken) {
    216225      // Set up the algorithm
    217226      if (SetSeedRandomly) Seed = new System.Random().Next();
     
    242251      Results.Add(new Result("Stored Solutions", table));
    243252
     253      base.Initialize(cancellationToken);
     254    }
     255
     256    protected override void Run(CancellationToken cancellationToken) {
    244257      // 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         }
     258      while (ResultsIterations < MaximumIterations) {
     259        cancellationToken.ThrowIfCancellationRequested();
     260        double fitness = iterate();
     261
     262        ResultsEvaluations = tracker.Evaluations;
     263        ResultsBestSolution = new BinaryVector(tracker.BestSolution);
     264        ResultsBestQuality = tracker.BestQuality;
     265        ResultsBestFoundOnEvaluation = tracker.BestFoundOnEvaluation;
     266        ResultsQualitiesBest.Values.Add(tracker.BestQuality);
     267        ResultsQualitiesIteration.Values.Add(fitness);
     268        ResultsLevels.Values.Add(pyramid.Count);
     269        ResultsSolutions.Values.Add(seen.Count);
     270
     271        ResultsIterations++;
    261272      }
    262273    }
Note: See TracChangeset for help on using the changeset viewer.