Changeset 13339 for branches/ProblemRefactoring
- Timestamp:
- 11/23/15 16:14:57 (9 years ago)
- Location:
- branches/ProblemRefactoring
- Files:
-
- 15 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/ProblemRefactoring/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/EvaluationTracker.cs
r12057 r13339 26 26 using HeuristicLab.Data; 27 27 using HeuristicLab.Encodings.BinaryVectorEncoding; 28 using HeuristicLab.Optimization; 28 29 using HeuristicLab.Parameters; 29 30 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 30 using HeuristicLab.Problems.Binary;31 31 32 32 namespace HeuristicLab.Algorithms.ParameterlessPopulationPyramid { … … 34 34 // B. W. Goldman and W. F. Punch, "Parameter-less Population Pyramid," GECCO, pp. 785–792, 2014 35 35 // and the original source code in C++11 available from: https://github.com/brianwgoldman/Parameter-less_Population_Pyramid 36 internal sealed class EvaluationTracker : BinaryProblem{37 private readonly BinaryProblemproblem;36 internal sealed class EvaluationTracker : SingleObjectiveProblem<BinaryVectorEncoding, BinaryVector> { 37 private readonly ISingleObjectiveProblem<BinaryVectorEncoding, BinaryVector> problem; 38 38 39 39 private int maxEvaluations; … … 59 59 private set; 60 60 } 61 62 public new BinaryVectorEncoding Encoding { 63 get { return problem.Encoding; } 64 } 61 65 #endregion 62 66 … … 75 79 return new EvaluationTracker(this, cloner); 76 80 } 77 public EvaluationTracker( BinaryProblemproblem, int maxEvaluations) {81 public EvaluationTracker(ISingleObjectiveProblem<BinaryVectorEncoding, BinaryVector> problem, int maxEvaluations) { 78 82 this.problem = problem; 79 83 this.maxEvaluations = maxEvaluations; 80 BestSolution = new BinaryVector( Length);84 BestSolution = new BinaryVector(problem.Encoding.Length); 81 85 BestQuality = double.NaN; 82 86 Evaluations = 0; … … 99 103 } 100 104 101 public override int Length {102 get { return problem.Length; }103 set { problem.Length = value; }104 }105 106 105 public override bool Maximization { 107 106 get { -
branches/ProblemRefactoring/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/HeuristicLab.Algorithms.ParameterlessPopulationPyramid-3.3.csproj
r11994 r13339 148 148 <Private>False</Private> 149 149 </ProjectReference> 150 <ProjectReference Include="..\..\HeuristicLab.Problems.Binary\3.3\HeuristicLab.Problems.Binary-3.3.csproj">151 <Project>{fc627be5-0f93-47d8-bd2e-530ea2b8aa5f}</Project>152 <Name>HeuristicLab.Problems.Binary-3.3</Name>153 <Private>False</Private>154 </ProjectReference>155 150 <ProjectReference Include="..\..\HeuristicLab.Random\3.3\HeuristicLab.Random-3.3.csproj"> 156 151 <Project>{f4539fb6-4708-40c9-be64-0a1390aea197}</Project> -
branches/ProblemRefactoring/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/HillClimber.cs
r13173 r13339 32 32 using HeuristicLab.Parameters; 33 33 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 34 using HeuristicLab.Problems.Binary;35 34 using HeuristicLab.Random; 36 35 … … 50 49 51 50 public override Type ProblemType { 52 get { return typeof( BinaryProblem); }51 get { return typeof(ISingleObjectiveProblem<BinaryVectorEncoding, BinaryVector>); } 53 52 } 54 public new BinaryProblemProblem {55 get { return ( BinaryProblem)base.Problem; }53 public new ISingleObjectiveProblem<BinaryVectorEncoding, BinaryVector> Problem { 54 get { return (ISingleObjectiveProblem<BinaryVectorEncoding, BinaryVector>)base.Problem; } 56 55 set { base.Problem = value; } 57 56 } … … 84 83 Results.Add(new Result("Best quality", BestQuality)); 85 84 for (int iteration = 0; iteration < Iterations; iteration++) { 86 var solution = new BinaryVector(Problem. Length);85 var solution = new BinaryVector(Problem.Encoding.Length); 87 86 for (int i = 0; i < solution.Length; i++) { 88 87 solution[i] = random.Next(2) == 1; … … 98 97 } 99 98 // In the GECCO paper, Section 2.1 100 public static double ImproveToLocalOptimum( BinaryProblemproblem, BinaryVector solution, double fitness, IRandom rand) {99 public static double ImproveToLocalOptimum(ISingleObjectiveProblem<BinaryVectorEncoding, BinaryVector> problem, BinaryVector solution, double fitness, IRandom rand) { 101 100 var tried = new HashSet<int>(); 102 101 do { -
branches/ProblemRefactoring/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/LinkageCrossover.cs
r12012 r13339 24 24 using HeuristicLab.Core; 25 25 using HeuristicLab.Encodings.BinaryVectorEncoding; 26 using HeuristicLab. Problems.Binary;26 using HeuristicLab.Optimization; 27 27 using HeuristicLab.Random; 28 28 … … 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, BinaryProblemproblem, IRandom rand) {35 public static double ImproveUsingTree(LinkageTree tree, IList<BinaryVector> donors, BinaryVector solution, double fitness, ISingleObjectiveProblem<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, BinaryProblemproblem, IRandom rand, out bool changed) {50 private static double Donate(BinaryVector solution, double fitness, BinaryVector source, IEnumerable<int> cluster, ISingleObjectiveProblem<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
r13173 r13339 32 32 using HeuristicLab.Parameters; 33 33 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 34 using HeuristicLab.Problems.Binary;35 34 using HeuristicLab.Random; 36 35 … … 44 43 public class ParameterlessPopulationPyramid : BasicAlgorithm { 45 44 public override Type ProblemType { 46 get { return typeof( BinaryProblem); }47 } 48 public new BinaryProblemProblem {49 get { return ( BinaryProblem)base.Problem; }45 get { return typeof(ISingleObjectiveProblem<BinaryVectorEncoding, BinaryVector>); } 46 } 47 public new ISingleObjectiveProblem<BinaryVectorEncoding, BinaryVector> Problem { 48 get { return (ISingleObjectiveProblem<BinaryVectorEncoding, BinaryVector>)base.Problem; } 50 49 set { base.Problem = value; } 51 50 } … … 183 182 if (seen.Contains(solution)) return; 184 183 if (level == pyramid.Count) { 185 pyramid.Add(new Population(tracker. Length, random));184 pyramid.Add(new Population(tracker.Encoding.Length, random)); 186 185 } 187 186 var copied = (BinaryVector)solution.Clone(); … … 193 192 private double iterate() { 194 193 // Create a random solution 195 BinaryVector solution = new BinaryVector(tracker. Length);194 BinaryVector solution = new BinaryVector(tracker.Encoding.Length); 196 195 for (int i = 0; i < solution.Length; i++) { 197 196 solution[i] = random.Next(2) == 1; … … 249 248 fitness = iterate(); 250 249 cancellationToken.ThrowIfCancellationRequested(); 251 } finally { 250 } 251 finally { 252 252 ResultsEvaluations = tracker.Evaluations; 253 253 ResultsBestSolution = new BinaryVector(tracker.BestSolution); -
branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/IMultiObjectiveProblemDefinition.cs
r13336 r13339 23 23 24 24 namespace HeuristicLab.Optimization { 25 public interface IMultiObjectiveProblemDefinition<TSolution> : IProblemDefinition 26 where TSolution : class, ISolution { 25 public interface IMultiObjectiveProblemDefinition<TEncoding, TSolution> : IProblemDefinition<TEncoding, TSolution> 26 where TEncoding : class, IEncoding<TSolution> 27 where TSolution : class, ISolution { 27 28 bool[] Maximization { get; } 28 29 double[] Evaluate(TSolution individual, IRandom random); -
branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/IProblemDefinition.cs
r13336 r13339 21 21 22 22 namespace HeuristicLab.Optimization { 23 public interface IProblemDefinition { 24 //IEncoding Encoding { get; } 23 public interface IProblemDefinition<TEncoding, TSolution> 24 where TEncoding : class, IEncoding<TSolution> 25 where TSolution : class, ISolution { 26 TEncoding Encoding { get; } 25 27 } 26 28 } -
branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/ISingleObjectiveProblem.cs
r13337 r13339 24 24 25 25 namespace HeuristicLab.Optimization { 26 public interface ISingleObjectiveProblem<TSolution> : IProblem<TSolution>, ISingleObjectiveProblemDefinition<TSolution>, ISingleObjectiveHeuristicOptimizationProblem 26 public interface ISingleObjectiveProblem<TEncoding, TSolution> : IProblem<TEncoding, TSolution>, ISingleObjectiveProblemDefinition<TEncoding, TSolution>, ISingleObjectiveHeuristicOptimizationProblem 27 where TEncoding : class, IEncoding<TSolution> 27 28 where TSolution : class, ISolution { 28 29 bool IsBetter(double quality, double bestQuality); -
branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/ISingleObjectiveProblemDefinition.cs
r13336 r13339 24 24 25 25 namespace HeuristicLab.Optimization { 26 public interface ISingleObjectiveProblemDefinition<TSolution> : IProblemDefinition 27 where TSolution : class, ISolution { 26 public interface ISingleObjectiveProblemDefinition<TEncoding, TSolution> : IProblemDefinition<TEncoding, TSolution> 27 where TEncoding : class, IEncoding<TSolution> 28 where TSolution : class, ISolution { 28 29 bool Maximization { get; } 29 30 double Evaluate(TSolution individual, IRandom random); -
branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/MultiObjectiveProblem.cs
r13336 r13339 29 29 namespace HeuristicLab.Optimization { 30 30 [StorableClass] 31 public abstract class MultiObjectiveProblem<TSolution> : Problem<TSolution, MultiObjectiveEvaluator<TSolution>>, IMultiObjectiveHeuristicOptimizationProblem, IMultiObjectiveProblemDefinition<TSolution> 32 where TSolution : class, ISolution { 31 public abstract class MultiObjectiveProblem<TEncoding, TSolution> : Problem<TEncoding, TSolution, MultiObjectiveEvaluator<TSolution>>, IMultiObjectiveHeuristicOptimizationProblem, IMultiObjectiveProblemDefinition<TEncoding, TSolution> 32 where TEncoding : class, IEncoding<TSolution> 33 where TSolution : class, ISolution { 33 34 [StorableConstructor] 34 35 protected MultiObjectiveProblem(bool deserializing) : base(deserializing) { } 35 36 36 protected MultiObjectiveProblem(MultiObjectiveProblem<T Solution> original, Cloner cloner)37 protected MultiObjectiveProblem(MultiObjectiveProblem<TEncoding, TSolution> original, Cloner cloner) 37 38 : base(original, cloner) { 38 39 ParameterizeOperators(); -
branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Problem.cs
r13336 r13339 30 30 namespace HeuristicLab.Optimization { 31 31 [StorableClass] 32 public abstract class Problem<TSolution, TEvaluator> : HeuristicOptimizationProblem<TEvaluator, ISolutionCreator<TSolution>>, IProblemDefinition, IStorableContent 32 public abstract class Problem<TEncoding, TSolution, TEvaluator> : HeuristicOptimizationProblem<TEvaluator, ISolutionCreator<TSolution>>, IProblemDefinition<TEncoding, TSolution>, IStorableContent 33 where TEncoding : class, IEncoding<TSolution> 33 34 where TSolution : class, ISolution 34 35 where TEvaluator : class, IEvaluator { … … 36 37 public string Filename { get; set; } 37 38 38 protected IValueParameter< IEncoding<TSolution>> EncodingParameter {39 get { return (IValueParameter< IEncoding<TSolution>>)Parameters["Encoding"]; }39 protected IValueParameter<TEncoding> EncodingParameter { 40 get { return (IValueParameter<TEncoding>)Parameters["Encoding"]; } 40 41 } 41 42 42 public IEncoding<TSolution>Encoding {43 public TEncoding Encoding { 43 44 get { return EncodingParameter.Value; } 44 45 protected set { … … 61 62 protected Problem() 62 63 : base() { 63 Parameters.Add(new ValueParameter<IEncoding<TSolution>>("Encoding", "Describes the configuration of the encoding, what the variables are called, what type they are and their bounds if any.")); 64 if (Encoding != null) Parameterize(); 64 Parameters.Add(new ValueParameter<TEncoding>("Encoding", "Describes the configuration of the encoding, what the variables are called, what type they are and their bounds if any.")); 65 if (Encoding != null) { 66 SolutionCreator = Encoding.SolutionCreator; 67 Parameterize(); 68 } 65 69 RegisterEvents(); 66 70 } 67 71 68 protected Problem(Problem<T Solution, TEvaluator> original, Cloner cloner)72 protected Problem(Problem<TEncoding, TSolution, TEvaluator> original, Cloner cloner) 69 73 : base(original, cloner) { 70 74 RegisterEvents(); -
branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/SingleObjectiveProblem.cs
r13336 r13339 30 30 namespace HeuristicLab.Optimization { 31 31 [StorableClass] 32 public abstract class SingleObjectiveProblem<T Solution> : Problem<TSolution, SingleObjectiveEvaluator<TSolution>>,33 ISingleObjectiveProblemDefinition<TSolution>, ISingleObjectiveHeuristicOptimizationProblem34 where TSolution : class, ISolution {32 public abstract class SingleObjectiveProblem<TEncoding, TSolution> : Problem<TEncoding, TSolution, SingleObjectiveEvaluator<TSolution>>, ISingleObjectiveProblem<TEncoding, TSolution> 33 where TEncoding : class, IEncoding<TSolution> 34 where TSolution : class, ISolution { 35 35 36 36 protected IValueParameter<DoubleValue> BestKnownQualityParameter { … … 52 52 protected SingleObjectiveProblem(bool deserializing) : base(deserializing) { } 53 53 54 protected SingleObjectiveProblem(SingleObjectiveProblem<T Solution> original, Cloner cloner)54 protected SingleObjectiveProblem(SingleObjectiveProblem<TEncoding, TSolution> original, Cloner cloner) 55 55 : base(original, cloner) { 56 56 ParameterizeOperators(); -
branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/Interfaces/IProblem.cs
r13336 r13339 36 36 } 37 37 38 public interface IProblem<TSolution> : IHeuristicOptimizationProblem where TSolution : class, ISolution { 39 IEncoding<TSolution> Encoding { get; } 38 public interface IProblem<TEncoding, TSolution> : IHeuristicOptimizationProblem, IProblemDefinition<TEncoding, TSolution> 39 where TEncoding : class, IEncoding<TSolution> 40 where TSolution : class, ISolution { 40 41 } 41 42 } -
branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/Problems/HeuristicOptimizationProblem.cs
r12012 r13339 99 99 } 100 100 101 pr otected virtualvoid SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {101 private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) { 102 102 OnSolutionCreatorChanged(); 103 103 } -
branches/ProblemRefactoring/HeuristicLab.Problems.Binary/3.3/BinaryProblem.cs
r13336 r13339 34 34 namespace HeuristicLab.Problems.Binary { 35 35 [StorableClass] 36 public abstract class BinaryProblem : SingleObjectiveProblem<BinaryVector > {36 public abstract class BinaryProblem : SingleObjectiveProblem<BinaryVectorEncoding, BinaryVector> { 37 37 public virtual int Length { 38 38 get { return Encoding.Length; } 39 39 set { Encoding.Length = value; } 40 }41 42 public new BinaryVectorEncoding Encoding {43 get { return (BinaryVectorEncoding)base.Encoding; }44 set { base.Encoding = value; }45 40 } 46 41 … … 65 60 var lengthParameter = new FixedValueParameter<IntValue>("Length", "The length of the BinaryVector.", new IntValue(10)); 66 61 Parameters.Add(lengthParameter); 67 Encoding = new BinaryVectorEncoding();68 62 Encoding.LengthParameter = lengthParameter; 69 63 RegisterEventHandlers();
Note: See TracChangeset
for help on using the changeset viewer.