Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/EvaluationTracker.cs @ 15802

Last change on this file since 15802 was 15584, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers on stable

File size: 4.2 KB
RevLine 
[11666]1#region License Information
2/* HeuristicLab
[15584]3 * Copyright (C) 2002-2018 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;
[12005]24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.BinaryVectorEncoding;
28using HeuristicLab.Parameters;
[15324]29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[12005]30using HeuristicLab.Problems.Binary;
[11666]31
32namespace HeuristicLab.Algorithms.ParameterlessPopulationPyramid {
[11838]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
[15324]36  [StorableClass]
[12005]37  internal sealed class EvaluationTracker : BinaryProblem {
[15324]38    [Storable]
[12005]39    private readonly BinaryProblem problem;
[15324]40    [Storable]
[11666]41    private int maxEvaluations;
42
[11669]43    #region Properties
[15324]44    [Storable]
[11666]45    public double BestQuality {
[11669]46      get;
47      private set;
[11666]48    }
[15324]49    [Storable]
[11666]50    public int Evaluations {
[11669]51      get;
52      private set;
[11666]53    }
[15324]54    [Storable]
[11666]55    public int BestFoundOnEvaluation {
[11669]56      get;
57      private set;
[11666]58    }
[15324]59    [Storable]
[12005]60    public BinaryVector BestSolution {
[11669]61      get;
62      private set;
[11666]63    }
[11669]64    #endregion
[11666]65
[15324]66
67    [StorableConstructor]
68    private EvaluationTracker(bool deserializing) : base(deserializing) { }
69
[12005]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;
[15324]77      BestSolution = cloner.Clone(original.BestSolution);
[12005]78    }
79    public override IDeepCloneable Clone(Cloner cloner) {
80      return new EvaluationTracker(this, cloner);
81    }
[15324]82
[12005]83    public EvaluationTracker(BinaryProblem problem, int maxEvaluations) {
[11666]84      this.problem = problem;
85      this.maxEvaluations = maxEvaluations;
[12005]86      BestSolution = new BinaryVector(Length);
[11669]87      BestQuality = double.NaN;
88      Evaluations = 0;
89      BestFoundOnEvaluation = 0;
[12005]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 });
[11666]93    }
94
[12005]95    public override double Evaluate(BinaryVector vector, IRandom random) {
[11669]96      if (Evaluations >= maxEvaluations) throw new OperationCanceledException("Maximum Evaluation Limit Reached");
97      Evaluations++;
[12005]98      double fitness = problem.Evaluate(vector, random);
[11669]99      if (double.IsNaN(BestQuality) || problem.IsBetter(fitness, BestQuality)) {
100        BestQuality = fitness;
[12005]101        BestSolution = (BinaryVector)vector.Clone();
[11669]102        BestFoundOnEvaluation = Evaluations;
[11666]103      }
104      return fitness;
105    }
106
[12005]107    public override int Length {
[11666]108      get { return problem.Length; }
[12005]109      set { problem.Length = value; }
[11666]110    }
[12005]111
112    public override bool Maximization {
113      get {
114        if (problem == null) return false;
115        return problem.Maximization;
116      }
[11666]117    }
[12005]118
[12121]119    public override bool IsBetter(double quality, double bestQuality) {
[11666]120      return problem.IsBetter(quality, bestQuality);
[11669]121    }
[12005]122
[11666]123  }
124}
Note: See TracBrowser for help on using the repository browser.