Free cookie consent management tool by TermsFeed Policy Generator

source: branches/MemPRAlgorithm/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/ParameterlessPopulationPyramid.cs @ 14895

Last change on this file since 14895 was 14562, checked in by abeham, 8 years ago

#2701: Updated branch to trunk

File size: 11.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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
23using System;
24using System.Collections.Generic;
25using System.Threading;
26using HeuristicLab.Analysis;
27using HeuristicLab.Common;
28using HeuristicLab.Core;
29using HeuristicLab.Data;
30using HeuristicLab.Encodings.BinaryVectorEncoding;
31using HeuristicLab.Optimization;
32using HeuristicLab.Parameters;
33using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
34using HeuristicLab.Problems.Binary;
35using HeuristicLab.Random;
36
37namespace HeuristicLab.Algorithms.ParameterlessPopulationPyramid {
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
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")]
42  [StorableClass]
43  [Creatable(CreatableAttribute.Categories.PopulationBasedAlgorithms, Priority = 400)]
44  public class ParameterlessPopulationPyramid : BasicAlgorithm {
45    public override Type ProblemType {
46      get { return typeof(BinaryProblem); }
47    }
48    public new BinaryProblem Problem {
49      get { return (BinaryProblem)base.Problem; }
50      set { base.Problem = value; }
51    }
52
53    private readonly IRandom random = new MersenneTwister();
54    private List<Population> pyramid;
55    private EvaluationTracker tracker;
56
57    // Tracks all solutions in Pyramid for quick membership checks
58    private readonly HashSet<BinaryVector> seen = new HashSet<BinaryVector>(new EnumerableBoolEqualityComparer());
59
60    #region ParameterNames
61    private const string MaximumIterationsParameterName = "Maximum Iterations";
62    private const string MaximumEvaluationsParameterName = "Maximum Evaluations";
63    private const string MaximumRuntimeParameterName = "Maximum Runtime";
64    private const string SeedParameterName = "Seed";
65    private const string SetSeedRandomlyParameterName = "SetSeedRandomly";
66    #endregion
67
68    #region ParameterProperties
69    public IFixedValueParameter<IntValue> MaximumIterationsParameter {
70      get { return (IFixedValueParameter<IntValue>)Parameters[MaximumIterationsParameterName]; }
71    }
72    public IFixedValueParameter<IntValue> MaximumEvaluationsParameter {
73      get { return (IFixedValueParameter<IntValue>)Parameters[MaximumEvaluationsParameterName]; }
74    }
75    public IFixedValueParameter<IntValue> MaximumRuntimeParameter {
76      get { return (IFixedValueParameter<IntValue>)Parameters[MaximumRuntimeParameterName]; }
77    }
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
85
86    #region Properties
87    public int MaximumIterations {
88      get { return MaximumIterationsParameter.Value.Value; }
89      set { MaximumIterationsParameter.Value.Value = value; }
90    }
91    public int MaximumEvaluations {
92      get { return MaximumEvaluationsParameter.Value.Value; }
93      set { MaximumEvaluationsParameter.Value.Value = value; }
94    }
95    public int MaximumRuntime {
96      get { return MaximumRuntimeParameter.Value.Value; }
97      set { MaximumRuntimeParameter.Value.Value = value; }
98    }
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    }
107    #endregion
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    }
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    }
153    #endregion
154
155    public override bool SupportsPause { get { return true; } }
156
157    [StorableConstructor]
158    protected ParameterlessPopulationPyramid(bool deserializing) : base(deserializing) { }
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() {
169      Parameters.Add(new FixedValueParameter<IntValue>(MaximumIterationsParameterName, "", new IntValue(Int32.MaxValue)));
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)));
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)));
174    }
175
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
183    private void AddIfUnique(BinaryVector solution, int level) {
184      // Don't add things you have seen
185      if (seen.Contains(solution)) return;
186      if (level == pyramid.Count) {
187        pyramid.Add(new Population(tracker.Length, random));
188      }
189      var copied = (BinaryVector)solution.Clone();
190      pyramid[level].Add(copied);
191      seen.Add(copied);
192    }
193
194    // In the GECCO paper, Figure 1
195    private double iterate() {
196      // Create a random solution
197      BinaryVector solution = new BinaryVector(tracker.Length);
198      for (int i = 0; i < solution.Length; i++) {
199        solution[i] = random.Next(2) == 1;
200      }
201      double fitness = tracker.Evaluate(solution, random);
202      fitness = HillClimber.ImproveToLocalOptimum(tracker, solution, fitness, random);
203      AddIfUnique(solution, 0);
204
205      for (int level = 0; level < pyramid.Count; level++) {
206        var current = pyramid[level];
207        double newFitness = LinkageCrossover.ImproveUsingTree(current.Tree, current.Solutions, solution, fitness, tracker, random);
208        // add it to the next level if its a strict fitness improvement
209        if (tracker.IsBetter(newFitness, fitness)) {
210          fitness = newFitness;
211          AddIfUnique(solution, level + 1);
212        }
213      }
214      return fitness;
215    }
216
217    protected override void Initialize(CancellationToken cancellationToken) {
218      // Set up the algorithm
219      if (SetSeedRandomly) Seed = new System.Random().Next();
220      pyramid = new List<Population>();
221      seen.Clear();
222      random.Reset(Seed);
223      tracker = new EvaluationTracker(Problem, MaximumEvaluations);
224
225      // Set up the results display
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));
237
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
246      base.Initialize(cancellationToken);
247    }
248
249    protected override void Run(CancellationToken cancellationToken) {
250      // Loop until iteration limit reached or canceled.
251      while (ResultsIterations < MaximumIterations) {
252        double fitness = double.NaN;
253
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        }
268      }
269    }
270  }
271}
Note: See TracBrowser for help on using the repository browser.