Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Async/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/EvaluationTracker.cs @ 14714

Last change on this file since 14714 was 12057, checked in by mkommend, 10 years ago

#2337: Corrected build warning in ParameterlessPopulationPyramid.

File size: 4.1 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.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
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  internal sealed class EvaluationTracker : BinaryProblem {
37    private readonly BinaryProblem 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    #endregion
62
63    [StorableConstructor]
64    private EvaluationTracker(bool deserializing) : base(deserializing) { }
65    private EvaluationTracker(EvaluationTracker original, Cloner cloner)
66      : base(original, cloner) {
67      problem = cloner.Clone(original.problem);
68      maxEvaluations = original.maxEvaluations;
69      BestQuality = original.BestQuality;
70      Evaluations = original.Evaluations;
71      BestFoundOnEvaluation = original.BestFoundOnEvaluation;
72      BestSolution = cloner.Clone(BestSolution);
73    }
74    public override IDeepCloneable Clone(Cloner cloner) {
75      return new EvaluationTracker(this, cloner);
76    }
77    public EvaluationTracker(BinaryProblem problem, int maxEvaluations) {
78      this.problem = problem;
79      this.maxEvaluations = maxEvaluations;
80      BestSolution = new BinaryVector(Length);
81      BestQuality = double.NaN;
82      Evaluations = 0;
83      BestFoundOnEvaluation = 0;
84
85      if (Parameters.ContainsKey("Maximization")) Parameters.Remove("Maximization");
86      Parameters.Add(new FixedValueParameter<BoolValue>("Maximization", "Set to false if the problem should be minimized.", (BoolValue)new BoolValue(Maximization).AsReadOnly()) { Hidden = true });
87    }
88
89    public override double Evaluate(BinaryVector vector, IRandom random) {
90      if (Evaluations >= maxEvaluations) throw new OperationCanceledException("Maximum Evaluation Limit Reached");
91      Evaluations++;
92      double fitness = problem.Evaluate(vector, random);
93      if (double.IsNaN(BestQuality) || problem.IsBetter(fitness, BestQuality)) {
94        BestQuality = fitness;
95        BestSolution = (BinaryVector)vector.Clone();
96        BestFoundOnEvaluation = Evaluations;
97      }
98      return fitness;
99    }
100
101    public override int Length {
102      get { return problem.Length; }
103      set { problem.Length = value; }
104    }
105
106    public override bool Maximization {
107      get {
108        if (problem == null) return false;
109        return problem.Maximization;
110      }
111    }
112
113    public override bool IsBetter(double quality, double bestQuality) {
114      return problem.IsBetter(quality, bestQuality);
115    }
116
117  }
118}
Note: See TracBrowser for help on using the repository browser.