示例#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
        public Node <State> TreeSearchMethod <State>(IProblem <State> problem, IFringe <Node <State> > fringe)
        {
            fringe.Add(new Node <State>(problem.InitialState, null, problem.Compare()));
            while (!fringe.IsEmpty)
            {
                Node <State> node = fringe.Pop();

                if (problem.IsGoal(node.StateOfNode))
                {
                    Console.Write("\n\nFinalne ustawienie:\n");
                    return(node);
                }

                Console.WriteLine("\n");
                problem.PrintState(node.StateOfNode);
                Console.Write(" --- Distance: ");
                Console.Write(problem.CheckDistance(node.StateOfNode));

                foreach (State state in problem.Expand(node.StateOfNode))
                {
                    if (!node.OnPathToRoot(state))
                    {
                        fringe.Add(new Node <State>(state, node, problem.Compare()));
                    }
                }
            }
            return(null);
        }
示例#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");
        }
        public static Node <State> Search(IProblem <State> problem, IFringe <Node <State> > fringe)
        {
            Func <Node <State>, Node <State>, int> compareStatesPriority = (node1, node2) =>
            {
                return(problem.CompareStatesPriority(node1.NodeState, node2.NodeState));
            };

            Func <Node <State>, Node <State>, int> compareStatesPriorityWithPathCost = (node1, node2) =>
            {
                return(problem.CompareStatesPriorityWithPathCost(node1.NodeState, node2.NodeState, node1.PathCost, node2.PathCost));
            };

            fringe.SetPriorityMethod(compareStatesPriority, compareStatesPriorityWithPathCost);
            fringe.Add(new Node <State>(problem.InitialState, null, 0.0));
            while (!fringe.IsEmpty)
            {
                var node = fringe.Pop();

                if (problem.IsGoal(node.NodeState))
                {
                    return(node);
                }
                foreach (var state in problem.Expand(node.NodeState))
                {
                    if (!node.OnPathToRoot(state, problem.AreStatesTheSame))
                    {
                        var newNode = new Node <State>(state, node, node.PathCost + problem.CalculateCostToNextState(node.NodeState, state));
                        fringe.Add(newNode);
                    }
                }
            }
            return(null);
        }
示例#5
0
        public static Node <State> TreeSearchMethod(IProblem <State> problem, IFringe <Node <State> > fringe, Enum method)
        {
            Func <Node <State>, int> calculatePriorityForBestFirstSearch = newState =>
                                                                           problem.CountOfConflicts(newState.StateOfNode);

            Func <Node <State>, int> calculatePriorityForAStar = newstate =>
                                                                 problem.CountDistancesToGoal(newstate.StateOfNode);


            fringe.SetCompareMethod(ComparePriority);

            var initNode = new Node <State>(problem.InitialState, null);

            initNode.Priority = CalculatePriorityMethod(method,
                                                        calculatePriorityForBestFirstSearch,
                                                        calculatePriorityForAStar, initNode);

            fringe.Add(initNode); ///tworzy root na stosie

            while (!fringe.IsEmpty)
            {
                var node = fringe.Pop();              //zdjecie ze stosu
                if (problem.IsGoal(node.StateOfNode)) //sprawdzenie zdjetego elementu ze stosu
                {
                    return(node);
                }

                problem.CountOfSteps++;

                foreach (var actualState in problem.Expand(node.StateOfNode))
                {
                    //foreach-a z możliwymy stanami, to tam sprawdzam czy dany stan z IListy
                    // już nie wystąpił, wywołując OnPathToRoot,
                    if (!node.OnPathToRoot(node.StateOfNode, actualState, problem.Compare))
                    //Wykonuje sie gdy nie ma znalezionego identycznego stanu
                    {
                        var nodeToAdd = new Node <State>(actualState, node);
                        nodeToAdd.Priority = CalculatePriorityMethod(method,
                                                                     calculatePriorityForBestFirstSearch,
                                                                     calculatePriorityForAStar, nodeToAdd);

                        fringe.Add(nodeToAdd);
                    }
                }
            }

            return(null);
        }
        public static Node <State> TreeSearchMetod(IProblem <State> problem, IFringe <Node <State> > fringe, Enum method)
        {
            Func <State, double> calculatePriorityForBestFirstSearch = newState =>
                                                                       problem.CalculateDistanceToDestinyCity(newState);
            //odleglosc w lini prostej

            Func <Node <State>, State, double> calculatePriorityForAStar = (parent, newState) =>
                                                                           problem.CalculatePriorityForAStar(parent.StateOfNode, newState);

            //Linia prosta + odleglosc krawedziowa, pozniej dodana jest
            //droga juz pokonana


            fringe.SetCompareMethod(ComparePriority);
            fringe.Add(new Node <State>(problem.InitialState, null, 1, double.MaxValue)); //tworzy root na stosie

            while (!fringe.IsEmpty)
            {
                var node = fringe.Pop();              //zdjecie ze stosu
                if (problem.IsGoal(node.StateOfNode)) //sprawdzenie zdjetego elementu ze stosu
                {
                    return(node);
                }

                foreach (var actualState in problem.Expand(node.StateOfNode))
                {
                    //foreach-a z możliwymy stanami, to tam sprawdzam czy dany stan z IListy
                    // już nie wystąpił, wywołując OnPathToRoot,
                    if (!node.OnPathToRoot(node.StateOfNode, actualState, problem.Compare))
                    //Wykonuje sie gdy nie ma znalezionego identycznego stanu
                    {
                        var nodeToAdd = new Node <State>(actualState, node, node.StepsForSolution++,
                                                         CalculatePriorityMethod(method, calculatePriorityForBestFirstSearch,
                                                                                 calculatePriorityForAStar, node, actualState));

                        nodeToAdd.TotalRoad = node.TotalRoad +
                                              problem.GetDistanceToCity(node.StateOfNode, nodeToAdd.StateOfNode);
                        fringe.Add(nodeToAdd);
                        CountOfSteps++;
                    }
                }
            }

            return(null);
        }
示例#7
0
文件: Program.cs 项目: Mashaav/SI
 public static Node <State> TreeSearch <State>(IProblem <State> problem, IFringe <Node <State> > queue)
 {
     queue.Add(new Node <State>(problem.InitialState, null));
     while (!queue.Empty())
     {
         Node <State> node = queue.Remove();
         if (problem.IsGoal(node.State))
         {
             return(node);
         }
         IList <State> successors = problem.Expand(node.State);
         foreach (State st in successors)
         {
             queue.Add(new Node <State>(st, node));
         }
     }
     return(null);
 }
示例#8
0
        public Node <State> TreeSearchMethodMap <State>(IProblem <State> problem, IFringe <Node <State> > fringe) //w programie głownym jest przegladanie drogi
        {
            fringe.Add(new Node <State>(problem.InitialState, null, problem.Compare()));
            while (!fringe.IsEmpty)
            {
                Node <State> node = fringe.Pop();
                if (problem.IsGoal(node.StateOfNode))
                {
                    return(node);
                }

                foreach (State state in problem.Expand(node.StateOfNode))
                {
                    if (!node.OnPathToRoot(state))
                    {
                        node.currentDistance = problem.CheckDistance(node.StateOfNode);
                        fringe.Add(new Node <State>(state, node, problem.Compare()));
                    }
                }
            }

            return(null);
        }
示例#9
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");
        }
示例#10
0
        public static Node <State> TreeSearchWithQueue <State> (IProblem <State> problem, IFringe <Node <State> > fringe, bool enableAStarMethod = false)
        {
            if (enableAStarMethod)
            {
                fringe.GetCost = (Node <State> node) => { return(problem.EstimatedCostToGoal(node.state) + problem.GetCurrentCost(node.state, node.node.state, node.CurrentCost)); };
            }
            else
            {
                fringe.GetCost = (Node <State> node) => { return(problem.EstimatedCostToGoal(node.state)); };
            }

            fringe.Add(new Node <State>(problem.InitialState, null));

            int a = 1;

            while (!fringe.IsEmpty)
            {
                Node <State> node = fringe.Pop();

                if (problem.IsGoal(node.state))
                {
                    return(node);
                }

                foreach (State state in problem.Expand(node.state))
                {
                    if (!node.OnPathToRoot(state, problem.StateCompare))
                    {
                        fringe.Add(new Node <State>(state, node, problem.GetCurrentCost(state, node.state, node.CurrentCost)));
                        if (rownyRzad(a))
                        {
                            problem.Print(state);
                        }
                        a++;
                    }
                }
            }
            return(null);
        }