Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3044_variableScaling/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/EvaluationTracker.cs

Last change on this file was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 4.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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.Parameters;
29using HEAL.Attic;
30using HeuristicLab.Problems.Binary;
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  [StorableType("D5F1358D-C100-40CF-9BA5-E95F72F64D1A")]
37  internal sealed class EvaluationTracker : BinaryProblem {
38    [Storable]
39    private readonly BinaryProblem problem;
40    [Storable]
41    private int maxEvaluations;
42
43    #region Properties
44    [Storable]
45    public double BestQuality {
46      get;
47      private set;
48    }
49    [Storable]
50    public int Evaluations {
51      get;
52      private set;
53    }
54    [Storable]
55    public int BestFoundOnEvaluation {
56      get;
57      private set;
58    }
59    [Storable]
60    public BinaryVector BestSolution {
61      get;
62      private set;
63    }
64    #endregion
65
66
67    [StorableConstructor]
68    private EvaluationTracker(StorableConstructorFlag _) : base(_) { }
69
70    private EvaluationTracker(EvaluationTracker original, Cloner cloner)
71      : base(original, cloner) {
72      problem = cloner.Clone(original.problem);
73      maxEvaluations = original.maxEvaluations;
74      BestQuality = original.BestQuality;
75      Evaluations = original.Evaluations;
76      BestFoundOnEvaluation = original.BestFoundOnEvaluation;
77      BestSolution = cloner.Clone(original.BestSolution);
78    }
79    public override IDeepCloneable Clone(Cloner cloner) {
80      return new EvaluationTracker(this, cloner);
81    }
82
83    public EvaluationTracker(BinaryProblem problem, int maxEvaluations) {
84      this.problem = problem;
85      this.maxEvaluations = maxEvaluations;
86      BestSolution = new BinaryVector(Length);
87      BestQuality = double.NaN;
88      Evaluations = 0;
89      BestFoundOnEvaluation = 0;
90
91      if (Parameters.ContainsKey("Maximization")) Parameters.Remove("Maximization");
92      Parameters.Add(new FixedValueParameter<BoolValue>("Maximization", "Set to false if the problem should be minimized.", (BoolValue)new BoolValue(Maximization).AsReadOnly()) { Hidden = true });
93    }
94
95    public override double Evaluate(BinaryVector vector, IRandom random) {
96      if (Evaluations >= maxEvaluations) throw new OperationCanceledException("Maximum Evaluation Limit Reached");
97      Evaluations++;
98      double fitness = problem.Evaluate(vector, random);
99      if (double.IsNaN(BestQuality) || problem.IsBetter(fitness, BestQuality)) {
100        BestQuality = fitness;
101        BestSolution = (BinaryVector)vector.Clone();
102        BestFoundOnEvaluation = Evaluations;
103      }
104      return fitness;
105    }
106
107    public override int Length {
108      get { return problem.Length; }
109      set { problem.Length = value; }
110    }
111
112    public override bool Maximization {
113      get {
114        if (problem == null) return false;
115        return problem.Maximization;
116      }
117    }
118
119    public override bool IsBetter(double quality, double bestQuality) {
120      return problem.IsBetter(quality, bestQuality);
121    }
122
123  }
124}
Note: See TracBrowser for help on using the repository browser.