1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2013 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 |
|
---|
23 | using System;
|
---|
24 | using HeuristicLab.Problems.BinaryVector;
|
---|
25 |
|
---|
26 | namespace HeuristicLab.Algorithms.ParameterlessPopulationPyramid {
|
---|
27 | // This code is based off the publication
|
---|
28 | // B. W. Goldman and W. F. Punch, "Parameter-less Population Pyramid," GECCO, pp. 785–792, 2014
|
---|
29 | // and the original source code in C++11 available from: https://github.com/brianwgoldman/Parameter-less_Population_Pyramid
|
---|
30 | public class EvaluationTracker : IBinaryVectorProblem {
|
---|
31 | private readonly IBinaryVectorProblem problem;
|
---|
32 |
|
---|
33 | private int maxEvaluations;
|
---|
34 |
|
---|
35 | #region Properties
|
---|
36 | public double BestQuality {
|
---|
37 | get;
|
---|
38 | private set;
|
---|
39 | }
|
---|
40 |
|
---|
41 | public int Evaluations {
|
---|
42 | get;
|
---|
43 | private set;
|
---|
44 | }
|
---|
45 |
|
---|
46 | public int BestFoundOnEvaluation {
|
---|
47 | get;
|
---|
48 | private set;
|
---|
49 | }
|
---|
50 |
|
---|
51 | public bool[] BestSolution {
|
---|
52 | get;
|
---|
53 | private set;
|
---|
54 | }
|
---|
55 | #endregion
|
---|
56 |
|
---|
57 | public EvaluationTracker(IBinaryVectorProblem problem, int maxEvaluations) {
|
---|
58 | this.problem = problem;
|
---|
59 | this.maxEvaluations = maxEvaluations;
|
---|
60 | BestSolution = new bool[0];
|
---|
61 | BestQuality = double.NaN;
|
---|
62 | Evaluations = 0;
|
---|
63 | BestFoundOnEvaluation = 0;
|
---|
64 | }
|
---|
65 |
|
---|
66 | public double Evaluate(bool[] individual) {
|
---|
67 | if (Evaluations >= maxEvaluations) throw new OperationCanceledException("Maximum Evaluation Limit Reached");
|
---|
68 | Evaluations++;
|
---|
69 | double fitness = problem.Evaluate(individual);
|
---|
70 | if (double.IsNaN(BestQuality) || problem.IsBetter(fitness, BestQuality)) {
|
---|
71 | BestQuality = fitness;
|
---|
72 | BestSolution = (bool[])individual.Clone();
|
---|
73 | BestFoundOnEvaluation = Evaluations;
|
---|
74 | }
|
---|
75 | return fitness;
|
---|
76 | }
|
---|
77 |
|
---|
78 | #region ForwardedInteraface
|
---|
79 | public int Length {
|
---|
80 | get { return problem.Length; }
|
---|
81 | }
|
---|
82 | public bool Maximization {
|
---|
83 | get { return problem.Maximization; }
|
---|
84 | }
|
---|
85 | public bool IsBetter(double quality, double bestQuality) {
|
---|
86 | return problem.IsBetter(quality, bestQuality);
|
---|
87 | }
|
---|
88 | #endregion
|
---|
89 | }
|
---|
90 | }
|
---|