Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
08/01/17 11:07:48 (7 years ago)
Author:
mkommend
Message:

#2815: Corrected cloning and serializing of P3 and associated classes.

Location:
trunk/sources/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/EvaluationTracker.cs

    r14185 r15301  
    2727using HeuristicLab.Encodings.BinaryVectorEncoding;
    2828using HeuristicLab.Parameters;
     29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2930using HeuristicLab.Problems.Binary;
    3031
     
    3334  // B. W. Goldman and W. F. Punch, "Parameter-less Population Pyramid," GECCO, pp. 785–792, 2014
    3435  // and the original source code in C++11 available from: https://github.com/brianwgoldman/Parameter-less_Population_Pyramid
     36  [StorableClass]
    3537  internal sealed class EvaluationTracker : BinaryProblem {
     38    [Storable]
    3639    private readonly BinaryProblem problem;
    37 
     40    [Storable]
    3841    private int maxEvaluations;
    3942
    4043    #region Properties
     44    [Storable]
    4145    public double BestQuality {
    4246      get;
    4347      private set;
    4448    }
    45 
     49    [Storable]
    4650    public int Evaluations {
    4751      get;
    4852      private set;
    4953    }
    50 
     54    [Storable]
    5155    public int BestFoundOnEvaluation {
    5256      get;
    5357      private set;
    5458    }
    55 
     59    [Storable]
    5660    public BinaryVector BestSolution {
    5761      get;
     
    5963    }
    6064    #endregion
     65
     66
     67    [StorableConstructor]
     68    private EvaluationTracker(bool deserializing) : base(deserializing) { }
    6169
    6270    private EvaluationTracker(EvaluationTracker original, Cloner cloner)
     
    6775      Evaluations = original.Evaluations;
    6876      BestFoundOnEvaluation = original.BestFoundOnEvaluation;
    69       BestSolution = cloner.Clone(BestSolution);
     77      BestSolution = cloner.Clone(original.BestSolution);
    7078    }
    7179    public override IDeepCloneable Clone(Cloner cloner) {
    7280      return new EvaluationTracker(this, cloner);
    7381    }
     82
    7483    public EvaluationTracker(BinaryProblem problem, int maxEvaluations) {
    7584      this.problem = problem;
  • trunk/sources/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/LinkageTree.cs

    r14185 r15301  
    2727using HeuristicLab.Core;
    2828using HeuristicLab.Encodings.BinaryVectorEncoding;
     29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2930using HeuristicLab.Random;
    3031
     
    3334  // B. W. Goldman and W. F. Punch, "Parameter-less Population Pyramid," GECCO, pp. 785–792, 2014
    3435  // and the original source code in C++11 available from: https://github.com/brianwgoldman/Parameter-less_Population_Pyramid
    35   public class LinkageTree {
     36  [StorableClass]
     37  public class LinkageTree : DeepCloneable {
     38    [Storable]
    3639    private readonly int[][][] occurances;
     40    [Storable]
    3741    private readonly List<int>[] clusters;
     42    [Storable]
    3843    private List<int> clusterOrdering;
     44    [Storable]
    3945    private readonly int length;
     46    [Storable]
    4047    private readonly IRandom rand;
     48    [Storable]
    4149    private bool rebuildRequired = false;
     50
     51
     52    [StorableConstructor]
     53    protected LinkageTree(bool deserializing) : base() { }
     54
     55
     56    protected LinkageTree(LinkageTree original, Cloner cloner) : base(original, cloner) {
     57      occurances = new int[original.occurances.Length][][];
     58      //mkommend: first entry is not used, cf. ctor line 83
     59      for (int i = 1; i < original.occurances.Length; i++) {
     60        occurances[i] = new int[original.occurances[i].Length][];
     61        for (int j = 0; j < original.occurances[i].Length; j++)
     62          occurances[i][j] = original.occurances[i][j].ToArray();
     63      }
     64
     65      clusters = original.clusters.Select(c => c.ToList()).ToArray();
     66      clusterOrdering = new List<int>(original.clusterOrdering);
     67      length = original.length;
     68      rand = cloner.Clone(original.rand);
     69      rebuildRequired = original.rebuildRequired;
     70    }
     71
     72
     73    public override IDeepCloneable Clone(Cloner cloner) {
     74      return new LinkageTree(this, cloner);
     75    }
    4276
    4377    public LinkageTree(int length, IRandom rand) {
     
    93127    // Uses the frequency table to calcuate the entropy distance between two indices.
    94128    // In the GECCO paper, calculates Equation 1
    95     private int[] bits = new int[4];
    96129    private double EntropyDistance(int i, int j) {
     130      int[] bits = new int[4];
    97131      // This ensures you are using the lower triangular part of "occurances"
    98132      if (i < j) {
     
    125159    // by I. Gronau and S. Moran
    126160    // In the GECCO paper, Figure 2 is a simplified version of this algorithm.
    127     private double[][] distances;
    128161    private void Rebuild() {
     162      double[][] distances = null;
    129163      if (distances == null) {
    130164        distances = new double[clusters.Length * 2 - 1][];
  • trunk/sources/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/ParameterlessPopulationPyramid.cs

    r14527 r15301  
    2323using System;
    2424using System.Collections.Generic;
     25using System.Linq;
    2526using System.Threading;
    2627using HeuristicLab.Analysis;
     
    5152    }
    5253
     54    [Storable]
    5355    private readonly IRandom random = new MersenneTwister();
    54     private List<Population> pyramid;
     56    [Storable]
     57    private List<Population> pyramid = new List<Population>();
     58    [Storable]
    5559    private EvaluationTracker tracker;
    5660
    5761    // Tracks all solutions in Pyramid for quick membership checks
    58     private readonly HashSet<BinaryVector> seen = new HashSet<BinaryVector>(new EnumerableBoolEqualityComparer());
     62
     63    private HashSet<BinaryVector> seen = new HashSet<BinaryVector>(new EnumerableBoolEqualityComparer());
     64    [Storable]
     65    private IEnumerable<BinaryVector> StorableSeen {
     66      get { return seen; }
     67      set { seen = new HashSet<BinaryVector>(value, new EnumerableBoolEqualityComparer()); }
     68    }
    5969
    6070    #region ParameterNames
     
    160170    protected ParameterlessPopulationPyramid(ParameterlessPopulationPyramid original, Cloner cloner)
    161171      : base(original, cloner) {
     172      random = cloner.Clone(original.random);
     173      pyramid = original.pyramid.Select(cloner.Clone).ToList();
     174      tracker = cloner.Clone(original.tracker);
     175      seen = new HashSet<BinaryVector>(original.seen.Select(cloner.Clone), new EnumerableBoolEqualityComparer());
    162176    }
    163177
     
    166180    }
    167181
    168     public ParameterlessPopulationPyramid() {
     182    public ParameterlessPopulationPyramid() : base() {
    169183      Parameters.Add(new FixedValueParameter<IntValue>(MaximumIterationsParameterName, "", new IntValue(Int32.MaxValue)));
    170184      Parameters.Add(new FixedValueParameter<IntValue>(MaximumEvaluationsParameterName, "", new IntValue(Int32.MaxValue)));
     
    256270          ResultsIterations++;
    257271          cancellationToken.ThrowIfCancellationRequested();
    258         } finally {
     272        }
     273        finally {
    259274          ResultsEvaluations = tracker.Evaluations;
    260275          ResultsBestSolution = new BinaryVector(tracker.BestSolution);
  • trunk/sources/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/Population.cs

    r14185 r15301  
    2222
    2323using System.Collections.Generic;
     24using System.Linq;
     25using HeuristicLab.Common;
    2426using HeuristicLab.Core;
    2527using HeuristicLab.Encodings.BinaryVectorEncoding;
     28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2629
    2730namespace HeuristicLab.Algorithms.ParameterlessPopulationPyramid {
     
    2932  // B. W. Goldman and W. F. Punch, "Parameter-less Population Pyramid," GECCO, pp. 785–792, 2014
    3033  // and the original source code in C++11 available from: https://github.com/brianwgoldman/Parameter-less_Population_Pyramid
    31   public class Population {
     34  [StorableClass]
     35  public class Population : DeepCloneable {
     36    [Storable]
    3237    public List<BinaryVector> Solutions {
    3338      get;
    3439      private set;
    3540    }
    36 
     41    [Storable]
    3742    public LinkageTree Tree {
    3843      get;
    3944      private set;
     45    }
     46
     47
     48    [StorableConstructor]
     49    protected Population(bool deserializing) : base() { }
     50
     51
     52    protected Population(Population original, Cloner cloner) : base(original, cloner) {
     53      Solutions = original.Solutions.Select(cloner.Clone).ToList();
     54      Tree = cloner.Clone(original.Tree);
     55    }
     56
     57    public override IDeepCloneable Clone(Cloner cloner) {
     58      return new Population(this, cloner);
    4059    }
    4160
Note: See TracChangeset for help on using the changeset viewer.