Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ProblemRefactoring/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/EvaluationTracker.cs @ 13339

Last change on this file since 13339 was 13339, checked in by mkommend, 8 years ago

#2521: Rectored problems and encodings.

File size: 4.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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 HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.BinaryVectorEncoding;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Algorithms.ParameterlessPopulationPyramid {
33  // This code is based off the publication
34  // B. W. Goldman and W. F. Punch, "Parameter-less Population Pyramid," GECCO, pp. 785–792, 2014
35  // and the original source code in C++11 available from: https://github.com/brianwgoldman/Parameter-less_Population_Pyramid
36  internal sealed class EvaluationTracker : SingleObjectiveProblem<BinaryVectorEncoding, BinaryVector> {
37    private readonly ISingleObjectiveProblem<BinaryVectorEncoding, BinaryVector> problem;
38
39    private int maxEvaluations;
40
41    #region Properties
42    public double BestQuality {
43      get;
44      private set;
45    }
46
47    public int Evaluations {
48      get;
49      private set;
50    }
51
52    public int BestFoundOnEvaluation {
53      get;
54      private set;
55    }
56
57    public BinaryVector BestSolution {
58      get;
59      private set;
60    }
61
62    public new BinaryVectorEncoding Encoding {
63      get { return problem.Encoding; }
64    }
65    #endregion
66
67    [StorableConstructor]
68    private EvaluationTracker(bool deserializing) : base(deserializing) { }
69    private EvaluationTracker(EvaluationTracker original, Cloner cloner)
70      : base(original, cloner) {
71      problem = cloner.Clone(original.problem);
72      maxEvaluations = original.maxEvaluations;
73      BestQuality = original.BestQuality;
74      Evaluations = original.Evaluations;
75      BestFoundOnEvaluation = original.BestFoundOnEvaluation;
76      BestSolution = cloner.Clone(BestSolution);
77    }
78    public override IDeepCloneable Clone(Cloner cloner) {
79      return new EvaluationTracker(this, cloner);
80    }
81    public EvaluationTracker(ISingleObjectiveProblem<BinaryVectorEncoding, BinaryVector> problem, int maxEvaluations) {
82      this.problem = problem;
83      this.maxEvaluations = maxEvaluations;
84      BestSolution = new BinaryVector(problem.Encoding.Length);
85      BestQuality = double.NaN;
86      Evaluations = 0;
87      BestFoundOnEvaluation = 0;
88
89      if (Parameters.ContainsKey("Maximization")) Parameters.Remove("Maximization");
90      Parameters.Add(new FixedValueParameter<BoolValue>("Maximization", "Set to false if the problem should be minimized.", (BoolValue)new BoolValue(Maximization).AsReadOnly()) { Hidden = true });
91    }
92
93    public override double Evaluate(BinaryVector vector, IRandom random) {
94      if (Evaluations >= maxEvaluations) throw new OperationCanceledException("Maximum Evaluation Limit Reached");
95      Evaluations++;
96      double fitness = problem.Evaluate(vector, random);
97      if (double.IsNaN(BestQuality) || problem.IsBetter(fitness, BestQuality)) {
98        BestQuality = fitness;
99        BestSolution = (BinaryVector)vector.Clone();
100        BestFoundOnEvaluation = Evaluations;
101      }
102      return fitness;
103    }
104
105    public override bool Maximization {
106      get {
107        if (problem == null) return false;
108        return problem.Maximization;
109      }
110    }
111
112    public override bool IsBetter(double quality, double bestQuality) {
113      return problem.IsBetter(quality, bestQuality);
114    }
115
116  }
117}
Note: See TracBrowser for help on using the repository browser.