Exemple #1
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);
        }
Exemple #2
0
        private static void TestQAPGeneration()
        {
            int[,] weights = new int[, ]
            {
                { 0, 2, 3 },
                { 2, 0, 1 },
                { 3, 1, 0 },
            };

            int[,] distances = new int[, ]
            {
                { 0, 7, 5 },
                { 7, 0, 9 },
                { 5, 9, 0 },
            };



            QuadraticAssignment qap = new QuadraticAssignment(3, weights, distances);


            List <int[]> solutions = new List <int[]>();

            solutions.Add(new int[] { 1, 2, 3 });
            solutions.Add(new int[] { 3, 2, 1 });
            solutions.Add(new int[] { 1, 3, 2 });

            foreach (int[] solution in solutions)
            {
                Console.WriteLine("Solution: [{0}]", string.Join(", ", solution));
                Console.WriteLine("Value: " + qap.Evaluate(solution));
            }
        }
 public GeneticAlgorithmQAP(QuadraticAssignment problem,
                            int populationSize,
                            int maxSteps,
                            double mutationChance,
                            int elitism  = 0,
                            int newBlood = 0) :
     base(populationSize, maxSteps, mutationChance, elitism, newBlood) //Super constructor
 {
     this.problem = problem;
     this.n       = problem.N;
 }
Exemple #4
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());
        }
Exemple #5
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);
        }
Exemple #6
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);
        }
Exemple #7
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();
            }
        }
Exemple #8
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);
        }
 public GeneticAlgorithmQAP(QuadraticAssignment problem)
 {
     this.problem = problem;
     this.n       = problem.N;
 }
Exemple #10
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;
                }
            }
        }
Exemple #11
0
 private static void ChangeInstance(string fname)
 {
     filename          = fname;
     qap               = new QuadraticAssignment(GetInstancePath(filename));
     bestKnownSolution = new QuadraticAssignmentSolution(GetSolutionPath(filename), true);
 }