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