Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Parameter-less Population Pyramid/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/Problems/EvaluationTracker.cs @ 11669

Last change on this file since 11669 was 11669, checked in by bgoldman, 9 years ago

#2282 Code cleanup, added Deceptive Step Trap problem.

File size: 2.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24
25namespace HeuristicLab.Algorithms.ParameterlessPopulationPyramid {
26  public class EvaluationTracker : IBinaryVectorProblem {
27    private readonly IBinaryVectorProblem problem;
28
29    private int maxEvaluations;
30
31    #region Properties
32    public double BestQuality {
33      get;
34      private set;
35    }
36
37    public int Evaluations {
38      get;
39      private set;
40    }
41
42    public int BestFoundOnEvaluation {
43      get;
44      private set;
45    }
46
47    public bool[] BestSolution {
48      get;
49      private set;
50    }
51    #endregion
52
53    public EvaluationTracker(IBinaryVectorProblem problem, int maxEvaluations) {
54      this.problem = problem;
55      this.maxEvaluations = maxEvaluations;
56      BestSolution = new bool[0];
57      BestQuality = double.NaN;
58      Evaluations = 0;
59      BestFoundOnEvaluation = 0;
60    }
61
62    public double Evaluate(bool[] individual) {
63      if (Evaluations >= maxEvaluations) throw new OperationCanceledException("Maximum Evaluation Limit Reached");
64      Evaluations++;
65      double fitness = problem.Evaluate(individual);
66      if (double.IsNaN(BestQuality) || problem.IsBetter(fitness, BestQuality)) {
67        BestQuality = fitness;
68        BestSolution = (bool[])individual.Clone();
69        BestFoundOnEvaluation = Evaluations;
70      }
71      return fitness;
72    }
73
74    #region ForwardedInteraface
75    public int Length {
76      get { return problem.Length; }
77    }
78    public bool Maximization {
79      get { return problem.Maximization; }
80    }
81    public bool IsBetter(double quality, double bestQuality) {
82      return problem.IsBetter(quality, bestQuality);
83    }
84    #endregion
85  }
86}
Note: See TracBrowser for help on using the repository browser.