[11666] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[16723] | 3 | * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[11838] | 4 | * and the BEACON Center for the Study of Evolution in Action.
|
---|
[11666] | 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;
|
---|
[13361] | 24 | using System.Collections.Generic;
|
---|
[16692] | 25 | using HeuristicLab.Common;
|
---|
[11987] | 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Encodings.BinaryVectorEncoding;
|
---|
[13339] | 28 | using HeuristicLab.Optimization;
|
---|
[16723] | 29 | using HEAL.Attic;
|
---|
[11666] | 30 |
|
---|
| 31 | namespace HeuristicLab.Algorithms.ParameterlessPopulationPyramid {
|
---|
[11838] | 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
|
---|
[16723] | 35 | [StorableType("D5F1358D-C100-40CF-9BA5-E95F72F64D1A")]
|
---|
[16692] | 36 | internal sealed class EvaluationTracker : Item, ISingleObjectiveProblemDefinition<BinaryVectorEncoding, BinaryVector> {
|
---|
| 37 | [Storable]
|
---|
| 38 | private SingleObjectiveProblem<BinaryVectorEncoding, BinaryVector> problem;
|
---|
| 39 | [Storable]
|
---|
[11666] | 40 | private int maxEvaluations;
|
---|
| 41 |
|
---|
[11669] | 42 | #region Properties
|
---|
[16692] | 43 | [Storable]
|
---|
[11666] | 44 | public double BestQuality {
|
---|
[11669] | 45 | get;
|
---|
| 46 | private set;
|
---|
[11666] | 47 | }
|
---|
[16692] | 48 | [Storable]
|
---|
[11666] | 49 | public int Evaluations {
|
---|
[11669] | 50 | get;
|
---|
| 51 | private set;
|
---|
[11666] | 52 | }
|
---|
[16692] | 53 | [Storable]
|
---|
[11666] | 54 | public int BestFoundOnEvaluation {
|
---|
[11669] | 55 | get;
|
---|
| 56 | private set;
|
---|
[11666] | 57 | }
|
---|
[16692] | 58 | [Storable]
|
---|
[11987] | 59 | public BinaryVector BestSolution {
|
---|
[11669] | 60 | get;
|
---|
| 61 | private set;
|
---|
[11666] | 62 | }
|
---|
[13339] | 63 |
|
---|
[13361] | 64 | public BinaryVectorEncoding Encoding {
|
---|
[13339] | 65 | get { return problem.Encoding; }
|
---|
| 66 | }
|
---|
[11669] | 67 | #endregion
|
---|
[11666] | 68 |
|
---|
[16692] | 69 |
|
---|
| 70 | [StorableConstructor]
|
---|
[16723] | 71 | private EvaluationTracker(StorableConstructorFlag _) : base(_) { }
|
---|
[16692] | 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) {
|
---|
[11666] | 87 | this.problem = problem;
|
---|
| 88 | this.maxEvaluations = maxEvaluations;
|
---|
[13339] | 89 | BestSolution = new BinaryVector(problem.Encoding.Length);
|
---|
[11669] | 90 | BestQuality = double.NaN;
|
---|
| 91 | Evaluations = 0;
|
---|
| 92 | BestFoundOnEvaluation = 0;
|
---|
[11666] | 93 | }
|
---|
| 94 |
|
---|
[13361] | 95 |
|
---|
| 96 |
|
---|
| 97 | public double Evaluate(BinaryVector vector, IRandom random) {
|
---|
[11669] | 98 | if (Evaluations >= maxEvaluations) throw new OperationCanceledException("Maximum Evaluation Limit Reached");
|
---|
| 99 | Evaluations++;
|
---|
[11987] | 100 | double fitness = problem.Evaluate(vector, random);
|
---|
[11669] | 101 | if (double.IsNaN(BestQuality) || problem.IsBetter(fitness, BestQuality)) {
|
---|
| 102 | BestQuality = fitness;
|
---|
[11987] | 103 | BestSolution = (BinaryVector)vector.Clone();
|
---|
[11669] | 104 | BestFoundOnEvaluation = Evaluations;
|
---|
[11666] | 105 | }
|
---|
| 106 | return fitness;
|
---|
| 107 | }
|
---|
| 108 |
|
---|
[13361] | 109 | public bool Maximization {
|
---|
[11999] | 110 | get {
|
---|
| 111 | if (problem == null) return false;
|
---|
| 112 | return problem.Maximization;
|
---|
| 113 | }
|
---|
[11666] | 114 | }
|
---|
[11987] | 115 |
|
---|
[13361] | 116 | public bool IsBetter(double quality, double bestQuality) {
|
---|
[11666] | 117 | return problem.IsBetter(quality, bestQuality);
|
---|
[11669] | 118 | }
|
---|
[11987] | 119 |
|
---|
[13361] | 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 | }
|
---|
[11666] | 127 | }
|
---|
| 128 | }
|
---|