示例#1
0
        private static RecuitSimuleParameters GetParamRecuit(out bool interrupted)
        {
            QuadraticAssignmentSolution initialSol = new QuadraticAssignmentSolution(qap.N);
            double initialTemp         = -1d;
            double temperatureDecrease = -1d;
            int    maxSteps            = -1;

            Console.WriteLine("Veuillez entrez les paramètres du Recuit : ");

            Console.WriteLine("Température Initiale (Prenez un grand nombre > 0 de l'ordre des fitness) :");
            if (!GetCorrectDouble(out initialTemp, (val) => val > 0d))
            {
                interrupted = true;
                return(null);
            }

            Console.WriteLine("Baisse de Température ( Réel à ]0,1[ ) :");
            if (!GetCorrectDouble(out temperatureDecrease, (val) => val > 0d && val < 1d))
            {
                interrupted = true;
                return(null);
            }

            Console.WriteLine("Nombre d'étapes de l'exécution ( > 0) : ");
            if (!GetCorrectInt(out maxSteps, (val) => val > 0))
            {
                interrupted = true;
                return(null);
            }


            interrupted = false;
            return(new RecuitSimuleParameters(initialSol, initialTemp, temperatureDecrease, maxSteps));
        }
示例#2
0
        private static void TestMultiplySolution()
        {
            QuadraticAssignmentSolution p1 = new QuadraticAssignmentSolution(new int[] { 2, 1, 3 });
            QuadraticAssignmentSolution p2 = new QuadraticAssignmentSolution(new int[] { 1, 3, 2 });

            Console.WriteLine("A = " + string.Join(",", p1));
            Console.WriteLine("B = " + string.Join(",", p2));

            QuadraticAssignmentSolution p3 = p1 * p2;

            Console.WriteLine("A * B = " + string.Join(",", p3));
            if (p3.Equals(new int[] { 3, 1, 2 }))
            {
                Console.WriteLine("\tSUCCESS");
            }
            else
            {
                Console.WriteLine("\tFAILED");
            }

            p3 = p2 * p1;
            Console.WriteLine("B * A = " + string.Join(",", p3));
            if (p3.Equals(new int[] { 2, 3, 1 }))
            {
                Console.WriteLine("\tSUCCESS");
            }
            else
            {
                Console.WriteLine("\tFAILED");
            }

            Console.WriteLine("-----");
        }
示例#3
0
        private static TabouParameters GetParamTabou(out bool interrupted)
        {
            QuadraticAssignmentSolution initialSol = new QuadraticAssignmentSolution(qap.N);
            int sizeTabou = -1;
            int steps     = -1;

            Console.WriteLine("Veuillez entrez les paramètres du Tabou : ");


            Console.WriteLine("Taille de la liste Tabou (>= 0 et plus petit que nb voisins possibles) :");
            if (!GetCorrectInt(out sizeTabou, (val) => val >= 0 && val < qap.Inversions.Length))
            {
                interrupted = true;
                return(null);
            }

            Console.WriteLine("Nombre d'étapes de l'exécution ( > 0) : ");
            if (!GetCorrectInt(out steps, (val) => val > 0))
            {
                interrupted = true;
                return(null);
            }

            interrupted = false;
            return(new TabouParameters(initialSol, sizeTabou, steps));
        }
示例#4
0
        private static void TestTabou()
        {
            string problemFilepath  = path + "tai40a" + ".dat";
            QuadraticAssignment qap = new QuadraticAssignment(problemFilepath);

            //Seed it!
            //RandomSingleton.Instance.Seed = 0;
            //QuadraticAssignmentSolution initialSol = QuadraticAssignmentSolution.GetIdentity(qap.N);

            RandomSingleton.Instance.Seed = 6;
            QuadraticAssignmentSolution initialSol = new QuadraticAssignmentSolution(qap.N);


            MethodeTabou.MethodTabou tabou = new MethodeTabou.MethodTabou(qap);
            tabou.Verbose = true;

            QuadraticAssignmentSolution best = tabou.Run(
                initialSol,
                80,
                500);

            Console.WriteLine(tabou.Logs.FinalLog.ToString());

            Console.WriteLine(tabou.Logs.ToStringImprovements());

            //Console.WriteLine("Best : " + best.ToString());
            //Console.WriteLine("Fitness : " + best.Fitness);
        }
示例#5
0
        private static void TestSolutionsValidity()
        {
            //We pair the tests to their expected results.
            Dictionary <string, bool> tests = new Dictionary <string, bool>();

            tests.Add("8 1 6 2 11 10 3 5 9 7 12 4", true);
            tests.Add("8 1 6 2 11 10 9 7 12 4", false);
            tests.Add("8 -1 6 2 11 10 3 5 9 7 12 4", false);
            tests.Add("8 1 6 2 11 10 3 5 9 7 13 4", false);
            tests.Add("8 1 6 2 11 10 3 5 9 7 13 4 3 4", false);


            Console.WriteLine("*** TEST SOLUTION COHERENCE***");

            //We execute the tests
            foreach (string test in tests.Keys)
            {
                Console.WriteLine("*** TEST");
                Console.WriteLine(test);

                bool isValid = QuadraticAssignmentSolution.IsValid(test);
                Console.Write(isValid + " ");
                if (isValid == tests[test])
                {
                    Console.WriteLine("SUCCESSFUL");
                }
                else
                {
                    Console.WriteLine("FAILED");
                }
            }
        }
示例#6
0
 public TabouParameters(QuadraticAssignmentSolution initialSol,
                        int sizeTabou,
                        int nbSteps)
 {
     this.InitialSol = initialSol;
     this.SizeTabou  = sizeTabou;
     this.NbSteps    = nbSteps;
 }
示例#7
0
        private static void TestRecuitSimule()
        {
            string problemFilepath  = path + "tai12a" + ".dat";
            QuadraticAssignment qap = new QuadraticAssignment(problemFilepath);

            QuadraticAssignmentSolution initialSol = qap.Identity;
            QuadraticAssignmentSolution best;

            RecuitSimule recuit = new RecuitSimule(qap);

            best = recuit.Execute(initialSol, 70000, 0.85f, 200);

            Console.WriteLine(recuit.Logs.FinalLog.ToString());
            Console.WriteLine(recuit.Logs.ToStringImprovements());
        }
示例#8
0
        private static void TestQAPFileReading()
        {
            QuadraticAssignment qapf = new QuadraticAssignment(path + filename);

            Console.WriteLine(qapf.ToString());

            QuadraticAssignmentSolution sol = new QuadraticAssignmentSolution("8 1 6 2 11 10 3 5 9 7 12 4");

            Console.WriteLine("Solution : ");
            Console.WriteLine(qapf.ToString());


            Console.WriteLine("Score : ");
            Console.WriteLine(qapf.Evaluate(sol));
            Console.WriteLine(qapf.Evaluate(sol) * 2);
        }
示例#9
0
        private static void TestAlgorithmGenetic()
        {
            string problemFilepath  = path + "tai12a" + ".dat";
            QuadraticAssignment qap = new QuadraticAssignment(problemFilepath);

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

            GeneticAlgorithmQAP ga = new GeneticAlgorithmQAP(qap, 100, 50, 0.05d, 2, 20);

            ga.Verbose  = true;
            ga.WithLogs = true;
            QuadraticAssignmentSolution best = ga.Run();

            string pathWrite = Path.Combine(pathCSV, "tai12a", "ga.csv");

            ga.Logs.SaveLogsTo(pathWrite);

            Console.WriteLine("Best Solution : " + best.ToString());
            Console.WriteLine("Fitness : " + best.Fitness);
        }
示例#10
0
        private static void TestEquals()
        {
            Console.WriteLine("---- TEST EQUALS SOLUTIONS -----");
            int[] p1 = new int[] { 1, 4, 2, 3, 5 };
            int[] p2 = new int[] { 2, 3, 5, 4, 1 };

            QuadraticAssignmentSolution s1 = new QuadraticAssignmentSolution(p1);
            QuadraticAssignmentSolution s2 = new QuadraticAssignmentSolution(p2);


            Console.WriteLine("s1 = " + s1.ToString());
            Console.WriteLine("s2 = " + s2.ToString());
            Console.WriteLine();

            Console.WriteLine("s1.Equals(s1) == true");
            DisplaySuccess(s1.Equals(s1) == true);

            Console.WriteLine("s1.Equals(s2) == false");
            DisplaySuccess(s1.Equals(s2) == false);

            Console.WriteLine();

            Console.WriteLine("s1.Equals(p1) == true");
            DisplaySuccess(s1.Equals(p1) == true);

            Console.WriteLine("s1.Equals(p2) == false");
            DisplaySuccess(s1.Equals(p2) == false);


            Console.WriteLine();

            Console.WriteLine("s1.Equals(new int[] { 1, 4, 2, 3, 5 }) == true");
            DisplaySuccess(s1.Equals(new int[] { 1, 4, 2, 3, 5 }) == true);

            Console.WriteLine("s1.Equals(new int[] { 2, 3, 5, 4, 1 }) == false");
            DisplaySuccess(s1.Equals(new int[] { 2, 3, 5, 4, 1 }) == false);


            Console.WriteLine("------");
        }
示例#11
0
        private static void TestDataReading()
        {
            List <string> filenames = new List <string>();

            filenames.Add("tai12a");
            //filenames.Add("tai12b");
            filenames.Add("tai15a");
            filenames.Add("tai17a");
            filenames.Add("tai20a");
            filenames.Add("tai25a");

            foreach (string fname in filenames)
            {
                Console.WriteLine("QAP : " + fname);

                string problemFilepath  = path + fname + ".dat";
                string solutionFilepath = path + fname + ".sln";

                QuadraticAssignment         qap      = new QuadraticAssignment(problemFilepath);
                QuadraticAssignmentSolution solution = new QuadraticAssignmentSolution(solutionFilepath, false);

                int evaluation = qap.Evaluate(solution);

                Console.WriteLine("Evaluation      : " + evaluation);
                Console.WriteLine("Expected result : " + solution.Fitness);
                if (evaluation == solution.Fitness)
                {
                    Console.WriteLine("SUCCESSFUL");
                }
                else
                {
                    Console.WriteLine("FAILED");
                }


                Console.WriteLine("Click on any key to continue.");
                Console.ReadKey();
            }
        }
示例#12
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);
        }
示例#13
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;
                }
            }
        }
示例#14
0
 private static void ChangeInstance(string fname)
 {
     filename          = fname;
     qap               = new QuadraticAssignment(GetInstancePath(filename));
     bestKnownSolution = new QuadraticAssignmentSolution(GetSolutionPath(filename), true);
 }
示例#15
0
 public TabouParameters(int n)
 {
     InitialSol = new QuadraticAssignmentSolution(n);
 }