Changeset 14700
- Timestamp:
- 02/23/17 19:51:45 (8 years ago)
- Location:
- branches/ichiriac/HeuristicLab.Algorithms.DifferentialEvolution
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/ichiriac/HeuristicLab.Algorithms.DifferentialEvolution
-
Property
svn:ignore
set to
obj
-
Property
svn:ignore
set to
-
branches/ichiriac/HeuristicLab.Algorithms.DifferentialEvolution/DifferentialEvolution.cs
r14086 r14700 34 34 using System.Threading; 35 35 36 namespace HeuristicLab.Algorithms.DifferentialEvolution 37 { 38 39 [Item("Differential Evolution (DE)", "A differential evolution algorithm.")] 40 [StorableClass] 41 [Creatable(CreatableAttribute.Categories.PopulationBasedAlgorithms, Priority = 400)] 42 public class DifferentialEvolution : BasicAlgorithm 43 { 44 public Func<IEnumerable<double>, double> Evaluation; 45 46 public override Type ProblemType 47 { 48 get { return typeof(SingleObjectiveTestFunctionProblem); } 49 } 50 public new SingleObjectiveTestFunctionProblem Problem 51 { 52 get { return (SingleObjectiveTestFunctionProblem)base.Problem; } 53 set { base.Problem = value; } 54 } 55 56 private readonly IRandom _random = new MersenneTwister(); 57 private int evals; 58 59 #region ParameterNames 60 private const string MaximumEvaluationsParameterName = "Maximum Evaluations"; 61 private const string SeedParameterName = "Seed"; 62 private const string SetSeedRandomlyParameterName = "SetSeedRandomly"; 63 private const string CrossoverProbabilityParameterName = "CrossoverProbability"; 64 private const string PopulationSizeParameterName = "PopulationSize"; 65 private const string ScalingFactorParameterName = "ScalingFactor"; 66 private const string ValueToReachParameterName = "ValueToReach"; 67 #endregion 68 69 #region ParameterProperties 70 public IFixedValueParameter<IntValue> MaximumEvaluationsParameter 71 { 72 get { return (IFixedValueParameter<IntValue>)Parameters[MaximumEvaluationsParameterName]; } 73 } 74 public IFixedValueParameter<IntValue> SeedParameter 75 { 76 get { return (IFixedValueParameter<IntValue>)Parameters[SeedParameterName]; } 77 } 78 public FixedValueParameter<BoolValue> SetSeedRandomlyParameter 79 { 80 get { return (FixedValueParameter<BoolValue>)Parameters[SetSeedRandomlyParameterName]; } 81 } 82 private ValueParameter<IntValue> PopulationSizeParameter 83 { 84 get { return (ValueParameter<IntValue>)Parameters[PopulationSizeParameterName]; } 85 } 86 public ValueParameter<DoubleValue> CrossoverProbabilityParameter 87 { 88 get { return (ValueParameter<DoubleValue>)Parameters[CrossoverProbabilityParameterName]; } 89 } 90 public ValueParameter<DoubleValue> ScalingFactorParameter 91 { 92 get { return (ValueParameter<DoubleValue>)Parameters[ScalingFactorParameterName]; } 93 } 94 public ValueParameter<DoubleValue> ValueToReachParameter 95 { 96 get { return (ValueParameter<DoubleValue>)Parameters[ValueToReachParameterName]; } 97 } 98 #endregion 99 100 #region Properties 101 public int MaximumEvaluations 102 { 103 get { return MaximumEvaluationsParameter.Value.Value; } 104 set { MaximumEvaluationsParameter.Value.Value = value; } 105 } 106 107 public Double CrossoverProbability 108 { 109 get { return CrossoverProbabilityParameter.Value.Value; } 110 set { CrossoverProbabilityParameter.Value.Value = value; } 111 } 112 public Double ScalingFactor 113 { 114 get { return ScalingFactorParameter.Value.Value; } 115 set { ScalingFactorParameter.Value.Value = value; } 116 } 117 public int Seed 118 { 119 get { return SeedParameter.Value.Value; } 120 set { SeedParameter.Value.Value = value; } 121 } 122 public bool SetSeedRandomly 123 { 124 get { return SetSeedRandomlyParameter.Value.Value; } 125 set { SetSeedRandomlyParameter.Value.Value = value; } 126 } 127 public IntValue PopulationSize 128 { 129 get { return PopulationSizeParameter.Value; } 130 set { PopulationSizeParameter.Value = value; } 131 } 132 public Double ValueToReach 133 { 134 get { return ValueToReachParameter.Value.Value; } 135 set { ValueToReachParameter.Value.Value = value; } 136 } 137 #endregion 138 139 #region ResultsProperties 140 private double ResultsBestQuality 141 { 142 get { return ((DoubleValue)Results["Best Quality"].Value).Value; } 143 set { ((DoubleValue)Results["Best Quality"].Value).Value = value; } 144 } 145 146 private double VTRBestQuality 147 { 148 get { return ((DoubleValue)Results["VTR"].Value).Value; } 149 set { ((DoubleValue)Results["VTR"].Value).Value = value; } 150 } 151 152 private RealVector ResultsBestSolution 153 { 154 get { return (RealVector)Results["Best Solution"].Value; } 155 set { Results["Best Solution"].Value = value; } 156 } 157 158 private int ResultsEvaluations 159 { 160 get { return ((IntValue)Results["Evaluations"].Value).Value; } 161 set { ((IntValue)Results["Evaluations"].Value).Value = value; } 162 } 163 private int ResultsIterations 164 { 165 get { return ((IntValue)Results["Iterations"].Value).Value; } 166 set { ((IntValue)Results["Iterations"].Value).Value = value; } 167 } 168 169 private DataTable ResultsQualities 170 { 171 get { return ((DataTable)Results["Qualities"].Value); } 172 } 173 private DataRow ResultsQualitiesBest 174 { 175 get { return ResultsQualities.Rows["Best Quality"]; } 176 } 177 178 #endregion 179 180 [StorableConstructor] 181 protected DifferentialEvolution(bool deserializing) : base(deserializing) { } 182 183 protected DifferentialEvolution(DifferentialEvolution original, Cloner cloner) 184 : base(original, cloner) 185 { 186 } 187 188 public override IDeepCloneable Clone(Cloner cloner) 189 { 190 return new DifferentialEvolution(this, cloner); 191 } 192 193 public DifferentialEvolution() 194 { 195 Parameters.Add(new FixedValueParameter<IntValue>(MaximumEvaluationsParameterName, "", new IntValue(Int32.MaxValue))); 196 Parameters.Add(new FixedValueParameter<IntValue>(SeedParameterName, "The random seed used to initialize the new pseudo random number generator.", new IntValue(0))); 197 Parameters.Add(new FixedValueParameter<BoolValue>(SetSeedRandomlyParameterName, "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true))); 198 Parameters.Add(new ValueParameter<IntValue>(PopulationSizeParameterName, "The size of the population of solutions.", new IntValue(100))); 199 Parameters.Add(new ValueParameter<DoubleValue>(CrossoverProbabilityParameterName, "The value for crossover rate", new DoubleValue(0.88))); 200 Parameters.Add(new ValueParameter<DoubleValue>(ScalingFactorParameterName, "The value for scaling factor", new DoubleValue(0.47))); 201 Parameters.Add(new ValueParameter<DoubleValue>(ValueToReachParameterName, "Value to reach (VTR) parameter", new DoubleValue(0.00000001))); 202 } 203 204 protected override void Run(CancellationToken cancellationToken) 205 { 206 207 // Set up the results display 208 Results.Add(new Result("Iterations", new IntValue(0))); 209 Results.Add(new Result("Evaluations", new IntValue(0))); 210 Results.Add(new Result("Best Solution", new RealVector())); 211 Results.Add(new Result("Best Quality", new DoubleValue(double.NaN))); 212 Results.Add(new Result("VTR", new DoubleValue(double.NaN))); 213 var table = new DataTable("Qualities"); 214 table.Rows.Add(new DataRow("Best Quality")); 215 Results.Add(new Result("Qualities", table)); 216 217 218 //problem variables 219 var dim = Problem.ProblemSize.Value; 220 var lb = Problem.Bounds[0, 0]; 221 var ub = Problem.Bounds[0, 1]; 222 var range = ub - lb; 223 this.evals = 0; 224 225 double[,] populationOld = new double[PopulationSizeParameter.Value.Value, Problem.ProblemSize.Value]; 226 double[,] mutationPopulation = new double[PopulationSizeParameter.Value.Value, Problem.ProblemSize.Value]; 227 double[,] trialPopulation = new double[PopulationSizeParameter.Value.Value, Problem.ProblemSize.Value]; 228 double[] bestPopulation = new double[Problem.ProblemSize.Value]; 229 double[] bestPopulationIteration = new double[Problem.ProblemSize.Value]; 230 231 //create initial population 232 //population is a matrix of size PopulationSize*ProblemSize 233 for (int i = 0; i < PopulationSizeParameter.Value.Value; i++) 234 { 235 for (int j = 0; j < Problem.ProblemSize.Value; j++) 236 { 237 populationOld[i, j] = _random.NextDouble() * range + lb; 238 } 36 namespace HeuristicLab.Algorithms.DifferentialEvolution { 37 38 [Item("Differential Evolution (DE)", "A differential evolution algorithm.")] 39 [StorableClass] 40 [Creatable(CreatableAttribute.Categories.PopulationBasedAlgorithms, Priority = 400)] 41 public class DifferentialEvolution : BasicAlgorithm { 42 public Func<IEnumerable<double>, double> Evaluation; 43 44 public override Type ProblemType { 45 get { return typeof(SingleObjectiveTestFunctionProblem); } 46 } 47 public new SingleObjectiveTestFunctionProblem Problem { 48 get { return (SingleObjectiveTestFunctionProblem)base.Problem; } 49 set { base.Problem = value; } 50 } 51 52 private readonly IRandom _random = new MersenneTwister(); 53 private int evals; 54 55 #region ParameterNames 56 private const string MaximumEvaluationsParameterName = "Maximum Evaluations"; 57 private const string SeedParameterName = "Seed"; 58 private const string SetSeedRandomlyParameterName = "SetSeedRandomly"; 59 private const string CrossoverProbabilityParameterName = "CrossoverProbability"; 60 private const string PopulationSizeParameterName = "PopulationSize"; 61 private const string ScalingFactorParameterName = "ScalingFactor"; 62 private const string ValueToReachParameterName = "ValueToReach"; 63 #endregion 64 65 #region ParameterProperties 66 public IFixedValueParameter<IntValue> MaximumEvaluationsParameter { 67 get { return (IFixedValueParameter<IntValue>)Parameters[MaximumEvaluationsParameterName]; } 68 } 69 public IFixedValueParameter<IntValue> SeedParameter { 70 get { return (IFixedValueParameter<IntValue>)Parameters[SeedParameterName]; } 71 } 72 public FixedValueParameter<BoolValue> SetSeedRandomlyParameter { 73 get { return (FixedValueParameter<BoolValue>)Parameters[SetSeedRandomlyParameterName]; } 74 } 75 private ValueParameter<IntValue> PopulationSizeParameter { 76 get { return (ValueParameter<IntValue>)Parameters[PopulationSizeParameterName]; } 77 } 78 public ValueParameter<DoubleValue> CrossoverProbabilityParameter { 79 get { return (ValueParameter<DoubleValue>)Parameters[CrossoverProbabilityParameterName]; } 80 } 81 public ValueParameter<DoubleValue> ScalingFactorParameter { 82 get { return (ValueParameter<DoubleValue>)Parameters[ScalingFactorParameterName]; } 83 } 84 public ValueParameter<DoubleValue> ValueToReachParameter { 85 get { return (ValueParameter<DoubleValue>)Parameters[ValueToReachParameterName]; } 86 } 87 #endregion 88 89 #region Properties 90 public int MaximumEvaluations { 91 get { return MaximumEvaluationsParameter.Value.Value; } 92 set { MaximumEvaluationsParameter.Value.Value = value; } 93 } 94 95 public Double CrossoverProbability { 96 get { return CrossoverProbabilityParameter.Value.Value; } 97 set { CrossoverProbabilityParameter.Value.Value = value; } 98 } 99 public Double ScalingFactor { 100 get { return ScalingFactorParameter.Value.Value; } 101 set { ScalingFactorParameter.Value.Value = value; } 102 } 103 public int Seed { 104 get { return SeedParameter.Value.Value; } 105 set { SeedParameter.Value.Value = value; } 106 } 107 public bool SetSeedRandomly { 108 get { return SetSeedRandomlyParameter.Value.Value; } 109 set { SetSeedRandomlyParameter.Value.Value = value; } 110 } 111 public IntValue PopulationSize { 112 get { return PopulationSizeParameter.Value; } 113 set { PopulationSizeParameter.Value = value; } 114 } 115 public Double ValueToReach { 116 get { return ValueToReachParameter.Value.Value; } 117 set { ValueToReachParameter.Value.Value = value; } 118 } 119 #endregion 120 121 #region ResultsProperties 122 private double ResultsBestQuality { 123 get { return ((DoubleValue)Results["Best Quality"].Value).Value; } 124 set { ((DoubleValue)Results["Best Quality"].Value).Value = value; } 125 } 126 127 private double VTRBestQuality { 128 get { return ((DoubleValue)Results["VTR"].Value).Value; } 129 set { ((DoubleValue)Results["VTR"].Value).Value = value; } 130 } 131 132 private RealVector ResultsBestSolution { 133 get { return (RealVector)Results["Best Solution"].Value; } 134 set { Results["Best Solution"].Value = value; } 135 } 136 137 private int ResultsEvaluations { 138 get { return ((IntValue)Results["Evaluations"].Value).Value; } 139 set { ((IntValue)Results["Evaluations"].Value).Value = value; } 140 } 141 private int ResultsIterations { 142 get { return ((IntValue)Results["Iterations"].Value).Value; } 143 set { ((IntValue)Results["Iterations"].Value).Value = value; } 144 } 145 146 private DataTable ResultsQualities { 147 get { return ((DataTable)Results["Qualities"].Value); } 148 } 149 private DataRow ResultsQualitiesBest { 150 get { return ResultsQualities.Rows["Best Quality"]; } 151 } 152 153 #endregion 154 155 [StorableConstructor] 156 protected DifferentialEvolution(bool deserializing) : base(deserializing) { } 157 158 protected DifferentialEvolution(DifferentialEvolution original, Cloner cloner) 159 : base(original, cloner) { 160 } 161 162 public override IDeepCloneable Clone(Cloner cloner) { 163 return new DifferentialEvolution(this, cloner); 164 } 165 166 public DifferentialEvolution() { 167 Parameters.Add(new FixedValueParameter<IntValue>(MaximumEvaluationsParameterName, "", new IntValue(Int32.MaxValue))); 168 Parameters.Add(new FixedValueParameter<IntValue>(SeedParameterName, "The random seed used to initialize the new pseudo random number generator.", new IntValue(0))); 169 Parameters.Add(new FixedValueParameter<BoolValue>(SetSeedRandomlyParameterName, "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true))); 170 Parameters.Add(new ValueParameter<IntValue>(PopulationSizeParameterName, "The size of the population of solutions.", new IntValue(100))); 171 Parameters.Add(new ValueParameter<DoubleValue>(CrossoverProbabilityParameterName, "The value for crossover rate", new DoubleValue(0.88))); 172 Parameters.Add(new ValueParameter<DoubleValue>(ScalingFactorParameterName, "The value for scaling factor", new DoubleValue(0.47))); 173 Parameters.Add(new ValueParameter<DoubleValue>(ValueToReachParameterName, "Value to reach (VTR) parameter", new DoubleValue(0.00000001))); 174 } 175 176 protected override void Run(CancellationToken cancellationToken) { 177 178 // Set up the results display 179 Results.Add(new Result("Iterations", new IntValue(0))); 180 Results.Add(new Result("Evaluations", new IntValue(0))); 181 Results.Add(new Result("Best Solution", new RealVector())); 182 Results.Add(new Result("Best Quality", new DoubleValue(double.NaN))); 183 Results.Add(new Result("VTR", new DoubleValue(double.NaN))); 184 var table = new DataTable("Qualities"); 185 table.Rows.Add(new DataRow("Best Quality")); 186 Results.Add(new Result("Qualities", table)); 187 188 189 //problem variables 190 var dim = Problem.ProblemSize.Value; 191 var lb = Problem.Bounds[0, 0]; 192 var ub = Problem.Bounds[0, 1]; 193 var range = ub - lb; 194 this.evals = 0; 195 196 double[,] populationOld = new double[PopulationSizeParameter.Value.Value, Problem.ProblemSize.Value]; 197 double[,] mutationPopulation = new double[PopulationSizeParameter.Value.Value, Problem.ProblemSize.Value]; 198 double[,] trialPopulation = new double[PopulationSizeParameter.Value.Value, Problem.ProblemSize.Value]; 199 double[] bestPopulation = new double[Problem.ProblemSize.Value]; 200 double[] bestPopulationIteration = new double[Problem.ProblemSize.Value]; 201 202 //create initial population 203 //population is a matrix of size PopulationSize*ProblemSize 204 for(int i = 0; i < PopulationSizeParameter.Value.Value; i++) { 205 for(int j = 0; j < Problem.ProblemSize.Value; j++) { 206 populationOld[i, j] = _random.NextDouble() * range + lb; 207 } 208 } 209 210 //evaluate the best member after the intialiazation 211 //the idea is to select first member and after that to check the others members from the population 212 213 int best_index = 0; 214 double[] populationRow = new double[Problem.ProblemSize.Value]; 215 double[] qualityPopulation = new double[PopulationSizeParameter.Value.Value]; 216 bestPopulation = getMatrixRow(populationOld, best_index); 217 RealVector bestPopulationVector = new RealVector(bestPopulation); 218 double bestPopulationValue = Obj(bestPopulationVector); 219 qualityPopulation[best_index] = bestPopulationValue; 220 RealVector selectionVector; 221 RealVector trialVector; 222 double qtrial; 223 224 225 for(var i = 1; i < PopulationSizeParameter.Value.Value; i++) { 226 populationRow = getMatrixRow(populationOld, i); 227 trialVector = new RealVector(populationRow); 228 229 qtrial = Obj(trialVector); 230 qualityPopulation[i] = qtrial; 231 232 if(qtrial > bestPopulationValue) { 233 bestPopulationVector = new RealVector(populationRow); 234 bestPopulationValue = qtrial; 235 best_index = i; 236 } 237 } 238 239 int iterations = 1; 240 241 // Loop until iteration limit reached or canceled. 242 // todo replace with a function 243 // && bestPopulationValue > Problem.BestKnownQuality.Value + ValueToReachParameter.Value.Value 244 while(ResultsEvaluations < MaximumEvaluations 245 && !cancellationToken.IsCancellationRequested 246 && (bestPopulationValue - Problem.BestKnownQuality.Value) > ValueToReachParameter.Value.Value) { 247 //mutation DE/rand/1/bin; classic DE 248 for(int i = 0; i < PopulationSizeParameter.Value.Value; i++) { 249 int r0, r1, r2; 250 251 //assure the selected vectors r0, r1 and r2 are different 252 do { 253 r0 = _random.Next(0, PopulationSizeParameter.Value.Value); 254 } while(r0 == i); 255 do { 256 r1 = _random.Next(0, PopulationSizeParameter.Value.Value); 257 } while(r1 == i || r1 == r0); 258 do { 259 r2 = _random.Next(0, PopulationSizeParameter.Value.Value); 260 } while(r2 == i || r2 == r0 || r2 == r1); 261 262 for(int j = 0; j < getMatrixRow(mutationPopulation, i).Length; j++) { 263 mutationPopulation[i, j] = populationOld[r0, j] + 264 ScalingFactorParameter.Value.Value * (populationOld[r1, j] - populationOld[r2, j]); 265 //check the problem upper and lower bounds 266 if(mutationPopulation[i, j] > ub) mutationPopulation[i, j] = ub; 267 if(mutationPopulation[i, j] < lb) mutationPopulation[i, j] = lb; 268 } 269 } 270 271 //uniform crossover 272 for(int i = 0; i < PopulationSizeParameter.Value.Value; i++) { 273 double rnbr = _random.Next(0, Problem.ProblemSize.Value); 274 for(int j = 0; j < getMatrixRow(mutationPopulation, i).Length; j++) { 275 if(_random.NextDouble() <= CrossoverProbabilityParameter.Value.Value || j == rnbr) { 276 trialPopulation[i, j] = mutationPopulation[i, j]; 277 } else { 278 trialPopulation[i, j] = populationOld[i, j]; 239 279 } 240 241 //evaluate the best member after the intialiazation 242 //the idea is to select first member and after that to check the others members from the population 243 244 int best_index = 0; 245 double[] populationRow = new double[Problem.ProblemSize.Value]; 246 double[] qualityPopulation = new double[PopulationSizeParameter.Value.Value]; 247 bestPopulation = getMatrixRow(populationOld, best_index); 248 RealVector bestPopulationVector = new RealVector(bestPopulation); 249 double bestPopulationValue = Obj(bestPopulationVector); 250 qualityPopulation[best_index] = bestPopulationValue; 251 RealVector selectionVector; 252 RealVector trialVector; 253 double qtrial; 254 255 256 for (var i = 1; i < PopulationSizeParameter.Value.Value; i++) 257 { 258 populationRow = getMatrixRow(populationOld, i); 259 trialVector = new RealVector(populationRow); 260 261 qtrial = Obj(trialVector); 262 qualityPopulation[i] = qtrial; 263 264 if (qtrial > bestPopulationValue) 265 { 266 bestPopulationVector = new RealVector(populationRow); 267 bestPopulationValue = qtrial; 268 best_index = i; 269 } 280 } 281 } 282 283 //One-to-One Survivor Selection 284 for(int i = 0; i < PopulationSizeParameter.Value.Value; i++) { 285 selectionVector = new RealVector(getMatrixRow(populationOld, i)); 286 trialVector = new RealVector(getMatrixRow(trialPopulation, i)); 287 288 var selectionEval = qualityPopulation[i]; 289 var trialEval = Obj(trialVector); 290 291 if(trialEval < selectionEval) { 292 for(int j = 0; j < getMatrixRow(populationOld, i).Length; j++) { 293 populationOld[i, j] = trialPopulation[i, j]; 270 294 } 271 272 int iterations = 1; 273 274 // Loop until iteration limit reached or canceled. 275 // todo replace with a function 276 // && bestPopulationValue > Problem.BestKnownQuality.Value + ValueToReachParameter.Value.Value 277 while (ResultsEvaluations < MaximumEvaluations 278 && !cancellationToken.IsCancellationRequested 279 && (bestPopulationValue - Problem.BestKnownQuality.Value) > ValueToReachParameter.Value.Value) 280 { 281 //mutation DE/rand/1/bin; classic DE 282 for (int i = 0; i < PopulationSizeParameter.Value.Value; i++) 283 { 284 int r0, r1, r2; 285 286 //assure the selected vectors r0, r1 and r2 are different 287 do 288 { 289 r0 = _random.Next(0, PopulationSizeParameter.Value.Value); 290 } while (r0 == i); 291 do 292 { 293 r1 = _random.Next(0, PopulationSizeParameter.Value.Value); 294 } while (r1 == i || r1 == r0); 295 do 296 { 297 r2 = _random.Next(0, PopulationSizeParameter.Value.Value); 298 } while (r2 == i || r2 == r0 || r2 == r1); 299 300 for (int j = 0; j < getMatrixRow(mutationPopulation, i).Length; j++) 301 { 302 mutationPopulation[i, j] = populationOld[r0, j] + 303 ScalingFactorParameter.Value.Value * (populationOld[r1, j] - populationOld[r2, j]); 304 //check the problem upper and lower bounds 305 if (mutationPopulation[i, j] > ub) mutationPopulation[i, j] = ub; 306 if (mutationPopulation[i, j] < lb) mutationPopulation[i, j] = lb; 307 } 308 } 309 310 //uniform crossover 311 for (int i = 0; i < PopulationSizeParameter.Value.Value; i++) 312 { 313 double rnbr = _random.Next(0, Problem.ProblemSize.Value); 314 for (int j = 0; j < getMatrixRow(mutationPopulation, i).Length; j++) 315 { 316 if (_random.NextDouble() <= CrossoverProbabilityParameter.Value.Value || j == rnbr) 317 { 318 trialPopulation[i, j] = mutationPopulation[i, j]; 319 } 320 else 321 { 322 trialPopulation[i, j] = populationOld[i, j]; 323 } 324 } 325 } 326 327 //One-to-One Survivor Selection 328 for (int i = 0; i < PopulationSizeParameter.Value.Value; i++) 329 { 330 selectionVector = new RealVector(getMatrixRow(populationOld, i)); 331 trialVector = new RealVector(getMatrixRow(trialPopulation, i)); 332 333 var selectionEval = qualityPopulation[i]; 334 var trialEval = Obj(trialVector); 335 336 if (trialEval < selectionEval) 337 { 338 for (int j = 0; j < getMatrixRow(populationOld, i).Length; j++) 339 { 340 populationOld[i, j] = trialPopulation[i, j]; 341 } 342 qualityPopulation[i] = trialEval; 343 } 344 } 345 346 //update the best candidate 347 for (int i = 0; i < PopulationSizeParameter.Value.Value; i++) 348 { 349 selectionVector = new RealVector(getMatrixRow(populationOld, i)); 350 var quality = qualityPopulation[i]; 351 if (quality < bestPopulationValue) 352 { 353 bestPopulationVector = (RealVector)selectionVector.Clone(); 354 bestPopulationValue = quality; 355 } 356 } 357 358 iterations = iterations + 1; 359 360 //update the results 361 ResultsEvaluations = evals; 362 ResultsIterations = iterations; 363 ResultsBestSolution = bestPopulationVector; 364 ResultsBestQuality = bestPopulationValue; 365 366 //update the results in view 367 if (iterations % 10 == 0) ResultsQualitiesBest.Values.Add(bestPopulationValue); 368 if (bestPopulationValue < Problem.BestKnownQuality.Value + ValueToReachParameter.Value.Value) 369 { 370 VTRBestQuality = bestPopulationValue; 371 } 372 } 373 } 374 375 //evaluate the vector 376 public double Obj(RealVector x) 377 { 378 evals = evals + 1; 379 if (Problem.Maximization.Value) 380 return -Problem.Evaluator.Evaluate(x); 381 382 return Problem.Evaluator.Evaluate(x); 383 } 384 385 // Get ith row from the matrix 386 public double[] getMatrixRow(double[,] Mat, int i) 387 { 388 double[] tmp = new double[Mat.GetUpperBound(1) + 1]; 389 390 for (int j = 0; j <= Mat.GetUpperBound(1); j++) 391 { 392 tmp[j] = Mat[i, j]; 393 } 394 395 return tmp; 396 } 397 } 295 qualityPopulation[i] = trialEval; 296 } 297 } 298 299 //update the best candidate 300 for(int i = 0; i < PopulationSizeParameter.Value.Value; i++) { 301 selectionVector = new RealVector(getMatrixRow(populationOld, i)); 302 var quality = qualityPopulation[i]; 303 if(quality < bestPopulationValue) { 304 bestPopulationVector = (RealVector)selectionVector.Clone(); 305 bestPopulationValue = quality; 306 } 307 } 308 309 iterations = iterations + 1; 310 311 //update the results 312 ResultsEvaluations = evals; 313 ResultsIterations = iterations; 314 ResultsBestSolution = bestPopulationVector; 315 ResultsBestQuality = bestPopulationValue; 316 317 //update the results in view 318 if(iterations % 10 == 0) ResultsQualitiesBest.Values.Add(bestPopulationValue); 319 if(bestPopulationValue < Problem.BestKnownQuality.Value + ValueToReachParameter.Value.Value) { 320 VTRBestQuality = bestPopulationValue; 321 } 322 } 323 } 324 325 public override bool SupportsPause { get { return false; } } // XXX is pause actually supported? 326 327 //evaluate the vector 328 public double Obj(RealVector x) { 329 evals = evals + 1; 330 if(Problem.Maximization.Value) 331 return -Problem.Evaluator.Evaluate(x); 332 333 return Problem.Evaluator.Evaluate(x); 334 } 335 336 // Get ith row from the matrix 337 public double[] getMatrixRow(double[,] Mat, int i) { 338 double[] tmp = new double[Mat.GetUpperBound(1) + 1]; 339 340 for(int j = 0; j <= Mat.GetUpperBound(1); j++) { 341 tmp[j] = Mat[i, j]; 342 } 343 344 return tmp; 345 } 346 } 398 347 } -
branches/ichiriac/HeuristicLab.Algorithms.DifferentialEvolution/HeuristicLab.Algorithms.DifferentialEvolution.csproj
r14086 r14700 122 122 <ItemGroup> 123 123 <Compile Include="DifferentialEvolution.cs" /> 124 <Compile Include="DifferentialEvolutionReproduction.cs" />125 <Compile Include="DifferentialEvolutionSelection.cs" />126 124 <Compile Include="Plugin.cs" /> 127 125 <Compile Include="Properties\AssemblyInfo.cs" /> 128 <Compile Include="Solution.cs" />129 126 </ItemGroup> 130 127 <ItemGroup> -
branches/ichiriac/HeuristicLab.Algorithms.DifferentialEvolution/Plugin.cs
r13619 r14700 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 1 7 2 using HeuristicLab.PluginInfrastructure; 8 3 9 namespace HeuristicLab.Algorithms.DifferentialEvolution 10 { 11 [Plugin("HeuristicLab.Algorithms.DifferentialEvolution", "Provides an implementation of DE algorithm", "3.3.9.0")] 12 [PluginFile("HeuristicLab.Algorithms.DifferentialEvolution.dll", PluginFileType.Assembly)] 13 public class Plugin : PluginBase 14 { 15 } 4 namespace HeuristicLab.Algorithms.DifferentialEvolution { 5 [Plugin("HeuristicLab.Algorithms.DifferentialEvolution", "Provides an implementation of DE algorithm", "3.3.9.0")] 6 [PluginFile("HeuristicLab.Algorithms.DifferentialEvolution.dll", PluginFileType.Assembly)] 7 public class Plugin : PluginBase { 8 } 16 9 }
Note: See TracChangeset
for help on using the changeset viewer.