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 |
|
---|
23 | using System;
|
---|
24 | using System.Collections.Generic;
|
---|
25 | using System.Threading;
|
---|
26 | using HEAL.Attic;
|
---|
27 | using HeuristicLab.Common;
|
---|
28 | using HeuristicLab.Core;
|
---|
29 | using HeuristicLab.Encodings.BinaryVectorEncoding;
|
---|
30 | using HeuristicLab.Optimization;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Algorithms.ParameterlessPopulationPyramid {
|
---|
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
|
---|
36 | [StorableType("D5F1358D-C100-40CF-9BA5-E95F72F64D1A")]
|
---|
37 | internal sealed class EvaluationTracker : Item, ISingleObjectiveProblemDefinition<BinaryVectorEncoding, BinaryVector> {
|
---|
38 | [Storable]
|
---|
39 | private SingleObjectiveProblem<BinaryVectorEncoding, BinaryVector> problem;
|
---|
40 | [Storable]
|
---|
41 | private int maxEvaluations;
|
---|
42 |
|
---|
43 | #region Properties
|
---|
44 | [Storable]
|
---|
45 | public double BestQuality {
|
---|
46 | get;
|
---|
47 | private set;
|
---|
48 | }
|
---|
49 | [Storable]
|
---|
50 | public int Evaluations {
|
---|
51 | get;
|
---|
52 | private set;
|
---|
53 | }
|
---|
54 | [Storable]
|
---|
55 | public int BestFoundOnEvaluation {
|
---|
56 | get;
|
---|
57 | private set;
|
---|
58 | }
|
---|
59 | [Storable]
|
---|
60 | public BinaryVector BestSolution {
|
---|
61 | get;
|
---|
62 | private set;
|
---|
63 | }
|
---|
64 |
|
---|
65 | public BinaryVectorEncoding Encoding {
|
---|
66 | get { return problem.Encoding; }
|
---|
67 | }
|
---|
68 | #endregion
|
---|
69 |
|
---|
70 |
|
---|
71 | [StorableConstructor]
|
---|
72 | private EvaluationTracker(StorableConstructorFlag _) : base(_) { }
|
---|
73 |
|
---|
74 | private EvaluationTracker(EvaluationTracker original, Cloner cloner)
|
---|
75 | : base(original, cloner) {
|
---|
76 | problem = cloner.Clone(original.problem);
|
---|
77 | maxEvaluations = original.maxEvaluations;
|
---|
78 | BestQuality = original.BestQuality;
|
---|
79 | Evaluations = original.Evaluations;
|
---|
80 | BestFoundOnEvaluation = original.BestFoundOnEvaluation;
|
---|
81 | BestSolution = cloner.Clone(original.BestSolution);
|
---|
82 | }
|
---|
83 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
84 | return new EvaluationTracker(this, cloner);
|
---|
85 | }
|
---|
86 |
|
---|
87 | public EvaluationTracker(SingleObjectiveProblem<BinaryVectorEncoding, BinaryVector> problem, int maxEvaluations) {
|
---|
88 | this.problem = problem;
|
---|
89 | this.maxEvaluations = maxEvaluations;
|
---|
90 | BestSolution = new BinaryVector(problem.Encoding.Length);
|
---|
91 | BestQuality = double.NaN;
|
---|
92 | Evaluations = 0;
|
---|
93 | BestFoundOnEvaluation = 0;
|
---|
94 | }
|
---|
95 |
|
---|
96 | public double Evaluate(BinaryVector vector, IRandom random) {
|
---|
97 | return Evaluate(vector, random, CancellationToken.None);
|
---|
98 | }
|
---|
99 |
|
---|
100 | public double Evaluate(BinaryVector vector, IRandom random, CancellationToken cancellationToken) {
|
---|
101 | if (Evaluations >= maxEvaluations) throw new OperationCanceledException("Maximum Evaluation Limit Reached");
|
---|
102 | Evaluations++;
|
---|
103 | double fitness = problem.Evaluate(vector, random);
|
---|
104 | if (double.IsNaN(BestQuality) || problem.IsBetter(fitness, BestQuality)) {
|
---|
105 | BestQuality = fitness;
|
---|
106 | BestSolution = (BinaryVector)vector.Clone();
|
---|
107 | BestFoundOnEvaluation = Evaluations;
|
---|
108 | }
|
---|
109 | return fitness;
|
---|
110 | }
|
---|
111 |
|
---|
112 | public bool Maximization {
|
---|
113 | get {
|
---|
114 | if (problem == null) return false;
|
---|
115 | return problem.Maximization;
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|
119 | public bool IsBetter(double quality, double bestQuality) {
|
---|
120 | return problem.IsBetter(quality, bestQuality);
|
---|
121 | }
|
---|
122 |
|
---|
123 | public void Analyze(BinaryVector[] individuals, double[] qualities, ResultCollection results, IRandom random) {
|
---|
124 | problem.Analyze(individuals, qualities, results, random);
|
---|
125 | }
|
---|
126 |
|
---|
127 | public IEnumerable<BinaryVector> GetNeighbors(BinaryVector individual, IRandom random) {
|
---|
128 | return problem.GetNeighbors(individual, random);
|
---|
129 | }
|
---|
130 | }
|
---|
131 | }
|
---|