1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | * and the BEACON Center for the Study of Evolution in Action.
|
---|
5 | *
|
---|
6 | * This file is part of HeuristicLab.
|
---|
7 | *
|
---|
8 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
9 | * it under the terms of the GNU General Public License as published by
|
---|
10 | * the Free Software Foundation, either version 3 of the License, or
|
---|
11 | * (at your option) any later version.
|
---|
12 | *
|
---|
13 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
16 | * GNU General Public License for more details.
|
---|
17 | *
|
---|
18 | * You should have received a copy of the GNU General Public License
|
---|
19 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
20 | */
|
---|
21 | #endregion
|
---|
22 |
|
---|
23 | using System;
|
---|
24 | using System.Collections.Generic;
|
---|
25 | using System.Linq;
|
---|
26 | using System.Threading;
|
---|
27 | using HEAL.Attic;
|
---|
28 | using HeuristicLab.Common;
|
---|
29 | using HeuristicLab.Core;
|
---|
30 | using HeuristicLab.Data;
|
---|
31 | using HeuristicLab.Encodings.BinaryVectorEncoding;
|
---|
32 | using HeuristicLab.Optimization;
|
---|
33 |
|
---|
34 | using HeuristicLab.Parameters;
|
---|
35 | using HeuristicLab.Random;
|
---|
36 |
|
---|
37 |
|
---|
38 | namespace HeuristicLab.Algorithms.ParameterlessPopulationPyramid {
|
---|
39 | // This code is based off the publication
|
---|
40 | // B. W. Goldman and W. F. Punch, "Parameter-less Population Pyramid," GECCO, pp. 785–792, 2014
|
---|
41 | // and the original source code in C++11 available from: https://github.com/brianwgoldman/Parameter-less_Population_Pyramid
|
---|
42 | [Item("Hill Climber (HC)", "Binary Hill Climber.")]
|
---|
43 | [StorableType("BA349010-6295-406E-8989-B271FB96ED86")]
|
---|
44 | [Creatable(CreatableAttribute.Categories.SingleSolutionAlgorithms, Priority = 150)]
|
---|
45 | public class HillClimber : BasicAlgorithm {
|
---|
46 | [Storable]
|
---|
47 | private readonly IRandom random;
|
---|
48 |
|
---|
49 | [Storable] public IFixedValueParameter<IntValue> MaximumIterationsParameter { get; private set; }
|
---|
50 |
|
---|
51 | [Storable] public IResult<DoubleValue> BestQualityResult { get; private set; }
|
---|
52 | [Storable] public IResult<IntValue> IterationsResult { get; private set; }
|
---|
53 |
|
---|
54 | public override Type ProblemType {
|
---|
55 | get { return typeof(ISingleObjectiveProblemDefinition<BinaryVectorEncoding, BinaryVector>); }
|
---|
56 | }
|
---|
57 | public new ISingleObjectiveProblemDefinition<BinaryVectorEncoding, BinaryVector> Problem {
|
---|
58 | get { return (ISingleObjectiveProblemDefinition<BinaryVectorEncoding, BinaryVector>)base.Problem; }
|
---|
59 | set { base.Problem = (IProblem)value; }
|
---|
60 | }
|
---|
61 |
|
---|
62 | public override bool SupportsPause { get { return false; } }
|
---|
63 |
|
---|
64 | public int MaximumIterations {
|
---|
65 | get { return MaximumIterationsParameter.Value.Value; }
|
---|
66 | set { MaximumIterationsParameter.Value.Value = value; }
|
---|
67 | }
|
---|
68 |
|
---|
69 | private int Iterations {
|
---|
70 | get => IterationsResult.Value.Value;
|
---|
71 | set => IterationsResult.Value.Value = value;
|
---|
72 | }
|
---|
73 |
|
---|
74 | private double BestQuality {
|
---|
75 | get => BestQualityResult.Value.Value;
|
---|
76 | set => BestQualityResult.Value.Value = value;
|
---|
77 | }
|
---|
78 |
|
---|
79 | [StorableConstructor]
|
---|
80 | protected HillClimber(StorableConstructorFlag _) : base(_) { }
|
---|
81 | protected HillClimber(HillClimber original, Cloner cloner)
|
---|
82 | : base(original, cloner) {
|
---|
83 | MaximumIterationsParameter = cloner.Clone(original.MaximumIterationsParameter);
|
---|
84 | BestQualityResult = cloner.Clone(original.BestQualityResult);
|
---|
85 | IterationsResult = cloner.Clone(original.IterationsResult);
|
---|
86 | }
|
---|
87 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
88 | return new HillClimber(this, cloner);
|
---|
89 | }
|
---|
90 |
|
---|
91 | public HillClimber()
|
---|
92 | : base() {
|
---|
93 | random = new MersenneTwister();
|
---|
94 | Parameters.Add(MaximumIterationsParameter = new FixedValueParameter<IntValue>("Maximum Iterations", "", new IntValue(100)));
|
---|
95 |
|
---|
96 | Results.Add(BestQualityResult = new Result<DoubleValue>("Best Quality", "The best quality found so far."));
|
---|
97 | Results.Add(IterationsResult = new Result<IntValue>("Iterations", "The current iteration."));
|
---|
98 | }
|
---|
99 |
|
---|
100 | protected override void Initialize(CancellationToken cancellationToken) {
|
---|
101 | base.Initialize(cancellationToken);
|
---|
102 |
|
---|
103 | IterationsResult.Value = new IntValue();
|
---|
104 | BestQualityResult.Value = new DoubleValue(double.NaN);
|
---|
105 | }
|
---|
106 |
|
---|
107 |
|
---|
108 |
|
---|
109 | protected override void Run(CancellationToken cancellationToken) {
|
---|
110 | while (IterationsResult.Value.Value < MaximumIterations) {
|
---|
111 | cancellationToken.ThrowIfCancellationRequested();
|
---|
112 |
|
---|
113 | var solution = new BinaryVector(Problem.Encoding.Length);
|
---|
114 | for (int i = 0; i < solution.Length; i++) {
|
---|
115 | solution[i] = random.Next(2) == 1;
|
---|
116 | }
|
---|
117 |
|
---|
118 | var evaluationResult = Problem.Evaluate(solution, random);
|
---|
119 | var fitness = evaluationResult.Quality;
|
---|
120 |
|
---|
121 | fitness = ImproveToLocalOptimum(Problem, solution, fitness, random);
|
---|
122 | var bestSoFar = BestQuality;
|
---|
123 | if (double.IsNaN(bestSoFar) || Problem.IsBetter(fitness, bestSoFar)) {
|
---|
124 | BestQuality = fitness;
|
---|
125 | }
|
---|
126 |
|
---|
127 | var context = new SingleObjectiveSolutionContext<BinaryVector>(solution);
|
---|
128 | context.EvaluationResult = new SingleObjectiveEvaluationResult(fitness);
|
---|
129 | Problem.Analyze(new[] { context }, random);
|
---|
130 |
|
---|
131 | Iterations++;
|
---|
132 | }
|
---|
133 | }
|
---|
134 | // In the GECCO paper, Section 2.1
|
---|
135 | public static double ImproveToLocalOptimum(ISingleObjectiveProblemDefinition<BinaryVectorEncoding, BinaryVector> problem, BinaryVector solution, double fitness, IRandom rand) {
|
---|
136 | var tried = new HashSet<int>();
|
---|
137 | do {
|
---|
138 | var options = Enumerable.Range(0, solution.Length).Shuffle(rand);
|
---|
139 | foreach (var option in options) {
|
---|
140 | if (tried.Contains(option)) continue;
|
---|
141 | solution[option] = !solution[option];
|
---|
142 | var newEvaluationResult = problem.Evaluate(solution, rand);
|
---|
143 | double newFitness = newEvaluationResult.Quality;
|
---|
144 | if (problem.IsBetter(newFitness, fitness)) {
|
---|
145 | fitness = newFitness;
|
---|
146 | tried.Clear();
|
---|
147 | } else {
|
---|
148 | solution[option] = !solution[option];
|
---|
149 | }
|
---|
150 | tried.Add(option);
|
---|
151 | }
|
---|
152 | } while (tried.Count != solution.Length);
|
---|
153 | return fitness;
|
---|
154 | }
|
---|
155 | }
|
---|
156 | }
|
---|