Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/HillClimber.cs @ 17517

Last change on this file since 17517 was 17517, checked in by abeham, 4 years ago

#2521: Worked on ResultParameter for Problem and Algorithms

  • Add ResultParameter to TSP, BinaryVectorProblem, and HillClimber
  • Refactor ResultParameter to allow presetting the ResultCollection instead of having to discover it (e.g. for use in BasicAlgorithms)
  • Unify Results property among EngineAlgorithm and BasicAlgorithm
    • There is now only a single instance which is storable
File size: 5.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 * and the BEACON Center for the Study of Evolution in Action.
5 *
6 * This file is part of HeuristicLab.
7 *
8 * HeuristicLab is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * HeuristicLab is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
20 */
21#endregion
22
23using System;
24using System.Collections.Generic;
25using System.Linq;
26using System.Threading;
27using HEAL.Attic;
28using HeuristicLab.Common;
29using HeuristicLab.Core;
30using HeuristicLab.Data;
31using HeuristicLab.Encodings.BinaryVectorEncoding;
32using HeuristicLab.Optimization;
33using HeuristicLab.Parameters;
34using HeuristicLab.Random;
35
36
37namespace HeuristicLab.Algorithms.ParameterlessPopulationPyramid {
38  // This code is based off the publication
39  // B. W. Goldman and W. F. Punch, "Parameter-less Population Pyramid," GECCO, pp. 785–792, 2014
40  // and the original source code in C++11 available from: https://github.com/brianwgoldman/Parameter-less_Population_Pyramid
41  [Item("Hill Climber (HC)", "Binary Hill Climber.")]
42  [StorableType("BA349010-6295-406E-8989-B271FB96ED86")]
43  [Creatable(CreatableAttribute.Categories.SingleSolutionAlgorithms, Priority = 150)]
44  public class HillClimber : BasicAlgorithm {
45    [Storable]
46    private IRandom random;
47
48    [Storable] public IFixedValueParameter<IntValue> MaximumIterationsParameter { get; private set; }
49    [Storable] public IResultParameter<DoubleValue> BestQualityResultParameter { get; private set; }
50    [Storable] public IResultParameter<IntValue> IterationsResultParameter { get; private set; }
51
52    public override Type ProblemType {
53      get { return typeof(ISingleObjectiveProblemDefinition<BinaryVectorEncoding, BinaryVector>); }
54    }
55    public new ISingleObjectiveProblemDefinition<BinaryVectorEncoding, BinaryVector> Problem {
56      get { return (ISingleObjectiveProblemDefinition<BinaryVectorEncoding, BinaryVector>)base.Problem; }
57      set { base.Problem = (IProblem)value; }
58    }
59
60    public override bool SupportsPause { get { return false; } }
61
62    public int MaximumIterations {
63      get { return MaximumIterationsParameter.Value.Value; }
64      set { MaximumIterationsParameter.Value.Value = value; }
65    }
66
67    [StorableConstructor]
68    protected HillClimber(StorableConstructorFlag _) : base(_) { }
69    protected HillClimber(HillClimber original, Cloner cloner)
70      : base(original, cloner) {
71      MaximumIterationsParameter = cloner.Clone(original.MaximumIterationsParameter);
72      BestQualityResultParameter = cloner.Clone(original.BestQualityResultParameter);
73      IterationsResultParameter = cloner.Clone(original.IterationsResultParameter);
74    }
75    public override IDeepCloneable Clone(Cloner cloner) {
76      return new HillClimber(this, cloner);
77    }
78
79    public HillClimber()
80      : base() {
81      random = new MersenneTwister();
82      Parameters.Add(MaximumIterationsParameter = new FixedValueParameter<IntValue>("Maximum Iterations", "", new IntValue(100)));
83      Parameters.Add(BestQualityResultParameter = new ResultParameter<DoubleValue>("Best Quality", "", "Results", new DoubleValue(double.NaN)));
84      Parameters.Add(IterationsResultParameter = new ResultParameter<IntValue>("Iterations", "", "Results", new IntValue(0)));
85    }
86
87    protected override void Run(CancellationToken cancellationToken) {
88      while (IterationsResultParameter.ActualValue.Value < MaximumIterations) {
89        cancellationToken.ThrowIfCancellationRequested();
90
91        var solution = new BinaryVector(Problem.Encoding.Length);
92        for (int i = 0; i < solution.Length; i++) {
93          solution[i] = random.Next(2) == 1;
94        }
95
96        var evaluationResult = Problem.Evaluate(solution, random);
97        var fitness = evaluationResult.Quality;
98
99        fitness = ImproveToLocalOptimum(Problem, solution, fitness, random);
100        var bestSoFar = BestQualityResultParameter.ActualValue.Value;
101        if (double.IsNaN(bestSoFar) || Problem.IsBetter(fitness, bestSoFar)) {
102          BestQualityResultParameter.ActualValue.Value = fitness;
103        }
104
105        IterationsResultParameter.ActualValue.Value++;
106      }
107    }
108    // In the GECCO paper, Section 2.1
109    public static double ImproveToLocalOptimum(ISingleObjectiveProblemDefinition<BinaryVectorEncoding, BinaryVector> problem, BinaryVector solution, double fitness, IRandom rand) {
110      var tried = new HashSet<int>();
111      do {
112        var options = Enumerable.Range(0, solution.Length).Shuffle(rand);
113        foreach (var option in options) {
114          if (tried.Contains(option)) continue;
115          solution[option] = !solution[option];
116          var newEvaluationResult = problem.Evaluate(solution, rand);
117          double newFitness = newEvaluationResult.Quality;
118          if (problem.IsBetter(newFitness, fitness)) {
119            fitness = newFitness;
120            tried.Clear();
121          } else {
122            solution[option] = !solution[option];
123          }
124          tried.Add(option);
125        }
126      } while (tried.Count != solution.Length);
127      return fitness;
128    }
129  }
130}
Note: See TracBrowser for help on using the repository browser.