Esempio n. 1
0
        public void MFO_Mutation(Mutation_Parameters mutation_Para, ref Chromosome ind, int num_Genes, Tasks tsk, Random rnd)
        {
            switch (mutation_Para.Mutation_Name)
            {
            case "SLIGHT_MUTATE":
                ind.Rnvec = slight_Mutate(ind.Rnvec, num_Genes, mutation_Para.Sigma, rnd);
                break;

            case "SWAP_MUTATION":
                ind.Invec = swap_Mutation(ind.Invec, num_Genes, mutation_Para.Mutation_Rate, rnd);
                break;

            case "PRUFER_SWAP_MUTATION":
                ind.Invec = swap_Mutation(ind.Invec, num_Genes, mutation_Para.Mutation_Rate, rnd);
                break;

            case "EDGE_MUTATION":
                ind.Edges_Matrix = edge_Mutation(ind.Edges_Matrix, num_Genes, mutation_Para.Mutation_Rate, rnd);
                break;

            case "EDGE_CLUSTERED_TREE_MUTATION":
                #region  EDGE_CLUSTERED_TREE_MUTATION
                ind.Edges_Matrix = edge_Clustered_Tree_Mutation(ind.Edges_Matrix, num_Genes, mutation_Para.Mutation_Rate, tsk.Num_Cluster, tsk.Vertex_In_Cluster, rnd);
                #endregion
                break;
            }
            return;
        }
Esempio n. 2
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);
        }