Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 11733 was 11672, checked in by bgoldman, 10 years ago

#2282 Commenting, added some basic unit tests for P3.

File size: 3.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.BinaryVectorEncoding;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31using HeuristicLab.Random;
32
33
34namespace HeuristicLab.Algorithms.ParameterlessPopulationPyramid {
35  [Item("Hill Climber", "Test algorithm.")]
36  [StorableClass]
37  [Creatable("Parameterless Population Pyramid")]
38  // In the GECCO paper, Section 2.1
39  public class HillClimber : AlgorithmBase {
40    [Storable]
41    private IRandom random;
42
43    private const string IterationsParameterName = "Iterations";
44
45    public IFixedValueParameter<IntValue> IterationsParameter {
46      get { return (IFixedValueParameter<IntValue>)Parameters[IterationsParameterName]; }
47    }
48
49    public int Iterations {
50      get { return IterationsParameter.Value.Value; }
51      set { IterationsParameter.Value.Value = value; }
52    }
53
54    [StorableConstructor]
55    protected HillClimber(bool deserializing) : base(deserializing) { }
56    protected HillClimber(HillClimber original, Cloner cloner)
57      : base(original, cloner) {
58    }
59    public override IDeepCloneable Clone(Cloner cloner) {
60      return new HillClimber(this, cloner);
61    }
62
63    public HillClimber()
64      : base() {
65      random = new MersenneTwister();
66      Parameters.Add(new FixedValueParameter<IntValue>(IterationsParameterName, "", new IntValue(100)));
67    }
68    protected override void Run() {
69      var BestQuality = new DoubleValue(double.NaN);
70      Results.Add(new Result("Best quality", BestQuality));
71      for (int iteration = 0; iteration < Iterations; iteration++) {
72        bool[] solution = new bool[Problem.Length];
73        for (int i = 0; i < solution.Length; i++) {
74          solution[i] = random.Next(2) == 1;
75        }
76
77        var fitness = Problem.Evaluate(solution);
78
79        fitness = ImproveToLocalOptimum(Problem, solution, fitness, random);
80        if (double.IsNaN(BestQuality.Value) || Problem.IsBetter(fitness, BestQuality.Value)) {
81          BestQuality.Value = fitness;
82        }
83      }
84    }
85
86    public static double ImproveToLocalOptimum(IBinaryVectorProblem problem, bool[] solution, double fitness, IRandom rand) {
87      var tried = new HashSet<int>();
88      do {
89        var options = Enumerable.Range(0, solution.Length).Shuffle(rand);
90        foreach (var option in options) {
91          if (tried.Contains(option)) continue;
92          solution[option] = !solution[option];
93          double newFitness = problem.Evaluate(solution);
94          if (problem.IsBetter(newFitness, fitness)) {
95            fitness = newFitness;
96            tried.Clear();
97          } else {
98            solution[option] = !solution[option];
99          }
100          tried.Add(option);
101        }
102      } while (tried.Count != solution.Length);
103      return fitness;
104    }
105  }
106}
Note: See TracBrowser for help on using the repository browser.