[11666] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12012] | 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;
|
---|
[13361] | 24 | using System.Collections.Generic;
|
---|
[11987] | 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Encodings.BinaryVectorEncoding;
|
---|
[13339] | 27 | using HeuristicLab.Optimization;
|
---|
[11666] | 28 |
|
---|
| 29 | namespace HeuristicLab.Algorithms.ParameterlessPopulationPyramid {
|
---|
[11838] | 30 | // This code is based off the publication
|
---|
| 31 | // B. W. Goldman and W. F. Punch, "Parameter-less Population Pyramid," GECCO, pp. 785–792, 2014
|
---|
| 32 | // and the original source code in C++11 available from: https://github.com/brianwgoldman/Parameter-less_Population_Pyramid
|
---|
[13361] | 33 | internal sealed class EvaluationTracker : ISingleObjectiveProblemDefinition<BinaryVectorEncoding, BinaryVector> {
|
---|
| 34 | private readonly ISingleObjectiveProblemDefinition<BinaryVectorEncoding, BinaryVector> problem;
|
---|
[11669] | 35 |
|
---|
[11666] | 36 | private int maxEvaluations;
|
---|
| 37 |
|
---|
[11669] | 38 | #region Properties
|
---|
[11666] | 39 | public double BestQuality {
|
---|
[11669] | 40 | get;
|
---|
| 41 | private set;
|
---|
[11666] | 42 | }
|
---|
| 43 |
|
---|
| 44 | public int Evaluations {
|
---|
[11669] | 45 | get;
|
---|
| 46 | private set;
|
---|
[11666] | 47 | }
|
---|
| 48 |
|
---|
| 49 | public int BestFoundOnEvaluation {
|
---|
[11669] | 50 | get;
|
---|
| 51 | private set;
|
---|
[11666] | 52 | }
|
---|
| 53 |
|
---|
[11987] | 54 | public BinaryVector BestSolution {
|
---|
[11669] | 55 | get;
|
---|
| 56 | private set;
|
---|
[11666] | 57 | }
|
---|
[13339] | 58 |
|
---|
[13361] | 59 | public BinaryVectorEncoding Encoding {
|
---|
[13339] | 60 | get { return problem.Encoding; }
|
---|
| 61 | }
|
---|
[11669] | 62 | #endregion
|
---|
[11666] | 63 |
|
---|
[13361] | 64 | public EvaluationTracker(ISingleObjectiveProblemDefinition<BinaryVectorEncoding, BinaryVector> problem, int maxEvaluations) {
|
---|
[11666] | 65 | this.problem = problem;
|
---|
| 66 | this.maxEvaluations = maxEvaluations;
|
---|
[13339] | 67 | BestSolution = new BinaryVector(problem.Encoding.Length);
|
---|
[11669] | 68 | BestQuality = double.NaN;
|
---|
| 69 | Evaluations = 0;
|
---|
| 70 | BestFoundOnEvaluation = 0;
|
---|
[11666] | 71 | }
|
---|
| 72 |
|
---|
[13361] | 73 |
|
---|
| 74 |
|
---|
| 75 | public double Evaluate(BinaryVector vector, IRandom random) {
|
---|
[11669] | 76 | if (Evaluations >= maxEvaluations) throw new OperationCanceledException("Maximum Evaluation Limit Reached");
|
---|
| 77 | Evaluations++;
|
---|
[11987] | 78 | double fitness = problem.Evaluate(vector, random);
|
---|
[11669] | 79 | if (double.IsNaN(BestQuality) || problem.IsBetter(fitness, BestQuality)) {
|
---|
| 80 | BestQuality = fitness;
|
---|
[11987] | 81 | BestSolution = (BinaryVector)vector.Clone();
|
---|
[11669] | 82 | BestFoundOnEvaluation = Evaluations;
|
---|
[11666] | 83 | }
|
---|
| 84 | return fitness;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
[13361] | 87 | public bool Maximization {
|
---|
[11999] | 88 | get {
|
---|
| 89 | if (problem == null) return false;
|
---|
| 90 | return problem.Maximization;
|
---|
| 91 | }
|
---|
[11666] | 92 | }
|
---|
[11987] | 93 |
|
---|
[13361] | 94 | public bool IsBetter(double quality, double bestQuality) {
|
---|
[11666] | 95 | return problem.IsBetter(quality, bestQuality);
|
---|
[11669] | 96 | }
|
---|
[11987] | 97 |
|
---|
[13361] | 98 | public void Analyze(BinaryVector[] individuals, double[] qualities, ResultCollection results, IRandom random) {
|
---|
| 99 | problem.Analyze(individuals, qualities, results, random);
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | public IEnumerable<BinaryVector> GetNeighbors(BinaryVector individual, IRandom random) {
|
---|
| 103 | return problem.GetNeighbors(individual, random);
|
---|
| 104 | }
|
---|
[11666] | 105 | }
|
---|
| 106 | }
|
---|