Free cookie consent management tool by TermsFeed Policy Generator

Changeset 13849


Ignore:
Timestamp:
05/18/16 11:27:21 (8 years ago)
Author:
ichiriac
Message:

Constrained test function

Check for constrained test function and evaluated the constraints violations
Add to dominance comparator also constraints violations

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

Legend:

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

    r13756 r13849  
    4646        private readonly IRandom _random = new MersenneTwister();
    4747        private int evals;
     48        private double IGDSumm;
    4849
    4950        #region ParameterNames
     
    5253        private const string PopulationSizeParameterName = "PopulationSize";
    5354        private const string ScalingFactorParameterName = "ScalingFactor";
     55
    5456        #endregion
    5557
     
    104106        }
    105107
     108        private double ResultsIGDMean
     109        {
     110            get { return ((DoubleValue)Results["IGDMeanValue"].Value).Value; }
     111            set { ((DoubleValue)Results["IGDMeanValue"].Value).Value = value; }
     112        }
     113
     114        private double ResultsIGDBest
     115        {
     116            get { return ((DoubleValue)Results["IGDBestValue"].Value).Value; }
     117            set { ((DoubleValue)Results["IGDBestValue"].Value).Value = value; }
     118        }
     119
     120        private double ResultsIGDWorst
     121        {
     122            get { return ((DoubleValue)Results["IGDWorstValue"].Value).Value; }
     123            set { ((DoubleValue)Results["IGDWorstValue"].Value).Value = value; }
     124        }
     125
    106126        private double ResultsInvertedGenerationalDistance
    107127        {
     
    136156            get { return ((DoubleValue)Results["GenerationalDistance"].Value).Value; }
    137157            set { ((DoubleValue)Results["GenerationalDistance"].Value).Value = value; }
    138         }
    139         private int ResultsIterations
    140         {
    141             get { return ((IntValue)Results["Iterations"].Value).Value; }
    142             set { ((IntValue)Results["Iterations"].Value).Value = value; }
    143158        }
    144159
     
    189204            Results.Add(new Result("GenerationalDistance", new DoubleValue(0)));
    190205            Results.Add(new Result("HyperVolumeValue", new DoubleValue(0)));
     206            Results.Add(new Result("IGDMeanValue", new DoubleValue(0)));
     207            Results.Add(new Result("IGDBestValue", new DoubleValue(Int32.MaxValue)));
     208            Results.Add(new Result("IGDWorstValue", new DoubleValue(0)));
     209
    191210            Results.Add(new Result("Spacing", new DoubleValue(0)));
    192211            Results.Add(new Result("Scatterplot", typeof(IMOFrontModel)));
     
    199218            List<SolutionSet> offspringPopulation;
    200219            SolutionSet[] parent;
     220            double IGDSumm = 0;
    201221           
    202222            //initialize population
     
    206226            {
    207227                var m = createIndividual();
     228                m.Quality = Problem.Evaluate(m.Population, _random);
     229                //the test function is constrained
     230                if (m.Quality.Length > Problem.Objectives)
     231                {
     232                    m.OverallConstrainViolation = m.Quality[Problem.Objectives];
     233                } else {
     234                    m.OverallConstrainViolation = 0;
     235                }
    208236                population.Add(m);
    209237            }
    210238
    211239            this.initProgress();
    212             int iterations = 1;
     240            int generations = 1;
    213241
    214242            while (ResultsGenerations < MaximumGenerationsParameter.Value.Value
     
    231259
    232260                    child.Quality = Problem.Evaluate(child.Population, _random);
     261
    233262                    this.updateProgres();
     263
     264                    //the test function is constrained
     265                    if (child.Quality.Length > Problem.Objectives)
     266                    {
     267                        child.OverallConstrainViolation = child.Quality[Problem.Objectives];
     268                    } else {
     269                        child.OverallConstrainViolation = 0;
     270                    }
    234271
    235272                    // Dominance test
    236273                    int result;
    237                     result = compareDomination(population[i].Quality, child.Quality);
     274                    result = compareDomination(population[i], child);
     275
    238276                    if (result == -1)
    239277                    { // Solution i dominates child
     
    250288                    }
    251289                }
     290
    252291                // Ranking the offspring population
    253292                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++)
    269                     {
    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)
    279                     {
    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);
     293                population = crowdingDistanceSelection(ranking);
     294                generations++;
     295                ResultsGenerations = generations;
     296                displayResults(population);
    303297            }
    304298        }
     
    306300        private void displayResults(List<SolutionSet> population)
    307301        {
    308             //compute the 0 front
    309             // Return the first non-dominated front
    310302            List<SolutionSet>[] rankingFinal = computeRanking(population);
    311303
     
    319311            }
    320312
     313            //compute the final qualities and population
    321314            double[][] qualitiesFinal = new double[rankingFinal[0].Count][];
     315            double[][] populationFinal = new double[rankingFinal[0].Count][];
    322316
    323317            for (int i = 0; i < rankingFinal[0].Count; ++i)
    324318            {
    325319                qualitiesFinal[i] = new double[Problem.Objectives];
     320                populationFinal[i] = new double[Problem.Objectives];
    326321                for (int j = 0; j < Problem.Objectives; ++j)
    327322                {
     323                    populationFinal[i][j] = rankingFinal[0][i].Population[j];
    328324                    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];
    340325                }
    341326            }
     
    352337            Results["Scatterplot"].Value = new MOSolution(qualitiesFinal, populationFinal, opf, objectives);
    353338            ResultsSpacing = Spacing.Calculate(qualitiesFinal);
     339
     340            if (ResultsIGDBest > ResultsInvertedGenerationalDistance) {
     341                ResultsIGDBest = ResultsInvertedGenerationalDistance;
     342            }
     343            if (ResultsIGDWorst < ResultsInvertedGenerationalDistance)
     344            {
     345                ResultsIGDWorst = ResultsInvertedGenerationalDistance;
     346            }
     347            this.IGDSumm += ResultsInvertedGenerationalDistance;
     348            ResultsIGDMean = this.IGDSumm / ResultsGenerations;
    354349        }
    355350
     
    370365                {
    371366                    candidateSolution = SolutionsList[i];
    372                     flag = compareDomination(worstKnown.Quality, candidateSolution.Quality);
     367                    flag = compareDomination(worstKnown, candidateSolution);
    373368                    if (flag == -1)
    374369                    {
     
    395390
    396391            }
    397             var m = new RealVector(v);
    398             solutionObject.Population = m;
    399             solutionObject.Quality = Problem.Evaluate(m, _random);
     392            solutionObject.createSolution(v);
    400393            return solutionObject;
    401394        }
     
    470463        }
    471464
    472         private List<SolutionSet> replacement(List<SolutionSet> population, List<SolutionSet> offspringPopulation)
    473         {
    474             List<SolutionSet> tmpList = new List<SolutionSet>();
    475             for (int i = 0; i < PopulationSizeParameter.Value.Value; i++)
    476             {
    477                 int result;
    478                 result = compareDomination(population[i].Quality, offspringPopulation[i].Quality);
    479                 if (result == -1)
    480                 { // Solution i dominates child
    481                     tmpList.Add(population[i]);
    482                 }
    483                 else if (result == 1)
    484                 { // child dominates
    485                     tmpList.Add(offspringPopulation[i]);
    486                 }
    487                 else
    488                 { // the two solutions are non-dominated
    489                     tmpList.Add(offspringPopulation[i]);
    490                     tmpList.Add(population[i]);
    491                 }
    492             }
    493 
    494             List<SolutionSet>[] ranking = computeRanking(tmpList);
    495             List<SolutionSet> finalPopulation = crowdingDistanceSelection(ranking);
    496             return finalPopulation;
    497         }
    498 
    499465        private List<SolutionSet> crowdingDistanceSelection(List<SolutionSet>[] ranking)
    500466        {
     
    509475                }
    510476                else {
    511                     crowdingDistanceAssignment(ranking[rankingIndex], rankingIndex);
     477                    crowdingDistanceAssignment(ranking[rankingIndex]);
    512478                    addLastRankedSolutionToPopulation(ranking, rankingIndex, population);
    513479                }
     
    519485        {
    520486            List<SolutionSet> currentRankedFront = ranking[rankingIndex];
    521             currentRankedFront.Sort((x, y) => x.CrowdingDistance.CompareTo(y.CrowdingDistance));
     487            //descending sort and add the front with highest crowding distance to the population
     488            currentRankedFront.Sort((x, y) => -x.CrowdingDistance.CompareTo(y.CrowdingDistance));
    522489            int i = 0;
    523490            while (population.Count < PopulationSizeParameter.Value.Value)
     
    528495        }
    529496
    530         public void crowdingDistanceAssignment(List<SolutionSet> rankingSubfront, int index)
     497        public void crowdingDistanceAssignment(List<SolutionSet> rankingSubfront)
    531498        {
    532499            int size = rankingSubfront.Count;
     
    556523
    557524            for (int i = 0; i < size; i++)
    558                 rankingSubfront[i].CrowdingDistance = 0.0;
     525                front[i].CrowdingDistance = 0.0;
    559526
    560527            double objetiveMaxn;
     
    564531            for (int i = 0; i < Problem.Objectives; i++)
    565532            {
    566                 // Sort the population by Obj n           
     533                // Sort the front population by the objective i         
    567534                front.Sort((x, y) => x.Quality[i].CompareTo(y.Quality[i]));
    568535                objetiveMinn = front[0].Quality[i];
    569536                objetiveMaxn = front[front.Count - 1].Quality[i];
    570537
    571                 //Set de crowding distance            
     538                //Set crowding distance for the current front           
    572539                front[0].CrowdingDistance = double.PositiveInfinity;
    573540                front[size - 1].CrowdingDistance = double.PositiveInfinity;
     
    627594                // Initialize the list of individuals that i dominate and the number
    628595                // of individuals that dominate me
    629                 iDominate[p] = new List<int>();
     596                iDominate[p] = new List<int>(); 
    630597                dominateMe[p] = 0;
    631598            }
     
    635602                for (int q = p + 1; q < tmpList.Count; q++)
    636603                {
    637                     flagDominate = compareDomination(tmpList[p].Quality, tmpList[q].Quality);
     604                    flagDominate = compareConstraintsViolation(tmpList[p], tmpList[q]);
     605                    if (flagDominate == 0) {
     606                        flagDominate = compareDomination(tmpList[p], tmpList[q]);
     607                    }
    638608                    if (flagDominate == -1)
    639609                    {
     
    693663        }
    694664
    695         private int compareDomination(double[] solution1, double[] solution2)
     665        private int compareDomination(SolutionSet solution1, SolutionSet solution2)
    696666        {
    697667            int dominate1; // dominate1 indicates if some objective of solution1
     
    704674            int flag; //stores the result of the comparison
    705675
     676            // Test to determine whether at least a solution violates some constraint
     677            if (needToCompareViolations(solution1, solution2))
     678            {
     679                return compareConstraintsViolation(solution1, solution2);
     680            }
     681
    706682            // Equal number of violated constraints. Applying a dominance Test then
    707683            double value1, value2;
    708684            for (int i = 0; i < Problem.Objectives; i++)
    709685            {
    710                 value1 = solution1[i];
    711                 value2 = solution2[i];
     686                value1 = solution1.Quality[i];
     687                value2 = solution2.Quality[i];
    712688                if (value1 < value2)
    713689                {
    714690                    flag = -1;
    715691                }
    716                 else if (value1 > value2)
     692                else if (value2 < value1)
    717693                {
    718694                    flag = 1;
     
    743719            }
    744720            return 1;    // solution2 dominate   
     721        }
     722
     723        private bool needToCompareViolations(SolutionSet solution1, SolutionSet solution2)
     724        {
     725            bool needToCompare;
     726            needToCompare = (solution1.OverallConstrainViolation < 0) || (solution2.OverallConstrainViolation < 0);
     727
     728            return needToCompare;
     729        }
     730
     731        private int compareConstraintsViolation(SolutionSet solution1, SolutionSet solution2)
     732        {
     733            int result;
     734            double overall1, overall2;
     735            overall1 = solution1.OverallConstrainViolation;
     736            overall2 = solution2.OverallConstrainViolation;
     737
     738            if ((overall1 < 0) && (overall2 < 0))
     739            {
     740                if (overall1 > overall2)
     741                {
     742                    result = -1;
     743                }
     744                else if (overall2 > overall1)
     745                {
     746                    result = 1;
     747                }
     748                else
     749                {
     750                    result = 0;
     751                }
     752            }
     753            else if ((overall1 == 0) && (overall2 < 0))
     754            {
     755                result = -1;
     756            }
     757            else if ((overall1 < 0) && (overall2 == 0))
     758            {
     759                result = 1;
     760            }
     761            else
     762            {
     763                result = 0;
     764            }
     765            return result;
    745766        }
    746767    }
  • branches/ichiriac/HeuristicLab.Algorithms.GDE3/SolutionSet.cs

    r13749 r13849  
    1212        private double crowdingDistance;
    1313        private double rank;
     14        private double overallConstrainViolation;
     15        public int populationSize;
     16
     17        //constructor set size equal with population size
     18        public SolutionSet(int populationSize)
     19        {
     20            this.populationSize = populationSize;
     21        }
    1422
    1523        public double CrowdingDistance
     
    3745        }
    3846
    39 
    40         public int populationSize;
    41         public SolutionSet(int populationSize) {
    42             this.populationSize = populationSize;
     47        public double OverallConstrainViolation
     48        {
     49            get { return overallConstrainViolation; }
     50            set { overallConstrainViolation = value; }
    4351        }
    4452
Note: See TracChangeset for help on using the changeset viewer.