[11636] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17246] | 3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[11838] | 4 | * and the BEACON Center for the Study of Evolution in Action.
|
---|
[11636] | 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 |
|
---|
[11791] | 23 | using System;
|
---|
[11636] | 24 | using System.Collections.Generic;
|
---|
| 25 | using System.Linq;
|
---|
[11791] | 26 | using System.Threading;
|
---|
[11636] | 27 | using HeuristicLab.Common;
|
---|
| 28 | using HeuristicLab.Core;
|
---|
| 29 | using HeuristicLab.Data;
|
---|
[11987] | 30 | using HeuristicLab.Encodings.BinaryVectorEncoding;
|
---|
[11636] | 31 | using HeuristicLab.Optimization;
|
---|
[11640] | 32 | using HeuristicLab.Parameters;
|
---|
[16662] | 33 | using HEAL.Attic;
|
---|
[11987] | 34 | using HeuristicLab.Problems.Binary;
|
---|
[11636] | 35 | using HeuristicLab.Random;
|
---|
| 36 |
|
---|
| 37 |
|
---|
| 38 | namespace HeuristicLab.Algorithms.ParameterlessPopulationPyramid {
|
---|
[11838] | 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
|
---|
[13173] | 42 | [Item("Hill Climber (HC)", "Binary Hill Climber.")]
|
---|
[16662] | 43 | [StorableType("BA349010-6295-406E-8989-B271FB96ED86")]
|
---|
[12504] | 44 | [Creatable(CreatableAttribute.Categories.SingleSolutionAlgorithms, Priority = 150)]
|
---|
[11791] | 45 | public class HillClimber : BasicAlgorithm {
|
---|
[11636] | 46 | [Storable]
|
---|
| 47 | private IRandom random;
|
---|
[11664] | 48 |
|
---|
[11640] | 49 | private const string IterationsParameterName = "Iterations";
|
---|
[14517] | 50 | private const string BestQualityResultName = "Best quality";
|
---|
| 51 | private const string IterationsResultName = "Iterations";
|
---|
[11636] | 52 |
|
---|
[11791] | 53 | public override Type ProblemType {
|
---|
[11987] | 54 | get { return typeof(BinaryProblem); }
|
---|
[11791] | 55 | }
|
---|
[14523] | 56 |
|
---|
| 57 | public override bool SupportsPause { get { return false; } }
|
---|
| 58 |
|
---|
[11987] | 59 | public new BinaryProblem Problem {
|
---|
| 60 | get { return (BinaryProblem)base.Problem; }
|
---|
[11791] | 61 | set { base.Problem = value; }
|
---|
| 62 | }
|
---|
| 63 |
|
---|
[11640] | 64 | public IFixedValueParameter<IntValue> IterationsParameter {
|
---|
| 65 | get { return (IFixedValueParameter<IntValue>)Parameters[IterationsParameterName]; }
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | public int Iterations {
|
---|
| 69 | get { return IterationsParameter.Value.Value; }
|
---|
| 70 | set { IterationsParameter.Value.Value = value; }
|
---|
| 71 | }
|
---|
| 72 |
|
---|
[14517] | 73 | #region ResultsProperties
|
---|
| 74 | private double ResultsBestQuality {
|
---|
| 75 | get { return ((DoubleValue)Results[BestQualityResultName].Value).Value; }
|
---|
| 76 | set { ((DoubleValue)Results[BestQualityResultName].Value).Value = value; }
|
---|
| 77 | }
|
---|
| 78 | private int ResultsIterations {
|
---|
| 79 | get { return ((IntValue)Results[IterationsResultName].Value).Value; }
|
---|
| 80 | set { ((IntValue)Results[IterationsResultName].Value).Value = value; }
|
---|
| 81 | }
|
---|
| 82 | #endregion
|
---|
| 83 |
|
---|
[11636] | 84 | [StorableConstructor]
|
---|
[16662] | 85 | protected HillClimber(StorableConstructorFlag _) : base(_) { }
|
---|
[11636] | 86 | protected HillClimber(HillClimber original, Cloner cloner)
|
---|
| 87 | : base(original, cloner) {
|
---|
| 88 | }
|
---|
| 89 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 90 | return new HillClimber(this, cloner);
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | public HillClimber()
|
---|
| 94 | : base() {
|
---|
| 95 | random = new MersenneTwister();
|
---|
[11640] | 96 | Parameters.Add(new FixedValueParameter<IntValue>(IterationsParameterName, "", new IntValue(100)));
|
---|
[11636] | 97 | }
|
---|
[14517] | 98 |
|
---|
| 99 |
|
---|
| 100 | protected override void Initialize(CancellationToken cancellationToken) {
|
---|
| 101 | Results.Add(new Result(BestQualityResultName, new DoubleValue(double.NaN)));
|
---|
| 102 | Results.Add(new Result(IterationsResultName, new IntValue(0)));
|
---|
| 103 | base.Initialize(cancellationToken);
|
---|
| 104 | }
|
---|
[11791] | 105 | protected override void Run(CancellationToken cancellationToken) {
|
---|
[14517] | 106 | while (ResultsIterations < Iterations) {
|
---|
| 107 | cancellationToken.ThrowIfCancellationRequested();
|
---|
| 108 |
|
---|
[11987] | 109 | var solution = new BinaryVector(Problem.Length);
|
---|
[11640] | 110 | for (int i = 0; i < solution.Length; i++) {
|
---|
| 111 | solution[i] = random.Next(2) == 1;
|
---|
| 112 | }
|
---|
| 113 |
|
---|
[11987] | 114 | var fitness = Problem.Evaluate(solution, random);
|
---|
[11640] | 115 |
|
---|
| 116 | fitness = ImproveToLocalOptimum(Problem, solution, fitness, random);
|
---|
[14517] | 117 | if (double.IsNaN(ResultsBestQuality) || Problem.IsBetter(fitness, ResultsBestQuality)) {
|
---|
| 118 | ResultsBestQuality = fitness;
|
---|
[11640] | 119 | }
|
---|
[14517] | 120 |
|
---|
| 121 | ResultsIterations++;
|
---|
[11636] | 122 | }
|
---|
| 123 | }
|
---|
[11838] | 124 | // In the GECCO paper, Section 2.1
|
---|
[11987] | 125 | public static double ImproveToLocalOptimum(BinaryProblem problem, BinaryVector solution, double fitness, IRandom rand) {
|
---|
[11636] | 126 | var tried = new HashSet<int>();
|
---|
| 127 | do {
|
---|
| 128 | var options = Enumerable.Range(0, solution.Length).Shuffle(rand);
|
---|
| 129 | foreach (var option in options) {
|
---|
[11668] | 130 | if (tried.Contains(option)) continue;
|
---|
[11636] | 131 | solution[option] = !solution[option];
|
---|
[11987] | 132 | double newFitness = problem.Evaluate(solution, rand);
|
---|
[11640] | 133 | if (problem.IsBetter(newFitness, fitness)) {
|
---|
[11636] | 134 | fitness = newFitness;
|
---|
| 135 | tried.Clear();
|
---|
| 136 | } else {
|
---|
| 137 | solution[option] = !solution[option];
|
---|
| 138 | }
|
---|
| 139 | tried.Add(option);
|
---|
| 140 | }
|
---|
| 141 | } while (tried.Count != solution.Length);
|
---|
| 142 | return fitness;
|
---|
[11637] | 143 | }
|
---|
[11636] | 144 | }
|
---|
| 145 | }
|
---|