Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/EvaluationTracker.cs @ 17226

Last change on this file since 17226 was 17226, checked in by mkommend, 5 years ago

#2521: Merged trunk changes into problem refactoring branch.

File size: 4.3 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 System.Collections.Generic;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Encodings.BinaryVectorEncoding;
28using HeuristicLab.Optimization;
29using HEAL.Attic;
30
31namespace HeuristicLab.Algorithms.ParameterlessPopulationPyramid {
32  // This code is based off the publication
33  // B. W. Goldman and W. F. Punch, "Parameter-less Population Pyramid," GECCO, pp. 785–792, 2014
34  // and the original source code in C++11 available from: https://github.com/brianwgoldman/Parameter-less_Population_Pyramid
35  [StorableType("D5F1358D-C100-40CF-9BA5-E95F72F64D1A")]
36  internal sealed class EvaluationTracker : Item, ISingleObjectiveProblemDefinition<BinaryVectorEncoding, BinaryVector> {
37    [Storable]
38    private SingleObjectiveProblem<BinaryVectorEncoding, BinaryVector> problem;
39    [Storable]
40    private int maxEvaluations;
41
42    #region Properties
43    [Storable]
44    public double BestQuality {
45      get;
46      private set;
47    }
48    [Storable]
49    public int Evaluations {
50      get;
51      private set;
52    }
53    [Storable]
54    public int BestFoundOnEvaluation {
55      get;
56      private set;
57    }
58    [Storable]
59    public BinaryVector BestSolution {
60      get;
61      private set;
62    }
63
64    public BinaryVectorEncoding Encoding {
65      get { return problem.Encoding; }
66    }
67    #endregion
68
69
70    [StorableConstructor]
71    private EvaluationTracker(StorableConstructorFlag _) : base(_) { }
72
73    private EvaluationTracker(EvaluationTracker original, Cloner cloner)
74      : base(original, cloner) {
75      problem = cloner.Clone(original.problem);
76      maxEvaluations = original.maxEvaluations;
77      BestQuality = original.BestQuality;
78      Evaluations = original.Evaluations;
79      BestFoundOnEvaluation = original.BestFoundOnEvaluation;
80      BestSolution = cloner.Clone(original.BestSolution);
81    }
82    public override IDeepCloneable Clone(Cloner cloner) {
83      return new EvaluationTracker(this, cloner);
84    }
85
86    public EvaluationTracker(SingleObjectiveProblem<BinaryVectorEncoding, BinaryVector> problem, int maxEvaluations) {
87      this.problem = problem;
88      this.maxEvaluations = maxEvaluations;
89      BestSolution = new BinaryVector(problem.Encoding.Length);
90      BestQuality = double.NaN;
91      Evaluations = 0;
92      BestFoundOnEvaluation = 0;
93    }
94
95
96
97    public double Evaluate(BinaryVector vector, IRandom random) {
98      if (Evaluations >= maxEvaluations) throw new OperationCanceledException("Maximum Evaluation Limit Reached");
99      Evaluations++;
100      double fitness = problem.Evaluate(vector, random);
101      if (double.IsNaN(BestQuality) || problem.IsBetter(fitness, BestQuality)) {
102        BestQuality = fitness;
103        BestSolution = (BinaryVector)vector.Clone();
104        BestFoundOnEvaluation = Evaluations;
105      }
106      return fitness;
107    }
108
109    public bool Maximization {
110      get {
111        if (problem == null) return false;
112        return problem.Maximization;
113      }
114    }
115
116    public bool IsBetter(double quality, double bestQuality) {
117      return problem.IsBetter(quality, bestQuality);
118    }
119
120    public void Analyze(BinaryVector[] individuals, double[] qualities, ResultCollection results, IRandom random) {
121      problem.Analyze(individuals, qualities, results, random);
122    }
123
124    public IEnumerable<BinaryVector> GetNeighbors(BinaryVector individual, IRandom random) {
125      return problem.GetNeighbors(individual, random);
126    }
127  }
128}
Note: See TracBrowser for help on using the repository browser.