Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2282 Evaluation counting, results and graphing, HIFF problem.

File size: 3.7 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  public class HillClimber : AlgorithmBase {
39    [Storable]
40    private IRandom random;
41
42    private const string IterationsParameterName = "Iterations";
43
44    public IFixedValueParameter<IntValue> IterationsParameter {
45      get { return (IFixedValueParameter<IntValue>)Parameters[IterationsParameterName]; }
46    }
47
48    public int Iterations {
49      get { return IterationsParameter.Value.Value; }
50      set { IterationsParameter.Value.Value = value; }
51    }
52
53    [StorableConstructor]
54    protected HillClimber(bool deserializing) : base(deserializing) { }
55    protected HillClimber(HillClimber original, Cloner cloner)
56      : base(original, cloner) {
57    }
58    public override IDeepCloneable Clone(Cloner cloner) {
59      return new HillClimber(this, cloner);
60    }
61
62    public HillClimber()
63      : base() {
64      random = new MersenneTwister();
65      Parameters.Add(new FixedValueParameter<IntValue>(IterationsParameterName, "", new IntValue(100)));
66    }
67    protected override void Run() {
68      var BestQuality = new DoubleValue(double.NaN);
69      Results.Add(new Result("Best quality", BestQuality));
70      for (int iteration = 0; iteration < Iterations; iteration++) {
71        bool[] solution = new bool[Problem.Length];
72        for (int i = 0; i < solution.Length; i++) {
73          solution[i] = random.Next(2) == 1;
74        }
75
76        var fitness = Problem.Evaluate(solution);
77
78        fitness = ImproveToLocalOptimum(Problem, solution, fitness, random);
79        if (double.IsNaN(BestQuality.Value) || Problem.IsBetter(fitness, BestQuality.Value)) {
80          BestQuality.Value = fitness;
81        }
82      }
83    }
84    public static double ImproveToLocalOptimum(IBinaryVectorProblem problem, bool[] solution, double fitness, IRandom rand) {
85      var tried = new HashSet<int>();
86      do {
87        var options = Enumerable.Range(0, solution.Length).Shuffle(rand);
88        foreach (var option in options) {
89          solution[option] = !solution[option];
90          double newFitness = problem.Evaluate(solution);
91          if (problem.IsBetter(newFitness, fitness)) {
92            fitness = newFitness;
93            tried.Clear();
94          } else {
95            solution[option] = !solution[option];
96          }
97          tried.Add(option);
98        }
99      } while (tried.Count != solution.Length);
100      return fitness;
101    }
102  }
103}
Note: See TracBrowser for help on using the repository browser.