Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17035 was 16565, checked in by gkronber, 6 years ago

#2520: merged changes from PersistenceOverhaul branch (r16451:16564) into trunk

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