Ejemplo n.º 1
0
 public void SimulatedAnnealingAlgorithm()
 {
     if (window.simulatedAnnealingAttributes.GetStartingTemperature() != 0 && window.simulatedAnnealingAttributes.GetCoolingFactor() != 0)
     {
         window.chessboard = new Chessboard(this.ChessboardSizeToInt(), window);
         SimulatedAnnealingAlgorithm simulatedAnnealing = new SimulatedAnnealingAlgorithm(window);
         simulatedAnnealing.SolveProblem(window.GetChessboard(),
                                         window.simulatedAnnealingAttributes.GetStartingTemperature(), window.simulatedAnnealingAttributes.GetCoolingFactor());
         this.SetUITextes(simulatedAnnealing);
     }
 }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
0
        private void StartButton_Click(object sender, EventArgs e)
        {
            this.Invoke(FireToggleButtons, false);
            try
            {
                var problemToSolve = TaskGetter.Invoke();
                SimulatedAnnealingAlgorithm simulatedAnnealing;
                switch (problemToSolve.ProblemName)
                {
                case "Quadratic":
                    simulatedAnnealing = new SimulatedAnnealingAlgorithm(new QuadraticFunction(), 2, 10, 0.01, 1000, 0.99);
                    break;

                case "Rastrigin":
                    simulatedAnnealing = new SimulatedAnnealingAlgorithm(new RastriginFunction(), problemToSolve.DefaultDimensions, problemToSolve.Parameters);
                    break;

                case "Rosenbrock":
                    simulatedAnnealing = new SimulatedAnnealingAlgorithm(new RosenbrockFunction(), problemToSolve.DefaultDimensions, problemToSolve.Parameters);
                    break;

                case "IHCP":
                    UtilitiesMethods utilitiesMethods = new UtilitiesMethods();
                    DirectProblem    directProblem    = new DirectProblem(utilitiesMethods.f, utilitiesMethods.g, utilitiesMethods.h, 1, 1, 15, 480, 1, 1, 1);
                    InverseHeatConductionProblemFunction inverseHeatConductionProblemFunction = new InverseHeatConductionProblemFunction(directProblem, 0);
                    simulatedAnnealing = new SimulatedAnnealingAlgorithm(inverseHeatConductionProblemFunction, 3, problemToSolve.Parameters);
                    break;

                default:
                    throw new Exception("Problem not implemented.");
                }
                Task <double> task   = null;
                var           parent = this;
                SetResultText("Calculating...");
                task = Task.Run(() =>
                {
                    cts = new CancellationTokenSource();
                    CancellationToken token = cts.Token;
                    try
                    {
                        Task.Run(() =>
                        {
                            try
                            {
                                while (true)
                                {
                                    Thread.Sleep(100);
                                    token.ThrowIfCancellationRequested();
                                }
                            }
                            catch (Exception ep)
                            {
                                parent.Invoke(tmpEvent, "Process stoped.");
                                parent.Invoke(FireToggleButtons, true);
                            }
                        }, token);
                    }
                    catch (Exception exc)
                    {
                        MessageBox.Show(exc.Message, "Information");
                    }
                    double solutionValue = 0;
                    if (simulatedAnnealing != null)
                    {
                        solutionValue = simulatedAnnealing.Solve();
                        parent.Invoke(tmpEvent, "Problem name: " + problemToSolve.ProblemName + ", Dimensions: " + problemToSolve.DefaultDimensions + Environment.NewLine +
                                      "Real solution: " + simulatedAnnealing.Function.Solution + Environment.NewLine +
                                      "Founded solution: " + solutionValue + Environment.NewLine + simulatedAnnealing.GetCurrentProblemGUISolution());
                        parent.Invoke(FireToggleButtons, true);
                    }
                    return(solutionValue);
                });
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information");
            }
        }