Changeset 13770
- Timestamp:
- 04/16/16 14:05:59 (9 years ago)
- Location:
- branches/ichiriac/HeuristicLab.Algorithms.DifferentialEvolution
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/ichiriac/HeuristicLab.Algorithms.DifferentialEvolution/DifferentialEvolution.cs
r13710 r13770 19 19 [StorableClass] 20 20 [Creatable(CreatableAttribute.Categories.PopulationBasedAlgorithms, Priority = 400)] 21 public class DifferentialEvolution : BasicAlgorithm 21 public class DifferentialEvolution : BasicAlgorithm 22 22 { 23 23 public Func<IEnumerable<double>, double> Evaluation; … … 200 200 var ub = Problem.Bounds[0, 1]; 201 201 var range = ub - lb; 202 202 this.evals = 0; 203 203 double[,] populationOld = new double[PopulationSizeParameter.Value.Value, Problem.ProblemSize.Value]; 204 204 double[,] mutationPopulation = new double[PopulationSizeParameter.Value.Value, Problem.ProblemSize.Value]; … … 222 222 int best_index = 0; 223 223 double[] populationRow = new double[Problem.ProblemSize.Value]; 224 224 double[] qualityPopulation = new double[PopulationSizeParameter.Value.Value]; 225 225 bestPopulation = getMatrixRow(populationOld, best_index); 226 226 RealVector bestPopulationVector = new RealVector(bestPopulation); 227 227 double bestPopulationValue = Obj(bestPopulationVector); 228 qualityPopulation[best_index] = bestPopulationValue; 228 229 RealVector selectionVector; 229 230 RealVector trialVector; … … 231 232 232 233 233 for (var i = 0; i < PopulationSizeParameter.Value.Value; i++)234 for (var i = 1; i < PopulationSizeParameter.Value.Value; i++) 234 235 { 235 236 populationRow = getMatrixRow(populationOld, i); … … 237 238 238 239 qtrial = Obj(trialVector); 240 qualityPopulation[i] = qtrial; 239 241 240 242 if (qtrial > bestPopulationValue) 241 243 { 242 244 bestPopulationVector = new RealVector(populationRow); 243 bestPopulationValue = Obj(bestPopulationVector);245 bestPopulationValue = qtrial; 244 246 best_index = i; 245 247 } … … 250 252 // Loop until iteration limit reached or canceled. 251 253 // todo replace with a function 252 while (Results Evaluations < MaximumEvaluations253 && !cancellationToken.IsCancellationRequested 254 while (ResultsIterations < MaximumEvaluations 255 && !cancellationToken.IsCancellationRequested 254 256 && bestPopulationValue > Problem.BestKnownQuality.Value + ValueToReachParameter.Value.Value) 255 257 { … … 262 264 //assure the selected vectors r0, r1 and r2 are different 263 265 do 264 265 266 267 268 269 270 271 272 273 274 266 { 267 r0 = _random.Next(0, PopulationSizeParameter.Value.Value); 268 } while (r0 == i); 269 do 270 { 271 r1 = _random.Next(0, PopulationSizeParameter.Value.Value); 272 } while (r1 == i || r1 == r0); 273 do 274 { 275 r2 = _random.Next(0, PopulationSizeParameter.Value.Value); 276 } while (r2 == i || r2 == r0 || r2 == r1); 275 277 276 278 for (int j = 0; j < getMatrixRow(mutationPopulation, i).Length; j++) 277 279 { 278 mutationPopulation[i, j] = populationOld[r0,j] +279 ScalingFactorParameter.Value.Value * (populationOld[r1, j] - populationOld[r2,j]);280 mutationPopulation[i, j] = populationOld[r0, j] + 281 ScalingFactorParameter.Value.Value * (populationOld[r1, j] - populationOld[r2, j]); 280 282 //check the problem upper and lower bounds 281 if (mutationPopulation[i, j] > ub) mutationPopulation[i,j] = ub;282 if (mutationPopulation[i, j] < lb) mutationPopulation[i,j] = lb;283 if (mutationPopulation[i, j] > ub) mutationPopulation[i, j] = ub; 284 if (mutationPopulation[i, j] < lb) mutationPopulation[i, j] = lb; 283 285 } 284 286 } … … 292 294 if (_random.NextDouble() <= CrossoverProbabilityParameter.Value.Value || j == rnbr) 293 295 { 294 trialPopulation[i, j] = mutationPopulation[i,j];296 trialPopulation[i, j] = mutationPopulation[i, j]; 295 297 } 296 298 else 297 299 { 298 trialPopulation[i, j] = populationOld[i,j];300 trialPopulation[i, j] = populationOld[i, j]; 299 301 } 300 302 } … … 307 309 trialVector = new RealVector(getMatrixRow(trialPopulation, i)); 308 310 309 var selectionEval = Obj(selectionVector);310 var tr ailEval = Obj(trialVector);311 312 if (tr ailEval < selectionEval)311 var selectionEval = qualityPopulation[i]; 312 var trialEval = Obj(trialVector); 313 314 if (trialEval < selectionEval) 313 315 { 314 316 for (int j = 0; j < getMatrixRow(populationOld, i).Length; j++) … … 316 318 populationOld[i, j] = trialPopulation[i, j]; 317 319 } 320 qualityPopulation[i] = trialEval; 318 321 } 319 322 } … … 323 326 { 324 327 selectionVector = new RealVector(getMatrixRow(populationOld, i)); 325 var quality = Obj(selectionVector);328 var quality = qualityPopulation[i]; 326 329 if (quality < bestPopulationValue) 327 330 { … … 337 340 ResultsIterations = iterations; 338 341 ResultsBestSolution = bestPopulationVector; 339 ResultsBestQuality = bestPopulationValue; 342 ResultsBestQuality = bestPopulationValue; 340 343 341 344 //update the results in view 342 if (iterations % 10 == 0 345 if (iterations % 10 == 0) ResultsQualitiesBest.Values.Add(bestPopulationValue); 343 346 if (bestPopulationValue < Problem.BestKnownQuality.Value + ValueToReachParameter.Value.Value) 344 347 { … … 347 350 } 348 351 } 349 352 350 353 //evaluate the vector 351 354 public double Obj(RealVector x) … … 353 356 evals = evals + 1; 354 357 if (Problem.Maximization.Value) 355 return -Problem.Evaluator.Evaluate(x);358 return -Problem.Evaluator.Evaluate(x); 356 359 357 360 return Problem.Evaluator.Evaluate(x); … … 359 362 360 363 // Get ith row from the matrix 361 public double[] getMatrixRow 364 public double[] getMatrixRow(double[,] Mat, int i) 362 365 { 363 366 double[] tmp = new double[Mat.GetUpperBound(1) + 1]; … … 372 375 } 373 376 } 374 -
branches/ichiriac/HeuristicLab.Algorithms.DifferentialEvolution/HeuristicLab.Algorithms.DifferentialEvolution.csproj
r13633 r13770 22 22 <ErrorReport>prompt</ErrorReport> 23 23 <WarningLevel>4</WarningLevel> 24 <GenerateSerializationAssemblies>Auto</GenerateSerializationAssemblies> 24 25 </PropertyGroup> 25 26 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> … … 95 96 <ItemGroup> 96 97 <Compile Include="DifferentialEvolution.cs" /> 98 <Compile Include="DifferentialEvolutionReproduction.cs" /> 99 <Compile Include="DifferentialEvolutionSelection.cs" /> 97 100 <Compile Include="Plugin.cs" /> 98 101 <Compile Include="Properties\AssemblyInfo.cs" /> 102 <Compile Include="Solution.cs" /> 99 103 </ItemGroup> 100 104 <ItemGroup>
Note: See TracChangeset
for help on using the changeset viewer.