Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/13/15 15:00:15 (9 years ago)
Author:
abeham
Message:

#2174, #2282: merged revisions r11961,r11963,r11967,r11970,r11971,r11982,r11984,r11998,r12001,r12002,r12003,r12004,r11939,r11945,r11956,r11958,r11959,r11960,r11983,r11987,r11988,r11990,r11993,r11994,r11996,r11999,r12000 to stable

Location:
stable
Files:
1 edited
2 copied

Legend:

Unmodified
Added
Removed
  • stable

  • stable/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/EvaluationTracker.cs

    r11956 r12005  
    2222
    2323using System;
    24 using HeuristicLab.Problems.BinaryVector;
     24using HeuristicLab.Common;
     25using HeuristicLab.Core;
     26using HeuristicLab.Data;
     27using HeuristicLab.Encodings.BinaryVectorEncoding;
     28using HeuristicLab.Parameters;
     29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     30using HeuristicLab.Problems.Binary;
    2531
    2632namespace HeuristicLab.Algorithms.ParameterlessPopulationPyramid {
     
    2834  // B. W. Goldman and W. F. Punch, "Parameter-less Population Pyramid," GECCO, pp. 785–792, 2014
    2935  // and the original source code in C++11 available from: https://github.com/brianwgoldman/Parameter-less_Population_Pyramid
    30   public class EvaluationTracker : IBinaryVectorProblem {
    31     private readonly IBinaryVectorProblem problem;
     36  internal sealed class EvaluationTracker : BinaryProblem {
     37    private readonly BinaryProblem problem;
    3238
    3339    private int maxEvaluations;
     
    4955    }
    5056
    51     public bool[] BestSolution {
     57    public BinaryVector BestSolution {
    5258      get;
    5359      private set;
     
    5561    #endregion
    5662
    57     public EvaluationTracker(IBinaryVectorProblem problem, int maxEvaluations) {
     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) {
    5878      this.problem = problem;
    5979      this.maxEvaluations = maxEvaluations;
    60       BestSolution = new bool[0];
     80      BestSolution = new BinaryVector(Length);
    6181      BestQuality = double.NaN;
    6282      Evaluations = 0;
    6383      BestFoundOnEvaluation = 0;
     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 });
    6487    }
    6588
    66     public double Evaluate(bool[] individual) {
     89    public override double Evaluate(BinaryVector vector, IRandom random) {
    6790      if (Evaluations >= maxEvaluations) throw new OperationCanceledException("Maximum Evaluation Limit Reached");
    6891      Evaluations++;
    69       double fitness = problem.Evaluate(individual);
     92      double fitness = problem.Evaluate(vector, random);
    7093      if (double.IsNaN(BestQuality) || problem.IsBetter(fitness, BestQuality)) {
    7194        BestQuality = fitness;
    72         BestSolution = (bool[])individual.Clone();
     95        BestSolution = (BinaryVector)vector.Clone();
    7396        BestFoundOnEvaluation = Evaluations;
    7497      }
     
    7699    }
    77100
    78     #region ForwardedInteraface
    79     public int Length {
     101    public override int Length {
    80102      get { return problem.Length; }
     103      set { problem.Length = value; }
    81104    }
    82     public bool Maximization {
    83       get { return problem.Maximization; }
     105
     106    public override bool Maximization {
     107      get {
     108        if (problem == null) return false;
     109        return problem.Maximization;
     110      }
    84111    }
     112
    85113    public bool IsBetter(double quality, double bestQuality) {
    86114      return problem.IsBetter(quality, bestQuality);
    87115    }
    88     #endregion
     116
    89117  }
    90118}
Note: See TracChangeset for help on using the changeset viewer.