Exemple #1
0
        private static void TestRecuitSimuleGA()
        {
            string problemFilepath  = path + "tai12a" + ".dat";
            QuadraticAssignment qap = new QuadraticAssignment(problemFilepath);

            //Seed it!
            RandomSingleton.Instance.Seed = 0;

            QuadraticAssignmentSolution initialSol = qap.Identity;

            RecuitSimule recuit = new RecuitSimule(qap);

            GeneticAlgorithmRecuit ga = new GeneticAlgorithmRecuit(recuit, 40, 15, 0.05d, 1, 3);

            ga.Verbose  = true;
            ga.WithLogs = true;
            RecuitSimuleParameters      bestParam = ga.Run();
            QuadraticAssignmentSolution best      = recuit.Execute(bestParam);

            Console.WriteLine("Best Params : " + bestParam.ToString());
            Console.WriteLine("\nBest Solution : " + best.ToString());
            Console.WriteLine("Fitness : " + best.Fitness);
        }
Exemple #2
0
        private static bool RunRecuit(bool verbose = true, RecuitSimuleParameters param = null)
        {
            algo = Algo.Recuit;
            bool interrupted = false;

            if (verbose)
            {
                param = GetParamRecuit(out interrupted);
                if (interrupted)
                {
                    return(false);
                }


                //param = new RecuitSimuleParameters(qap.N);
                Console.WriteLine("Tout les paramètres sont entrées, commencer l'exécution ? ( o/n, y/n )");
                Console.WriteLine(param.ToString());

                string str = "";
                if (!GetCorrectString(out str, (s) => IsValidation(s)))
                {
                    return(false);
                }

                if (!IsYes(str))
                {
                    return(false);
                }
            }


            //Lancer l'exécution
            RecuitSimule recuit = new RecuitSimule(qap);

            recuit.Verbose = true;

            bestFitness = recuit.Execute(param).Fitness;

            Console.WriteLine("Paramètres :");
            Console.WriteLine(param.ToString());

            Console.WriteLine("Résultats :");
            Console.WriteLine(recuit.Logs.FinalLog.ToString());

            paramString  = param.ToString();
            resultString = recuit.Logs.FinalLog.ToString();

            //Save Results
            SaveResults();
            Console.WriteLine("Résultats sauvegardées dans " + GetResultPath() + "!");

            //Save Logs
            recuit.Logs.SaveLogsTo(GetCSVPath());
            Console.WriteLine("Logs sauvegardées dans " + GetCSVPath() + "!");

            if (verbose)
            {
                Console.WriteLine("\nAppuyez sur une touche pour revenir au menu.");
                Console.ReadKey();
                Console.WriteLine();
            }


            return(true);
        }
Exemple #3
0
        static void Main()
        {
            seed = new Random().Next(0, 1000); //random default seed
            RandomSingleton.Instance.Seed = seed;

            string[] line;

            qap = new QuadraticAssignment(GetInstancePath(filename));
            bestKnownSolution = new QuadraticAssignmentSolution(GetSolutionPath(filename), true);

            List <string> allNames    = new List <string>();
            bool          interrupted = false;
            string        allText;

            while (state != State.Quit)
            {
                Console.WriteLine("------------");
                Console.WriteLine(WelcomeMessage());
                Console.WriteLine(GetInstanceInfo());
                Console.WriteLine("------------");

                line = GetLine();

                RandomSingleton.Instance.ResetCurrentAlgoRandom();

                switch (line[0].ToLower())
                {
                case "q":
                case "quit":
                    state = State.Quit;
                    break;

                case "s":
                case "seed":
                    Console.WriteLine("Veuillez entrer un nouveau seed > 0:");

                    int newSeed = -1;
                    if (!GetCorrectInt(out newSeed, val => val > 0))
                    {
                        break;
                    }

                    seed = newSeed;
                    RandomSingleton.Instance.Seed = seed;
                    Console.WriteLine("Le nouveau seed est : " + seed);
                    break;

                case "t":
                case "tai":

                    bool found = false;
                    while (!found)
                    {
                        Console.WriteLine("Tapez le nom de l'instance de taillard à charger (sans son .dat):");
                        line = GetLine();

                        //early exit or quit
                        if (TryExitOrQuit(line[0]))
                        {
                            break;
                        }

                        found = FindTaillardInstance(line[0]);

                        if (!found)
                        {
                            Console.WriteLine("Fichier non trouvé ! Réessayer ou quitter (x or exit):");
                        }
                        else
                        {
                            //Update QAP
                            filename          = line[0];
                            qap               = new QuadraticAssignment(GetInstancePath(filename));
                            bestKnownSolution = new QuadraticAssignmentSolution(GetSolutionPath(filename), true);
                        }
                    }

                    break;

                case "r":
                case "rec":
                case "recuit":
                    RunRecuit();
                    break;

                case "tab":
                case "tabou":
                    RunTabou();
                    break;

                case "ga":
                case "genetic":
                    algo = Algo.GA;
                    GeneticAlgorithmQAP ga = new GeneticAlgorithmQAP(qap);

                    Console.WriteLine("Vous avez sélectionné l'Algorithme Génétique !");
                    RunGeneticAlgorithm(ga);
                    break;

                case "recga":
                case "recuitga":
                case "garecuit":
                case "garec":
                    algo = Algo.RecuitGA;
                    RecuitSimule recuit = new RecuitSimule(qap);
                    recuit.Verbose = false;
                    GeneticAlgorithmRecuit garec = new GeneticAlgorithmRecuit(recuit);

                    Console.WriteLine("Vous avez sélectionné l'Algorithme Génétique pour RECUIT SIMULE !");

                    RunGeneticAlgorithm(garec);
                    break;

                case "alltab":
                case "allt":
                    allNames    = GetAllInstances();
                    interrupted = false;

                    TabouParameters paramTabou = GetParamTabou(out interrupted);
                    allText = paramTabou.ToString();

                    if (interrupted)
                    {
                        break;
                    }

                    foreach (string instanceName in allNames)
                    {
                        ChangeInstance(instanceName);

                        paramTabou.InitialSol = new QuadraticAssignmentSolution(qap.N);
                        RunTabou(false, paramTabou);

                        allText += "\n\n" + instanceName + ":\n";
                        allText += "Resultats:\n" + resultString + ComparisonWithBest();
                        allText += "\n----";
                    }

                    allText = allText.Replace("\r\n", "\n").Replace("\r", "\n").Replace("\n", "\r\n");
                    string fullNameAllTabou = Path.Combine(resultPath, "all_" + algo.GetString() + "_s" + seed + ".txt");
                    System.IO.File.WriteAllText(fullNameAllTabou, allText);

                    break;

                case "allrec":
                case "allr":
                    allNames    = GetAllInstances();
                    interrupted = false;

                    RecuitSimuleParameters paramRec = GetParamRecuit(out interrupted);
                    allText = paramRec.ToString();

                    if (interrupted)
                    {
                        break;
                    }

                    foreach (string instanceName in allNames)
                    {
                        ChangeInstance(instanceName);

                        paramRec.InitialSol = new QuadraticAssignmentSolution(qap.N);
                        RunRecuit(false, paramRec);

                        allText += "\n\n" + instanceName + ":\n";
                        allText += "Resultats:\n" + resultString + ComparisonWithBest();
                        allText += "\n----";
                    }

                    allText = allText.Replace("\r\n", "\n").Replace("\r", "\n").Replace("\n", "\r\n");
                    string fullNameAllRec = Path.Combine(resultPath, "all_" + algo.GetString() + "_s" + seed + ".txt");
                    System.IO.File.WriteAllText(fullNameAllRec, allText);

                    break;

                case "allga":
                case "allg":
                    allNames    = GetAllInstances();
                    interrupted = false;

                    algo = Algo.GA;
                    GeneticAlgorithmParameters paramGA = GetParamGA(out interrupted);
                    allText = paramGA.ToString();

                    if (interrupted)
                    {
                        break;
                    }

                    foreach (string instanceName in allNames)
                    {
                        ChangeInstance(instanceName);
                        GeneticAlgorithmQAP geneticAlgo = new GeneticAlgorithmQAP(qap);
                        //paramGA. = new QuadraticAssignmentSolution(qap.N);
                        RunGeneticAlgorithm <QuadraticAssignmentSolution>(geneticAlgo, false, paramGA);

                        allText += "\n\n" + instanceName + ":\n";
                        allText += "Resultats:\n" + resultString + ComparisonWithBest();
                        allText += "\n----";
                    }

                    allText = allText.Replace("\r\n", "\n").Replace("\r", "\n").Replace("\n", "\r\n");
                    string fullNameAllGA = Path.Combine(resultPath, "all_" + algo.GetString() + "_s" + seed + ".txt");
                    System.IO.File.WriteAllText(fullNameAllGA, allText);

                    break;

                case "autoga":

                    string instanceCurrent = "tai12a";
                    ChangeInstance(instanceCurrent);

                    algo = Algo.GA;
                    List <GeneticAlgorithmParameters> paramsGa = new List <GeneticAlgorithmParameters>();

                    paramsGa.Add(new GeneticAlgorithmParameters(100, 25, 0.005, 0, 0));
                    paramsGa.Add(new GeneticAlgorithmParameters(300, 25, 0.005, 0, 0));
                    paramsGa.Add(new GeneticAlgorithmParameters(100, 50, 0.005, 0, 0));
                    paramsGa.Add(new GeneticAlgorithmParameters(100, 25, 0.1, 0, 0));
                    paramsGa.Add(new GeneticAlgorithmParameters(100, 25, 0.005, 5, 0));
                    paramsGa.Add(new GeneticAlgorithmParameters(100, 25, 0.005, 20, 0));
                    paramsGa.Add(new GeneticAlgorithmParameters(100, 25, 0.005, 0, 20));
                    paramsGa.Add(new GeneticAlgorithmParameters(100, 25, 0.005, 0, 50));


                    paramsGa.Add(new GeneticAlgorithmParameters(100, 25, 0.005, 2, 20));

                    GeneticAlgorithmQAP geneticAlgoAll = new GeneticAlgorithmQAP(qap);
                    allText = instanceCurrent + "\n";


                    foreach (GeneticAlgorithmParameters parGA in paramsGa)
                    {
                        RunGeneticAlgorithm <QuadraticAssignmentSolution>(geneticAlgoAll, false, parGA);

                        allText += parGA.ToString();
                        allText += "\nResultats:\n" + resultString + ComparisonWithBest();
                        allText += "\n----";
                    }

                    allText = allText.Replace("\r\n", "\n").Replace("\r", "\n").Replace("\n", "\r\n");
                    string fulenamega = Path.Combine(resultPath, "all_" + algo.GetString() + "_s" + seed + ".txt");
                    System.IO.File.WriteAllText(fulenamega, allText);

                    break;

                default:
                    Console.WriteLine("\nCommande invalide!");
                    break;
                }
            }
        }