Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ProblemRefactoring/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/EvaluationTracker.cs @ 14778

Last change on this file since 14778 was 13361, checked in by mkommend, 9 years ago

#2521: Adapted real vector encoding, test function problems, P3, CMA-ES and optimization.

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