Пример #1
0
        private static IEnumerable <IndividualBase> SPEA2Test(ProblemBase mainProblem)
        {
            MultiObjectiveGeneticAlgorithm SPEA2 = new SPEA2(
                mainProblem,
                200,
                200,
                400,
                10);

            return(SPEA2.Execute());
        }
        /// <summary>
        /// multi objective SPEA-2 for Emilie
        /// </summary>
        /// <param name="args"></param>
        static void MultiObjective_Main(string[] args)
        {
            int nVar = 5;                            //number of decision variables
            int mObj = 3;                            //number of objectives

            double[] lb = new double[nVar];          //lower bound values for each variable
            double[] ub = new double[nVar];          //upper bound values for each variable
            for (int i = 0; i < nVar; i++)           //here I'm assigning lower bound 0 and upper bound 1 to every variable
            {
                lb[i] = 0;
                ub[i] = 10;
            }
            bool[] intX = new bool[nVar];
            //intX[0] = true;
            //intX[1] = true;
            //intX[2] = true;
            //intX[3] = true;
            //intX[4] = true;
            //intX[5] = false;
            //intX[6] = false;
            //intX[7] = false;
            //intX[8] = false;
            //intX[9] = false;
            //intX[10] = false;
            //intX[11] = false;

            Func <double[], double[]> testfunc = testfnc;                                            //evaluation function
            //int randomSeed = ((int)DateTime.Now.Ticks & 0x0000FFFF);                               // random seed for random number generator
            int randomSeed = 1;

            //SPEA2
            List <double[]> x0pop = new List <double[]>();
            //x0pop.Add(new double[12] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 });        //initial solution 1
            //x0pop.Add(new double[12] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 });        //initial solution 2
            //x0pop.Add(new double[5] { 0, 0, 0, 0, 0 });        //initial solution 1
            //x0pop.Add(new double[5] { 1, 1, 1, 1, 1 });        //initial solution 2

            SPEA2 spea2 = new SPEA2(mObj, nVar, lb, ub, testfunc, randomSeed, x0pop, intX);

            //change solver parameters
            spea2.ga_genmax     = 3000;                   // max amount of generations (which are the solver iterations)
            spea2.ga_nPop       = 20;                     //population size: number of solution candidates per iteration
            spea2.ga_PCrossover = 0.8;
            spea2.ga_PMutation  = 0.2;


            //initialize solver
            Console.WriteLine("Initializing Algorithm:... {0}", spea2.GetSolverName());
            Console.WriteLine("On Problem:... {0}", "MO.MOM_DTLZ4");
            spea2.initialize();
            Console.ReadKey();
            //foreach (double[] current_best_objvalues in spea2.objvalsBestPop_MO)
            //{
            //    Console.WriteLine("{0}, {1}, {2}", current_best_objvalues[0], current_best_objvalues[1], current_best_objvalues[2]);
            //}

            ////show decision variables for each solution
            //int j = 0;
            //foreach (double[] current_best_x in spea2.xPopulationArchive)
            //{
            //    Console.WriteLine(" ");
            //    Console.Write("individual {0}, x:  ", j);
            //    for (int n = 0; n < spea2.nVar; n++)
            //    {
            //        Console.Write("{0}, ", current_best_x[n]);
            //    }
            //    j++;
            //}



            //for each generation
            for (int t = 0; t < 100; t++)
            {
                //solve one generation
                Console.WriteLine("***************************");
                Console.WriteLine("Generation: {0}", t);
                spea2.Solve(1);

                //looping through pareto front of the current generation
                //show three objective function values for each solution
                foreach (double[] current_best_objvalues in spea2.objvalsBestPop_MO)
                {
                    Console.WriteLine("{0}, {1}, {2}", current_best_objvalues[0], current_best_objvalues[1], current_best_objvalues[2]);
                }

                ////show decision variables for each solution
                //int j = 0;
                //foreach (double[] current_best_x in spea2.xPopulationArchive)
                //{
                //    Console.WriteLine(" ");
                //    Console.Write("individual {0}, x:  ", j);
                //    for (int n = 0; n < spea2.nVar; n++)
                //    {
                //        Console.Write("{0}, ", current_best_x[n]);
                //    }
                //    j++;
                //}

                Console.WriteLine(" ");
                Console.ReadKey();
            }
            Console.WriteLine(" ");
            Console.ReadKey();
        }