Exemplo n.º 1
0
        private static void RunAlgorithmsWithLocalSearch(IGraph graph, ISolver solver, string coordinatesPath, string resultsPath)
        {
            var edgeFinder     = new EdgeFinder();
            var graspEdgeFiner = new GraspEdgeFinder(3);

            var localSearchSolver = new TspLocalSearchSolver(graph);

            var localSearchALgorithm = new LocalSearchAlgorithm(Steps, edgeFinder);

            var generatedPaths = solver.Solve(new NearestNeighborAlgorithm(Steps, edgeFinder));

            localSearchSolver.Solve(localSearchALgorithm, generatedPaths);
            LocalSearchResultPrinter(localSearchSolver, "NN_LS", coordinatesPath, resultsPath).Print("NN with local search opt");

            generatedPaths = solver.Solve(new GreedyCycleAlgorithm(Steps, edgeFinder));
            localSearchSolver.Solve(localSearchALgorithm, generatedPaths);
            LocalSearchResultPrinter(localSearchSolver, "GC_LS", coordinatesPath, resultsPath).Print("GC with local search opt");

            generatedPaths = solver.Solve(new NearestNeighborAlgorithm(Steps, graspEdgeFiner));
            localSearchSolver.Solve(localSearchALgorithm, generatedPaths);
            LocalSearchResultPrinter(localSearchSolver, "NNG_LS", coordinatesPath, resultsPath).Print("NN Grasp with local search opt");

            generatedPaths = solver.Solve(new GreedyCycleAlgorithm(Steps, graspEdgeFiner));
            localSearchSolver.Solve(localSearchALgorithm, generatedPaths);
            LocalSearchResultPrinter(localSearchSolver, "GCG_LS", coordinatesPath, resultsPath).Print("GC GRASP with local search opt");

            generatedPaths = solver.Solve(new RandomPathAlgorithm(Steps, edgeFinder));
            localSearchSolver.Solve(localSearchALgorithm, generatedPaths);
            LocalSearchResultPrinter(localSearchSolver, "Random_LS", coordinatesPath, resultsPath).Print("RANDOM with local search opt");
        }
Exemplo n.º 2
0
        private static void RunRandomLSPathsStatistics(IGraph graph, TspSolver solver, string coordinatesPath)
        {
            var localSearchSolver = new PathSimilaritySolver(graph, new InitializationSolver(solver,
                                                                                             new RandomPathAlgorithm(Steps, new EdgeFinder())),
                                                             new ISimilarityCalculationStrategy[] { new EdgeSimillarityStrategy(), new NodeSimilarityStrategy() });
            var localSearchAlgorithm = new LocalSearchAlgorithm(Steps, new EdgeFinder());

            localSearchSolver.Solve(localSearchAlgorithm);
        }
Exemplo n.º 3
0
        private static void RunMSLSAlgorithms(IGraph graph, ISolver solver, string coordinatesPath, string resultsPath)
        {
            var localSearchSolver    = new TspMultipleStartLocalSearchSolver(graph, solver, new NearestNeighborAlgorithm(Steps, new GraspEdgeFinder(3)));
            var localSearchAlgorithm = new LocalSearchAlgorithm(Steps, new EdgeFinder());

            SolveAndPrint(localSearchSolver, localSearchAlgorithm,
                          "NN Grasp with local search (MSLS)", LocalSearchResultPrinter(localSearchSolver, "NNG_MSLS", coordinatesPath, resultsPath));

            localSearchSolver    = new TspMultipleStartLocalSearchSolver(graph, solver, new GreedyCycleAlgorithm(Steps, new GraspEdgeFinder(3)));
            localSearchAlgorithm = new LocalSearchAlgorithm(Steps, new EdgeFinder());
            SolveAndPrint(localSearchSolver, localSearchAlgorithm,
                          "GC Grasp with local search (MSLS)", LocalSearchResultPrinter(localSearchSolver, "GCG_MSLS", coordinatesPath, resultsPath));

            localSearchSolver    = new TspMultipleStartLocalSearchSolver(graph, solver, new RandomPathAlgorithm(Steps, new EdgeFinder()));
            localSearchAlgorithm = new LocalSearchAlgorithm(Steps, new EdgeFinder());
            SolveAndPrint(localSearchSolver, localSearchAlgorithm,
                          "Random with local search (MSLS)", LocalSearchResultPrinter(localSearchSolver, "Random_MSLS", coordinatesPath, resultsPath));
        }
Exemplo n.º 4
0
        static int Main(string[] args)
        {
            Algorithm s;
            Solution  ss;
            Graph     graph;
            int       timeout = -1;;
            string    algorithm;
            string    file;

            if (args.Length == 0)
            {
                Console.WriteLine("graph-coloring FILE ALGORITHM [TIMEOUT [OPTIONS]]");
                Console.WriteLine("Parameters:");
                Console.WriteLine("\tFILE - filename of a DIMACS graph file to process");
                Console.WriteLine("\t       specify a number for a random graph");
                Console.WriteLine("\t       with that amount of nodes");
                Console.WriteLine("\tALGORITHM - algorithm to use during the search");
                Console.WriteLine("\tTIMEOUT - milliseconds after which the program will finish");
                Console.WriteLine("\t          processing some algorithms");
                Console.WriteLine("\tOPTIONS - algorithm-specific options, like start ");
                Console.WriteLine("\t          temperature for simulated annealing (see below)");
                Console.WriteLine("");
                Console.WriteLine("Currently supported algorithms:");
                Console.WriteLine("\tbranch-bound");
                Console.WriteLine("\tgenetic-onepoint");
                Console.WriteLine("\t\tOptions:");
                Console.WriteLine("\t\t\tamount of start solutions (default 100)");
                Console.WriteLine("\t\t\tmutation probability (default 0.05)");
                Console.WriteLine("\tgenetic-twopoint");
                Console.WriteLine("\t\tOptions:");
                Console.WriteLine("\t\t\tamount of start solutions (default 100)");
                Console.WriteLine("\t\t\tmutation probability (default 0.05)");
                Console.WriteLine("\tlocal-search");
                Console.WriteLine("\tsimulated-annealing");
                Console.WriteLine("\t\tOptions:");
                Console.WriteLine("\t\t\tstart temperature to use (default 30)");
                Console.WriteLine("\t\t\tannealing factor to use (default 1.0)");
                Console.WriteLine("\ttaboo-search");
                Console.WriteLine("\t\tOptions:");
                Console.WriteLine("\t\t\ttaboo list length to use (default amount of nodes in graph)");
                return(0);
            }
            else if (args.Length == 1)
            {
                Console.WriteLine("You must at least specify an algorithm to use");
                return(1);
            }

            if (args.Length >= 3)
            {
                try
                {
                    timeout = int.Parse(args[2]);
                    if (timeout <= 0)
                    {
                        Console.WriteLine("You cannot disable timeouts.");
                        return(1);
                    }
                }
                catch (FormatException)
                {
                    Console.WriteLine("timeout must be set with a numeric value.");
                    return(1);
                }
            }
            file = args[0];
            try
            {
                graph = RandomGraphGenerator.Generate(int.Parse(file));
            }
            catch (FormatException)
            {
                if (!File.Exists(file))
                {
                    Console.WriteLine("This file doesn't exist.");
                    return(1);
                }
                graph = DIMACSParser.Read(file);
            }
            Console.WriteLine("Graph with " + graph.NodeCount + " nodes and " + graph.EdgeCount + " edges loaded successfully.");

            algorithm = args[1];

            switch (algorithm)
            {
            case "local-search":
                s = new LocalSearchAlgorithm(graph);
                break;

            case "simulated-annealing":
                s = new SimulatedAnnealingAlgorithm(graph);
                break;

            case "taboo-search":
                s = new TabooSearchAlgorithm(graph);
                break;

            case "genetic-onepoint":
                s = new GeneticAlgorithm(graph);
                ((GeneticAlgorithm)s).SetCrossoverStrategy(1);
                break;

            case "genetic-twopoint":
                s = new GeneticAlgorithm(graph);
                ((GeneticAlgorithm)s).SetCrossoverStrategy(2);
                break;

            case "branch-bound":
                s = new BranchBoundAlgorithm(graph);
                break;

            default:
                Console.WriteLine("no algorithm with name " + algorithm + " found");
                return(1);
            }

            Console.WriteLine("Running " + s.GetName() + " on graph");
            if (timeout >= 0)
            {
                s.SetTimeout(timeout);
            }

            timeout = s.GetTimeout();
            if (timeout == -1)
            {
                Console.WriteLine("No timeout applied.");
            }
            else
            {
                Console.WriteLine("Timeout set to " + timeout + " milliseconds.");
            }

            try
            {
                s.SetParameters(args.Skip(3).ToArray());
            }
            catch (ArgumentException ex)
            {
                Console.WriteLine("Error: " + ex.Message);
                return(1);
            }

            ss = s.Run();
            Console.WriteLine("Coloring through " + s.GetName() + " finished successfully.");
            Console.WriteLine("Finished with " + ss.ColorCount + " colors needed");
            Console.WriteLine("Algorithm took " + s.Duration + " to run.");
            if (ss.IsValid() == true)
            {
                Console.WriteLine("Algorithm result is a feasable solution.");
            }
            else
            {
                Console.WriteLine("The algorithm result isn't a feasable solution and therefore");
                Console.WriteLine("still requires tweaking.");
            }
            return(0);
        }
Exemplo n.º 5
0
        public static void Main(string[] args)
        {
            var buildConfig = BuildConfiguration();
            var config      = buildConfig.GetSection(nameof(AppConfiguration));

            var dataPath         = config["data"];
            var resultPath       = config["results"];
            var graphEdges       = System.IO.Path.Combine(dataPath, config["graphEdges"]);
            var graphCoordinates = System.IO.Path.Combine(dataPath, config["graphCoordinates"]);

            var dataLoader = new GraphLoader(graphEdges, 100);
            var graph      = dataLoader.Load();

            var calculationStrategies = new ISimilarityCalculationStrategy[]
            { new EdgeSimillarityStrategy(), new NodeSimilarityStrategy() };

            var simpleSolver      = new TspSolver(graph);
            var localSearchSolver = new TspLocalSearchSolver(graph);
            var edgeFinder        = new GraspEdgeFinder(3);

            var evolutinarySolver = new EvolutionarySolver(graph,
                                                           new Recombinator(new SimilarityFinder(calculationStrategies), Steps, graph),
                                                           new Selector(), 41000);
            var localSearch = new LocalSearchAlgorithm(Steps, edgeFinder);

            var stats = new BasicSolverStatistics();

            var bestCost = int.MaxValue;
            ISolverStatistics bestStatistics = new BasicSolverStatistics();

            for (var i = 0; i < 10; i++)
            {
                var generatedPaths = simpleSolver.Solve(new RandomPathAlgorithm(Steps, edgeFinder));
                generatedPaths = localSearchSolver.Solve(localSearch, generatedPaths);

                evolutinarySolver.Solve(localSearch, generatedPaths);

                if (evolutinarySolver.SolvingStatistics.BestPath.Cost < bestCost)
                {
                    bestCost       = evolutinarySolver.SolvingStatistics.BestPath.Cost;
                    bestStatistics = evolutinarySolver.SolvingStatistics;
                }

                stats.UpdateSolvingResults(evolutinarySolver.SolvingStatistics.BestPath, evolutinarySolver.SolvingStatistics.MeanSolvingTime);
            }

            var statsPrinter = new FilePrinter(resultPath, "evo_stats.res");

            statsPrinter.Print(stats.ToString());

            var output = new StringBuilder();

            output.AppendLine($"{"Id".PadRight(4)}\tCost\tTime");
            for (var i = 0; i < bestStatistics.Costs.Count; i++)
            {
                output.AppendLine($"{i.ToString().PadRight(4)}\t{bestStatistics.Costs[i].ToString().PadRight(4)}\t{bestStatistics.SolvingTimes[i].Milliseconds:D}");
            }

            var evoResultsPrinter = new FilePrinter(resultPath, "evolutionary_results.res");

            evoResultsPrinter.Print(output.ToString());

            Console.WriteLine("Evolutionary solver ended his work!");
            //SimilaritySolver(resultPath, graph, calculationStrategies, edgeFinder);
        }