Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2282: Implemented fitness for OneMaxProblem without lambdas.

File size: 3.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.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.BinaryVectorEncoding;
28using HeuristicLab.Optimization;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Random;
31
32
33namespace HeuristicLab.Algorithms.ParameterlessPopulationPyramid {
34  [Item("Hill Climber", "Test algorithm.")]
35  [StorableClass]
36  [Creatable("Parameterless Population Pyramid")]
37  public class HillClimber : AlgorithmBase {
38    [Storable]
39    private IRandom random;
40
41    [StorableConstructor]
42    protected HillClimber(bool deserializing) : base(deserializing) { }
43    protected HillClimber(HillClimber original, Cloner cloner)
44      : base(original, cloner) {
45    }
46    public override IDeepCloneable Clone(Cloner cloner) {
47      return new HillClimber(this, cloner);
48    }
49
50    public HillClimber()
51      : base() {
52      random = new MersenneTwister();
53    }
54    protected override void Run() {
55      bool[] solution = new bool[Problem.Length];
56      for (int i = 0; i < solution.Length; i++) {
57        solution[i] = random.Next(2) == 1;
58      }
59      var fitness = Problem.Evaluate(solution);
60      fitness = ImproveToLocalOptimum(Problem, solution, fitness, random);
61
62      Results.Add(new Result("Best quality", new DoubleValue(fitness)));
63      Results.Add(new Result("Best solution", new BinaryVector(solution)));
64    }
65    public static double ImproveToLocalOptimum(BinaryVectorProblem problem, bool[] solution, double fitness, IRandom rand) {
66      var tried = new HashSet<int>();
67      do {
68        var options = Enumerable.Range(0, solution.Length).Shuffle(rand);
69        foreach (var option in options) {
70          solution[option] = !solution[option];
71          double newFitness = problem.Evaluate(solution);
72          if ((problem.Maximization && fitness < newFitness) || (!problem.Maximization && fitness > newFitness)) {
73            fitness = newFitness;
74            tried.Clear();
75          } else {
76            solution[option] = !solution[option];
77          }
78          tried.Add(option);
79        }
80      } while (tried.Count != solution.Length);
81      return fitness;
82    }
83  }
84}
Note: See TracBrowser for help on using the repository browser.