コード例 #1
0
        private static void SolveRomaniaMap(IFringe <Node <City> > fringe, City initial, City goal, List <City> cities)
        {
            Console.WriteLine("Romania map problem");
            Console.WriteLine("Trace " + initial.Name + " - " + goal.Name + ": \n");

            var romaniaMap = new RomaniaMap(initial, goal, cities);
            var watch      = System.Diagnostics.Stopwatch.StartNew();
            var node       = TreeSearchWithQueue <City> .Search(romaniaMap, fringe);

            watch.Stop();
            if (node == null)
            {
                Console.WriteLine("Solution not found");
            }
            else
            {
                Console.WriteLine("Solution:");
                var trace = node.ListOfNodes;
                foreach (var city in trace)
                {
                    Console.WriteLine(city.Name);
                }
                Console.WriteLine("\nNumber of steps: " + trace.Count);
                Console.WriteLine("Total cost: " + node.PathCost);
            }
            var elapsedMs = watch.ElapsedMilliseconds;

            Console.WriteLine("\nUsed fringe: " + fringe.GetName());
            Console.WriteLine("Time elapsed [ms]: " + elapsedMs);
            Console.WriteLine(new String('-', 40));
            Console.WriteLine("\n");
        }
コード例 #2
0
        private static void SolvePrzesuwanka(IList <IFringe <Node <byte[, ]> > > fringes, int problemSize)
        {
            Console.WriteLine("Przesuwanka {0} x {0}", problemSize);

            var przesuwanka = new Przesuwanka(problemSize);

            foreach (var fringe in fringes)
            {
                var watch = System.Diagnostics.Stopwatch.StartNew();
                var node  = TreeSearchWithQueue <byte[, ]> .Search(przesuwanka, fringe);

                watch.Stop();
                if (node == null)
                {
                    Console.WriteLine("Solution not found");
                }
                else
                {
                    Console.WriteLine("Solution:");
                    PrintTable(node.NodeState);
                    var trace = node.ListOfNodes;
                    Console.WriteLine("Number of steps: " + trace.Count);
                }
                var elapsedMs = watch.ElapsedMilliseconds;
                Console.WriteLine("\nUsed fringe: " + fringe.GetName());
                Console.WriteLine("Time elapsed [ms]: " + elapsedMs);
                Console.WriteLine(new String('-', 40));
                Console.WriteLine("\n");
            }
        }
コード例 #3
0
        private static void SolveNQueens(IFringe <Node <byte[]> > fringe, int problemSize)
        {
            Console.WriteLine("{0} queens problem", problemSize);

            var nQueens = new NQueens(problemSize);
            var watch   = System.Diagnostics.Stopwatch.StartNew();
            var node    = TreeSearchWithQueue <byte[]> .Search(nQueens, fringe);

            watch.Stop();
            if (node == null)
            {
                Console.WriteLine("Solution not found");
            }
            else
            {
                Console.WriteLine("Solution:");
                PrintTable(node.NodeState);
                var trace = node.ListOfNodes;
                Console.WriteLine("Number of steps: " + trace.Count);
            }
            var elapsedMs = watch.ElapsedMilliseconds;

            Console.WriteLine("\nUsed fringe: " + fringe.GetName());
            Console.WriteLine("Time elapsed [ms]: " + elapsedMs);
            Console.WriteLine(new String('-', 40));
            Console.WriteLine("\n");
        }
コード例 #4
0
        private static void SolveProblemWithFringe <State>(IProblem <State> problem, Action <State> printState, Action <List <State> > printTrace, IFringe <Node <State> > fringe)
        {
            Console.WriteLine("\nSolving with " + fringe.GetName() + "...");
            var watch = System.Diagnostics.Stopwatch.StartNew();

            printState(problem.InitialState);
            var node = TreeSearchWithQueue <State> .Search(problem, fringe);

            watch.Stop();
            if (node == null)
            {
                Console.WriteLine("Solution not found");
            }
            else
            {
                Console.WriteLine("Solution:");
                var trace = node.ListOfNodes;
                if (printTrace != null)
                {
                    printTrace(trace);
                }
                else
                {
                    printState(node.NodeState);
                }
                Console.WriteLine("Number of steps: " + trace.Count);
                Console.WriteLine("Total cost: " + node.PathCost);
            }
            var elapsedMs = watch.ElapsedMilliseconds;

            Console.WriteLine("Time elapsed [ms]: " + elapsedMs);
            Console.WriteLine(new String('-', 40));
            Console.WriteLine("\n");
        }