Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 11791 was 11791, checked in by mkommend, 9 years ago

#2282: Implemented stop button in PPP and adapted to new BasicAlgorithm.

File size: 4.1 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;
23using System.Collections.Generic;
24using System.Linq;
25using System.Threading;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32using HeuristicLab.Random;
33
34
35namespace HeuristicLab.Algorithms.ParameterlessPopulationPyramid {
36  [Item("Hill Climber", "Test algorithm.")]
37  [StorableClass]
38  [Creatable("Parameterless Population Pyramid")]
39  // In the GECCO paper, Section 2.1
40  public class HillClimber : BasicAlgorithm {
41    [Storable]
42    private IRandom random;
43
44    private const string IterationsParameterName = "Iterations";
45
46    public override Type ProblemType {
47      get { return typeof(BinaryVectorProblem); }
48    }
49    public new BinaryVectorProblem Problem {
50      get { return (BinaryVectorProblem)base.Problem; }
51      set { base.Problem = value; }
52    }
53
54    public IFixedValueParameter<IntValue> IterationsParameter {
55      get { return (IFixedValueParameter<IntValue>)Parameters[IterationsParameterName]; }
56    }
57
58    public int Iterations {
59      get { return IterationsParameter.Value.Value; }
60      set { IterationsParameter.Value.Value = value; }
61    }
62
63    [StorableConstructor]
64    protected HillClimber(bool deserializing) : base(deserializing) { }
65    protected HillClimber(HillClimber original, Cloner cloner)
66      : base(original, cloner) {
67    }
68    public override IDeepCloneable Clone(Cloner cloner) {
69      return new HillClimber(this, cloner);
70    }
71
72    public HillClimber()
73      : base() {
74      random = new MersenneTwister();
75      Parameters.Add(new FixedValueParameter<IntValue>(IterationsParameterName, "", new IntValue(100)));
76    }
77    protected override void Run(CancellationToken cancellationToken) {
78      var BestQuality = new DoubleValue(double.NaN);
79      Results.Add(new Result("Best quality", BestQuality));
80      for (int iteration = 0; iteration < Iterations; iteration++) {
81        bool[] solution = new bool[Problem.Length];
82        for (int i = 0; i < solution.Length; i++) {
83          solution[i] = random.Next(2) == 1;
84        }
85
86        var fitness = Problem.Evaluate(solution);
87
88        fitness = ImproveToLocalOptimum(Problem, solution, fitness, random);
89        if (double.IsNaN(BestQuality.Value) || Problem.IsBetter(fitness, BestQuality.Value)) {
90          BestQuality.Value = fitness;
91        }
92      }
93    }
94
95    public static double ImproveToLocalOptimum(IBinaryVectorProblem problem, bool[] solution, double fitness, IRandom rand) {
96      var tried = new HashSet<int>();
97      do {
98        var options = Enumerable.Range(0, solution.Length).Shuffle(rand);
99        foreach (var option in options) {
100          if (tried.Contains(option)) continue;
101          solution[option] = !solution[option];
102          double newFitness = problem.Evaluate(solution);
103          if (problem.IsBetter(newFitness, fitness)) {
104            fitness = newFitness;
105            tried.Clear();
106          } else {
107            solution[option] = !solution[option];
108          }
109          tried.Add(option);
110        }
111      } while (tried.Count != solution.Length);
112      return fitness;
113    }
114  }
115}
Note: See TracBrowser for help on using the repository browser.