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