Free cookie consent management tool by TermsFeed Policy Generator

Changeset 13756


Ignore:
Timestamp:
04/13/16 11:47:30 (8 years ago)
Author:
ichiriac
Message:

Add HyperVolume and Spacing calculators

Display hypervolume and spacing in the results

Location:
branches/ichiriac/HeuristicLab.Algorithms.GDE3
Files:
2 edited

Legend:

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

    r13749 r13756  
    1616using HeuristicLab.Random;
    1717using System.Threading;
     18using HeuristicLab.Algorithms.GDE3;
    1819
    1920namespace HeuristicLab.Algoritms.GDE3
     
    2324    [StorableClass]
    2425    [Creatable(CreatableAttribute.Categories.PopulationBasedAlgorithms, Priority = 400)]
    25     public class GDE3 : BasicAlgorithm 
     26    public class GDE3 : BasicAlgorithm
    2627    {
    27         public Func<IEnumerable<double>, double> Evaluation;
    28         private enum DominationResult { Dominates, IsDominated, IsNonDominated };
    29         IComparer<RealVector> dominance;
    30         public double[] ReferencePoint(int objectives)
    31         {
    32             return new double[] { 11, 11 };
    33         }
    34 
    3528        public override Type ProblemType
    3629        {
    3730            get { return typeof(MultiObjectiveTestFunctionProblem); }
    3831        }
    39          public new MultiObjectiveTestFunctionProblem Problem
     32        public new MultiObjectiveTestFunctionProblem Problem
    4033        {
    4134            get { return (MultiObjectiveTestFunctionProblem)base.Problem; }
     
    5548
    5649        #region ParameterNames
    57         private const string MaximumEvaluationsParameterName = "Maximum Evaluations";
     50        private const string MaximumGenerationsParameterName = "Maximum Generations";
    5851        private const string CrossoverProbabilityParameterName = "CrossoverProbability";
    5952        private const string PopulationSizeParameterName = "PopulationSize";
     
    6255
    6356        #region ParameterProperties
    64         public IFixedValueParameter<IntValue> MaximumEvaluationsParameter
    65         {
    66             get { return (IFixedValueParameter<IntValue>)Parameters[MaximumEvaluationsParameterName]; }
     57        public IFixedValueParameter<IntValue> MaximumGenerationsParameter
     58        {
     59            get { return (IFixedValueParameter<IntValue>)Parameters[MaximumGenerationsParameterName]; }
    6760        }
    6861        private ValueParameter<IntValue> PopulationSizeParameter
     
    8376        public int MaximumEvaluations
    8477        {
    85             get { return MaximumEvaluationsParameter.Value.Value; }
    86             set { MaximumEvaluationsParameter.Value.Value = value; }
     78            get { return MaximumGenerationsParameter.Value.Value; }
     79            set { MaximumGenerationsParameter.Value.Value = value; }
    8780        }
    8881
     
    111104        }
    112105
    113         private double ResultsGenerationalDistance
     106        private double ResultsInvertedGenerationalDistance
    114107        {
    115108            get { return ((DoubleValue)Results["InvertedGenerationalDistance"].Value).Value; }
     
    119112        private double ResultsHypervolume
    120113        {
    121             get { return ((DoubleValue)Results["HipervolumeValue"].Value).Value; }
    122             set { ((DoubleValue)Results["HipervolumeValue"].Value).Value = value; }
     114            get { return ((DoubleValue)Results["HyperVolumeValue"].Value).Value; }
     115            set { ((DoubleValue)Results["HyperVolumeValue"].Value).Value = value; }
    123116        }
    124117
     
    134127            set { ((IntValue)Results["Evaluations"].Value).Value = value; }
    135128        }
     129        private int ResultsGenerations
     130        {
     131            get { return ((IntValue)Results["Generations"].Value).Value; }
     132            set { ((IntValue)Results["Generations"].Value).Value = value; }
     133        }
     134        private double ResultsGenerationalDistance
     135        {
     136            get { return ((DoubleValue)Results["GenerationalDistance"].Value).Value; }
     137            set { ((DoubleValue)Results["GenerationalDistance"].Value).Value = value; }
     138        }
    136139        private int ResultsIterations
    137140        {
     
    140143        }
    141144
    142         private DataTable ResultsQualities
    143         {
    144             get { return ((DataTable)Results["Qualities"].Value); }
    145         }
    146         private DataRow ResultsQualitiesBest
    147         {
    148             get { return ResultsQualities.Rows["Best Quality"]; }
     145        private double ResultsSpacing
     146        {
     147            get { return ((DoubleValue)Results["Spacing"].Value).Value; }
     148            set { ((DoubleValue)Results["Spacing"].Value).Value = value; }
     149        }
     150
     151        private double ResultsCrowding
     152        {
     153            get { return ((DoubleValue)Results["Crowding"].Value).Value; }
     154            set { ((DoubleValue)Results["Crowding"].Value).Value = value; }
    149155        }
    150156
    151157        #endregion
    152 
    153         [Storable]
    154         private RankBasedParetoFrontAnalyzer paretoFrontAnalyzer;
    155158
    156159        [StorableConstructor]
     
    160163          : base(original, cloner)
    161164        {
    162             paretoFrontAnalyzer = (RankBasedParetoFrontAnalyzer)cloner.Clone(original.paretoFrontAnalyzer);
    163165        }
    164166
     
    170172        public GDE3()
    171173        {
    172             Parameters.Add(new FixedValueParameter<IntValue>(MaximumEvaluationsParameterName, "", new IntValue(Int32.MaxValue)));
     174            Parameters.Add(new FixedValueParameter<IntValue>(MaximumGenerationsParameterName, "", new IntValue(1000)));
    173175            Parameters.Add(new ValueParameter<IntValue>(PopulationSizeParameterName, "The size of the population of solutions.", new IntValue(100)));
    174176            Parameters.Add(new ValueParameter<DoubleValue>(CrossoverProbabilityParameterName, "The value for crossover rate", new DoubleValue(0.5)));
     
    180182        {
    181183            // Set up the results display
    182             Results.Add(new Result("Iterations", new IntValue(0)));
     184            Results.Add(new Result("Generations", new IntValue(0)));
    183185            Results.Add(new Result("Evaluations", new IntValue(0)));
    184186            Results.Add(new Result("Best Front", new DoubleMatrix()));
     187            Results.Add(new Result("Crowding", new DoubleValue(0)));
    185188            Results.Add(new Result("InvertedGenerationalDistance", new DoubleValue(0)));
    186             Results.Add(new Result("HipervolumeValue", new DoubleValue(0)));
     189            Results.Add(new Result("GenerationalDistance", new DoubleValue(0)));
     190            Results.Add(new Result("HyperVolumeValue", new DoubleValue(0)));
     191            Results.Add(new Result("Spacing", new DoubleValue(0)));
    187192            Results.Add(new Result("Scatterplot", typeof(IMOFrontModel)));
    188193            var table = new DataTable("Qualities");
     
    190195            Results.Add(new Result("Qualities", table));
    191196
    192             //create initial population
    193             var population = new List<SolutionSet>(PopulationSizeParameter.Value.Value);
    194             var offspringPopulation = new List<SolutionSet>(PopulationSizeParameter.Value.Value);
    195             var matingPopulation = new List<SolutionSet>();
     197            //setup the variables
     198            List<SolutionSet> population;
     199            List<SolutionSet> offspringPopulation;
     200            SolutionSet[] parent;
     201           
     202            //initialize population
     203            population = new List<SolutionSet>(PopulationSizeParameter.Value.Value);
    196204
    197205            for (int i = 0; i < PopulationSizeParameter.Value.Value; ++i)
     
    199207                var m = createIndividual();
    200208                population.Add(m);
    201                 var n = createEmptyIndividual();
    202                 offspringPopulation.Add(n);
    203209            }
    204210
     
    206212            int iterations = 1;
    207213
    208             while (ResultsEvaluations < MaximumEvaluations
     214            while (ResultsGenerations < MaximumGenerationsParameter.Value.Value
    209215               && !cancellationToken.IsCancellationRequested)
    210216            {
    211                 matingPopulation = selection(population);
    212                 offspringPopulation = reproduction(matingPopulation, population, offspringPopulation);
    213 
    214                 population = replacement(population, offspringPopulation);
    215 
    216                 double[][] qualitiesFinal = new double[PopulationSizeParameter.Value.Value][];
    217 
    218                 for (int i = 0; i < PopulationSizeParameter.Value.Value; ++i)
    219                 {
    220                     qualitiesFinal[i] = new double[Problem.Objectives];
    221                     for (int j = 0; j < Problem.Objectives; ++j)
     217                var populationSize = PopulationSizeParameter.Value.Value;
     218
     219                // Create the offSpring solutionSet
     220                offspringPopulation = new List<SolutionSet>(PopulationSizeParameter.Value.Value * 2);
     221
     222                for (int i = 0; i < populationSize; i++)
     223                {
     224                    // Obtain parents. Two parameters are required: the population and the
     225                    //                 index of the current individual
     226                    parent = selection(population, i);
     227
     228                    SolutionSet child;
     229                    // Crossover. The parameters are the current individual and the index of the array of parents
     230                    child = reproduction(population[i], parent);
     231
     232                    child.Quality = Problem.Evaluate(child.Population, _random);
     233                    this.updateProgres();
     234
     235                    // Dominance test
     236                    int result;
     237                    result = compareDomination(population[i].Quality, child.Quality);
     238                    if (result == -1)
     239                    { // Solution i dominates child
     240                        offspringPopulation.Add(population[i]);
     241                    }
     242                    else if (result == 1)
     243                    { // child dominates
     244                        offspringPopulation.Add(child);
     245                    }
     246                    else
     247                    { // the two solutions are non-dominated
     248                        offspringPopulation.Add(child);
     249                        offspringPopulation.Add(population[i]);
     250                    }
     251                }
     252                // Ranking the offspring population
     253                List<SolutionSet>[] ranking = computeRanking(offspringPopulation);
     254
     255                int remain = populationSize;
     256                int index = 0;
     257                population.Clear();
     258                List<SolutionSet> front = null;
     259
     260                // Obtain the next front
     261                front = ranking[index];
     262
     263                while ((remain > 0) && (remain >= front.Count))
     264                {
     265                    //Assign crowding distance to individuals
     266                    crowdingDistanceAssignment(front, index);
     267                    //Add the individuals of this front
     268                    for (int k = 0; k < front.Count; k++)
    222269                    {
    223                         qualitiesFinal[i][j] = population[i].Quality[j];
    224                     }
    225                 }
    226 
    227                 double[][] populationFinal = new double[PopulationSizeParameter.Value.Value][];
    228 
    229                 for (int i = 0; i < PopulationSizeParameter.Value.Value; ++i)
    230                 {
    231                     populationFinal[i] = new double[Problem.ProblemSize];
    232                     for (int j = 0; j < Problem.ProblemSize; ++j)
     270                        population.Add(front[k]);
     271                    } // for
     272
     273                    //Decrement remain
     274                    remain = remain - front.Count;
     275
     276                    //Obtain the next front
     277                    index++;
     278                    if (remain > 0)
    233279                    {
    234                         populationFinal[i][j] = population[i].Population[j];
    235                     }
    236                 }
    237 
    238                 int objectives = Problem.Objectives;
    239                 var optimalfront = Problem.TestFunction.OptimalParetoFront(objectives);
    240 
    241                 IEnumerable<double[]> en = qualitiesFinal;
    242 
    243                 double[][] opf = new double[0][];
    244                 if (optimalfront != null)
    245                 {
    246                     opf = optimalfront.Select(s => s.ToArray()).ToArray();
    247                 }
    248 
    249                 iterations = iterations + 1;
    250 
    251                 IEnumerable<double[]> front = NonDominatedSelect.selectNonDominatedVectors(qualitiesFinal, Problem.TestFunction.Maximization(objectives), true);
    252                 //update the results
    253                 ResultsEvaluations = evals;
    254                 ResultsIterations = iterations;
    255                 ResultsBestFront = new DoubleMatrix(MultiObjectiveTestFunctionProblem.To2D(qualitiesFinal));
    256                 ResultsGenerationalDistance = InvertedGenerationalDistance.Calculate(en, optimalfront, 1);
    257                 ResultsHypervolume = Hypervolume.Calculate(front, ReferencePoint(objectives), Problem.TestFunction.Maximization(objectives));
    258 
    259                 Results["Scatterplot"].Value = new MOSolution(qualitiesFinal, populationFinal, opf, objectives);
    260             }
    261         }
    262        
     280                        front = ranking[index];
     281                    }
     282                }
     283
     284                // remain is less than front(index).size, insert only the best one
     285                if (remain > 0)
     286                {  // front contains individuals to insert
     287                    while (front.Count > remain)
     288                    {
     289                        crowdingDistanceAssignment(front, index);
     290                        int indx = getWorstIndex(front);
     291                        front.RemoveAt(indx);
     292                    }
     293                    for (int k = 0; k < front.Count; k++)
     294                    {
     295                        population.Add(front[k]);
     296                    }
     297
     298                    remain = 0;
     299                }
     300                iterations++;
     301                ResultsGenerations = iterations;
     302                displayResults(front);
     303            }
     304        }
     305
     306        private void displayResults(List<SolutionSet> population)
     307        {
     308            //compute the 0 front
     309            // Return the first non-dominated front
     310            List<SolutionSet>[] rankingFinal = computeRanking(population);
     311
     312            int objectives = Problem.Objectives;
     313            var optimalfront = Problem.TestFunction.OptimalParetoFront(objectives);
     314
     315            double[][] opf = new double[0][];
     316            if (optimalfront != null)
     317            {
     318                opf = optimalfront.Select(s => s.ToArray()).ToArray();
     319            }
     320
     321            double[][] qualitiesFinal = new double[rankingFinal[0].Count][];
     322
     323            for (int i = 0; i < rankingFinal[0].Count; ++i)
     324            {
     325                qualitiesFinal[i] = new double[Problem.Objectives];
     326                for (int j = 0; j < Problem.Objectives; ++j)
     327                {
     328                    qualitiesFinal[i][j] = rankingFinal[0][i].Quality[j];
     329                }
     330            }
     331
     332            double[][] populationFinal = new double[rankingFinal[0].Count][];
     333
     334            for (int i = 0; i < rankingFinal[0].Count; ++i)
     335            {
     336                populationFinal[i] = new double[Problem.ProblemSize];
     337                for (int j = 0; j < Problem.ProblemSize; ++j)
     338                {
     339                    populationFinal[i][j] = rankingFinal[0][i].Population[j];
     340                }
     341            }
     342            IEnumerable<double[]> en = qualitiesFinal;
     343            IEnumerable<double[]> frontVectors = NonDominatedSelect.selectNonDominatedVectors(qualitiesFinal, Problem.TestFunction.Maximization(objectives), true);
     344            //update the results
     345
     346            ResultsEvaluations = this.evals;
     347            ResultsBestFront = new DoubleMatrix(MultiObjectiveTestFunctionProblem.To2D(qualitiesFinal));
     348            ResultsCrowding = Crowding.Calculate(qualitiesFinal, Problem.TestFunction.Bounds(objectives));
     349            ResultsInvertedGenerationalDistance = InvertedGenerationalDistance.Calculate(en, optimalfront, 1);
     350            ResultsHypervolume = Hypervolume.Calculate(frontVectors, Problem.TestFunction.ReferencePoint(objectives), Problem.TestFunction.Maximization(objectives));
     351            ResultsGenerationalDistance = GenerationalDistance.Calculate(qualitiesFinal, optimalfront, 1);
     352            Results["Scatterplot"].Value = new MOSolution(qualitiesFinal, populationFinal, opf, objectives);
     353            ResultsSpacing = Spacing.Calculate(qualitiesFinal);
     354        }
     355
     356        private int getWorstIndex(List<SolutionSet> SolutionsList)
     357        {
     358            int result = 0;
     359
     360            if ((SolutionsList == null) || SolutionsList.Count == 0)
     361            {
     362                result = 0;
     363            }
     364            else
     365            {
     366                SolutionSet worstKnown = SolutionsList[0],
     367                            candidateSolution;
     368                int flag;
     369                for (int i = 1; i < SolutionsList.Count; i++)
     370                {
     371                    candidateSolution = SolutionsList[i];
     372                    flag = compareDomination(worstKnown.Quality, candidateSolution.Quality);
     373                    if (flag == -1)
     374                    {
     375                        result = i;
     376                        worstKnown = candidateSolution;
     377                    }
     378                }
     379            }
     380            return result;
     381        }
     382
    263383        protected SolutionSet createIndividual()
    264384        {
     
    296416        protected void updateProgres()
    297417        {
    298             this.evals += PopulationSizeParameter.Value.Value;
    299         }
    300 
    301         protected List<SolutionSet> selection(List<SolutionSet> population)
    302         {
    303             List<SolutionSet> parents = new List<SolutionSet>();
    304             for (int i = 0; i < PopulationSizeParameter.Value.Value; i++)
    305             {
    306                 int r0, r1, r2;
    307                 //assure the selected vectors r0, r1 and r2 are different
    308                 do
    309                 {
    310                     r0 = _random.Next(0, PopulationSizeParameter.Value.Value);
    311                 } while (r0 == i);
    312                 do
    313                 {
    314                     r1 = _random.Next(0, PopulationSizeParameter.Value.Value);
    315                 } while (r1 == i || r1 == r0);
    316                 do
    317                 {
    318                     r2 = _random.Next(0, PopulationSizeParameter.Value.Value);
    319                 } while (r2 == i || r2 == r0 || r2 == r1);
    320 
    321                 parents.Add(population[r0]);
    322                 parents.Add(population[r1]);
    323                 parents.Add(population[r2]);
    324             }
     418            this.evals++;
     419        }
     420
     421        protected SolutionSet[] selection(List<SolutionSet> population, int i)
     422        {
     423            SolutionSet[] parents = new SolutionSet[3];
     424            int r0, r1, r2;
     425            //assure the selected vectors r0, r1 and r2 are different
     426            do
     427            {
     428                r0 = _random.Next(0, PopulationSizeParameter.Value.Value);
     429            } while (r0 == i);
     430            do
     431            {
     432                r1 = _random.Next(0, PopulationSizeParameter.Value.Value);
     433            } while (r1 == i || r1 == r0);
     434            do
     435            {
     436                r2 = _random.Next(0, PopulationSizeParameter.Value.Value);
     437            } while (r2 == i || r2 == r0 || r2 == r1);
     438
     439            parents[0] = population[r0];
     440            parents[1] = population[r1];
     441            parents[2] = population[r2];
     442
    325443            return parents;
    326444        }
    327445
    328         protected List<SolutionSet> reproduction(List<SolutionSet> matingPopulation, List<SolutionSet> parentPopulation, List<SolutionSet> offspringPopulation)
    329         {
    330             for (int i = 0; i < PopulationSizeParameter.Value.Value; i++)
    331             {
    332                 List<SolutionSet> parents = new List<SolutionSet>(3);
    333                 for (int j = 0; j < 3; j++) {
    334                     parents.Add(matingPopulation[0]);
    335                     matingPopulation.RemoveAt(0);
    336                 }
    337 
    338                 double rnbr = _random.Next(0, Problem.ProblemSize);
    339                 for (int m = 0; m < Problem.ProblemSize; m++)
    340                 {
    341                     if (_random.NextDouble() < CrossoverProbabilityParameter.Value.Value || m == rnbr)
    342                     {
    343                         offspringPopulation[i].Population[m] = parents[2].Population[m] +
    344                             ScalingFactorParameter.Value.Value * (parents[1].Population[m] - parents[2].Population[0]);
    345                         //check the problem upper and lower bounds
    346                         if (offspringPopulation[i].Population[m] > Problem.Bounds[0, 1]) offspringPopulation[i].Population[m] = Problem.Bounds[0, 1];
    347                         if (offspringPopulation[i].Population[m] < Problem.Bounds[0, 0]) offspringPopulation[i].Population[m] = Problem.Bounds[0, 0];
    348                     }
    349                     else
    350                     {
    351                         offspringPopulation[i].Population[m] = parentPopulation[i].Population[m]; // check again
    352                     }
    353                 }
    354                 offspringPopulation[i].Quality = Problem.Evaluate(offspringPopulation[i].Population, _random);
    355             }
    356             this.updateProgres();
    357             return offspringPopulation;
     446        protected SolutionSet reproduction(SolutionSet parent, SolutionSet[] parentsSolutions)
     447        {
     448            var individual = createEmptyIndividual();
     449            double rnbr = _random.Next(0, Problem.ProblemSize);
     450            for (int m = 0; m < Problem.ProblemSize; m++)
     451            {
     452                if (_random.NextDouble() < CrossoverProbabilityParameter.Value.Value || m == rnbr)
     453                {
     454                    double value;
     455                    value = parentsSolutions[2].Population[m] +
     456                        ScalingFactorParameter.Value.Value * (parentsSolutions[0].Population[m] - parentsSolutions[1].Population[m]);
     457                    //check the problem upper and lower bounds
     458                    if (value > Problem.Bounds[0, 1]) value = Problem.Bounds[0, 1];
     459                    if (value < Problem.Bounds[0, 0]) value = Problem.Bounds[0, 0];
     460                    individual.Population[m] = value;
     461                }
     462                else
     463                {
     464                    double value;
     465                    value = parent.Population[m];
     466                    individual.Population[m] = value;
     467                }
     468            }
     469            return individual;
    358470        }
    359471
     
    380492            }
    381493
    382             List<SolutionSet>[]ranking = computeRanking(tmpList);
     494            List<SolutionSet>[] ranking = computeRanking(tmpList);
    383495            List<SolutionSet> finalPopulation = crowdingDistanceSelection(ranking);
    384496            return finalPopulation;
     
    391503            while (populationIsNotFull(population))
    392504            {
    393                 if (subFrontFillsIntoThePopulation(ranking, rankingIndex, population)){
     505                if (subFrontFillsIntoThePopulation(ranking, rankingIndex, population))
     506                {
    394507                    addRankedSolutionToPopulation(ranking, rankingIndex, population);
    395508                    rankingIndex++;
    396                 } else {
     509                }
     510                else {
    397511                    crowdingDistanceAssignment(ranking[rankingIndex], rankingIndex);
    398512                    addLastRankedSolutionToPopulation(ranking, rankingIndex, population);
     
    448562            double distance;
    449563
    450             for (int i = 0; i < Problem.Objectives ; i++)
     564            for (int i = 0; i < Problem.Objectives; i++)
    451565            {
    452566                // Sort the population by Obj n           
     
    456570
    457571                //Set de crowding distance           
    458                 front[0].CrowdingDistance = double.PositiveInfinity; 
     572                front[0].CrowdingDistance = double.PositiveInfinity;
    459573                front[size - 1].CrowdingDistance = double.PositiveInfinity;
    460574
     
    471585        private void addRankedSolutionToPopulation(List<SolutionSet>[] ranking, int rankingIndex, List<SolutionSet> population)
    472586        {
    473             foreach(SolutionSet solution in ranking[rankingIndex])
     587            foreach (SolutionSet solution in ranking[rankingIndex])
    474588            {
    475589                population.Add(solution);
     
    497611            // front[i] contains the list of individuals belonging to the front i
    498612            List<int>[] front = new List<int>[tmpList.Count + 1];
    499             int[] solutionsRanks = new int[tmpList.Count];
    500613
    501614            // flagDominate is an auxiliar encodings.variable
  • branches/ichiriac/HeuristicLab.Algorithms.GDE3/HeuristicLab.Algorithms.GDE3.csproj

    r13749 r13756  
    114114  <ItemGroup>
    115115    <Compile Include="GDE3.cs" />
     116    <Compile Include="GenerationalDistanceCalculator.cs" />
    116117    <Compile Include="Plugin.cs" />
    117118    <Compile Include="Properties\AssemblyInfo.cs" />
Note: See TracChangeset for help on using the changeset viewer.