Beispiel #1
0
        public double[,] edge_Clustered_Tree_Mutation(double[,] par, int num_Vertex, double mutation_Rate, int num_Cluster, int[][] vertex_In_Cluster, Random rnd)
        {
            Initialize_Chromosome init_Chrome = new Initialize_Chromosome();

            double[,] child = new double[num_Vertex, num_Vertex];
            for (int i = 0; i < num_Vertex; i++)
            {
                for (int j = 0; j < num_Vertex; j++)
                {
                    child[i, j] = par[i, j];
                }
            }

            for (int ii = 0; ii < num_Vertex; ii++)
            {
                int idx_Cluster = rnd.Next(num_Cluster);
                while (vertex_In_Cluster[idx_Cluster].Length < 3)
                {
                    idx_Cluster = rnd.Next(num_Cluster);
                }
                //01. Chuyển ma trận của cluster thành ma trận cây để áp dụng đột biến
                double[,] weight_Cluster_Matrix;
                double[,] spanning_Tree_of_Cluster;
                int num_Vertex_in_Cluster = vertex_In_Cluster[idx_Cluster].Length;
                weight_Cluster_Matrix    = init_Chrome.create_Weight_Matrix_for_Cluster(num_Vertex, num_Vertex_in_Cluster, par, vertex_In_Cluster[idx_Cluster]);
                spanning_Tree_of_Cluster = edge_Mutation(weight_Cluster_Matrix, num_Vertex_in_Cluster, mutation_Rate, rnd);

                //Chuyen ra cay khung cua do thi G
                int[] cluster = new int[num_Vertex_in_Cluster];
                for (int i = 0; i < num_Vertex_in_Cluster; i++)
                {
                    cluster[i] = vertex_In_Cluster[idx_Cluster][i];
                }
                for (int k = 0; k < num_Vertex_in_Cluster; k++)
                {
                    for (int j = 0; j < num_Vertex_in_Cluster; j++)
                    {
                        //child[cluster[k], cluster[j]] = 0;
                        child[cluster[k], cluster[j]] = spanning_Tree_of_Cluster[k, j];
                    }
                }
            }


            return(child);
        }
Beispiel #2
0
        /*********************************************************************************************************************************************
         * Tìm ma trận liên kết các cluster từ cây khung
         * + Hướng 1: Chỉ tìm liên kết không quan tâm tới trọng số
         * + Hướng 2: Tìm liên kết có lưu trọng số, và các cạnh nối giữa các cluster để thực hiện lai ghép về sau <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
         **********************************************************************************************************************************************/

        private double[,] primRST_Cluster_Crossover(double[,] par_1, double[,] par_2, int num_Vertex, int num_Cluster, int[][] vertex_In_Cluster, Random rnd)
        {
            double[,] G_cr = new double[num_Vertex, num_Vertex];
            double[,] T    = new double[num_Vertex, num_Vertex];
            Initialize_Chromosome init_Chrome = new Initialize_Chromosome();

            //Khoi tao
            for (int i = 0; i < num_Vertex; i++)
            {
                for (int j = i; j < num_Vertex; j++)
                {
                    T[i, j] = 0;
                    T[j, i] = 0;
                }
            }


            //Ap dung PrimRST crossover cho tung cluster
            double[,] weight_Cluster_Matrix_Par_1, weight_Cluster_Matrix_Par_2;
            double[,] spanning_Tree_of_Cluster;
            int num_Vertex_in_Cluster = 0;

            for (int i = 0; i < num_Cluster; i++)
            {
                num_Vertex_in_Cluster       = vertex_In_Cluster[i].Length;
                weight_Cluster_Matrix_Par_1 = init_Chrome.create_Weight_Matrix_for_Cluster(num_Vertex, num_Vertex_in_Cluster, par_1, vertex_In_Cluster[i]);
                weight_Cluster_Matrix_Par_2 = init_Chrome.create_Weight_Matrix_for_Cluster(num_Vertex, num_Vertex_in_Cluster, par_2, vertex_In_Cluster[i]);
                spanning_Tree_of_Cluster    = primRST_Crossover(weight_Cluster_Matrix_Par_1, weight_Cluster_Matrix_Par_2, num_Vertex_in_Cluster, rnd);
                //Chuyen ra cay khung cua do thi G
                for (int k = 0; k < num_Vertex_in_Cluster; k++)
                {
                    for (int j = 0; j < num_Vertex_in_Cluster; j++)
                    {
                        if (spanning_Tree_of_Cluster[k, j] > 0)
                        {
                            T[vertex_In_Cluster[i][k], vertex_In_Cluster[i][j]] = spanning_Tree_of_Cluster[k, j];
                        }
                    }
                }
            }

            //Tạo cây khung cho đại diện các nhóm
            //01. Tim cạnh liên kết các nhóm của cha mẹ => các cạnh của cha mẹ có thể liên kết tới các đỉnh khác nhau của cùng 1 nhóm =>
            weight_Cluster_Matrix_Par_1 = find_Spanning_Tree_Between_Clusters(par_1, num_Vertex, num_Cluster, vertex_In_Cluster);
            weight_Cluster_Matrix_Par_2 = find_Spanning_Tree_Between_Clusters(par_2, num_Vertex, num_Cluster, vertex_In_Cluster);

            //02. Tạo các thể con cho các cạnh nay
            spanning_Tree_of_Cluster = primRST_Crossover(weight_Cluster_Matrix_Par_1, weight_Cluster_Matrix_Par_2, num_Cluster, rnd);
            //Chuyen ra cay khung cua do thi G
            int[] idx_Cluster = new int[num_Cluster];
            for (int i = 0; i < num_Cluster; i++)
            {
                int vt = rnd.Next(vertex_In_Cluster[i].Length);
                idx_Cluster[i] = vertex_In_Cluster[i][vt];
            }
            for (int k = 0; k < num_Cluster; k++)
            {
                for (int j = 0; j < num_Cluster; j++)
                {
                    if (spanning_Tree_of_Cluster[k, j] > 0)
                    {
                        T[idx_Cluster[k], idx_Cluster[j]] = spanning_Tree_of_Cluster[k, j];
                    }
                }
            }
            return(T);
        }
Beispiel #3
0
        /*********************************************************************************************************************************************
         *  G. R. Raidl and B. A. Julstrom, “Edge sets: an effective evolutionary coding of spanning trees,” IEEE Transactions on evolutionary computation, vol. 7, no. 3, pp. 225–239, 2003.
         *  Lai ghép cho biểu diễn cây sử dụng thuậ toán PrimRST để tạo cây
         *
         ********************************************************************************************************************************************/
        private double[,] primRST_Crossover(double[,] par1, double[,] par2, int num_Vertex, Random rnd)
        {
            double[,] G_cr = new double[num_Vertex, num_Vertex];
            int[,] aaa     = new int[num_Vertex, num_Vertex];
            Initialize_Chromosome init_Chrome = new Initialize_Chromosome();

            //01. Tao do thi Gcr
            for (int i = 0; i < num_Vertex; i++)
            {
                for (int j = 0; j < num_Vertex; j++)
                {
                    G_cr[i, j] = par1[i, j] + par2[i, j];
                }
            }


            ReadWriteFile ioFile = new ReadWriteFile();

            //for (int i = 0; i < num_Vertex; i++)
            //{
            //    for (int j = 0; j < num_Vertex; j++)
            //    {
            //        aaa[i, j] = (int)par1[i, j];
            //    }
            //}
            //ioFile.draw_Plot_in_Matlab("par_1" + @".m", num_Vertex, aaa);
            //for (int i = 0; i < num_Vertex; i++)
            //{
            //    for (int j = 0; j < num_Vertex; j++)
            //    {
            //        aaa[i, j] = (int)par2[i, j];
            //    }
            //}
            //ioFile.draw_Plot_in_Matlab("par_2" + @".m", num_Vertex, aaa);

            //for (int i = 0; i < num_Vertex; i++)
            //{
            //    for (int j = 0; j < num_Vertex; j++)
            //    {
            //        aaa[i, j] = (int)par1[i, j] + (int)par2[i, j];
            //    }
            //}
            //ioFile.draw_Plot_in_Matlab("aaa" + @".m", num_Vertex, aaa);

            //02. Ap dung PrimRST de tao ca the con
            double[,] aaaaa = init_Chrome.primRST(num_Vertex, G_cr, rnd);

            //if (aaaaa == null)
            //{
            //    //ReadWriteFile ioFile = new ReadWriteFile();
            //    ioFile.writeMatrixToFile("G_cr.txt", G_cr, num_Vertex, false);
            //    Console.WriteLine("null child");

            //    for (int i = 0; i < num_Vertex; i++)
            //    {
            //        for (int j = 0; j < num_Vertex; j++)
            //        {
            //            aaa[i, j] = (int)par1[i, j];
            //        }
            //    }
            //    ioFile.draw_Plot_in_Matlab("par_1" + @".m", num_Vertex, aaa);
            //    for (int i = 0; i < num_Vertex; i++)
            //    {
            //        for (int j = 0; j < num_Vertex; j++)
            //        {
            //            aaa[i, j] = (int)par2[i, j];
            //        }
            //    }
            //    ioFile.draw_Plot_in_Matlab("par_2" + @".m", num_Vertex, aaa);

            //    for (int i = 0; i < num_Vertex; i++)
            //    {
            //        for (int j = 0; j < num_Vertex; j++)
            //        {
            //            aaa[i, j] = (int)par1[i, j] + (int)par2[i, j];
            //        }
            //    }
            //    ioFile.draw_Plot_in_Matlab("aaa" + @".m", num_Vertex, aaa);
            //    Console.ReadKey();
            //}

            //for (int i = 0; i < num_Vertex; i++)
            //{
            //    for (int j = 0; j < num_Vertex; j++)
            //    {
            //        aaa[i, j] = (int)aaaaa[i, j];
            //    }
            //}
            //ioFile.draw_Plot_in_Matlab("child" + @".m", num_Vertex, aaa);

            return(aaaaa);
        }
Beispiel #4
0
        public Data_MFEA main_GA_1(string init_Individual_Alg, TerminationCondition termination_Condition, int pop_Size, Tasks[] tsk, double lambda,
                                   Crossover_Parameters cros_Para, Mutation_Parameters mu_Para, string selection_pressure, string local_Search_Method, string population_Diversity_Method, Random rnd)
        {
            Initialize_Chromosome init_Chromosome = new Initialize_Chromosome();
            double best_Fitness_In_Generation     = double.MaxValue,        //Cá thể tốt nhất của mỗi TAS
                   diversity_of_Pop                = 0.0f;
            List <double> ev_Best_Fitness          = new List <double>();   //Lưu best fitness tốt nhất qua các thế hệ
            List <double> diversity_of_pop_in_Gers = new List <double>();   //Lưu độ đa dạng quần thể qua các thế hệ

            Chromosome best_Ind_data;
            Evaluate   evaluate = new Evaluate();

            Chromosome[] current_pop    = new Chromosome[pop_Size];
            Chromosome[] offspring_pop  = new Chromosome[pop_Size];
            Chromosome[] c_pop          = new Chromosome[2 * pop_Size];
            int          num_Genens     = tsk[0].Dims, //D_multitask
                         count_Evaluate = 0,           //Biến trung gian đếm số lần đánh giá khi gọi hàm evaluate
                         bst_Idx;

            best_Ind_data = new Chromosome(num_Genens, 1);

            for (int i = 0; i < pop_Size; i++)
            {
                current_pop[i]      = new Chromosome(num_Genens, 1);
                offspring_pop[i]    = new Chromosome(num_Genens, 1);
                c_pop[i]            = new Chromosome(num_Genens, 1);
                c_pop[i + pop_Size] = new Chromosome(num_Genens, 1);
            }

            //Initial Population
            for (int i = 0; i < pop_Size; i++)
            {
                init_Chromosome.initialize_Chromosome(init_Individual_Alg, num_Genens, tsk[0], rnd, ref current_pop[i]);
                current_pop[i].Skill_Factor            = -1;
                current_pop[i].Constraint_violation[0] = 0;//DO CHI CO 1 TASSK
            }

            number_of_evaluations = 0;
            //Evaluate population
            bst_Idx = -1;
            for (int i = 0; i < pop_Size; i++)
            {
                evaluate.evaluate(tsk, 1, lambda, ref current_pop[i], ref count_Evaluate);
                number_of_evaluations = number_of_evaluations + count_Evaluate;
                if (current_pop[i].Factorial_Cost[0] < best_Fitness_In_Generation)
                {
                    best_Fitness_In_Generation = current_pop[i].Factorial_Cost[0];//do chi co 1 task
                    bst_Idx = i;
                }
            }
            mfea_Class.copy_Individual(current_pop[bst_Idx], ref best_Ind_data, num_Genens, 1);
            ev_Best_Fitness.Add(best_Fitness_In_Generation);

            diversity_of_Pop = evaluate.compute_Population_Diversity(current_pop, pop_Size, num_Genens, population_Diversity_Method);
            diversity_of_pop_in_Gers.Add(diversity_of_Pop);

            int generation = 1;

            termination_Condition.Number_of_evaluations = number_of_evaluations;

            //while (generation < max_Generations)
            while (termination_Condition.checkTerminationCondition())
            {
                //i. Apply genetic operators on current-pop to generate an offspring-pop (C). Refer to Algorithm 2.
                int count_Child = 0;
                while (count_Child < pop_Size)
                {
                    int pos1 = rnd.Next(pop_Size);
                    int pos2 = rnd.Next(pop_Size);
                    while (pos1 == pos2)
                    {
                        pos2 = rnd.Next(pop_Size);
                    }
                    List <Chromosome> lst_Child = new List <Chromosome>();
                    mfea_Class.apply_Genetic_Operators(current_pop[pos1], current_pop[pos2], num_Genens, 1, cros_Para, mu_Para, ref lst_Child, tsk[0], rnd);
                    for (int i = 0; i < lst_Child.Count; i++)
                    {
                        mfea_Class.copy_Individual(lst_Child[i], ref offspring_pop[count_Child + i], num_Genens, 1);
                    }
                    count_Child = count_Child + lst_Child.Count;
                }

                //ii. Evaluate the individuals in offspring-pop for selected optimization tasks only (see Algorithm 3)
                bst_Idx = -1;
                for (int i = 0; i < pop_Size; i++)
                {
                    evaluate.evaluate(tsk, 1, lambda, ref offspring_pop[i], ref count_Evaluate);
                    number_of_evaluations = number_of_evaluations + count_Evaluate;
                    if (offspring_pop[i].Factorial_Cost[0] < best_Fitness_In_Generation)
                    {
                        best_Fitness_In_Generation = offspring_pop[i].Factorial_Cost[0];//do chi co 1 task
                        bst_Idx = i;
                    }
                    termination_Condition.Number_of_evaluations = number_of_evaluations;
                    //Nếu đủ điều kiện thì dừng
                    if (!termination_Condition.checkTerminationCondition())
                    {
                        break;
                    }
                }
                if (bst_Idx != -1)
                {
                    mfea_Class.copy_Individual(offspring_pop[bst_Idx], ref best_Ind_data, num_Genens, 1);
                }

                ev_Best_Fitness.Add(best_Fitness_In_Generation);

                //iii. Concatenate offspring-pop and current-pop to form an intermediate-pop (P ∪ C).
                for (int i = 0; i < pop_Size; i++)
                {
                    mfea_Class.copy_Individual(current_pop[i], ref c_pop[i], num_Genens, 1);
                    mfea_Class.copy_Individual(offspring_pop[i], ref c_pop[i + pop_Size], num_Genens, 1);
                }

                //v. Select the fittest individuals from intermediate-pop to form the next current-pop (P).
                if (string.Equals(selection_pressure, "elitist"))
                {
                    Array.Sort(c_pop, new SortAscFactorialCostIdx());
                    for (int j = 0; j < pop_Size; j++)
                    {
                        mfea_Class.copy_Individual(c_pop[j], ref current_pop[j], num_Genens, 1);
                    }
                }
                else
                {
                    if (string.Equals(selection_pressure, "roulette wheel"))
                    {
                    }
                }

                //Local search
                if (!string.IsNullOrEmpty(local_Search_Method) && (local_Search_Method.Trim() != "NONE"))
                {
                    for (int i = 0; i < pop_Size; i++)
                    {
                        local_Search.local_search_alg(tsk[0], local_Search_Method, num_Genens, rnd, ref offspring_pop[i], ref count_Evaluate);
                        number_of_evaluations = number_of_evaluations + count_Evaluate;
                        termination_Condition.Number_of_evaluations = number_of_evaluations;
                        //Nếu đủ điều kiện thì dừng
                        if (!termination_Condition.checkTerminationCondition())
                        {
                            break;
                        }
                    }
                }

                diversity_of_Pop = evaluate.compute_Population_Diversity(current_pop, pop_Size, num_Genens, population_Diversity_Method);
                diversity_of_pop_in_Gers.Add(diversity_of_Pop);

                generation++;
                frmCmdOut.ClearFromPosToEndOfCurrentLine(50);
                Console.Write(String.Format("{0, 7} |   {1, 13} |", generation, ""));
                termination_Condition.Number_of_evaluations = number_of_evaluations;
            } //while

            Data_MFEA data_MFEA = new Data_MFEA(num_Genens, 1);

            mfea_Class.copy_Individual(best_Ind_data, ref data_MFEA.Best_Ind_Data[0], num_Genens, 1);

            foreach (double items in ev_Best_Fitness)
            {
                double[] tmp = new double[1];
                tmp[0] = items;
                data_MFEA.Ev_Best_Fitness.Add(tmp);
            }
            foreach (double items in diversity_of_pop_in_Gers)
            {
                data_MFEA.Diversity_of_Pop.Add(items);
            }
            return(data_MFEA);
        }