Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/12/15 15:39:28 (9 years ago)
Author:
abeham
Message:

#2282:

  • Renamed BinaryVectorProblem to BinaryProblem
  • Removed IBinaryVectorProblem interface
  • Derived BinaryProblem from SingleObjectiveBasicProblem
  • Changed bool[] datatype to BinaryVector
Location:
trunk/sources/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3
Files:
7 edited

Legend:

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

    r11956 r11987  
    2222
    2323using System;
    24 using HeuristicLab.Problems.BinaryVector;
     24using HeuristicLab.Common;
     25using HeuristicLab.Core;
     26using HeuristicLab.Encodings.BinaryVectorEncoding;
     27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     28using HeuristicLab.Problems.Binary;
    2529
    2630namespace HeuristicLab.Algorithms.ParameterlessPopulationPyramid {
     
    2832  // B. W. Goldman and W. F. Punch, "Parameter-less Population Pyramid," GECCO, pp. 785–792, 2014
    2933  // 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;
     34  internal sealed class EvaluationTracker : BinaryProblem {
     35    private readonly BinaryProblem problem;
    3236
    3337    private int maxEvaluations;
     
    4953    }
    5054
    51     public bool[] BestSolution {
     55    public BinaryVector BestSolution {
    5256      get;
    5357      private set;
     
    5559    #endregion
    5660
    57     public EvaluationTracker(IBinaryVectorProblem problem, int maxEvaluations) {
     61    [StorableConstructor]
     62    private EvaluationTracker(bool deserializing) : base(deserializing) { }
     63    private EvaluationTracker(EvaluationTracker original, Cloner cloner)
     64      : base(original, cloner) {
     65      problem = cloner.Clone(original.problem);
     66      maxEvaluations = original.maxEvaluations;
     67      BestQuality = original.BestQuality;
     68      Evaluations = original.Evaluations;
     69      BestFoundOnEvaluation = original.BestFoundOnEvaluation;
     70      BestSolution = cloner.Clone(BestSolution);
     71    }
     72    public override IDeepCloneable Clone(Cloner cloner) {
     73      return new EvaluationTracker(this, cloner);
     74    }
     75    public EvaluationTracker(BinaryProblem problem, int maxEvaluations) {
    5876      this.problem = problem;
    5977      this.maxEvaluations = maxEvaluations;
    60       BestSolution = new bool[0];
     78      BestSolution = new BinaryVector(Length);
    6179      BestQuality = double.NaN;
    6280      Evaluations = 0;
     
    6482    }
    6583
    66     public double Evaluate(bool[] individual) {
     84
     85
     86    public override double Evaluate(BinaryVector vector, IRandom random) {
    6787      if (Evaluations >= maxEvaluations) throw new OperationCanceledException("Maximum Evaluation Limit Reached");
    6888      Evaluations++;
    69       double fitness = problem.Evaluate(individual);
     89      double fitness = problem.Evaluate(vector, random);
    7090      if (double.IsNaN(BestQuality) || problem.IsBetter(fitness, BestQuality)) {
    7191        BestQuality = fitness;
    72         BestSolution = (bool[])individual.Clone();
     92        BestSolution = (BinaryVector)vector.Clone();
    7393        BestFoundOnEvaluation = Evaluations;
    7494      }
     
    7696    }
    7797
    78     #region ForwardedInteraface
    79     public int Length {
     98    public override int Length {
    8099      get { return problem.Length; }
     100      set { problem.Length = value; }
    81101    }
    82     public bool Maximization {
     102
     103    public override bool Maximization {
    83104      get { return problem.Maximization; }
    84105    }
     106
    85107    public bool IsBetter(double quality, double bestQuality) {
    86108      return problem.IsBetter(quality, bestQuality);
    87109    }
    88     #endregion
     110
    89111  }
    90112}
  • trunk/sources/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/HeuristicLab.Algorithms.ParameterlessPopulationPyramid-3.3.csproj

    r11961 r11987  
    148148      <Private>False</Private>
    149149    </ProjectReference>
    150     <ProjectReference Include="..\..\HeuristicLab.Problems.BinaryVector\3.3\HeuristicLab.Problems.BinaryVector-3.3.csproj">
     150    <ProjectReference Include="..\..\HeuristicLab.Problems.Binary\3.3\HeuristicLab.Problems.Binary-3.3.csproj">
    151151      <Project>{fc627be5-0f93-47d8-bd2e-530ea2b8aa5f}</Project>
    152152      <Name>HeuristicLab.Problems.BinaryVector-3.3</Name>
  • trunk/sources/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/HillClimber.cs

    r11960 r11987  
    2828using HeuristicLab.Core;
    2929using HeuristicLab.Data;
     30using HeuristicLab.Encodings.BinaryVectorEncoding;
    3031using HeuristicLab.Optimization;
    3132using HeuristicLab.Parameters;
    3233using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    33 using HeuristicLab.Problems.BinaryVector;
     34using HeuristicLab.Problems.Binary;
    3435using HeuristicLab.Random;
    3536
     
    4950
    5051    public override Type ProblemType {
    51       get { return typeof(BinaryVectorProblem); }
     52      get { return typeof(BinaryProblem); }
    5253    }
    53     public new BinaryVectorProblem Problem {
    54       get { return (BinaryVectorProblem)base.Problem; }
     54    public new BinaryProblem Problem {
     55      get { return (BinaryProblem)base.Problem; }
    5556      set { base.Problem = value; }
    5657    }
     
    8384      Results.Add(new Result("Best quality", BestQuality));
    8485      for (int iteration = 0; iteration < Iterations; iteration++) {
    85         bool[] solution = new bool[Problem.Length];
     86        var solution = new BinaryVector(Problem.Length);
    8687        for (int i = 0; i < solution.Length; i++) {
    8788          solution[i] = random.Next(2) == 1;
    8889        }
    8990
    90         var fitness = Problem.Evaluate(solution);
     91        var fitness = Problem.Evaluate(solution, random);
    9192
    9293        fitness = ImproveToLocalOptimum(Problem, solution, fitness, random);
     
    9798    }
    9899    // In the GECCO paper, Section 2.1
    99     public static double ImproveToLocalOptimum(IBinaryVectorProblem problem, bool[] solution, double fitness, IRandom rand) {
     100    public static double ImproveToLocalOptimum(BinaryProblem problem, BinaryVector solution, double fitness, IRandom rand) {
    100101      var tried = new HashSet<int>();
    101102      do {
     
    104105          if (tried.Contains(option)) continue;
    105106          solution[option] = !solution[option];
    106           double newFitness = problem.Evaluate(solution);
     107          double newFitness = problem.Evaluate(solution, rand);
    107108          if (problem.IsBetter(newFitness, fitness)) {
    108109            fitness = newFitness;
  • trunk/sources/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/LinkageCrossover.cs

    r11956 r11987  
    2323using System.Linq;
    2424using HeuristicLab.Core;
    25 using HeuristicLab.Problems.BinaryVector;
     25using HeuristicLab.Encodings.BinaryVectorEncoding;
     26using HeuristicLab.Problems.Binary;
    2627using HeuristicLab.Random;
    2728
     
    3233  public static class LinkageCrossover {
    3334    // In the GECCO paper, Figure 3
    34     public static double ImproveUsingTree(LinkageTree tree, IList<bool[]> donors, bool[] solution, double fitness, IBinaryVectorProblem problem, IRandom rand) {
     35    public static double ImproveUsingTree(LinkageTree tree, IList<BinaryVector> donors, BinaryVector solution, double fitness, BinaryProblem problem, IRandom rand) {
    3536      var options = Enumerable.Range(0, donors.Count).ToArray();
    3637      foreach (var cluster in tree.Clusters) {
     
    4041        foreach (var donorIndex in options.ShuffleList(rand)) {
    4142          // Attempt the donation
    42           fitness = Donate(solution, fitness, donors[donorIndex], cluster, problem, out donorFound);
     43          fitness = Donate(solution, fitness, donors[donorIndex], cluster, problem, rand, out donorFound);
    4344          if (donorFound) break;
    4445        }
     
    4748    }
    4849
    49     private static double Donate(bool[] solution, double fitness, bool[] source, IEnumerable<int> cluster, IBinaryVectorProblem problem, out bool changed) {
     50    private static double Donate(BinaryVector solution, double fitness, BinaryVector source, IEnumerable<int> cluster, BinaryProblem problem, IRandom rand, out bool changed) {
    5051      // keep track of which bits flipped to make the donation
    5152      List<int> flipped = new List<int>();
     
    5859      changed = flipped.Count > 0;
    5960      if (changed) {
    60         double newFitness = problem.Evaluate(solution);
     61        double newFitness = problem.Evaluate(solution, rand);
    6162        // if the original is strictly better, revert the change
    6263        if (problem.IsBetter(fitness, newFitness)) {
  • trunk/sources/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/LinkageTree.cs

    r11939 r11987  
    2626using HeuristicLab.Common;
    2727using HeuristicLab.Core;
     28using HeuristicLab.Encodings.BinaryVectorEncoding;
    2829using HeuristicLab.Random;
    2930
     
    6465    }
    6566
    66     public void Add(bool[] solution) {
     67    public void Add(BinaryVector solution) {
    6768      if (solution.Length != length) throw new ArgumentException("The individual has not the correct length.");
    6869      for (int i = 1; i < solution.Length; i++) {
  • trunk/sources/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/ParameterlessPopulationPyramid.cs

    r11960 r11987  
    3232using HeuristicLab.Parameters;
    3333using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    34 using HeuristicLab.Problems.BinaryVector;
     34using HeuristicLab.Problems.Binary;
    3535using HeuristicLab.Random;
    3636
     
    4444  public class ParameterlessPopulationPyramid : BasicAlgorithm {
    4545    public override Type ProblemType {
    46       get { return typeof(BinaryVectorProblem); }
    47     }
    48     public new BinaryVectorProblem Problem {
    49       get { return (BinaryVectorProblem)base.Problem; }
     46      get { return typeof(BinaryProblem); }
     47    }
     48    public new BinaryProblem Problem {
     49      get { return (BinaryProblem)base.Problem; }
    5050      set { base.Problem = value; }
    5151    }
     
    5656
    5757    // Tracks all solutions in Pyramid for quick membership checks
    58     private HashSet<bool[]> seen = new HashSet<bool[]>(new EnumerableBoolEqualityComparer());
     58    private HashSet<BinaryVector> seen = new HashSet<BinaryVector>(new EnumerableBoolEqualityComparer());
    5959
    6060    #region ParameterNames
     
    179179    }
    180180
    181     private void AddIfUnique(bool[] solution, int level) {
     181    private void AddIfUnique(BinaryVector solution, int level) {
    182182      // Don't add things you have seen
    183183      if (seen.Contains(solution)) return;
     
    185185        pyramid.Add(new Population(tracker.Length, random));
    186186      }
    187       var copied = (bool[])solution.Clone();
     187      var copied = (BinaryVector)solution.Clone();
    188188      pyramid[level].Add(copied);
    189189      seen.Add(copied);
     
    193193    private double iterate() {
    194194      // Create a random solution
    195       bool[] solution = new bool[tracker.Length];
     195      BinaryVector solution = new BinaryVector(tracker.Length);
    196196      for (int i = 0; i < solution.Length; i++) {
    197197        solution[i] = random.Next(2) == 1;
    198198      }
    199       double fitness = tracker.Evaluate(solution);
     199      double fitness = tracker.Evaluate(solution, random);
    200200      fitness = HillClimber.ImproveToLocalOptimum(tracker, solution, fitness, random);
    201201      AddIfUnique(solution, 0);
     
    249249          fitness = iterate();
    250250          cancellationToken.ThrowIfCancellationRequested();
    251         }
    252         finally {
     251        } finally {
    253252          ResultsEvaluations = tracker.Evaluations;
    254253          ResultsBestSolution = new BinaryVector(tracker.BestSolution);
  • trunk/sources/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/Population.cs

    r11838 r11987  
    2323using System.Collections.Generic;
    2424using HeuristicLab.Core;
     25using HeuristicLab.Encodings.BinaryVectorEncoding;
    2526
    2627namespace HeuristicLab.Algorithms.ParameterlessPopulationPyramid {
     
    2930  // and the original source code in C++11 available from: https://github.com/brianwgoldman/Parameter-less_Population_Pyramid
    3031  public class Population {
    31     public List<bool[]> Solutions {
     32    public List<BinaryVector> Solutions {
    3233      get;
    3334      private set;
     
    4041
    4142    public Population(int length, IRandom rand) {
    42       Solutions = new List<bool[]>();
     43      Solutions = new List<BinaryVector>();
    4344      Tree = new LinkageTree(length, rand);
    4445    }
    45     public void Add(bool[] solution) {
     46    public void Add(BinaryVector solution) {
    4647      Solutions.Add(solution);
    4748      Tree.Add(solution);
Note: See TracChangeset for help on using the changeset viewer.