Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Parameter-less Population Pyramid/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/HillClimber.cs @ 11838

Last change on this file since 11838 was 11838, checked in by bgoldman, 9 years ago

#2282: Added BEACON to the copyright on P3 files and included comments referring to the publication

File size: 4.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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.Optimization;
31using HeuristicLab.Parameters;
32using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
33using HeuristicLab.Random;
34
35
36namespace HeuristicLab.Algorithms.ParameterlessPopulationPyramid {
37  // This code is based off the publication
38  // B. W. Goldman and W. F. Punch, "Parameter-less Population Pyramid," GECCO, pp. 785–792, 2014
39  // and the original source code in C++11 available from: https://github.com/brianwgoldman/Parameter-less_Population_Pyramid
40  [Item("Hill Climber", "Test algorithm.")]
41  [StorableClass]
42  [Creatable("Parameterless Population Pyramid")]
43  public class HillClimber : BasicAlgorithm {
44    [Storable]
45    private IRandom random;
46
47    private const string IterationsParameterName = "Iterations";
48
49    public override Type ProblemType {
50      get { return typeof(BinaryVectorProblem); }
51    }
52    public new BinaryVectorProblem Problem {
53      get { return (BinaryVectorProblem)base.Problem; }
54      set { base.Problem = value; }
55    }
56
57    public IFixedValueParameter<IntValue> IterationsParameter {
58      get { return (IFixedValueParameter<IntValue>)Parameters[IterationsParameterName]; }
59    }
60
61    public int Iterations {
62      get { return IterationsParameter.Value.Value; }
63      set { IterationsParameter.Value.Value = value; }
64    }
65
66    [StorableConstructor]
67    protected HillClimber(bool deserializing) : base(deserializing) { }
68    protected HillClimber(HillClimber original, Cloner cloner)
69      : base(original, cloner) {
70    }
71    public override IDeepCloneable Clone(Cloner cloner) {
72      return new HillClimber(this, cloner);
73    }
74
75    public HillClimber()
76      : base() {
77      random = new MersenneTwister();
78      Parameters.Add(new FixedValueParameter<IntValue>(IterationsParameterName, "", new IntValue(100)));
79    }
80    protected override void Run(CancellationToken cancellationToken) {
81      var BestQuality = new DoubleValue(double.NaN);
82      Results.Add(new Result("Best quality", BestQuality));
83      for (int iteration = 0; iteration < Iterations; iteration++) {
84        bool[] solution = new bool[Problem.Length];
85        for (int i = 0; i < solution.Length; i++) {
86          solution[i] = random.Next(2) == 1;
87        }
88
89        var fitness = Problem.Evaluate(solution);
90
91        fitness = ImproveToLocalOptimum(Problem, solution, fitness, random);
92        if (double.IsNaN(BestQuality.Value) || Problem.IsBetter(fitness, BestQuality.Value)) {
93          BestQuality.Value = fitness;
94        }
95      }
96    }
97    // In the GECCO paper, Section 2.1
98    public static double ImproveToLocalOptimum(IBinaryVectorProblem problem, bool[] solution, double fitness, IRandom rand) {
99      var tried = new HashSet<int>();
100      do {
101        var options = Enumerable.Range(0, solution.Length).Shuffle(rand);
102        foreach (var option in options) {
103          if (tried.Contains(option)) continue;
104          solution[option] = !solution[option];
105          double newFitness = problem.Evaluate(solution);
106          if (problem.IsBetter(newFitness, fitness)) {
107            fitness = newFitness;
108            tried.Clear();
109          } else {
110            solution[option] = !solution[option];
111          }
112          tried.Add(option);
113        }
114      } while (tried.Count != solution.Length);
115      return fitness;
116    }
117  }
118}
Note: See TracBrowser for help on using the repository browser.