Changeset 13361 for branches/ProblemRefactoring/HeuristicLab.Algorithms.ParameterlessPopulationPyramid
- Timestamp:
- 11/24/15 16:01:02 (9 years ago)
- Location:
- branches/ProblemRefactoring/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/ProblemRefactoring/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/EvaluationTracker.cs
r13339 r13361 22 22 23 23 using System; 24 using HeuristicLab.Common;24 using System.Collections.Generic; 25 25 using HeuristicLab.Core; 26 using HeuristicLab.Data;27 26 using HeuristicLab.Encodings.BinaryVectorEncoding; 28 27 using HeuristicLab.Optimization; 29 using HeuristicLab.Parameters;30 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;31 28 32 29 namespace HeuristicLab.Algorithms.ParameterlessPopulationPyramid { … … 34 31 // B. W. Goldman and W. F. Punch, "Parameter-less Population Pyramid," GECCO, pp. 785–792, 2014 35 32 // and the original source code in C++11 available from: https://github.com/brianwgoldman/Parameter-less_Population_Pyramid 36 internal sealed class EvaluationTracker : SingleObjectiveProblem<BinaryVectorEncoding, BinaryVector> {37 private readonly ISingleObjectiveProblem <BinaryVectorEncoding, BinaryVector> problem;33 internal sealed class EvaluationTracker : ISingleObjectiveProblemDefinition<BinaryVectorEncoding, BinaryVector> { 34 private readonly ISingleObjectiveProblemDefinition<BinaryVectorEncoding, BinaryVector> problem; 38 35 39 36 private int maxEvaluations; … … 60 57 } 61 58 62 public newBinaryVectorEncoding Encoding {59 public BinaryVectorEncoding Encoding { 63 60 get { return problem.Encoding; } 64 61 } 65 62 #endregion 66 63 67 [StorableConstructor] 68 private EvaluationTracker(bool deserializing) : base(deserializing) { } 69 private EvaluationTracker(EvaluationTracker original, Cloner cloner) 70 : base(original, cloner) { 71 problem = cloner.Clone(original.problem); 72 maxEvaluations = original.maxEvaluations; 73 BestQuality = original.BestQuality; 74 Evaluations = original.Evaluations; 75 BestFoundOnEvaluation = original.BestFoundOnEvaluation; 76 BestSolution = cloner.Clone(BestSolution); 77 } 78 public override IDeepCloneable Clone(Cloner cloner) { 79 return new EvaluationTracker(this, cloner); 80 } 81 public EvaluationTracker(ISingleObjectiveProblem<BinaryVectorEncoding, BinaryVector> problem, int maxEvaluations) { 64 public EvaluationTracker(ISingleObjectiveProblemDefinition<BinaryVectorEncoding, BinaryVector> problem, int maxEvaluations) { 82 65 this.problem = problem; 83 66 this.maxEvaluations = maxEvaluations; … … 86 69 Evaluations = 0; 87 70 BestFoundOnEvaluation = 0; 88 89 if (Parameters.ContainsKey("Maximization")) Parameters.Remove("Maximization");90 Parameters.Add(new FixedValueParameter<BoolValue>("Maximization", "Set to false if the problem should be minimized.", (BoolValue)new BoolValue(Maximization).AsReadOnly()) { Hidden = true });91 71 } 92 72 93 public override double Evaluate(BinaryVector vector, IRandom random) { 73 74 75 public double Evaluate(BinaryVector vector, IRandom random) { 94 76 if (Evaluations >= maxEvaluations) throw new OperationCanceledException("Maximum Evaluation Limit Reached"); 95 77 Evaluations++; … … 103 85 } 104 86 105 public overridebool Maximization {87 public bool Maximization { 106 88 get { 107 89 if (problem == null) return false; … … 110 92 } 111 93 112 public overridebool IsBetter(double quality, double bestQuality) {94 public bool IsBetter(double quality, double bestQuality) { 113 95 return problem.IsBetter(quality, bestQuality); 114 96 } 115 97 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 } 116 105 } 117 106 } -
branches/ProblemRefactoring/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/HillClimber.cs
r13339 r13361 49 49 50 50 public override Type ProblemType { 51 get { return typeof(ISingleObjectiveProblem <BinaryVectorEncoding, BinaryVector>); }51 get { return typeof(ISingleObjectiveProblemDefinition<BinaryVectorEncoding, BinaryVector>); } 52 52 } 53 public new ISingleObjectiveProblem <BinaryVectorEncoding, BinaryVector> Problem {54 get { return (ISingleObjectiveProblem <BinaryVectorEncoding, BinaryVector>)base.Problem; }55 set { base.Problem = value; }53 public new ISingleObjectiveProblemDefinition<BinaryVectorEncoding, BinaryVector> Problem { 54 get { return (ISingleObjectiveProblemDefinition<BinaryVectorEncoding, BinaryVector>)base.Problem; } 55 set { base.Problem = (IProblem)value; } 56 56 } 57 57 … … 97 97 } 98 98 // In the GECCO paper, Section 2.1 99 public static double ImproveToLocalOptimum(ISingleObjectiveProblem <BinaryVectorEncoding, BinaryVector> problem, BinaryVector solution, double fitness, IRandom rand) {99 public static double ImproveToLocalOptimum(ISingleObjectiveProblemDefinition<BinaryVectorEncoding, BinaryVector> problem, BinaryVector solution, double fitness, IRandom rand) { 100 100 var tried = new HashSet<int>(); 101 101 do { -
branches/ProblemRefactoring/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/LinkageCrossover.cs
r13339 r13361 33 33 public static class LinkageCrossover { 34 34 // In the GECCO paper, Figure 3 35 public static double ImproveUsingTree(LinkageTree tree, IList<BinaryVector> donors, BinaryVector solution, double fitness, ISingleObjectiveProblem <BinaryVectorEncoding, BinaryVector> problem, IRandom rand) {35 public static double ImproveUsingTree(LinkageTree tree, IList<BinaryVector> donors, BinaryVector solution, double fitness, ISingleObjectiveProblemDefinition<BinaryVectorEncoding, BinaryVector> problem, IRandom rand) { 36 36 var options = Enumerable.Range(0, donors.Count).ToArray(); 37 37 foreach (var cluster in tree.Clusters) { … … 48 48 } 49 49 50 private static double Donate(BinaryVector solution, double fitness, BinaryVector source, IEnumerable<int> cluster, ISingleObjectiveProblem <BinaryVectorEncoding, BinaryVector> problem, IRandom rand, out bool changed) {50 private static double Donate(BinaryVector solution, double fitness, BinaryVector source, IEnumerable<int> cluster, ISingleObjectiveProblemDefinition<BinaryVectorEncoding, BinaryVector> problem, IRandom rand, out bool changed) { 51 51 // keep track of which bits flipped to make the donation 52 52 List<int> flipped = new List<int>(); -
branches/ProblemRefactoring/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/ParameterlessPopulationPyramid.cs
r13339 r13361 45 45 get { return typeof(ISingleObjectiveProblem<BinaryVectorEncoding, BinaryVector>); } 46 46 } 47 public new ISingleObjectiveProblem <BinaryVectorEncoding, BinaryVector> Problem {48 get { return (ISingleObjectiveProblem <BinaryVectorEncoding, BinaryVector>)base.Problem; }49 set { base.Problem = value; }47 public new ISingleObjectiveProblemDefinition<BinaryVectorEncoding, BinaryVector> Problem { 48 get { return (ISingleObjectiveProblemDefinition<BinaryVectorEncoding, BinaryVector>)base.Problem; } 49 set { base.Problem = (IProblem)value; } 50 50 } 51 51 … … 182 182 if (seen.Contains(solution)) return; 183 183 if (level == pyramid.Count) { 184 pyramid.Add(new Population( tracker.Encoding.Length, random));184 pyramid.Add(new Population(Problem.Encoding.Length, random)); 185 185 } 186 186 var copied = (BinaryVector)solution.Clone(); … … 192 192 private double iterate() { 193 193 // Create a random solution 194 BinaryVector solution = new BinaryVector( tracker.Encoding.Length);194 BinaryVector solution = new BinaryVector(Problem.Encoding.Length); 195 195 for (int i = 0; i < solution.Length; i++) { 196 196 solution[i] = random.Next(2) == 1;
Note: See TracChangeset
for help on using the changeset viewer.