[11666] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12009] | 3 | * Copyright (C) 2002-2015 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;
|
---|
[12005] | 24 | using HeuristicLab.Common;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Data;
|
---|
| 27 | using HeuristicLab.Encodings.BinaryVectorEncoding;
|
---|
| 28 | using HeuristicLab.Parameters;
|
---|
| 29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 30 | using HeuristicLab.Problems.Binary;
|
---|
[11666] | 31 |
|
---|
| 32 | namespace HeuristicLab.Algorithms.ParameterlessPopulationPyramid {
|
---|
[11838] | 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
|
---|
[12005] | 36 | internal sealed class EvaluationTracker : BinaryProblem {
|
---|
| 37 | private readonly BinaryProblem problem;
|
---|
[11669] | 38 |
|
---|
[11666] | 39 | private int maxEvaluations;
|
---|
| 40 |
|
---|
[11669] | 41 | #region Properties
|
---|
[11666] | 42 | public double BestQuality {
|
---|
[11669] | 43 | get;
|
---|
| 44 | private set;
|
---|
[11666] | 45 | }
|
---|
| 46 |
|
---|
| 47 | public int Evaluations {
|
---|
[11669] | 48 | get;
|
---|
| 49 | private set;
|
---|
[11666] | 50 | }
|
---|
| 51 |
|
---|
| 52 | public int BestFoundOnEvaluation {
|
---|
[11669] | 53 | get;
|
---|
| 54 | private set;
|
---|
[11666] | 55 | }
|
---|
| 56 |
|
---|
[12005] | 57 | public BinaryVector BestSolution {
|
---|
[11669] | 58 | get;
|
---|
| 59 | private set;
|
---|
[11666] | 60 | }
|
---|
[11669] | 61 | #endregion
|
---|
[11666] | 62 |
|
---|
[12005] | 63 | [StorableConstructor]
|
---|
| 64 | private EvaluationTracker(bool deserializing) : base(deserializing) { }
|
---|
| 65 | private EvaluationTracker(EvaluationTracker original, Cloner cloner)
|
---|
| 66 | : base(original, cloner) {
|
---|
| 67 | problem = cloner.Clone(original.problem);
|
---|
| 68 | maxEvaluations = original.maxEvaluations;
|
---|
| 69 | BestQuality = original.BestQuality;
|
---|
| 70 | Evaluations = original.Evaluations;
|
---|
| 71 | BestFoundOnEvaluation = original.BestFoundOnEvaluation;
|
---|
| 72 | BestSolution = cloner.Clone(BestSolution);
|
---|
| 73 | }
|
---|
| 74 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 75 | return new EvaluationTracker(this, cloner);
|
---|
| 76 | }
|
---|
| 77 | public EvaluationTracker(BinaryProblem problem, int maxEvaluations) {
|
---|
[11666] | 78 | this.problem = problem;
|
---|
| 79 | this.maxEvaluations = maxEvaluations;
|
---|
[12005] | 80 | BestSolution = new BinaryVector(Length);
|
---|
[11669] | 81 | BestQuality = double.NaN;
|
---|
| 82 | Evaluations = 0;
|
---|
| 83 | BestFoundOnEvaluation = 0;
|
---|
[12005] | 84 |
|
---|
| 85 | if (Parameters.ContainsKey("Maximization")) Parameters.Remove("Maximization");
|
---|
| 86 | Parameters.Add(new FixedValueParameter<BoolValue>("Maximization", "Set to false if the problem should be minimized.", (BoolValue)new BoolValue(Maximization).AsReadOnly()) { Hidden = true });
|
---|
[11666] | 87 | }
|
---|
| 88 |
|
---|
[12005] | 89 | public override double Evaluate(BinaryVector vector, IRandom random) {
|
---|
[11669] | 90 | if (Evaluations >= maxEvaluations) throw new OperationCanceledException("Maximum Evaluation Limit Reached");
|
---|
| 91 | Evaluations++;
|
---|
[12005] | 92 | double fitness = problem.Evaluate(vector, random);
|
---|
[11669] | 93 | if (double.IsNaN(BestQuality) || problem.IsBetter(fitness, BestQuality)) {
|
---|
| 94 | BestQuality = fitness;
|
---|
[12005] | 95 | BestSolution = (BinaryVector)vector.Clone();
|
---|
[11669] | 96 | BestFoundOnEvaluation = Evaluations;
|
---|
[11666] | 97 | }
|
---|
| 98 | return fitness;
|
---|
| 99 | }
|
---|
| 100 |
|
---|
[12005] | 101 | public override int Length {
|
---|
[11666] | 102 | get { return problem.Length; }
|
---|
[12005] | 103 | set { problem.Length = value; }
|
---|
[11666] | 104 | }
|
---|
[12005] | 105 |
|
---|
| 106 | public override bool Maximization {
|
---|
| 107 | get {
|
---|
| 108 | if (problem == null) return false;
|
---|
| 109 | return problem.Maximization;
|
---|
| 110 | }
|
---|
[11666] | 111 | }
|
---|
[12005] | 112 |
|
---|
[11666] | 113 | public bool IsBetter(double quality, double bestQuality) {
|
---|
| 114 | return problem.IsBetter(quality, bestQuality);
|
---|
[11669] | 115 | }
|
---|
[12005] | 116 |
|
---|
[11666] | 117 | }
|
---|
| 118 | }
|
---|