Changeset 11640 for branches/Parameter-less Population Pyramid/HeuristicLab.Algorithms.ParameterlessPopulationPyramid
- Timestamp:
- 12/03/14 17:14:56 (10 years ago)
- Location:
- branches/Parameter-less Population Pyramid/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3
- Files:
-
- 1 added
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/Parameter-less Population Pyramid/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/HeuristicLab.Algorithms.ParameterlessPopulationPyramid-3.3.csproj
r11636 r11640 100 100 <ItemGroup> 101 101 <Compile Include="AlgorithmBase.cs" /> 102 <Compile Include="DeceptiveTrapProblem.cs" /> 102 103 <Compile Include="HillClimber.cs" /> 103 104 <Compile Include="Problems\BinaryVectorProblem.cs" /> -
branches/Parameter-less Population Pyramid/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/HillClimber.cs
r11637 r11640 27 27 using HeuristicLab.Encodings.BinaryVectorEncoding; 28 28 using HeuristicLab.Optimization; 29 using HeuristicLab.Parameters; 29 30 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 30 31 using HeuristicLab.Random; … … 38 39 [Storable] 39 40 private IRandom random; 41 42 private const string IterationsParameterName = "Iterations"; 43 44 public IFixedValueParameter<IntValue> IterationsParameter { 45 get { return (IFixedValueParameter<IntValue>)Parameters[IterationsParameterName]; } 46 } 47 48 public int Iterations { 49 get { return IterationsParameter.Value.Value; } 50 set { IterationsParameter.Value.Value = value; } 51 } 40 52 41 53 [StorableConstructor] … … 51 63 : base() { 52 64 random = new MersenneTwister(); 65 Parameters.Add(new FixedValueParameter<IntValue>(IterationsParameterName, "", new IntValue(100))); 53 66 } 54 67 protected override void Run() { 55 bool[] solution = new bool[Problem.Length]; 56 for (int i = 0; i < solution.Length; i++) { 57 solution[i] = random.Next(2) == 1; 68 var BestQuality = new DoubleValue(double.NaN); 69 Results.Add(new Result("Best quality", BestQuality)); 70 for (int iteration = 0; iteration < Iterations; iteration++) { 71 bool[] solution = new bool[Problem.Length]; 72 for (int i = 0; i < solution.Length; i++) { 73 solution[i] = random.Next(2) == 1; 74 } 75 76 var fitness = Problem.Evaluate(solution); 77 78 fitness = ImproveToLocalOptimum(Problem, solution, fitness, random); 79 if (double.IsNaN(BestQuality.Value) || Problem.IsBetter(fitness, BestQuality.Value)) { 80 BestQuality.Value = fitness; 81 } 58 82 } 59 var fitness = Problem.Evaluate(solution);60 fitness = ImproveToLocalOptimum(Problem, solution, fitness, random);61 62 Results.Add(new Result("Best quality", new DoubleValue(fitness)));63 Results.Add(new Result("Best solution", new BinaryVector(solution)));64 83 } 65 84 public static double ImproveToLocalOptimum(BinaryVectorProblem problem, bool[] solution, double fitness, IRandom rand) { … … 70 89 solution[option] = !solution[option]; 71 90 double newFitness = problem.Evaluate(solution); 72 if ( (problem.Maximization && fitness < newFitness) || (!problem.Maximization && fitness > newFitness)) {91 if (problem.IsBetter(newFitness, fitness)) { 73 92 fitness = newFitness; 74 93 tried.Clear(); -
branches/Parameter-less Population Pyramid/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/Problems/BinaryVectorProblem.cs
r11636 r11640 48 48 protected BinaryVectorProblem(bool deserializing) : base(deserializing) { } 49 49 protected BinaryVectorProblem(BinaryVectorProblem original, Cloner cloner) : base(original, cloner) { } 50 public bool IsBetter(double quality, double bestQuality) { 51 return (Maximization && quality > bestQuality || !Maximization && quality < bestQuality); 52 } 50 53 51 54 public BinaryVectorProblem() 52 55 : base() { 53 Parameters.Add(new FixedValueParameter<IntValue>(LengthParameterName, "", new IntValue( 5)));56 Parameters.Add(new FixedValueParameter<IntValue>(LengthParameterName, "", new IntValue(20))); 54 57 } 55 58 -
branches/Parameter-less Population Pyramid/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/Problems/OneMaxProblem.cs
r11637 r11640 42 42 public override double Evaluate(bool[] individual) { 43 43 if (individual.Length != Length) throw new ArgumentException("The individual has not the correct length."); 44 //return individual.Count(x => x);45 44 double quality = 0; 46 45 for (int i = 0; i < individual.Length; i++)
Note: See TracChangeset
for help on using the changeset viewer.