Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/EvaluationTracker.cs @ 11987

Last change on this file since 11987 was 11987, checked in by abeham, 9 years ago

#2282:

  • Renamed BinaryVectorProblem to BinaryProblem
  • Removed IBinaryVectorProblem interface
  • Derived BinaryProblem from SingleObjectiveBasicProblem
  • Changed bool[] datatype to BinaryVector
File size: 3.7 KB
RevLine 
[11666]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[11838]4 * and the BEACON Center for the Study of Evolution in Action.
[11666]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;
[11987]24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Encodings.BinaryVectorEncoding;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using HeuristicLab.Problems.Binary;
[11666]29
30namespace HeuristicLab.Algorithms.ParameterlessPopulationPyramid {
[11838]31  // This code is based off the publication
32  // B. W. Goldman and W. F. Punch, "Parameter-less Population Pyramid," GECCO, pp. 785–792, 2014
33  // and the original source code in C++11 available from: https://github.com/brianwgoldman/Parameter-less_Population_Pyramid
[11987]34  internal sealed class EvaluationTracker : BinaryProblem {
35    private readonly BinaryProblem problem;
[11669]36
[11666]37    private int maxEvaluations;
38
[11669]39    #region Properties
[11666]40    public double BestQuality {
[11669]41      get;
42      private set;
[11666]43    }
44
45    public int Evaluations {
[11669]46      get;
47      private set;
[11666]48    }
49
50    public int BestFoundOnEvaluation {
[11669]51      get;
52      private set;
[11666]53    }
54
[11987]55    public BinaryVector BestSolution {
[11669]56      get;
57      private set;
[11666]58    }
[11669]59    #endregion
[11666]60
[11987]61    [StorableConstructor]
62    private EvaluationTracker(bool deserializing) : base(deserializing) { }
63    private EvaluationTracker(EvaluationTracker original, Cloner cloner)
64      : base(original, cloner) {
65      problem = cloner.Clone(original.problem);
66      maxEvaluations = original.maxEvaluations;
67      BestQuality = original.BestQuality;
68      Evaluations = original.Evaluations;
69      BestFoundOnEvaluation = original.BestFoundOnEvaluation;
70      BestSolution = cloner.Clone(BestSolution);
71    }
72    public override IDeepCloneable Clone(Cloner cloner) {
73      return new EvaluationTracker(this, cloner);
74    }
75    public EvaluationTracker(BinaryProblem problem, int maxEvaluations) {
[11666]76      this.problem = problem;
77      this.maxEvaluations = maxEvaluations;
[11987]78      BestSolution = new BinaryVector(Length);
[11669]79      BestQuality = double.NaN;
80      Evaluations = 0;
81      BestFoundOnEvaluation = 0;
[11666]82    }
83
[11987]84
85
86    public override double Evaluate(BinaryVector vector, IRandom random) {
[11669]87      if (Evaluations >= maxEvaluations) throw new OperationCanceledException("Maximum Evaluation Limit Reached");
88      Evaluations++;
[11987]89      double fitness = problem.Evaluate(vector, random);
[11669]90      if (double.IsNaN(BestQuality) || problem.IsBetter(fitness, BestQuality)) {
91        BestQuality = fitness;
[11987]92        BestSolution = (BinaryVector)vector.Clone();
[11669]93        BestFoundOnEvaluation = Evaluations;
[11666]94      }
95      return fitness;
96    }
97
[11987]98    public override int Length {
[11666]99      get { return problem.Length; }
[11987]100      set { problem.Length = value; }
[11666]101    }
[11987]102
103    public override bool Maximization {
[11666]104      get { return problem.Maximization; }
105    }
[11987]106
[11666]107    public bool IsBetter(double quality, double bestQuality) {
108      return problem.IsBetter(quality, bestQuality);
[11669]109    }
[11987]110
[11666]111  }
112}
Note: See TracBrowser for help on using the repository browser.