Esempio n. 1
0
        public void CrowdingDistanceAssignment(Population_NSGA population)
        {
            int size = population.Population_size;

            if (size == 0)
            {
                return;
            }

            if (size == 1)
            {
                population.Get(0).CrowdingDistance = Double.MaxValue;
                return;
            }
            if (size == 2)
            {
                population.Get(0).CrowdingDistance = Double.MaxValue;
                population.Get(1).CrowdingDistance = Double.MaxValue;
                return;
            }

            Population_NSGA front = new Population_NSGA(size);

            for (int i = 0; i < size; i++)
            {
                front.Add(population.Get(i));
            }

            for (int i = 0; i < size; i++)
            {
                front.Get(i).CrowdingDistance = 0.0;
            }

            double objetiveMaxn;
            double objetiveMinn;
            double distance;

            //2 hàm mục tiêu
            for (int i = 0; i < 2; i++)
            {
                // Sort the population by Obj n
                front.Sort(new ObjectiveComparator(i));
                objetiveMinn = front.Get(0).Objective[i];
                objetiveMaxn = front.Get(front.Size() - 1).Objective[i];

                //Set de crowding distance
                front.Get(0).CrowdingDistance        = double.PositiveInfinity;
                front.Get(size - 1).CrowdingDistance = double.PositiveInfinity;

                for (int j = 1; j < size - 1; j++)
                {
                    distance  = front.Get(j + 1).Objective[i] - front.Get(j - 1).Objective[i];
                    distance  = distance / (objetiveMaxn - objetiveMinn);
                    distance += front.Get(j).CrowdingDistance;
                    front.Get(j).CrowdingDistance = distance;
                }
            }
        }
Esempio n. 2
0
        } // Train

        public Population_NSGA TrainWithNSGAII(double[][] trainData, int maxEvaluations)
        {
            // Chu y cho nay:
            DoCentroids(trainData);   // find representative data, store their x-values into this.centroids
            DoWidths(this.centroids); // measure of how far apart centroids are
            Population_NSGA bestWeights = DoWeightsWithNSGAII(trainData, maxEvaluations);

            return(bestWeights);
        } // Train
Esempio n. 3
0
        } // DoWeights

        private Population_NSGA DoWeightsWithNSGAII(double[][] trainData, int maxEvaluations)
        {
            int             populationSize     = 100;
            int             numberOfObjectives = 2;
            int             Dim             = (numHidden * numOutput) + numOutput; // dimensions is num weights + num biases
            NSGAII          nsgaIIAlgorithm = new NSGAII(populationSize, Dim, this, maxEvaluations, trainData, numberOfObjectives);
            Population_NSGA bestWeights     = new Population_NSGA();

            bestWeights = nsgaIIAlgorithm.Execute();
            ///
            Population_NSGA returnResult = new Population_NSGA();

            return(returnResult);
        } // DoWeights
Esempio n. 4
0
        /// <summary>
        /// Runs the NSGA-II algorithm.
        /// </summary>
        /// <returns>a <code>SolutionSet</code> that is a set of non dominated solutions as a result of the algorithm execution</returns>
        public Population_NSGA Execute()
        {
            int             evaluations;
            Population_NSGA population;
            Population_NSGA offspringPopulation;
            Population_NSGA union;

            //Initialize the variables
            evaluations = 0;

            // Bước 1: Khởi tạo quần thể
            // Create the initial solutionSet
            population = new Population_NSGA(m_Population_size);
            population = (Population_NSGA)Population.Clone();
            // Danh gia ham muc tieu 1 cho quan the Pt
            for (int i = 0; i < m_Population_size; i++)
            {
                Individual_NSGA individual = population.IndividualList[i];
                individual.Objective[0] = CalculateMSE(individual, inputData);
            }
            // Danh gia ham muc tieu 2 cho quan the Pt
            Diversity(population);
            // Vòng lặp tiến hóa
            while (evaluations < m_MaxEvaluations)
            {
                // Create the offSpring solutionSet
                offspringPopulation = new Population_NSGA(m_Population_size); // Tập cá thể con
                Individual_NSGA[] parents = new Individual_NSGA[2];

                for (int i = 0; i < (m_Population_size / 2); i++) // N/2
                {
                    if (evaluations < m_MaxEvaluations)
                    {
                        //Lấy 2 Cha parents
                        int sol1 = (int)(m_Random.NextDouble() * (m_Population_size - 1));
                        int sol2 = (int)(m_Random.NextDouble() * (m_Population_size - 1));
                        parents[0] = population.IndividualList[sol1];
                        parents[1] = population.IndividualList[sol2];
                        //
                        while (sol1 == sol2)
                        {
                            sol2       = (int)(m_Random.NextDouble() * (m_Population_size - 1));
                            parents[1] = population.IndividualList[sol2];
                        }

                        // Sử dụng SBX crossover tạo ra 2 thằng con
                        Individual_NSGA[] offSpring = SBXCrossover(parents);
                        //Dot bien
                        DoMutation(offSpring[0]);
                        DoMutation(offSpring[1]);

                        //Danh gia ham muc tieu 1 cho quan the moi Qt
                        offSpring[0].Objective[0] = CalculateMSE(offSpring[0], inputData);
                        offSpring[1].Objective[0] = CalculateMSE(offSpring[1], inputData);

                        // Đưa 2 con vào danh sách
                        offspringPopulation.Add(offSpring[0]);
                        offspringPopulation.Add(offSpring[1]);
                        evaluations += 2;
                    }
                }
                //Danh gia ham muc tieu 2 cho quan the moi Qt
                Diversity(offspringPopulation);
                // Create the solutionSet union of solutionSet and offSpring
                union = ((Population_NSGA)population).Union(offspringPopulation);

                // Ranking the union - Sxep ko troi - non-dominated sorting
                Ranking ranking = new Ranking(union);

                int             remain = m_Population_size;
                int             index  = 0;
                Population_NSGA front  = null;
                population.Clear();

                // Obtain the next front
                front = ranking.GetSubfront(index);
                // Đưa các Front vào quần thể mới, đến thằng cuối cùng
                while ((remain > 0) && (remain >= front.Size()))
                {
                    //Assign crowding distance to individuals
                    // Gắn khoang cach CrowdingDistance cho ca the
                    CrowdingDistanceAssignment(front);

                    //Add the individuals of this front
                    for (int k = 0; k < front.Size(); k++)
                    {
                        population.Add(front.Get(k));
                    }

                    //Decrement remain
                    remain = remain - front.Size();

                    //Obtain the next front
                    index++;
                    if (remain > 0)
                    {
                        front = ranking.GetSubfront(index);
                    }
                }

                // Remain is less than front(index).size, insert only the best one
                if (remain > 0)
                {  // front contains individuals to insert
                    CrowdingDistanceAssignment(front);
                    front.Sort(new CrowdingComparator());
                    for (int k = 0; k < remain; k++)
                    {
                        population.Add(front.Get(k));
                    }

                    remain = 0;
                }
            }
            // Return the first non-dominated front
            Ranking         rank   = new Ranking(population);
            Population_NSGA result = rank.GetSubfront(0);

            return(result);
        }
Esempio n. 5
0
        public void Diversity(Population_NSGA population)
        {
            double objetiveMaxn;
            double objetiveMinn;
            double distance;
            int    N = population.Size();

            if (N == 1)
            {
                population.IndividualList[0].Objective[1] = 1;
                return;
            }
            //sắp xếp quần thể theo hàm mục tiêu từ bé đến lớn
            population.Sort(new ObjectiveComparator(0));
            //nếu quần thể chỉ có 2 cá thể thì ta lấy cả 2 chứ ko cần tính f2 nữa
            //gán 1,2 để vẽ đồ thị.
            if (N == 2)
            {
                population.IndividualList[0].Objective[1] = 1;
                population.IndividualList[1].Objective[1] = 2;
                return;
            }
            //nếu quần thể có từ 3 cá thể trở lên
            objetiveMinn = population.IndividualList[0].Objective[0];
            objetiveMaxn = population.IndividualList[N - 1].Objective[0];

            population.IndividualList[0].Objective[1]     = 0;
            population.IndividualList[N - 1].Objective[1] = Double.MaxValue; //gán thế này cái cuối cùng sẽ luôn bị loại vì nhỏ hơn thì được ưu tiên
                                                                             //if (indexF2 == 2)//cicle
                                                                             //{
                                                                             //tìm bán kính lớn nhât
            double bkmax = 0;

            for (int i = 0; i < N - 1; i++)
            {
                double temp = (population.IndividualList[i + 1].Objective[0] - population.IndividualList[i].Objective[0]);
                if (bkmax < temp)
                {
                    bkmax = temp;
                }
            }
            double r = bkmax;

            for (int i = 1; i < N - 1; i++)
            {
                distance = 0;
                int dem = 0;
                for (int j = 0; j < N; j++)
                {
                    double dis = Math.Abs(population.IndividualList[i].Objective[0] - population.IndividualList[j].Objective[0]);
                    if (dis <= r)
                    {
                        distance += dis;
                        dem++;
                    }
                }
                if (dem <= 1)
                {
                    distance /= (dem - 1);//vì tính cả chính nó nên phải trừ đi 1
                }
                population.IndividualList[i].Objective[1] = distance;
            }

            //}
            //if (indexF2 == 0)//crowding
            //{
            //    for (int j = 1; j < N - 1; j++)
            //    {
            //        distance = population.IndividualList[j + 1].Objective[0] - population.IndividualList[j - 1].Objective[0];
            //        distance = distance / (objetiveMaxn - objetiveMinn);

            //        population.IndividualList[j].Objective[1] = distance;
            //    }
            //}
            //if (indexF2 == 1)//distance
            //{
            //    for (int i = 1; i < N - 1; i++)
            //    {
            //        distance = 0;
            //        for (int j = 0; j < N; j++)
            //        {
            //            distance += Math.Abs((population.IndividualList[i].Objective[0] - population.IndividualList[j].Objective[0]));
            //        }
            //        distance /= (N - 1);
            //        population.IndividualList[i].Objective[1] = distance;
            //    }
            //}
        }
Esempio n. 6
0
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="population">The <code>SolutionSet</code> to be ranked.</param>
        public Ranking(Population_NSGA population)
        {
            this.population = population;

            // dominateMe[i] contains the number of solutions dominating i
            int[] dominateMe = new int[this.population.Size()];

            // iDominate[k] contains the list of solutions dominated by k
            List <int>[] iDominate = new List <int> [this.population.Size()];

            // front[i] contains the list of individuals belonging to the front i
            List <int>[] front = new List <int> [this.population.Size() + 1];

            // flagDominate is an auxiliar encodings.variable
            int flagDominate;

            // Initialize the fronts
            for (int i = 0; i < front.Length; i++)
            {
                front[i] = new List <int>();
            }

            //-> Fast non dominated sorting algorithm
            // Contribution of Guillaume Jacquenot
            for (int p = 0; p < this.population.Size(); p++)
            {
                // Initialize the list of individuals that i dominate and the number
                // of individuals that dominate me
                iDominate[p]  = new List <int>();
                dominateMe[p] = 0;
            }
            for (int p = 0; p < (this.population.Size() - 1); p++)
            {
                // For all q individuals , calculate if p dominates q or vice versa
                for (int q = p + 1; q < this.population.Size(); q++)
                {
                    flagDominate = constraint.Compare(population.Get(p), population.Get(q));
                    if (flagDominate == 0)
                    {
                        flagDominate = dominance.Compare(population.Get(p), population.Get(q));
                    }
                    if (flagDominate == -1)
                    {
                        iDominate[p].Add(q);
                        dominateMe[q]++;
                    }
                    else if (flagDominate == 1)
                    {
                        iDominate[q].Add(p);
                        dominateMe[p]++;
                    }
                }
                // If nobody dominates p, p belongs to the first front
            }
            for (int p = 0; p < this.population.Size(); p++)
            {
                if (dominateMe[p] == 0)
                {
                    front[0].Add(p);
                    population.Get(p).Rank = 0;
                }
            }

            //Obtain the rest of fronts
            int k = 0;

            while (front[k].Count != 0)
            {
                k++;
                foreach (var it1 in front[k - 1])
                {
                    foreach (var it2 in iDominate[it1])
                    {
                        int index = it2;
                        dominateMe[index]--;
                        if (dominateMe[index] == 0)
                        {
                            front[k].Add(index);
                            this.population.Get(index).Rank = k;
                        }
                    }
                }
            }
            //<-

            ranking = new Population_NSGA[k];
            //0,1,2,....,i-1 are front, then i fronts
            for (int j = 0; j < k; j++)
            {
                ranking[j] = new Population_NSGA(front[j].Count);
                foreach (var it1 in front[j])
                {
                    ranking[j].Add(population.Get(it1));
                }
            }
        }