[11664] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[14185] | 3 | * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[11838] | 4 | * and the BEACON Center for the Study of Evolution in Action.
|
---|
| 5 | *
|
---|
[11664] | 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;
|
---|
[11791] | 25 | using System.Threading;
|
---|
[11666] | 26 | using HeuristicLab.Analysis;
|
---|
[11664] | 27 | using HeuristicLab.Common;
|
---|
| 28 | using HeuristicLab.Core;
|
---|
| 29 | using HeuristicLab.Data;
|
---|
[11666] | 30 | using HeuristicLab.Encodings.BinaryVectorEncoding;
|
---|
[11664] | 31 | using HeuristicLab.Optimization;
|
---|
| 32 | using HeuristicLab.Parameters;
|
---|
[14927] | 33 | using HeuristicLab.Persistence;
|
---|
[11987] | 34 | using HeuristicLab.Problems.Binary;
|
---|
[11664] | 35 | using HeuristicLab.Random;
|
---|
| 36 |
|
---|
| 37 | namespace HeuristicLab.Algorithms.ParameterlessPopulationPyramid {
|
---|
[11838] | 38 | // This code is based off the publication
|
---|
| 39 | // B. W. Goldman and W. F. Punch, "Parameter-less Population Pyramid," GECCO, pp. 785–792, 2014
|
---|
| 40 | // and the original source code in C++11 available from: https://github.com/brianwgoldman/Parameter-less_Population_Pyramid
|
---|
[13173] | 41 | [Item("Parameter-less Population Pyramid (P3)", "Binary value optimization algorithm which requires no configuration. B. W. Goldman and W. F. Punch, Parameter-less Population Pyramid, GECCO, pp. 785–792, 2014")]
|
---|
[14927] | 42 | [StorableType("7588f673-a2be-4bc0-a7b2-1991613a32da")]
|
---|
[13173] | 43 | [Creatable(CreatableAttribute.Categories.PopulationBasedAlgorithms, Priority = 400)]
|
---|
[11791] | 44 | public class ParameterlessPopulationPyramid : BasicAlgorithm {
|
---|
| 45 | public override Type ProblemType {
|
---|
[11987] | 46 | get { return typeof(BinaryProblem); }
|
---|
[11791] | 47 | }
|
---|
[11987] | 48 | public new BinaryProblem Problem {
|
---|
| 49 | get { return (BinaryProblem)base.Problem; }
|
---|
[11791] | 50 | set { base.Problem = value; }
|
---|
| 51 | }
|
---|
[11667] | 52 |
|
---|
[11666] | 53 | private readonly IRandom random = new MersenneTwister();
|
---|
[11664] | 54 | private List<Population> pyramid;
|
---|
[11666] | 55 | private EvaluationTracker tracker;
|
---|
[11664] | 56 |
|
---|
| 57 | // Tracks all solutions in Pyramid for quick membership checks
|
---|
[14523] | 58 | private readonly HashSet<BinaryVector> seen = new HashSet<BinaryVector>(new EnumerableBoolEqualityComparer());
|
---|
[11681] | 59 |
|
---|
[11669] | 60 | #region ParameterNames
|
---|
[11666] | 61 | private const string MaximumIterationsParameterName = "Maximum Iterations";
|
---|
[11669] | 62 | private const string MaximumEvaluationsParameterName = "Maximum Evaluations";
|
---|
[11791] | 63 | private const string MaximumRuntimeParameterName = "Maximum Runtime";
|
---|
[11669] | 64 | private const string SeedParameterName = "Seed";
|
---|
| 65 | private const string SetSeedRandomlyParameterName = "SetSeedRandomly";
|
---|
| 66 | #endregion
|
---|
[11681] | 67 |
|
---|
[11669] | 68 | #region ParameterProperties
|
---|
[11666] | 69 | public IFixedValueParameter<IntValue> MaximumIterationsParameter {
|
---|
| 70 | get { return (IFixedValueParameter<IntValue>)Parameters[MaximumIterationsParameterName]; }
|
---|
[11664] | 71 | }
|
---|
[11669] | 72 | public IFixedValueParameter<IntValue> MaximumEvaluationsParameter {
|
---|
| 73 | get { return (IFixedValueParameter<IntValue>)Parameters[MaximumEvaluationsParameterName]; }
|
---|
| 74 | }
|
---|
[11791] | 75 | public IFixedValueParameter<IntValue> MaximumRuntimeParameter {
|
---|
| 76 | get { return (IFixedValueParameter<IntValue>)Parameters[MaximumRuntimeParameterName]; }
|
---|
| 77 | }
|
---|
[11669] | 78 | public IFixedValueParameter<IntValue> SeedParameter {
|
---|
| 79 | get { return (IFixedValueParameter<IntValue>)Parameters[SeedParameterName]; }
|
---|
| 80 | }
|
---|
| 81 | public FixedValueParameter<BoolValue> SetSeedRandomlyParameter {
|
---|
| 82 | get { return (FixedValueParameter<BoolValue>)Parameters[SetSeedRandomlyParameterName]; }
|
---|
| 83 | }
|
---|
| 84 | #endregion
|
---|
[11667] | 85 |
|
---|
[11669] | 86 | #region Properties
|
---|
[11666] | 87 | public int MaximumIterations {
|
---|
| 88 | get { return MaximumIterationsParameter.Value.Value; }
|
---|
| 89 | set { MaximumIterationsParameter.Value.Value = value; }
|
---|
[11664] | 90 | }
|
---|
[11666] | 91 | public int MaximumEvaluations {
|
---|
| 92 | get { return MaximumEvaluationsParameter.Value.Value; }
|
---|
| 93 | set { MaximumEvaluationsParameter.Value.Value = value; }
|
---|
| 94 | }
|
---|
[11791] | 95 | public int MaximumRuntime {
|
---|
| 96 | get { return MaximumRuntimeParameter.Value.Value; }
|
---|
| 97 | set { MaximumRuntimeParameter.Value.Value = value; }
|
---|
| 98 | }
|
---|
[11666] | 99 | public int Seed {
|
---|
| 100 | get { return SeedParameter.Value.Value; }
|
---|
| 101 | set { SeedParameter.Value.Value = value; }
|
---|
| 102 | }
|
---|
| 103 | public bool SetSeedRandomly {
|
---|
| 104 | get { return SetSeedRandomlyParameter.Value.Value; }
|
---|
| 105 | set { SetSeedRandomlyParameter.Value.Value = value; }
|
---|
| 106 | }
|
---|
[11669] | 107 | #endregion
|
---|
[11666] | 108 |
|
---|
| 109 | #region ResultsProperties
|
---|
| 110 | private double ResultsBestQuality {
|
---|
| 111 | get { return ((DoubleValue)Results["Best Quality"].Value).Value; }
|
---|
| 112 | set { ((DoubleValue)Results["Best Quality"].Value).Value = value; }
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | private BinaryVector ResultsBestSolution {
|
---|
| 116 | get { return (BinaryVector)Results["Best Solution"].Value; }
|
---|
| 117 | set { Results["Best Solution"].Value = value; }
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | private int ResultsBestFoundOnEvaluation {
|
---|
| 121 | get { return ((IntValue)Results["Evaluation Best Solution Was Found"].Value).Value; }
|
---|
| 122 | set { ((IntValue)Results["Evaluation Best Solution Was Found"].Value).Value = value; }
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | private int ResultsEvaluations {
|
---|
| 126 | get { return ((IntValue)Results["Evaluations"].Value).Value; }
|
---|
| 127 | set { ((IntValue)Results["Evaluations"].Value).Value = value; }
|
---|
| 128 | }
|
---|
| 129 | private int ResultsIterations {
|
---|
| 130 | get { return ((IntValue)Results["Iterations"].Value).Value; }
|
---|
| 131 | set { ((IntValue)Results["Iterations"].Value).Value = value; }
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | private DataTable ResultsQualities {
|
---|
| 135 | get { return ((DataTable)Results["Qualities"].Value); }
|
---|
| 136 | }
|
---|
| 137 | private DataRow ResultsQualitiesBest {
|
---|
| 138 | get { return ResultsQualities.Rows["Best Quality"]; }
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | private DataRow ResultsQualitiesIteration {
|
---|
| 142 | get { return ResultsQualities.Rows["Iteration Quality"]; }
|
---|
| 143 | }
|
---|
[11681] | 144 |
|
---|
| 145 |
|
---|
| 146 | private DataRow ResultsLevels {
|
---|
| 147 | get { return ((DataTable)Results["Pyramid Levels"].Value).Rows["Levels"]; }
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | private DataRow ResultsSolutions {
|
---|
| 151 | get { return ((DataTable)Results["Stored Solutions"].Value).Rows["Solutions"]; }
|
---|
| 152 | }
|
---|
[11666] | 153 | #endregion
|
---|
| 154 |
|
---|
[14523] | 155 | public override bool SupportsPause { get { return true; } }
|
---|
| 156 |
|
---|
[11664] | 157 | [StorableConstructor]
|
---|
[15018] | 158 | protected ParameterlessPopulationPyramid(StorableConstructorFlag deserializing) : base(deserializing) { }
|
---|
[11664] | 159 |
|
---|
| 160 | protected ParameterlessPopulationPyramid(ParameterlessPopulationPyramid original, Cloner cloner)
|
---|
| 161 | : base(original, cloner) {
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 165 | return new ParameterlessPopulationPyramid(this, cloner);
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | public ParameterlessPopulationPyramid() {
|
---|
[11668] | 169 | Parameters.Add(new FixedValueParameter<IntValue>(MaximumIterationsParameterName, "", new IntValue(Int32.MaxValue)));
|
---|
[11791] | 170 | Parameters.Add(new FixedValueParameter<IntValue>(MaximumEvaluationsParameterName, "", new IntValue(Int32.MaxValue)));
|
---|
| 171 | Parameters.Add(new FixedValueParameter<IntValue>(MaximumRuntimeParameterName, "The maximum runtime in seconds after which the algorithm stops. Use -1 to specify no limit for the runtime", new IntValue(3600)));
|
---|
[11666] | 172 | Parameters.Add(new FixedValueParameter<IntValue>(SeedParameterName, "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
|
---|
| 173 | Parameters.Add(new FixedValueParameter<BoolValue>(SetSeedRandomlyParameterName, "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
|
---|
[11664] | 174 | }
|
---|
| 175 |
|
---|
[11791] | 176 | protected override void OnExecutionTimeChanged() {
|
---|
| 177 | base.OnExecutionTimeChanged();
|
---|
| 178 | if (CancellationTokenSource == null) return;
|
---|
| 179 | if (MaximumRuntime == -1) return;
|
---|
| 180 | if (ExecutionTime.TotalSeconds > MaximumRuntime) CancellationTokenSource.Cancel();
|
---|
| 181 | }
|
---|
| 182 |
|
---|
[11987] | 183 | private void AddIfUnique(BinaryVector solution, int level) {
|
---|
[11664] | 184 | // Don't add things you have seen
|
---|
| 185 | if (seen.Contains(solution)) return;
|
---|
| 186 | if (level == pyramid.Count) {
|
---|
[11666] | 187 | pyramid.Add(new Population(tracker.Length, random));
|
---|
[11664] | 188 | }
|
---|
[11987] | 189 | var copied = (BinaryVector)solution.Clone();
|
---|
[11667] | 190 | pyramid[level].Add(copied);
|
---|
| 191 | seen.Add(copied);
|
---|
[11664] | 192 | }
|
---|
| 193 |
|
---|
[11672] | 194 | // In the GECCO paper, Figure 1
|
---|
[11664] | 195 | private double iterate() {
|
---|
| 196 | // Create a random solution
|
---|
[11987] | 197 | BinaryVector solution = new BinaryVector(tracker.Length);
|
---|
[11664] | 198 | for (int i = 0; i < solution.Length; i++) {
|
---|
| 199 | solution[i] = random.Next(2) == 1;
|
---|
| 200 | }
|
---|
[11987] | 201 | double fitness = tracker.Evaluate(solution, random);
|
---|
[11666] | 202 | fitness = HillClimber.ImproveToLocalOptimum(tracker, solution, fitness, random);
|
---|
[11664] | 203 | AddIfUnique(solution, 0);
|
---|
[11667] | 204 |
|
---|
[11664] | 205 | for (int level = 0; level < pyramid.Count; level++) {
|
---|
| 206 | var current = pyramid[level];
|
---|
[11666] | 207 | double newFitness = LinkageCrossover.ImproveUsingTree(current.Tree, current.Solutions, solution, fitness, tracker, random);
|
---|
[11664] | 208 | // add it to the next level if its a strict fitness improvement
|
---|
[11666] | 209 | if (tracker.IsBetter(newFitness, fitness)) {
|
---|
[11664] | 210 | fitness = newFitness;
|
---|
| 211 | AddIfUnique(solution, level + 1);
|
---|
| 212 | }
|
---|
| 213 | }
|
---|
| 214 | return fitness;
|
---|
| 215 | }
|
---|
| 216 |
|
---|
[14517] | 217 | protected override void Initialize(CancellationToken cancellationToken) {
|
---|
[11669] | 218 | // Set up the algorithm
|
---|
[11666] | 219 | if (SetSeedRandomly) Seed = new System.Random().Next();
|
---|
[11664] | 220 | pyramid = new List<Population>();
|
---|
[11667] | 221 | seen.Clear();
|
---|
[11666] | 222 | random.Reset(Seed);
|
---|
| 223 | tracker = new EvaluationTracker(Problem, MaximumEvaluations);
|
---|
[11669] | 224 |
|
---|
| 225 | // Set up the results display
|
---|
[11666] | 226 | Results.Add(new Result("Iterations", new IntValue(0)));
|
---|
| 227 | Results.Add(new Result("Evaluations", new IntValue(0)));
|
---|
| 228 | Results.Add(new Result("Best Solution", new BinaryVector(tracker.BestSolution)));
|
---|
| 229 | Results.Add(new Result("Best Quality", new DoubleValue(tracker.BestQuality)));
|
---|
| 230 | Results.Add(new Result("Evaluation Best Solution Was Found", new IntValue(tracker.BestFoundOnEvaluation)));
|
---|
| 231 | var table = new DataTable("Qualities");
|
---|
| 232 | table.Rows.Add(new DataRow("Best Quality"));
|
---|
| 233 | var iterationRows = new DataRow("Iteration Quality");
|
---|
| 234 | iterationRows.VisualProperties.LineStyle = DataRowVisualProperties.DataRowLineStyle.Dot;
|
---|
| 235 | table.Rows.Add(iterationRows);
|
---|
| 236 | Results.Add(new Result("Qualities", table));
|
---|
[11669] | 237 |
|
---|
[11681] | 238 | table = new DataTable("Pyramid Levels");
|
---|
| 239 | table.Rows.Add(new DataRow("Levels"));
|
---|
| 240 | Results.Add(new Result("Pyramid Levels", table));
|
---|
| 241 |
|
---|
| 242 | table = new DataTable("Stored Solutions");
|
---|
| 243 | table.Rows.Add(new DataRow("Solutions"));
|
---|
| 244 | Results.Add(new Result("Stored Solutions", table));
|
---|
| 245 |
|
---|
[14517] | 246 | base.Initialize(cancellationToken);
|
---|
| 247 | }
|
---|
| 248 |
|
---|
| 249 | protected override void Run(CancellationToken cancellationToken) {
|
---|
[11669] | 250 | // Loop until iteration limit reached or canceled.
|
---|
[14517] | 251 | while (ResultsIterations < MaximumIterations) {
|
---|
[14527] | 252 | double fitness = double.NaN;
|
---|
[11666] | 253 |
|
---|
[14527] | 254 | try {
|
---|
| 255 | fitness = iterate();
|
---|
| 256 | ResultsIterations++;
|
---|
| 257 | cancellationToken.ThrowIfCancellationRequested();
|
---|
| 258 | } finally {
|
---|
| 259 | ResultsEvaluations = tracker.Evaluations;
|
---|
| 260 | ResultsBestSolution = new BinaryVector(tracker.BestSolution);
|
---|
| 261 | ResultsBestQuality = tracker.BestQuality;
|
---|
| 262 | ResultsBestFoundOnEvaluation = tracker.BestFoundOnEvaluation;
|
---|
| 263 | ResultsQualitiesBest.Values.Add(tracker.BestQuality);
|
---|
| 264 | ResultsQualitiesIteration.Values.Add(fitness);
|
---|
| 265 | ResultsLevels.Values.Add(pyramid.Count);
|
---|
| 266 | ResultsSolutions.Values.Add(seen.Count);
|
---|
| 267 | }
|
---|
[11664] | 268 | }
|
---|
| 269 | }
|
---|
| 270 | }
|
---|
| 271 | }
|
---|