- Timestamp:
- 03/16/16 10:59:29 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/ichiriac/HeuristicLab.Algorithms.DifferentialEvolution/DifferentialEvolution.cs
r13674 r13710 11 11 using System; 12 12 using System.Collections.Generic; 13 using System.Linq;14 13 using System.Threading; 15 14 … … 35 34 36 35 private readonly IRandom _random = new MersenneTwister(); 37 36 private int evals; 37 38 38 #region ParameterNames 39 39 private const string MaximumEvaluationsParameterName = "Maximum Evaluations"; … … 206 206 double[] bestPopulation = new double[Problem.ProblemSize.Value]; 207 207 double[] bestPopulationIteration = new double[Problem.ProblemSize.Value]; 208 int evals = 0; // number of function evaluations209 208 210 209 //create initial population … … 212 211 for (int i = 0; i < PopulationSizeParameter.Value.Value; i++) 213 212 { 214 for (int j = 0; j < Problem.ProblemSize.Value; ++j)215 213 for (int j = 0; j < Problem.ProblemSize.Value; j++) 214 { 216 215 populationOld[i, j] = _random.NextDouble() * range + lb; 217 216 } … … 224 223 double[] populationRow = new double[Problem.ProblemSize.Value]; 225 224 226 bestPopulation = Get_ith_row(populationOld, best_index);225 bestPopulation = getMatrixRow(populationOld, best_index); 227 226 RealVector bestPopulationVector = new RealVector(bestPopulation); 228 double bestPopulationValue = Obj(bestPopulationVector , evals);227 double bestPopulationValue = Obj(bestPopulationVector); 229 228 RealVector selectionVector; 230 229 RealVector trialVector; … … 234 233 for (var i = 0; i < PopulationSizeParameter.Value.Value; i++) 235 234 { 236 populationRow = Get_ith_row(populationOld, i);235 populationRow = getMatrixRow(populationOld, i); 237 236 trialVector = new RealVector(populationRow); 238 237 239 qtrial = Obj(trialVector , evals);238 qtrial = Obj(trialVector); 240 239 241 240 if (qtrial > bestPopulationValue) 242 241 { 243 242 bestPopulationVector = new RealVector(populationRow); 244 bestPopulationValue = Obj(bestPopulationVector , evals);243 bestPopulationValue = Obj(bestPopulationVector); 245 244 best_index = i; 246 245 } … … 250 249 251 250 // Loop until iteration limit reached or canceled. 252 // todo replace with a function253 while ( evals < MaximumEvaluations251 // todo replace with a function 252 while (ResultsEvaluations < MaximumEvaluations 254 253 && !cancellationToken.IsCancellationRequested 255 254 && bestPopulationValue > Problem.BestKnownQuality.Value + ValueToReachParameter.Value.Value) … … 259 258 for (int i = 0; i < PopulationSizeParameter.Value.Value; i++) 260 259 { 261 int r1 = _random.Next(0, PopulationSizeParameter.Value.Value), 262 r2 = _random.Next(0, PopulationSizeParameter.Value.Value), 263 r3 = _random.Next(0, PopulationSizeParameter.Value.Value); 264 265 for (int j = 0; j < Get_ith_row(mutationPopulation, i).Length; j++) 260 int r0, r1, r2; 261 262 //assure the selected vectors r0, r1 and r2 are different 263 do 264 { 265 r0 = _random.Next(0, PopulationSizeParameter.Value.Value); 266 } while (r0 == i); 267 do 268 { 269 r1 = _random.Next(0, PopulationSizeParameter.Value.Value); 270 } while (r1 == i || r1 == r0); 271 do 272 { 273 r2 = _random.Next(0, PopulationSizeParameter.Value.Value); 274 } while (r2 == i || r2 == r0 || r2 == r1); 275 276 for (int j = 0; j < getMatrixRow(mutationPopulation, i).Length; j++) 266 277 { 267 mutationPopulation[i,j] = populationOld[r 1,j] +268 ScalingFactorParameter.Value.Value * (populationOld[r 2,j] - populationOld[r3,j]);278 mutationPopulation[i,j] = populationOld[r0,j] + 279 ScalingFactorParameter.Value.Value * (populationOld[r1,j] - populationOld[r2,j]); 269 280 //check the problem upper and lower bounds 270 281 if (mutationPopulation[i,j] > ub) mutationPopulation[i,j] = ub; … … 276 287 for (int i = 0; i < PopulationSizeParameter.Value.Value; i++) 277 288 { 278 double rnbr = Math.Floor(_random.NextDouble() *Problem.ProblemSize.Value);279 for (int j = 0; j < Get_ith_row(mutationPopulation, i).Length; j++)289 double rnbr = _random.Next(0, Problem.ProblemSize.Value); 290 for (int j = 0; j < getMatrixRow(mutationPopulation, i).Length; j++) 280 291 { 281 292 if (_random.NextDouble() <= CrossoverProbabilityParameter.Value.Value || j == rnbr) … … 293 304 for (int i = 0; i < PopulationSizeParameter.Value.Value; i++) 294 305 { 295 selectionVector = new RealVector(Get_ith_row(populationOld, i)); 296 trialVector = new RealVector(Get_ith_row(trialPopulation, i)); 297 298 var selectionEval = Obj(selectionVector, evals); 299 var trailEval = Obj(trialVector, evals); 300 306 selectionVector = new RealVector(getMatrixRow(populationOld, i)); 307 trialVector = new RealVector(getMatrixRow(trialPopulation, i)); 308 309 var selectionEval = Obj(selectionVector); 310 var trailEval = Obj(trialVector); 301 311 302 312 if (trailEval < selectionEval) 303 313 { 304 for (int j = 0; j < Get_ith_row(populationOld, i).Length; j++)314 for (int j = 0; j < getMatrixRow(populationOld, i).Length; j++) 305 315 { 306 316 populationOld[i, j] = trialPopulation[i, j]; … … 312 322 for (int i = 0; i < PopulationSizeParameter.Value.Value; i++) 313 323 { 314 evals = evals + 1; 315 selectionVector = new RealVector(Get_ith_row(populationOld, i)); 316 var quality = Obj(selectionVector, evals); 324 selectionVector = new RealVector(getMatrixRow(populationOld, i)); 325 var quality = Obj(selectionVector); 317 326 if (quality < bestPopulationValue) 318 327 { … … 331 340 332 341 //update the results in view 333 if ( evals % 100 == 0 ) ResultsQualitiesBest.Values.Add(bestPopulationValue);342 if (iterations % 10 == 0 ) ResultsQualitiesBest.Values.Add(bestPopulationValue); 334 343 if (bestPopulationValue < Problem.BestKnownQuality.Value + ValueToReachParameter.Value.Value) 335 344 { … … 337 346 } 338 347 } 339 340 348 } 341 349 342 350 //evaluate the vector 343 public double Obj(RealVector x , int evals)351 public double Obj(RealVector x) 344 352 { 345 353 evals = evals + 1; 346 if (Problem.Maximization.Value)354 if (Problem.Maximization.Value) 347 355 return -Problem.Evaluator.Evaluate(x); 348 356 … … 351 359 352 360 // Get ith row from the matrix 353 public double[] Get_ith_row(double[,] Mat, int i)361 public double[] getMatrixRow (double[,] Mat, int i) 354 362 { 355 363 double[] tmp = new double[Mat.GetUpperBound(1) + 1];
Note: See TracChangeset
for help on using the changeset viewer.