Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2282 Initial version of Hill Climber

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