Free cookie consent management tool by TermsFeed Policy Generator

Changeset 13710


Ignore:
Timestamp:
03/16/16 10:59:29 (8 years ago)
Author:
ichiriac
Message:

Fix evaluation bug

Correctly update the evaluation number.
Modify the selection function to select different vectors.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/ichiriac/HeuristicLab.Algorithms.DifferentialEvolution/DifferentialEvolution.cs

    r13674 r13710  
    1111using System;
    1212using System.Collections.Generic;
    13 using System.Linq;
    1413using System.Threading;
    1514
     
    3534
    3635        private readonly IRandom _random = new MersenneTwister();
    37      
     36        private int evals;
     37
    3838        #region ParameterNames
    3939        private const string MaximumEvaluationsParameterName = "Maximum Evaluations";
     
    206206            double[] bestPopulation = new double[Problem.ProblemSize.Value];
    207207            double[] bestPopulationIteration = new double[Problem.ProblemSize.Value];
    208             int evals = 0; // number of function evaluations
    209208
    210209            //create initial population
     
    212211            for (int i = 0; i < PopulationSizeParameter.Value.Value; i++)
    213212            {
    214                 for (int j = 0; j < Problem.ProblemSize.Value; ++j)
    215                   {
     213                for (int j = 0; j < Problem.ProblemSize.Value; j++)
     214                {
    216215                    populationOld[i, j] = _random.NextDouble() * range + lb;
    217216                }
     
    224223            double[] populationRow = new double[Problem.ProblemSize.Value];
    225224
    226             bestPopulation = Get_ith_row(populationOld, best_index);
     225            bestPopulation = getMatrixRow(populationOld, best_index);
    227226            RealVector bestPopulationVector = new RealVector(bestPopulation);
    228             double bestPopulationValue = Obj(bestPopulationVector, evals);
     227            double bestPopulationValue = Obj(bestPopulationVector);
    229228            RealVector selectionVector;
    230229            RealVector trialVector;
     
    234233            for (var i = 0; i < PopulationSizeParameter.Value.Value; i++)
    235234            {
    236                 populationRow = Get_ith_row(populationOld, i);
     235                populationRow = getMatrixRow(populationOld, i);
    237236                trialVector = new RealVector(populationRow);
    238237
    239                 qtrial = Obj(trialVector, evals);
     238                qtrial = Obj(trialVector);
    240239
    241240                if (qtrial > bestPopulationValue)
    242241                {
    243242                    bestPopulationVector = new RealVector(populationRow);
    244                     bestPopulationValue = Obj(bestPopulationVector, evals);
     243                    bestPopulationValue = Obj(bestPopulationVector);
    245244                    best_index = i;
    246245                }
     
    250249
    251250            // Loop until iteration limit reached or canceled.
    252             //todo replace with a function
    253             while (evals < MaximumEvaluations
     251            // todo replace with a function
     252            while (ResultsEvaluations < MaximumEvaluations
    254253                && !cancellationToken.IsCancellationRequested
    255254                && bestPopulationValue > Problem.BestKnownQuality.Value + ValueToReachParameter.Value.Value)
     
    259258                for (int i = 0; i < PopulationSizeParameter.Value.Value; i++)
    260259                {
    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++)
    266277                    {
    267                         mutationPopulation[i,j] = populationOld[r1,j] +
    268                             ScalingFactorParameter.Value.Value * (populationOld[r2,j] - populationOld[r3,j]);
     278                        mutationPopulation[i,j] = populationOld[r0,j] +
     279                            ScalingFactorParameter.Value.Value * (populationOld[r1,j] - populationOld[r2,j]);
    269280                        //check the problem upper and lower bounds
    270281                        if (mutationPopulation[i,j] > ub) mutationPopulation[i,j] = ub;
     
    276287                for (int i = 0; i < PopulationSizeParameter.Value.Value; i++)
    277288                {
    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++)
    280291                    {
    281292                        if (_random.NextDouble() <= CrossoverProbabilityParameter.Value.Value || j == rnbr)
     
    293304                for (int i = 0; i < PopulationSizeParameter.Value.Value; i++)
    294305                {
    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);
    301311
    302312                    if (trailEval < selectionEval)
    303313                    {
    304                         for (int j = 0; j < Get_ith_row(populationOld, i).Length; j++)
     314                        for (int j = 0; j < getMatrixRow(populationOld, i).Length; j++)
    305315                        {
    306316                            populationOld[i, j] = trialPopulation[i, j];
     
    312322                for (int i = 0; i < PopulationSizeParameter.Value.Value; i++)
    313323                {
    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);
    317326                    if (quality < bestPopulationValue)
    318327                    {
     
    331340
    332341                //update the results in view
    333                 if (evals % 100 == 0 ) ResultsQualitiesBest.Values.Add(bestPopulationValue);
     342                if (iterations % 10 == 0 ) ResultsQualitiesBest.Values.Add(bestPopulationValue);
    334343                if (bestPopulationValue < Problem.BestKnownQuality.Value + ValueToReachParameter.Value.Value)
    335344                {
     
    337346                }
    338347            }
    339 
    340348        }
    341349       
    342350        //evaluate the vector
    343         public double Obj(RealVector x, int evals)
     351        public double Obj(RealVector x)
    344352        {
    345353            evals = evals + 1;
    346             if(Problem.Maximization.Value)
     354            if (Problem.Maximization.Value)
    347355            return -Problem.Evaluator.Evaluate(x);
    348356
     
    351359
    352360        // Get ith row from the matrix
    353         public double[] Get_ith_row(double[,] Mat, int i)
     361        public double[] getMatrixRow (double[,] Mat, int i)
    354362        {
    355363            double[] tmp = new double[Mat.GetUpperBound(1) + 1];
Note: See TracChangeset for help on using the changeset viewer.