コード例 #1
0
        static void GeneticAlgorithm(Instance instance, Process process, Random rnd, Stopwatch clock, int sizePopulation, string choice)
        {
            PathGenetic incumbentSol    = new PathGenetic();
            PathGenetic currentBestPath = null;

            List <PathGenetic> OriginallyPopulated = new List <PathGenetic>();
            List <PathGenetic> ChildPoulation      = new List <PathGenetic>();

            List <int>[] listArray = Utility.BuildSLComplete(instance);

            //Generate the first population
            for (int i = 0; i < sizePopulation; i++)
            {
                OriginallyPopulated.Add(Utility.NearestNeightborGenetic(instance, rnd, true, listArray));
            }
            do
            {
                //Generate the child
                for (int i = 0; i < sizePopulation; i++)
                {
                    if (i % 2 != 0)
                    {
                        ChildPoulation.Add(Utility.GenerateChild(instance, rnd, OriginallyPopulated[i], OriginallyPopulated[i - 1], listArray));
                    }
                }

                OriginallyPopulated = Utility.NextPopulation(instance, sizePopulation, OriginallyPopulated, ChildPoulation);

                //currentBestPath contains the best path of the current population
                currentBestPath = Utility.BestSolution(OriginallyPopulated);

                if (currentBestPath.cost < incumbentSol.cost)
                {
                    incumbentSol = (PathGenetic)currentBestPath.Clone();
                    Utility.PrintGeneticSolution(instance, process, incumbentSol);
                }

                // We empty the list that contain the child
                ChildPoulation.RemoveRange(0, ChildPoulation.Count);
            } while (clock.ElapsedMilliseconds / 1000.0 < instance.TimeLimit);

            Console.WriteLine("Best distance found within the timelit is: " + incumbentSol.cost);
        }
コード例 #2
0
        static void Polishing(Cplex cplex, Instance instance, Process process, Random rnd, int sizePopulation, Stopwatch clock)
        {
            //Definisco l' oggetto che codifica la soluzione che rappresenta l' incumbent(miglio soluzione in assoluto fra tutte le generazioni)
            PathGenetic incumbentSol = new PathGenetic();

            //Miglior soluzione all' interno di una generazione
            PathGenetic currentBestPath = null;

            //Popolazione padre corrente
            List <PathGenetic> OriginallyPopulated = new List <PathGenetic>();

            //Popolazione figlia
            List <PathGenetic> ChildPoulation = new List <PathGenetic>();

            List <int>[] listArray = Utility.BuildSLComplete(instance);

            //Costruisco il modello
            INumVar[] x = Utility.BuildModel(cplex, instance, -1);

            //Installo la lazy
            cplex.Use(new TSPLazyConsCallback(cplex, x, instance, process, false));

            //Ripristino il numero di thread al numero di core logici
            cplex.SetParam(Cplex.Param.Threads, cplex.GetNumCores());

            //Setto un EpGap di 0.2(molto alto) in modo da produrre nel più breve tempo possibile una soluzione
            cplex.SetParam(Cplex.DoubleParam.EpGap, 0.1);

            for (int i = 0; i < sizePopulation; i++)
            {
                OriginallyPopulated.Add(Utility.NearestNeightborGeneticPolish(instance, rnd, listArray));
            }

            do
            {
                for (int i = 0; i < sizePopulation; i++)
                {
                    if ((i != 0) && (i % 2 != 0))
                    {
                        ChildPoulation.Add(Utility.GenerateChildPolish(cplex, instance, x, OriginallyPopulated[i], OriginallyPopulated[i - 1]));
                    }
                }

                OriginallyPopulated = Utility.NextPopulation(instance, sizePopulation, OriginallyPopulated, ChildPoulation);

                //Miglio soluzione della popolazione attualE
                currentBestPath = Utility.BestSolution(OriginallyPopulated);

                if (currentBestPath.cost < incumbentSol.cost)
                {
                    //Aggiorno la soluzione incumbent
                    incumbentSol = (PathGenetic)currentBestPath.Clone();

                    //Print the new incombent solution

                    StreamWriter file = new StreamWriter(instance.InputFile + ".dat", false);

                    for (int i = 0; i < instance.NNodes; i++)
                    {
                        for (int j = i + 1; j < instance.NNodes; j++)
                        {
                            int pos = Utility.xPos(i, j, instance.NNodes);

                            if (incumbentSol.path[pos] >= 0.5)
                            {
                                file.WriteLine(instance.Coord[i].X + " " + instance.Coord[i].Y + " " + (i + 1));
                                file.WriteLine(instance.Coord[j].X + " " + instance.Coord[j].Y + " " + (j + 1) + "\n");
                            }
                        }
                    }

                    file.Close();
                    Utility.PrintGNUPlot(process, instance.InputFile, 1, incumbentSol.cost, -1);
                }

                ChildPoulation.RemoveRange(0, ChildPoulation.Count);
            } while (clock.ElapsedMilliseconds / 1000.0 < instance.TimeLimit);

            Console.WriteLine("Best distance found within the timelit is: " + incumbentSol.cost);
        }