Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ichiriac/HeuristicLab.Algorithms.Shade/Shade.cs @ 14866

Last change on this file since 14866 was 14699, checked in by gkronber, 8 years ago

#2646: fixed compile errors in SHADE implementation (and set svn:ignore properties)

File size: 24.0 KB
RevLine 
[14091]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * The implementation is inspired by the implementation in JAVA of SHADE algorithm https://sites.google.com/site/tanaberyoji/software/SHADE1.0.1_CEC2013.zip?attredirects=0&d=1
8 *
9 * HeuristicLab is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * HeuristicLab is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
21 */
[14699]22
23#endregion
[14091]24using HeuristicLab.Analysis;
[14088]25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.RealVectorEncoding;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32using HeuristicLab.Problems.TestFunctions;
33using HeuristicLab.Random;
34using System;
35using System.Collections.Generic;
36using System.Threading;
37
[14699]38namespace HeuristicLab.Algorithms.Shade {
[14088]39
[14699]40  [Item("Success-History Based Parameter Adaptation for DE (SHADE)", "A self-adaptive version of differential evolution")]
41  [StorableClass]
42  [Creatable(CreatableAttribute.Categories.PopulationBasedAlgorithms, Priority = 400)]
43  public class Shade : BasicAlgorithm {
44    public Func<IEnumerable<double>, double> Evaluation;
[14088]45
[14699]46    public override Type ProblemType {
47      get { return typeof(SingleObjectiveTestFunctionProblem); }
48    }
49    public new SingleObjectiveTestFunctionProblem Problem {
50      get { return (SingleObjectiveTestFunctionProblem)base.Problem; }
51      set { base.Problem = value; }
52    }
[14088]53
[14699]54    private readonly IRandom _random = new MersenneTwister();
55    private int evals;
56    private int pop_size;
57    private double arc_rate;
58    private int arc_size;
59    private double p_best_rate;
60    private int memory_size;
[14088]61
[14699]62    private double[][] pop;
63    private double[] fitness;
64    private double[][] children;
65    private double[] children_fitness;
[14088]66
[14699]67    private double[] bsf_solution;
68    private double bsf_fitness = 1e+30;
69    private double[,] archive;
70    private int num_arc_inds = 0;
[14088]71
[14699]72    #region ParameterNames
73    private const string MaximumEvaluationsParameterName = "Maximum Evaluations";
74    private const string SeedParameterName = "Seed";
75    private const string SetSeedRandomlyParameterName = "SetSeedRandomly";
76    private const string CrossoverProbabilityParameterName = "CrossoverProbability";
77    private const string PopulationSizeParameterName = "PopulationSize";
78    private const string ScalingFactorParameterName = "ScalingFactor";
79    private const string ValueToReachParameterName = "ValueToReach";
80    private const string ArchiveRateParameterName = "ArchiveRate";
81    private const string MemorySizeParameterName = "MemorySize";
82    private const string BestRateParameterName = "BestRate";
83    #endregion
[14088]84
[14699]85    #region ParameterProperties
86    public IFixedValueParameter<IntValue> MaximumEvaluationsParameter {
87      get { return (IFixedValueParameter<IntValue>)Parameters[MaximumEvaluationsParameterName]; }
88    }
89    public IFixedValueParameter<IntValue> SeedParameter {
90      get { return (IFixedValueParameter<IntValue>)Parameters[SeedParameterName]; }
91    }
92    public FixedValueParameter<BoolValue> SetSeedRandomlyParameter {
93      get { return (FixedValueParameter<BoolValue>)Parameters[SetSeedRandomlyParameterName]; }
94    }
95    private ValueParameter<IntValue> PopulationSizeParameter {
96      get { return (ValueParameter<IntValue>)Parameters[PopulationSizeParameterName]; }
97    }
98    public ValueParameter<DoubleValue> CrossoverProbabilityParameter {
99      get { return (ValueParameter<DoubleValue>)Parameters[CrossoverProbabilityParameterName]; }
100    }
101    public ValueParameter<DoubleValue> ScalingFactorParameter {
102      get { return (ValueParameter<DoubleValue>)Parameters[ScalingFactorParameterName]; }
103    }
104    public ValueParameter<DoubleValue> ValueToReachParameter {
105      get { return (ValueParameter<DoubleValue>)Parameters[ValueToReachParameterName]; }
106    }
107    public ValueParameter<DoubleValue> ArchiveRateParameter {
108      get { return (ValueParameter<DoubleValue>)Parameters[ArchiveRateParameterName]; }
109    }
110    public ValueParameter<IntValue> MemorySizeParameter {
111      get { return (ValueParameter<IntValue>)Parameters[MemorySizeParameterName]; }
112    }
113    public ValueParameter<DoubleValue> BestRateParameter {
114      get { return (ValueParameter<DoubleValue>)Parameters[BestRateParameterName]; }
115    }
116    #endregion
[14088]117
[14699]118    #region Properties
119    public int MaximumEvaluations {
120      get { return MaximumEvaluationsParameter.Value.Value; }
121      set { MaximumEvaluationsParameter.Value.Value = value; }
122    }
[14088]123
[14699]124    public Double CrossoverProbability {
125      get { return CrossoverProbabilityParameter.Value.Value; }
126      set { CrossoverProbabilityParameter.Value.Value = value; }
127    }
128    public Double ScalingFactor {
129      get { return ScalingFactorParameter.Value.Value; }
130      set { ScalingFactorParameter.Value.Value = value; }
131    }
132    public int Seed {
133      get { return SeedParameter.Value.Value; }
134      set { SeedParameter.Value.Value = value; }
135    }
136    public bool SetSeedRandomly {
137      get { return SetSeedRandomlyParameter.Value.Value; }
138      set { SetSeedRandomlyParameter.Value.Value = value; }
139    }
140    public IntValue PopulationSize {
141      get { return PopulationSizeParameter.Value; }
142      set { PopulationSizeParameter.Value = value; }
143    }
144    public Double ValueToReach {
145      get { return ValueToReachParameter.Value.Value; }
146      set { ValueToReachParameter.Value.Value = value; }
147    }
148    public Double ArchiveRate {
149      get { return ArchiveRateParameter.Value.Value; }
150      set { ArchiveRateParameter.Value.Value = value; }
151    }
152    public IntValue MemorySize {
153      get { return MemorySizeParameter.Value; }
154      set { MemorySizeParameter.Value = value; }
155    }
156    public Double BestRate {
157      get { return BestRateParameter.Value.Value; }
158      set { BestRateParameter.Value.Value = value; }
159    }
160    #endregion
[14088]161
[14699]162    #region ResultsProperties
163    private double ResultsBestQuality {
164      get { return ((DoubleValue)Results["Best Quality"].Value).Value; }
165      set { ((DoubleValue)Results["Best Quality"].Value).Value = value; }
166    }
[14088]167
[14699]168    private double VTRBestQuality {
169      get { return ((DoubleValue)Results["VTR"].Value).Value; }
170      set { ((DoubleValue)Results["VTR"].Value).Value = value; }
171    }
[14088]172
[14699]173    private RealVector ResultsBestSolution {
174      get { return (RealVector)Results["Best Solution"].Value; }
175      set { Results["Best Solution"].Value = value; }
176    }
[14088]177
[14699]178    private int ResultsEvaluations {
179      get { return ((IntValue)Results["Evaluations"].Value).Value; }
180      set { ((IntValue)Results["Evaluations"].Value).Value = value; }
181    }
182    private int ResultsIterations {
183      get { return ((IntValue)Results["Iterations"].Value).Value; }
184      set { ((IntValue)Results["Iterations"].Value).Value = value; }
185    }
[14088]186
[14699]187    private DataTable ResultsQualities {
188      get { return ((DataTable)Results["Qualities"].Value); }
189    }
190    private DataRow ResultsQualitiesBest {
191      get { return ResultsQualities.Rows["Best Quality"]; }
192    }
[14088]193
[14699]194    #endregion
[14088]195
[14699]196    [StorableConstructor]
197    protected Shade(bool deserializing) : base(deserializing) { }
[14088]198
[14699]199    protected Shade(Shade original, Cloner cloner)
200      : base(original, cloner) {
201    }
[14088]202
[14699]203    public override IDeepCloneable Clone(Cloner cloner) {
204      return new Shade(this, cloner);
205    }
[14088]206
[14699]207    public Shade() {
208      Parameters.Add(new FixedValueParameter<IntValue>(MaximumEvaluationsParameterName, "", new IntValue(Int32.MaxValue)));
209      Parameters.Add(new ValueParameter<IntValue>(PopulationSizeParameterName, "The size of the population of solutions.", new IntValue(75)));
210      Parameters.Add(new ValueParameter<DoubleValue>(ValueToReachParameterName, "Value to reach (VTR) parameter", new DoubleValue(0.00000001)));
211      Parameters.Add(new ValueParameter<DoubleValue>(ArchiveRateParameterName, "Archive rate parameter", new DoubleValue(2.0)));
212      Parameters.Add(new ValueParameter<IntValue>(MemorySizeParameterName, "Memory size parameter", new IntValue(0)));
213      Parameters.Add(new ValueParameter<DoubleValue>(BestRateParameterName, "Best rate parameter", new DoubleValue(0.1)));
214    }
[14088]215
[14699]216    protected override void Run(CancellationToken cancellationToken) {
[14088]217
[14699]218      // Set up the results display
219      Results.Add(new Result("Iterations", new IntValue(0)));
220      Results.Add(new Result("Evaluations", new IntValue(0)));
221      Results.Add(new Result("Best Solution", new RealVector()));
222      Results.Add(new Result("Best Quality", new DoubleValue(double.NaN)));
223      Results.Add(new Result("VTR", new DoubleValue(double.NaN)));
224      var table = new DataTable("Qualities");
225      table.Rows.Add(new DataRow("Best Quality"));
226      Results.Add(new Result("Qualities", table));
[14088]227
228
[14699]229      this.evals = 0;
230      int archive_size = (int)Math.Round(ArchiveRateParameter.Value.Value * PopulationSize.Value);
231      int problem_size = Problem.ProblemSize.Value;
[14088]232
[14699]233      int pop_size = PopulationSizeParameter.Value.Value;
234      this.arc_rate = ArchiveRateParameter.Value.Value;
235      this.arc_size = (int)Math.Round(this.arc_rate * pop_size);
236      this.p_best_rate = BestRateParameter.Value.Value;
237      this.memory_size = MemorySizeParameter.Value.Value;
[14088]238
[14699]239      this.pop = new double[pop_size][];
240      this.fitness = new double[pop_size];
241      this.children = new double[pop_size][];
242      this.children_fitness = new double[pop_size];
[14088]243
[14699]244      this.bsf_solution = new double[problem_size];
245      this.bsf_fitness = 1e+30;
246      this.archive = new double[arc_size, Problem.ProblemSize.Value];
247      this.num_arc_inds = 0;
[14088]248
[14699]249      double[,] populationOld = new double[PopulationSizeParameter.Value.Value, Problem.ProblemSize.Value];
250      double[,] mutationPopulation = new double[PopulationSizeParameter.Value.Value, Problem.ProblemSize.Value];
251      double[,] trialPopulation = new double[PopulationSizeParameter.Value.Value, Problem.ProblemSize.Value];
252      double[] bestPopulation = new double[Problem.ProblemSize.Value];
253      double[] bestPopulationIteration = new double[Problem.ProblemSize.Value];
254      double[,] archive = new double[archive_size, Problem.ProblemSize.Value];
[14088]255
256
[14699]257      // //for external archive
258      int rand_arc_ind;
[14088]259
[14699]260      int num_success_params;
[14088]261
[14699]262      double[] success_sf = new double[PopulationSizeParameter.Value.Value];
263      double[] success_cr = new double[PopulationSizeParameter.Value.Value];
264      double[] dif_fitness = new double[PopulationSizeParameter.Value.Value];
265      double[] fitness = new double[PopulationSizeParameter.Value.Value];
[14088]266
[14699]267      // the contents of M_f and M_cr are all initialiezed 0.5
268      double[] memory_sf = new double[MemorySizeParameter.Value.Value];
269      double[] memory_cr = new double[MemorySizeParameter.Value.Value];
[14088]270
[14699]271      for(int i = 0; i < MemorySizeParameter.Value.Value; i++) {
272        memory_sf[i] = 0.5;
273        memory_cr[i] = 0.5;
274      }
[14088]275
[14699]276      //memory index counter
277      int memory_pos = 0;
278      double temp_sum_sf1, temp_sum_sf2, temp_sum_cr1, temp_sum_cr2, temp_sum, temp_weight;
[14088]279
[14699]280      //for new parameters sampling
281      double mu_sf, mu_cr;
282      int rand_mem_index;
[14088]283
[14699]284      double[] pop_sf = new double[PopulationSizeParameter.Value.Value];
285      double[] pop_cr = new double[PopulationSizeParameter.Value.Value];
[14088]286
[14699]287      //for current-to-pbest/1
288      int p_best_ind;
289      double m = PopulationSizeParameter.Value.Value * BestRateParameter.Value.Value;
290      int p_num = (int)Math.Round(m);
291      int[] sorted_array = new int[PopulationSizeParameter.Value.Value];
292      double[] sorted_fitness = new double[PopulationSizeParameter.Value.Value];
[14088]293
[14699]294      //initialize the population
295      populationOld = makeNewIndividuals();
[14088]296
[14699]297      //evaluate the best member after the intialiazation
298      //the idea is to select first member and after that to check the others members from the population
[14088]299
[14699]300      int best_index = 0;
301      double[] populationRow = new double[Problem.ProblemSize.Value];
302      bestPopulation = getMatrixRow(populationOld, best_index);
303      RealVector bestPopulationVector = new RealVector(bestPopulation);
304      double bestPopulationValue = Obj(bestPopulationVector);
305      fitness[best_index] = bestPopulationValue;
306      RealVector selectionVector;
307      RealVector trialVector;
308      double qtrial;
[14088]309
310
[14699]311      for(var i = 0; i < PopulationSizeParameter.Value.Value; i++) {
312        populationRow = getMatrixRow(populationOld, i);
313        trialVector = new RealVector(populationRow);
[14088]314
[14699]315        qtrial = Obj(trialVector);
316        fitness[i] = qtrial;
[14088]317
[14699]318        if(qtrial > bestPopulationValue) {
319          bestPopulationVector = new RealVector(populationRow);
320          bestPopulationValue = qtrial;
321          best_index = i;
322        }
323      }
[14088]324
[14699]325      int iterations = 1;
[14088]326
[14699]327      // Loop until iteration limit reached or canceled.
328      // todo replace with a function
329      // && bestPopulationValue > Problem.BestKnownQuality.Value + ValueToReachParameter.Value.Value
330      while(ResultsEvaluations < MaximumEvaluations
331          && !cancellationToken.IsCancellationRequested &&
332          bestPopulationValue > Problem.BestKnownQuality.Value + ValueToReachParameter.Value.Value) {
333        for(int i = 0; i < PopulationSizeParameter.Value.Value; i++) sorted_array[i] = i;
334        for(int i = 0; i < PopulationSizeParameter.Value.Value; i++) sorted_fitness[i] = fitness[i];
[14088]335
[14699]336        Quicksort(sorted_fitness, 0, PopulationSizeParameter.Value.Value - 1, sorted_array);
[14088]337
[14699]338        for(int target = 0; target < PopulationSizeParameter.Value.Value; target++) {
339          rand_mem_index = (int)(_random.NextDouble() * MemorySizeParameter.Value.Value);
340          mu_sf = memory_sf[rand_mem_index];
341          mu_cr = memory_cr[rand_mem_index];
[14088]342
[14699]343          //generate CR_i and repair its value
344          if(mu_cr == -1) {
345            pop_cr[target] = 0;
346          } else {
347            pop_cr[target] = gauss(mu_cr, 0.1);
348            if(pop_cr[target] > 1) pop_cr[target] = 1;
349            else if(pop_cr[target] < 0) pop_cr[target] = 0;
350          }
[14088]351
[14699]352          //generate F_i and repair its value
353          do {
354            pop_sf[target] = cauchy_g(mu_sf, 0.1);
355          } while(pop_sf[target] <= 0);
[14088]356
[14699]357          if(pop_sf[target] > 1) pop_sf[target] = 1;
[14088]358
[14699]359          //p-best individual is randomly selected from the top pop_size *  p_i members
360          p_best_ind = sorted_array[(int)(_random.NextDouble() * p_num)];
[14088]361
[14699]362          trialPopulation = operateCurrentToPBest1BinWithArchive(populationOld, trialPopulation, target, p_best_ind, pop_sf[target], pop_cr[target]);
363        }
[14088]364
[14699]365        for(int i = 0; i < pop_size; i++) {
366          trialVector = new RealVector(getMatrixRow(trialPopulation, i));
367          children_fitness[i] = Obj(trialVector);
368        }
[14088]369
[14699]370        //update bfs solution
371        for(var i = 0; i < PopulationSizeParameter.Value.Value; i++) {
372          populationRow = getMatrixRow(populationOld, i);
373          qtrial = fitness[i];
[14088]374
[14699]375          if(qtrial > bestPopulationValue) {
376            bestPopulationVector = new RealVector(populationRow);
377            bestPopulationValue = qtrial;
378            best_index = i;
379          }
380        }
[14088]381
[14699]382        num_success_params = 0;
[14088]383
[14699]384        //generation alternation
385        for(int i = 0; i < pop_size; i++) {
386          if(children_fitness[i] == fitness[i]) {
387            fitness[i] = children_fitness[i];
388            for(int j = 0; j < problem_size; j++) populationOld[i, j] = trialPopulation[i, j];
389          } else if(children_fitness[i] < fitness[i]) {
390            //parent vectors x_i which were worse than the trial vectors u_i are preserved
391            if(arc_size > 1) {
392              if(num_arc_inds < arc_size) {
393                for(int j = 0; j < problem_size; j++) this.archive[num_arc_inds, j] = populationOld[i, j];
394                num_arc_inds++;
[14088]395
[14699]396              }
397              //Whenever the size of the archive exceeds, randomly selected elements are deleted to make space for the newly inserted elements
398              else {
399                rand_arc_ind = (int)(_random.NextDouble() * arc_size);
400                for(int j = 0; j < problem_size; j++) this.archive[rand_arc_ind, j] = populationOld[i, j];
401              }
402            }
[14088]403
[14699]404            dif_fitness[num_success_params] = Math.Abs(fitness[i] - children_fitness[i]);
[14088]405
[14699]406            fitness[i] = children_fitness[i];
407            for(int j = 0; j < problem_size; j++) populationOld[i, j] = trialPopulation[i, j];
[14088]408
[14699]409            //successful parameters are preserved in S_F and S_CR
410            success_sf[num_success_params] = pop_sf[i];
411            success_cr[num_success_params] = pop_cr[i];
412            num_success_params++;
413          }
414        }
[14088]415
[14699]416        if(num_success_params > 0) {
417          temp_sum_sf1 = 0;
418          temp_sum_sf2 = 0;
419          temp_sum_cr1 = 0;
420          temp_sum_cr2 = 0;
421          temp_sum = 0;
422          temp_weight = 0;
[14088]423
[14699]424          for(int i = 0; i < num_success_params; i++) temp_sum += dif_fitness[i];
[14088]425
[14699]426          //weighted lehmer mean
427          for(int i = 0; i < num_success_params; i++) {
428            temp_weight = dif_fitness[i] / temp_sum;
[14088]429
[14699]430            temp_sum_sf1 += temp_weight * success_sf[i] * success_sf[i];
431            temp_sum_sf2 += temp_weight * success_sf[i];
[14088]432
[14699]433            temp_sum_cr1 += temp_weight * success_cr[i] * success_cr[i];
434            temp_sum_cr2 += temp_weight * success_cr[i];
435          }
[14088]436
[14699]437          memory_sf[memory_pos] = temp_sum_sf1 / temp_sum_sf2;
[14088]438
[14699]439          if(temp_sum_cr2 == 0 || memory_cr[memory_pos] == -1) {
440            memory_cr[memory_pos] = -1;
441          } else {
442            memory_cr[memory_pos] = temp_sum_cr1 / temp_sum_cr2;
443          }
[14088]444
[14699]445          //increment the counter
446          memory_pos++;
447          if(memory_pos >= memory_size) memory_pos = 0;
448        }
[14088]449
[14699]450        //update the best candidate
451        for(int i = 0; i < PopulationSizeParameter.Value.Value; i++) {
452          selectionVector = new RealVector(getMatrixRow(populationOld, i));
453          var quality = fitness[i];
454          if(quality < bestPopulationValue) {
455            bestPopulationVector = (RealVector)selectionVector.Clone();
456            bestPopulationValue = quality;
457          }
458        }
[14088]459
[14699]460        iterations = iterations + 1;
[14088]461
[14699]462        //update the results
463        ResultsEvaluations = evals;
464        ResultsIterations = iterations;
465        ResultsBestSolution = bestPopulationVector;
466        ResultsBestQuality = bestPopulationValue;
[14088]467
[14699]468        //update the results in view
469        if(iterations % 10 == 0) ResultsQualitiesBest.Values.Add(bestPopulationValue);
470        if(bestPopulationValue < Problem.BestKnownQuality.Value + ValueToReachParameter.Value.Value) {
471          VTRBestQuality = bestPopulationValue;
[14088]472        }
[14699]473      }
474    }
[14088]475
[14699]476    public override bool SupportsPause { get { return false; } } // TODO (can we pause?)
[14088]477
[14699]478    //evaluate the vector
479    public double Obj(RealVector x) {
480      evals = evals + 1;
481      if(Problem.Maximization.Value)
482        return -Problem.Evaluator.Evaluate(x);
[14088]483
[14699]484      return Problem.Evaluator.Evaluate(x);
485    }
[14088]486
[14699]487    // Get ith row from the matrix
488    public double[] getMatrixRow(double[,] Mat, int i) {
489      double[] tmp = new double[Mat.GetUpperBound(1) + 1];
[14088]490
[14699]491      for(int j = 0; j <= Mat.GetUpperBound(1); j++) {
492        tmp[j] = Mat[i, j];
493      }
[14088]494
[14699]495      return tmp;
496    }
[14088]497
[14699]498    /*
499        Return random value from Cauchy distribution with mean "mu" and variance "gamma"
500        http://www.sat.t.u-tokyo.ac.jp/~omi/random_variables_generation.html#Cauchy
501    */
502    private double cauchy_g(double mu, double gamma) {
503      return mu + gamma * Math.Tan(Math.PI * (_random.NextDouble() - 0.5));
504    }
[14088]505
[14699]506    /*
507         Return random value from normal distribution with mean "mu" and variance "gamma"
508         http://www.sat.t.u-tokyo.ac.jp/~omi/random_variables_generation.html#Gauss
509    */
510    private double gauss(double mu, double sigma) {
511      return mu + sigma * Math.Sqrt(-2.0 * Math.Log(_random.NextDouble())) * Math.Sin(2.0 * Math.PI * _random.NextDouble());
512    }
[14088]513
[14699]514    private double[,] makeNewIndividuals() {
515      //problem variables
516      var dim = Problem.ProblemSize.Value;
517      var lb = Problem.Bounds[0, 0];
518      var ub = Problem.Bounds[0, 1];
519      var range = ub - lb;
520      double[,] population = new double[PopulationSizeParameter.Value.Value, Problem.ProblemSize.Value];
521
522      //create initial population
523      //population is a matrix of size PopulationSize*ProblemSize
524      for(int i = 0; i < PopulationSizeParameter.Value.Value; i++) {
525        for(int j = 0; j < Problem.ProblemSize.Value; j++) {
526          population[i, j] = _random.NextDouble() * range + lb;
[14088]527        }
[14699]528      }
529      return population;
530    }
[14088]531
[14699]532    private static void Quicksort(double[] elements, int left, int right, int[] index) {
533      int i = left, j = right;
534      double pivot = elements[(left + right) / 2];
535      double tmp_var = 0;
536      int tmp_index = 0;
[14088]537
[14699]538      while(i <= j) {
539        while(elements[i].CompareTo(pivot) < 0) {
540          i++;
541        }
[14088]542
[14699]543        while(elements[j].CompareTo(pivot) > 0) {
544          j--;
545        }
[14088]546
[14699]547        if(i <= j) {
548          // Swap
549          tmp_var = elements[i];
550          elements[i] = elements[j];
551          elements[j] = tmp_var;
[14088]552
[14699]553          tmp_index = index[i];
554          index[i] = index[j];
555          index[j] = tmp_index;
[14088]556
[14699]557          i++;
558          j--;
559        }
560      }
[14088]561
[14699]562      // Recursive calls
563      if(left < j) {
564        Quicksort(elements, left, j, index);
565      }
[14088]566
[14699]567      if(i < right) {
568        Quicksort(elements, i, right, index);
569      }
570    }
[14088]571
[14699]572    // current to best selection scheme with archive
573    // analyze how the archive is implemented
574    private double[,] operateCurrentToPBest1BinWithArchive(double[,] pop, double[,] children, int target, int p_best_individual, double scaling_factor, double cross_rate) {
575      int r1, r2;
576      int num_arc_inds = 0;
577      var lb = Problem.Bounds[0, 0];
578      var ub = Problem.Bounds[0, 1];
[14088]579
[14699]580      do {
581        r1 = (int)(_random.NextDouble() * PopulationSizeParameter.Value.Value);
582      } while(r1 == target);
583      do {
584        r2 = (int)(_random.NextDouble() * (PopulationSizeParameter.Value.Value + num_arc_inds));
585      } while((r2 == target) || (r2 == r1));
[14088]586
[14699]587      int random_variable = (int)(_random.NextDouble() * Problem.ProblemSize.Value);
[14088]588
[14699]589      if(r2 >= PopulationSizeParameter.Value.Value) {
590        r2 -= PopulationSizeParameter.Value.Value;
591        for(int i = 0; i < Problem.ProblemSize.Value; i++) {
592          if((_random.NextDouble() < cross_rate) || (i == random_variable)) children[target, i] = pop[target, i] + scaling_factor * (pop[p_best_individual, i] - pop[target, i]) + scaling_factor * (pop[r1, i] - archive[r2, i]);
593          else children[target, i] = pop[target, i];
594        }
595      } else {
596        for(int i = 0; i < Problem.ProblemSize.Value; i++) {
597          if((_random.NextDouble() < cross_rate) || (i == random_variable)) children[target, i] = pop[target, i] + scaling_factor * (pop[p_best_individual, i] - pop[target, i]) + scaling_factor * (pop[r1, i] - pop[r2, i]);
598          else children[target, i] = pop[target, i];
599        }
600      }
[14088]601
[14699]602      for(int i = 0; i < Problem.ProblemSize.Value; i++) {
603        if(children[target, i] < lb) children[target, i] = (lb + pop[target, i]) / 2.0;
604        else if(children[target, i] > ub) children[target, i] = (ub + pop[target, i]) / 2.0;
605      }
[14088]606
[14699]607      return children;
[14088]608    }
[14699]609  }
[14088]610}
Note: See TracBrowser for help on using the repository browser.