- Timestamp:
- 02/12/15 15:39:28 (9 years ago)
- 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 22 22 23 23 using System; 24 using HeuristicLab.Problems.BinaryVector; 24 using HeuristicLab.Common; 25 using HeuristicLab.Core; 26 using HeuristicLab.Encodings.BinaryVectorEncoding; 27 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 28 using HeuristicLab.Problems.Binary; 25 29 26 30 namespace HeuristicLab.Algorithms.ParameterlessPopulationPyramid { … … 28 32 // B. W. Goldman and W. F. Punch, "Parameter-less Population Pyramid," GECCO, pp. 785–792, 2014 29 33 // 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; 32 36 33 37 private int maxEvaluations; … … 49 53 } 50 54 51 public bool[]BestSolution {55 public BinaryVector BestSolution { 52 56 get; 53 57 private set; … … 55 59 #endregion 56 60 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) { 58 76 this.problem = problem; 59 77 this.maxEvaluations = maxEvaluations; 60 BestSolution = new bool[0];78 BestSolution = new BinaryVector(Length); 61 79 BestQuality = double.NaN; 62 80 Evaluations = 0; … … 64 82 } 65 83 66 public double Evaluate(bool[] individual) { 84 85 86 public override double Evaluate(BinaryVector vector, IRandom random) { 67 87 if (Evaluations >= maxEvaluations) throw new OperationCanceledException("Maximum Evaluation Limit Reached"); 68 88 Evaluations++; 69 double fitness = problem.Evaluate( individual);89 double fitness = problem.Evaluate(vector, random); 70 90 if (double.IsNaN(BestQuality) || problem.IsBetter(fitness, BestQuality)) { 71 91 BestQuality = fitness; 72 BestSolution = ( bool[])individual.Clone();92 BestSolution = (BinaryVector)vector.Clone(); 73 93 BestFoundOnEvaluation = Evaluations; 74 94 } … … 76 96 } 77 97 78 #region ForwardedInteraface 79 public int Length { 98 public override int Length { 80 99 get { return problem.Length; } 100 set { problem.Length = value; } 81 101 } 82 public bool Maximization { 102 103 public override bool Maximization { 83 104 get { return problem.Maximization; } 84 105 } 106 85 107 public bool IsBetter(double quality, double bestQuality) { 86 108 return problem.IsBetter(quality, bestQuality); 87 109 } 88 #endregion 110 89 111 } 90 112 } -
trunk/sources/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/HeuristicLab.Algorithms.ParameterlessPopulationPyramid-3.3.csproj
r11961 r11987 148 148 <Private>False</Private> 149 149 </ProjectReference> 150 <ProjectReference Include="..\..\HeuristicLab.Problems.Binary Vector\3.3\HeuristicLab.Problems.BinaryVector-3.3.csproj">150 <ProjectReference Include="..\..\HeuristicLab.Problems.Binary\3.3\HeuristicLab.Problems.Binary-3.3.csproj"> 151 151 <Project>{fc627be5-0f93-47d8-bd2e-530ea2b8aa5f}</Project> 152 152 <Name>HeuristicLab.Problems.BinaryVector-3.3</Name> -
trunk/sources/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/HillClimber.cs
r11960 r11987 28 28 using HeuristicLab.Core; 29 29 using HeuristicLab.Data; 30 using HeuristicLab.Encodings.BinaryVectorEncoding; 30 31 using HeuristicLab.Optimization; 31 32 using HeuristicLab.Parameters; 32 33 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 33 using HeuristicLab.Problems.Binary Vector;34 using HeuristicLab.Problems.Binary; 34 35 using HeuristicLab.Random; 35 36 … … 49 50 50 51 public override Type ProblemType { 51 get { return typeof(Binary VectorProblem); }52 get { return typeof(BinaryProblem); } 52 53 } 53 public new Binary VectorProblem Problem {54 get { return (Binary VectorProblem)base.Problem; }54 public new BinaryProblem Problem { 55 get { return (BinaryProblem)base.Problem; } 55 56 set { base.Problem = value; } 56 57 } … … 83 84 Results.Add(new Result("Best quality", BestQuality)); 84 85 for (int iteration = 0; iteration < Iterations; iteration++) { 85 bool[] solution = new bool[Problem.Length];86 var solution = new BinaryVector(Problem.Length); 86 87 for (int i = 0; i < solution.Length; i++) { 87 88 solution[i] = random.Next(2) == 1; 88 89 } 89 90 90 var fitness = Problem.Evaluate(solution );91 var fitness = Problem.Evaluate(solution, random); 91 92 92 93 fitness = ImproveToLocalOptimum(Problem, solution, fitness, random); … … 97 98 } 98 99 // 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) { 100 101 var tried = new HashSet<int>(); 101 102 do { … … 104 105 if (tried.Contains(option)) continue; 105 106 solution[option] = !solution[option]; 106 double newFitness = problem.Evaluate(solution );107 double newFitness = problem.Evaluate(solution, rand); 107 108 if (problem.IsBetter(newFitness, fitness)) { 108 109 fitness = newFitness; -
trunk/sources/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/LinkageCrossover.cs
r11956 r11987 23 23 using System.Linq; 24 24 using HeuristicLab.Core; 25 using HeuristicLab.Problems.BinaryVector; 25 using HeuristicLab.Encodings.BinaryVectorEncoding; 26 using HeuristicLab.Problems.Binary; 26 27 using HeuristicLab.Random; 27 28 … … 32 33 public static class LinkageCrossover { 33 34 // 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) { 35 36 var options = Enumerable.Range(0, donors.Count).ToArray(); 36 37 foreach (var cluster in tree.Clusters) { … … 40 41 foreach (var donorIndex in options.ShuffleList(rand)) { 41 42 // 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); 43 44 if (donorFound) break; 44 45 } … … 47 48 } 48 49 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) { 50 51 // keep track of which bits flipped to make the donation 51 52 List<int> flipped = new List<int>(); … … 58 59 changed = flipped.Count > 0; 59 60 if (changed) { 60 double newFitness = problem.Evaluate(solution );61 double newFitness = problem.Evaluate(solution, rand); 61 62 // if the original is strictly better, revert the change 62 63 if (problem.IsBetter(fitness, newFitness)) { -
trunk/sources/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/LinkageTree.cs
r11939 r11987 26 26 using HeuristicLab.Common; 27 27 using HeuristicLab.Core; 28 using HeuristicLab.Encodings.BinaryVectorEncoding; 28 29 using HeuristicLab.Random; 29 30 … … 64 65 } 65 66 66 public void Add( bool[]solution) {67 public void Add(BinaryVector solution) { 67 68 if (solution.Length != length) throw new ArgumentException("The individual has not the correct length."); 68 69 for (int i = 1; i < solution.Length; i++) { -
trunk/sources/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/ParameterlessPopulationPyramid.cs
r11960 r11987 32 32 using HeuristicLab.Parameters; 33 33 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 34 using HeuristicLab.Problems.Binary Vector;34 using HeuristicLab.Problems.Binary; 35 35 using HeuristicLab.Random; 36 36 … … 44 44 public class ParameterlessPopulationPyramid : BasicAlgorithm { 45 45 public override Type ProblemType { 46 get { return typeof(Binary VectorProblem); }47 } 48 public new Binary VectorProblem Problem {49 get { return (Binary VectorProblem)base.Problem; }46 get { return typeof(BinaryProblem); } 47 } 48 public new BinaryProblem Problem { 49 get { return (BinaryProblem)base.Problem; } 50 50 set { base.Problem = value; } 51 51 } … … 56 56 57 57 // 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()); 59 59 60 60 #region ParameterNames … … 179 179 } 180 180 181 private void AddIfUnique( bool[]solution, int level) {181 private void AddIfUnique(BinaryVector solution, int level) { 182 182 // Don't add things you have seen 183 183 if (seen.Contains(solution)) return; … … 185 185 pyramid.Add(new Population(tracker.Length, random)); 186 186 } 187 var copied = ( bool[])solution.Clone();187 var copied = (BinaryVector)solution.Clone(); 188 188 pyramid[level].Add(copied); 189 189 seen.Add(copied); … … 193 193 private double iterate() { 194 194 // Create a random solution 195 bool[] solution = new bool[tracker.Length];195 BinaryVector solution = new BinaryVector(tracker.Length); 196 196 for (int i = 0; i < solution.Length; i++) { 197 197 solution[i] = random.Next(2) == 1; 198 198 } 199 double fitness = tracker.Evaluate(solution );199 double fitness = tracker.Evaluate(solution, random); 200 200 fitness = HillClimber.ImproveToLocalOptimum(tracker, solution, fitness, random); 201 201 AddIfUnique(solution, 0); … … 249 249 fitness = iterate(); 250 250 cancellationToken.ThrowIfCancellationRequested(); 251 } 252 finally { 251 } finally { 253 252 ResultsEvaluations = tracker.Evaluations; 254 253 ResultsBestSolution = new BinaryVector(tracker.BestSolution); -
trunk/sources/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/Population.cs
r11838 r11987 23 23 using System.Collections.Generic; 24 24 using HeuristicLab.Core; 25 using HeuristicLab.Encodings.BinaryVectorEncoding; 25 26 26 27 namespace HeuristicLab.Algorithms.ParameterlessPopulationPyramid { … … 29 30 // and the original source code in C++11 available from: https://github.com/brianwgoldman/Parameter-less_Population_Pyramid 30 31 public class Population { 31 public List< bool[]> Solutions {32 public List<BinaryVector> Solutions { 32 33 get; 33 34 private set; … … 40 41 41 42 public Population(int length, IRandom rand) { 42 Solutions = new List< bool[]>();43 Solutions = new List<BinaryVector>(); 43 44 Tree = new LinkageTree(length, rand); 44 45 } 45 public void Add( bool[]solution) {46 public void Add(BinaryVector solution) { 46 47 Solutions.Add(solution); 47 48 Tree.Add(solution);
Note: See TracChangeset
for help on using the changeset viewer.