source: branches/ProblemRefactoring/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/HillClimber.cs @ 13361

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

#2521: Adapted real vector encoding, test function problems, P3, CMA-ES and optimization.

File size: 4.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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 HeuristicLab.Common;
28using HeuristicLab.Core;
29using HeuristicLab.Data;
30using HeuristicLab.Encodings.BinaryVectorEncoding;
31using HeuristicLab.Optimization;
32using HeuristicLab.Parameters;
33using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
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  [StorableClass]
43  [Creatable(CreatableAttribute.Categories.SingleSolutionAlgorithms, Priority = 150)]
44  public class HillClimber : BasicAlgorithm {
45    [Storable]
46    private IRandom random;
47
48    private const string IterationsParameterName = "Iterations";
49
50    public override Type ProblemType {
51      get { return typeof(ISingleObjectiveProblemDefinition<BinaryVectorEncoding, BinaryVector>); }
52    }
53    public new ISingleObjectiveProblemDefinition<BinaryVectorEncoding, BinaryVector> Problem {
54      get { return (ISingleObjectiveProblemDefinition<BinaryVectorEncoding, BinaryVector>)base.Problem; }
55      set { base.Problem = (IProblem)value; }
56    }
57
58    public IFixedValueParameter<IntValue> IterationsParameter {
59      get { return (IFixedValueParameter<IntValue>)Parameters[IterationsParameterName]; }
60    }
61
62    public int Iterations {
63      get { return IterationsParameter.Value.Value; }
64      set { IterationsParameter.Value.Value = value; }
65    }
66
67    [StorableConstructor]
68    protected HillClimber(bool deserializing) : base(deserializing) { }
69    protected HillClimber(HillClimber original, Cloner cloner)
70      : base(original, cloner) {
71    }
72    public override IDeepCloneable Clone(Cloner cloner) {
73      return new HillClimber(this, cloner);
74    }
75
76    public HillClimber()
77      : base() {
78      random = new MersenneTwister();
79      Parameters.Add(new FixedValueParameter<IntValue>(IterationsParameterName, "", new IntValue(100)));
80    }
81    protected override void Run(CancellationToken cancellationToken) {
82      var BestQuality = new DoubleValue(double.NaN);
83      Results.Add(new Result("Best quality", BestQuality));
84      for (int iteration = 0; iteration < Iterations; iteration++) {
85        var solution = new BinaryVector(Problem.Encoding.Length);
86        for (int i = 0; i < solution.Length; i++) {
87          solution[i] = random.Next(2) == 1;
88        }
89
90        var fitness = Problem.Evaluate(solution, random);
91
92        fitness = ImproveToLocalOptimum(Problem, solution, fitness, random);
93        if (double.IsNaN(BestQuality.Value) || Problem.IsBetter(fitness, BestQuality.Value)) {
94          BestQuality.Value = fitness;
95        }
96      }
97    }
98    // In the GECCO paper, Section 2.1
99    public static double ImproveToLocalOptimum(ISingleObjectiveProblemDefinition<BinaryVectorEncoding, BinaryVector> problem, BinaryVector solution, double fitness, IRandom rand) {
100      var tried = new HashSet<int>();
101      do {
102        var options = Enumerable.Range(0, solution.Length).Shuffle(rand);
103        foreach (var option in options) {
104          if (tried.Contains(option)) continue;
105          solution[option] = !solution[option];
106          double newFitness = problem.Evaluate(solution, rand);
107          if (problem.IsBetter(newFitness, fitness)) {
108            fitness = newFitness;
109            tried.Clear();
110          } else {
111            solution[option] = !solution[option];
112          }
113          tried.Add(option);
114        }
115      } while (tried.Count != solution.Length);
116      return fitness;
117    }
118  }
119}
Note: See TracBrowser for help on using the repository browser.