[11666] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17226] | 3 | * Copyright (C) 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;
|
---|
[17363] | 25 | using System.Linq;
|
---|
[17320] | 26 | using System.Threading;
|
---|
| 27 | using HEAL.Attic;
|
---|
[16692] | 28 | using HeuristicLab.Common;
|
---|
[11987] | 29 | using HeuristicLab.Core;
|
---|
| 30 | using HeuristicLab.Encodings.BinaryVectorEncoding;
|
---|
[13339] | 31 | using HeuristicLab.Optimization;
|
---|
[11666] | 32 |
|
---|
| 33 | namespace HeuristicLab.Algorithms.ParameterlessPopulationPyramid {
|
---|
[11838] | 34 | // This code is based off the publication
|
---|
| 35 | // B. W. Goldman and W. F. Punch, "Parameter-less Population Pyramid," GECCO, pp. 785–792, 2014
|
---|
| 36 | // and the original source code in C++11 available from: https://github.com/brianwgoldman/Parameter-less_Population_Pyramid
|
---|
[16723] | 37 | [StorableType("D5F1358D-C100-40CF-9BA5-E95F72F64D1A")]
|
---|
[16692] | 38 | internal sealed class EvaluationTracker : Item, ISingleObjectiveProblemDefinition<BinaryVectorEncoding, BinaryVector> {
|
---|
| 39 | [Storable]
|
---|
| 40 | private SingleObjectiveProblem<BinaryVectorEncoding, BinaryVector> problem;
|
---|
| 41 | [Storable]
|
---|
[11666] | 42 | private int maxEvaluations;
|
---|
| 43 |
|
---|
[11669] | 44 | #region Properties
|
---|
[16692] | 45 | [Storable]
|
---|
[11666] | 46 | public double BestQuality {
|
---|
[11669] | 47 | get;
|
---|
| 48 | private set;
|
---|
[11666] | 49 | }
|
---|
[16692] | 50 | [Storable]
|
---|
[11666] | 51 | public int Evaluations {
|
---|
[11669] | 52 | get;
|
---|
| 53 | private set;
|
---|
[11666] | 54 | }
|
---|
[16692] | 55 | [Storable]
|
---|
[11666] | 56 | public int BestFoundOnEvaluation {
|
---|
[11669] | 57 | get;
|
---|
| 58 | private set;
|
---|
[11666] | 59 | }
|
---|
[16692] | 60 | [Storable]
|
---|
[11987] | 61 | public BinaryVector BestSolution {
|
---|
[11669] | 62 | get;
|
---|
| 63 | private set;
|
---|
[11666] | 64 | }
|
---|
[13339] | 65 |
|
---|
[13361] | 66 | public BinaryVectorEncoding Encoding {
|
---|
[13339] | 67 | get { return problem.Encoding; }
|
---|
| 68 | }
|
---|
[11669] | 69 | #endregion
|
---|
[11666] | 70 |
|
---|
[16692] | 71 |
|
---|
| 72 | [StorableConstructor]
|
---|
[16723] | 73 | private EvaluationTracker(StorableConstructorFlag _) : base(_) { }
|
---|
[16692] | 74 |
|
---|
| 75 | private EvaluationTracker(EvaluationTracker original, Cloner cloner)
|
---|
| 76 | : base(original, cloner) {
|
---|
| 77 | problem = cloner.Clone(original.problem);
|
---|
| 78 | maxEvaluations = original.maxEvaluations;
|
---|
| 79 | BestQuality = original.BestQuality;
|
---|
| 80 | Evaluations = original.Evaluations;
|
---|
| 81 | BestFoundOnEvaluation = original.BestFoundOnEvaluation;
|
---|
| 82 | BestSolution = cloner.Clone(original.BestSolution);
|
---|
| 83 | }
|
---|
| 84 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 85 | return new EvaluationTracker(this, cloner);
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | public EvaluationTracker(SingleObjectiveProblem<BinaryVectorEncoding, BinaryVector> problem, int maxEvaluations) {
|
---|
[11666] | 89 | this.problem = problem;
|
---|
| 90 | this.maxEvaluations = maxEvaluations;
|
---|
[13339] | 91 | BestSolution = new BinaryVector(problem.Encoding.Length);
|
---|
[11669] | 92 | BestQuality = double.NaN;
|
---|
| 93 | Evaluations = 0;
|
---|
| 94 | BestFoundOnEvaluation = 0;
|
---|
[11666] | 95 | }
|
---|
| 96 |
|
---|
[17382] | 97 | public ISingleObjectiveEvaluationResult Evaluate(BinaryVector vector, IRandom random) {
|
---|
[17320] | 98 | return Evaluate(vector, random, CancellationToken.None);
|
---|
| 99 | }
|
---|
[13361] | 100 |
|
---|
[17382] | 101 | public ISingleObjectiveEvaluationResult Evaluate(BinaryVector vector, IRandom random, CancellationToken cancellationToken) {
|
---|
[11669] | 102 | if (Evaluations >= maxEvaluations) throw new OperationCanceledException("Maximum Evaluation Limit Reached");
|
---|
| 103 | Evaluations++;
|
---|
[17382] | 104 |
|
---|
| 105 | var evaluationResult = problem.Evaluate(vector, random);
|
---|
| 106 | double fitness = evaluationResult.Quality;
|
---|
[11669] | 107 | if (double.IsNaN(BestQuality) || problem.IsBetter(fitness, BestQuality)) {
|
---|
| 108 | BestQuality = fitness;
|
---|
[11987] | 109 | BestSolution = (BinaryVector)vector.Clone();
|
---|
[11669] | 110 | BestFoundOnEvaluation = Evaluations;
|
---|
[11666] | 111 | }
|
---|
[17382] | 112 | return evaluationResult;
|
---|
[11666] | 113 | }
|
---|
| 114 |
|
---|
[17363] | 115 | public void Evaluate(ISingleObjectiveSolutionContext<BinaryVector> solutionContext, IRandom random) {
|
---|
| 116 | Evaluate(solutionContext, random, CancellationToken.None);
|
---|
| 117 | }
|
---|
| 118 | public void Evaluate(ISingleObjectiveSolutionContext<BinaryVector> solutionContext, IRandom random, CancellationToken cancellationToken) {
|
---|
[17382] | 119 | var evaluationResult = Evaluate(solutionContext.EncodedSolution, random, cancellationToken);
|
---|
| 120 | solutionContext.EvaluationResult = evaluationResult;
|
---|
[17363] | 121 | }
|
---|
| 122 |
|
---|
[13361] | 123 | public bool Maximization {
|
---|
[11999] | 124 | get {
|
---|
| 125 | if (problem == null) return false;
|
---|
| 126 | return problem.Maximization;
|
---|
| 127 | }
|
---|
[11666] | 128 | }
|
---|
[11987] | 129 |
|
---|
[13361] | 130 | public bool IsBetter(double quality, double bestQuality) {
|
---|
[11666] | 131 | return problem.IsBetter(quality, bestQuality);
|
---|
[11669] | 132 | }
|
---|
[11987] | 133 |
|
---|
[17745] | 134 | public void Analyze(ISingleObjectiveSolutionContext<BinaryVector>[] solutionContexts, IRandom random) {
|
---|
| 135 | problem.Analyze(solutionContexts, random);
|
---|
[13361] | 136 | }
|
---|
| 137 |
|
---|
| 138 | public IEnumerable<BinaryVector> GetNeighbors(BinaryVector individual, IRandom random) {
|
---|
| 139 | return problem.GetNeighbors(individual, random);
|
---|
| 140 | }
|
---|
[17363] | 141 | public IEnumerable<ISingleObjectiveSolutionContext<BinaryVector>> GetNeighbors(ISingleObjectiveSolutionContext<BinaryVector> solutionContext, IRandom random) {
|
---|
| 142 | return GetNeighbors(solutionContext.EncodedSolution, random).Select(n => new SingleObjectiveSolutionContext<BinaryVector>(n));
|
---|
| 143 | }
|
---|
[11666] | 144 | }
|
---|
| 145 | }
|
---|