Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17594 was 17594, checked in by mkommend, 4 years ago

#2521: Added first version of new results. The first algorithm that has been adapted for testing purposes is the hill climber.

File size: 5.5 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;
33
34using HeuristicLab.Parameters;
35using HeuristicLab.Random;
36
37
38namespace HeuristicLab.Algorithms.ParameterlessPopulationPyramid {
39  // This code is based off the publication
40  // B. W. Goldman and W. F. Punch, "Parameter-less Population Pyramid," GECCO, pp. 785–792, 2014
41  // and the original source code in C++11 available from: https://github.com/brianwgoldman/Parameter-less_Population_Pyramid
42  [Item("Hill Climber (HC)", "Binary Hill Climber.")]
43  [StorableType("BA349010-6295-406E-8989-B271FB96ED86")]
44  [Creatable(CreatableAttribute.Categories.SingleSolutionAlgorithms, Priority = 150)]
45  public class HillClimber : BasicAlgorithm {
46    [Storable]
47    private IRandom random;
48
49    [Storable] public IFixedValueParameter<IntValue> MaximumIterationsParameter { get; private set; }
50
51    [Storable] public IResult<DoubleValue> BestQualityResult { get; private set; }
52    [Storable] public IResult<IntValue> IterationsResult { get; private set; }
53
54    public override Type ProblemType {
55      get { return typeof(ISingleObjectiveProblemDefinition<BinaryVectorEncoding, BinaryVector>); }
56    }
57    public new ISingleObjectiveProblemDefinition<BinaryVectorEncoding, BinaryVector> Problem {
58      get { return (ISingleObjectiveProblemDefinition<BinaryVectorEncoding, BinaryVector>)base.Problem; }
59      set { base.Problem = (IProblem)value; }
60    }
61
62    public override bool SupportsPause { get { return false; } }
63
64    public int MaximumIterations {
65      get { return MaximumIterationsParameter.Value.Value; }
66      set { MaximumIterationsParameter.Value.Value = value; }
67    }
68
69    [StorableConstructor]
70    protected HillClimber(StorableConstructorFlag _) : base(_) { }
71    protected HillClimber(HillClimber original, Cloner cloner)
72      : base(original, cloner) {
73      MaximumIterationsParameter = cloner.Clone(original.MaximumIterationsParameter);
74      BestQualityResult = cloner.Clone(original.BestQualityResult);
75      IterationsResult = cloner.Clone(original.IterationsResult);
76    }
77    public override IDeepCloneable Clone(Cloner cloner) {
78      return new HillClimber(this, cloner);
79    }
80
81    public HillClimber()
82      : base() {
83      random = new MersenneTwister();
84      Parameters.Add(MaximumIterationsParameter = new FixedValueParameter<IntValue>("Maximum Iterations", "", new IntValue(100)));
85
86      Results.Add(BestQualityResult = new Result<DoubleValue>("Best Quality"));
87      Results.Add(IterationsResult = new Result<IntValue>("Iterations"));
88    }
89
90
91
92    protected override void Run(CancellationToken cancellationToken) {
93      IterationsResult.Value = new IntValue();
94      BestQualityResult.Value = new DoubleValue(double.NaN);
95
96      while (IterationsResult.Value.Value < MaximumIterations) {
97        cancellationToken.ThrowIfCancellationRequested();
98
99        var solution = new BinaryVector(Problem.Encoding.Length);
100        for (int i = 0; i < solution.Length; i++) {
101          solution[i] = random.Next(2) == 1;
102        }
103
104        var evaluationResult = Problem.Evaluate(solution, random);
105        var fitness = evaluationResult.Quality;
106
107        fitness = ImproveToLocalOptimum(Problem, solution, fitness, random);
108        var bestSoFar = BestQualityResult.Value.Value;
109        if (double.IsNaN(bestSoFar) || Problem.IsBetter(fitness, bestSoFar)) {
110          BestQualityResult.Value.Value = fitness;
111        }
112
113        IterationsResult.Value.Value++;
114      }
115    }
116    // In the GECCO paper, Section 2.1
117    public static double ImproveToLocalOptimum(ISingleObjectiveProblemDefinition<BinaryVectorEncoding, BinaryVector> problem, BinaryVector solution, double fitness, IRandom rand) {
118      var tried = new HashSet<int>();
119      do {
120        var options = Enumerable.Range(0, solution.Length).Shuffle(rand);
121        foreach (var option in options) {
122          if (tried.Contains(option)) continue;
123          solution[option] = !solution[option];
124          var newEvaluationResult = problem.Evaluate(solution, rand);
125          double newFitness = newEvaluationResult.Quality;
126          if (problem.IsBetter(newFitness, fitness)) {
127            fitness = newFitness;
128            tried.Clear();
129          } else {
130            solution[option] = !solution[option];
131          }
132          tried.Add(option);
133        }
134      } while (tried.Count != solution.Length);
135      return fitness;
136    }
137  }
138}
Note: See TracBrowser for help on using the repository browser.