Changeset 11669
- Timestamp:
- 12/07/14 17:25:50 (10 years ago)
- Location:
- branches/Parameter-less Population Pyramid/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3
- Files:
-
- 2 added
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/Parameter-less Population Pyramid/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/EnumerableBoolEqualityComparer.cs
r11667 r11669 23 23 using System.Collections.Generic; 24 24 using System.Linq; 25 using System.Text;26 using System.Threading.Tasks;27 25 28 26 namespace HeuristicLab.Algorithms.ParameterlessPopulationPyramid { … … 45 43 } 46 44 } 45 // combine in any remaining content 46 if (word > 1) { 47 hash ^= word; 48 } 47 49 return hash; 48 50 } -
branches/Parameter-less Population Pyramid/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/HeuristicLab.Algorithms.ParameterlessPopulationPyramid-3.3.csproj
r11667 r11669 106 106 <Compile Include="EnumerableBoolEqualityComparer.cs" /> 107 107 <Compile Include="LinkageCrossover.cs" /> 108 <Compile Include="ListExtensions.cs" /> 108 109 <Compile Include="Plugin.cs" /> 110 <Compile Include="Problems\DeceptiveStepTrapProblem.cs" /> 109 111 <Compile Include="Problems\DeceptiveTrapProblem.cs" /> 110 112 <Compile Include="HillClimber.cs" /> -
branches/Parameter-less Population Pyramid/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/LinkageTree.cs
r11667 r11669 27 27 28 28 namespace HeuristicLab.Algorithms.ParameterlessPopulationPyramid { 29 public static class ListExtensions { 30 public static IList<T> Swap<T>(this IList<T> list, int indexA, int indexB) { 31 T tmp = list[indexA]; 32 list[indexA] = list[indexB]; 33 list[indexB] = tmp; 34 return list; 35 } 36 public static IList<T> ShuffleInPlace<T>(this IList<T> list, IRandom random) { 37 return list.ShuffleInPlace(random, 0, list.Count - 1); 38 } 39 public static IList<T> ShuffleInPlace<T>(this IList<T> list, IRandom random, int maxIndex) { 40 return list.ShuffleInPlace(random, 0, maxIndex); 41 } 42 public static IList<T> ShuffleInPlace<T>(this IList<T> list, IRandom random, int minIndex, int maxIndex) { 43 for (int i = maxIndex; i > minIndex; i--) { 44 int swapIndex = random.Next(minIndex, i + 1); 45 list.Swap(i, swapIndex); 46 } 47 return list; 48 } 49 50 public static IEnumerable<T> ShuffleList<T>(this IList<T> source, IRandom random) { 51 for (int i = source.Count - 1; i > 0; i--) { 52 // Swap element "i" with a random earlier element (including itself) 53 int swapIndex = random.Next(i + 1); 54 source.Swap(i, swapIndex); 55 yield return source[i]; 56 } 57 yield return source[0]; 58 } 59 60 } 29 61 30 public class LinkageTree { 62 31 -
branches/Parameter-less Population Pyramid/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/ParameterlessPopulationPyramid.cs
r11668 r11669 47 47 // Tracks all solutions in Pyramid for quick membership checks 48 48 private HashSet<bool[]> seen = new HashSet<bool[]>(new EnumerableBoolEqualityComparer()); 49 49 50 #region ParameterNames 50 51 private const string MaximumIterationsParameterName = "Maximum Iterations"; 51 52 private const string MaximumEvaluationsParameterName = "Maximum Evaluations"; 53 private const string SeedParameterName = "Seed"; 54 private const string SetSeedRandomlyParameterName = "SetSeedRandomly"; 55 #endregion 56 57 #region ParameterProperties 52 58 public IFixedValueParameter<IntValue> MaximumIterationsParameter { 53 59 get { return (IFixedValueParameter<IntValue>)Parameters[MaximumIterationsParameterName]; } 54 60 } 55 61 public IFixedValueParameter<IntValue> MaximumEvaluationsParameter { 62 get { return (IFixedValueParameter<IntValue>)Parameters[MaximumEvaluationsParameterName]; } 63 } 64 public IFixedValueParameter<IntValue> SeedParameter { 65 get { return (IFixedValueParameter<IntValue>)Parameters[SeedParameterName]; } 66 } 67 public FixedValueParameter<BoolValue> SetSeedRandomlyParameter { 68 get { return (FixedValueParameter<BoolValue>)Parameters[SetSeedRandomlyParameterName]; } 69 } 70 #endregion 71 72 #region Properties 56 73 public int MaximumIterations { 57 74 get { return MaximumIterationsParameter.Value.Value; } … … 59 76 } 60 77 61 private const string MaximumEvaluationsParameterName = "Maximum Evaluations";62 63 public IFixedValueParameter<IntValue> MaximumEvaluationsParameter {64 get { return (IFixedValueParameter<IntValue>)Parameters[MaximumEvaluationsParameterName]; }65 }66 67 78 public int MaximumEvaluations { 68 79 get { return MaximumEvaluationsParameter.Value.Value; } … … 70 81 } 71 82 72 73 private const string SeedParameterName = "Seed";74 75 public IFixedValueParameter<IntValue> SeedParameter {76 get { return (IFixedValueParameter<IntValue>)Parameters[SeedParameterName]; }77 }78 79 83 public int Seed { 80 84 get { return SeedParameter.Value.Value; } … … 82 86 } 83 87 84 private const string SetSeedRandomlyParameterName = "SetSeedRandomly";85 86 public FixedValueParameter<BoolValue> SetSeedRandomlyParameter {87 get { return (FixedValueParameter<BoolValue>)Parameters[SetSeedRandomlyParameterName]; }88 }89 90 88 public bool SetSeedRandomly { 91 89 get { return SetSeedRandomlyParameter.Value.Value; } 92 90 set { SetSeedRandomlyParameter.Value.Value = value; } 93 91 } 92 #endregion 94 93 95 94 #region ResultsProperties … … 128 127 get { return ResultsQualities.Rows["Iteration Quality"]; } 129 128 } 130 131 129 #endregion 132 130 … … 183 181 184 182 protected override void Run() { 183 // Set up the algorithm 185 184 if (SetSeedRandomly) Seed = new System.Random().Next(); 186 185 pyramid = new List<Population>(); … … 188 187 random.Reset(Seed); 189 188 tracker = new EvaluationTracker(Problem, MaximumEvaluations); 189 190 // Set up the results display 190 191 Results.Add(new Result("Iterations", new IntValue(0))); 191 192 Results.Add(new Result("Evaluations", new IntValue(0))); … … 199 200 table.Rows.Add(iterationRows); 200 201 Results.Add(new Result("Qualities", table)); 202 203 // Loop until iteration limit reached or canceled. 201 204 for (ResultsIterations = 0; ResultsIterations < MaximumIterations; ResultsIterations++) { 202 205 double fitness = double.NaN; -
branches/Parameter-less Population Pyramid/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/Population.cs
r11664 r11669 20 20 #endregion 21 21 22 using System;23 22 using System.Collections.Generic; 24 using System.Linq;25 using System.Text;26 using System.Threading.Tasks;27 23 using HeuristicLab.Core; 28 24 29 25 namespace HeuristicLab.Algorithms.ParameterlessPopulationPyramid { 30 26 public class Population { 31 private readonly List<bool[]> solutions = new List<bool[]>(); 32 private LinkageTree tree; 27 public List<bool[]> Solutions { 28 get; 29 private set; 30 } 33 31 34 32 public LinkageTree Tree { 35 get { return tree; } 36 } 37 38 public List<bool[]> Solutions { 39 get { return solutions; } 33 get; 34 private set; 40 35 } 41 36 42 37 public Population(int length, IRandom rand) { 43 tree = new LinkageTree(length, rand); 38 Solutions = new List<bool[]>(); 39 Tree = new LinkageTree(length, rand); 44 40 } 45 41 public void Add(bool[] solution) { 46 solutions.Add(solution);47 tree.Add(solution);42 Solutions.Add(solution); 43 Tree.Add(solution); 48 44 } 49 45 } -
branches/Parameter-less Population Pyramid/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/Problems/DeceptiveTrapProblem.cs
r11666 r11669 22 22 using System; 23 23 using System.Collections.Generic; 24 using System.Linq;25 using System.Text;26 using System.Threading.Tasks;27 24 using HeuristicLab.Common; 28 25 using HeuristicLab.Core; … … 59 56 set { TrapSizeParameter.Value.Value = value; } 60 57 } 58 59 protected virtual int TrapMaximum { 60 get { return TrapSize; } 61 } 62 61 63 public DeceptiveTrapProblem() 62 64 : base() { 63 Parameters.Add(new FixedValueParameter<IntValue>(TrapSizeParameterName, "", new IntValue(5))); 65 Parameters.Add(new FixedValueParameter<IntValue>(TrapSizeParameterName, "", new IntValue(7))); 66 Length = 49; 67 } 68 69 protected virtual int Score(bool[] individual, int trapIndex) { 70 int result = 0; 71 for (int index = trapIndex; index < trapIndex + TrapSize; index++) { 72 if (individual[index]) result++; 73 } 74 75 // Make it deceptive 76 if (result < TrapSize) { 77 result = TrapSize - result - 1; 78 } 79 return result; 64 80 } 65 81 … … 68 84 int total = 0; 69 85 for (int i = 0; i < individual.Length; i += TrapSize) { 70 int partial = 0; 71 for (int index = i; index < i + TrapSize; index++) { 72 if (individual[index]) partial++; 73 } 74 75 // Make it deceptive 76 if (partial < TrapSize) { 77 partial = TrapSize - partial - 1; 78 } 79 total += partial; 86 total += Score(individual, i); 80 87 } 81 return total;88 return (double)(total * TrapSize) / (TrapMaximum * individual.Length); 82 89 } 83 90 } -
branches/Parameter-less Population Pyramid/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/Problems/EvaluationTracker.cs
r11666 r11669 22 22 using System; 23 23 using System.Collections.Generic; 24 using System.Linq;25 using System.Text;26 using System.Threading.Tasks;27 24 28 25 namespace HeuristicLab.Algorithms.ParameterlessPopulationPyramid { 29 26 public class EvaluationTracker : IBinaryVectorProblem { 30 27 private readonly IBinaryVectorProblem problem; 28 31 29 private int maxEvaluations; 32 30 33 private double bestQuality = double.NaN;31 #region Properties 34 32 public double BestQuality { 35 get { return bestQuality; } 33 get; 34 private set; 36 35 } 37 36 38 private int evaluations = 0;39 37 public int Evaluations { 40 get { return evaluations; } 38 get; 39 private set; 41 40 } 42 41 43 private int bestFoundOnEvaluation = 0;44 42 public int BestFoundOnEvaluation { 45 get { return bestFoundOnEvaluation; } 43 get; 44 private set; 46 45 } 47 46 48 47 public bool[] BestSolution { 49 get; private set; 48 get; 49 private set; 50 50 } 51 #endregion 51 52 52 53 public EvaluationTracker(IBinaryVectorProblem problem, int maxEvaluations) { … … 54 55 this.maxEvaluations = maxEvaluations; 55 56 BestSolution = new bool[0]; 57 BestQuality = double.NaN; 58 Evaluations = 0; 59 BestFoundOnEvaluation = 0; 56 60 } 57 61 58 62 public double Evaluate(bool[] individual) { 59 if ( evaluations >= maxEvaluations) throw new OperationCanceledException("Maximum Evaluation Limit Reached");60 evaluations++;63 if (Evaluations >= maxEvaluations) throw new OperationCanceledException("Maximum Evaluation Limit Reached"); 64 Evaluations++; 61 65 double fitness = problem.Evaluate(individual); 62 if (double.IsNaN( bestQuality) || problem.IsBetter(fitness, bestQuality)) {63 bestQuality = fitness;66 if (double.IsNaN(BestQuality) || problem.IsBetter(fitness, BestQuality)) { 67 BestQuality = fitness; 64 68 BestSolution = (bool[])individual.Clone(); 65 bestFoundOnEvaluation = evaluations;69 BestFoundOnEvaluation = Evaluations; 66 70 } 67 71 return fitness; 68 72 } 69 73 70 74 #region ForwardedInteraface 71 75 public int Length { 72 76 get { return problem.Length; } … … 77 81 public bool IsBetter(double quality, double bestQuality) { 78 82 return problem.IsBetter(quality, bestQuality); 79 } 83 } 84 #endregion 80 85 } 81 86 } -
branches/Parameter-less Population Pyramid/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/Problems/HIFFProblem.cs
r11666 r11669 22 22 using System; 23 23 using System.Collections.Generic; 24 using System.Linq;25 using System.Text;26 using System.Threading.Tasks;27 24 using HeuristicLab.Common; 28 25 using HeuristicLab.Core; … … 48 45 49 46 public HIFFProblem() : base() { 50 Length = 32;47 Length = 64; 51 48 } 52 49 -
branches/Parameter-less Population Pyramid/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/Problems/IBinaryVectorProblem.cs
r11666 r11669 20 20 #endregion 21 21 22 using System;23 using System.Collections.Generic;24 using System.Linq;25 using System.Text;26 using System.Threading.Tasks;27 28 22 namespace HeuristicLab.Algorithms.ParameterlessPopulationPyramid { 29 23 public interface IBinaryVectorProblem {
Note: See TracChangeset
for help on using the changeset viewer.