예제 #1
0
        public Solution DFSGraphSearchRecursive(int start, int stop)
        {
            List <int>  solution = new List <int>();
            List <Arc>  arcList  = new List <Arc>();
            Stack <int> frontier = new Stack <int>();
            List <int>  explored = new List <int>();

            frontier.Push(start);
            bool isFound = false;

            Stopwatch sw = new Stopwatch();

            sw.Start();
            //DFS checks if solution available
            isFound = DFSGraphRecursive(stop, ref frontier, ref arcList, ref explored);

            sw.Stop();
            time       = sw.ElapsedMilliseconds;
            memorySize = stackCallCounter;
            Console.WriteLine("Memory size:" + memorySize);
            System.IO.File.WriteAllText("output.txt", "Memory size:" + memorySize + "\n");
            Console.WriteLine("Search done in " + sw.ElapsedMilliseconds + " miliseconds = " + sw.Elapsed);
            System.IO.File.AppendAllText("output.txt", "Search done in " + sw.ElapsedMilliseconds + " miliseconds = " + sw.Elapsed);

            //Find Parents
            if (isFound == true)
            {
                Arc arc = arcList.First(a => a.child == stop);
                solution.Add(stop);
                while (arc.parent != start)
                {
                    arc = arcList.First(a => a.child == arc.parent);
                    solution.Add(arc.child);
                }
                solution.Add(start);
                solution.Reverse();
            }
            else
            {
                return new Solution()
                       {
                           isSolved = false
                       }
            };

            return(new Solution(solution, RouteFind.FindRoute(solution)));
        }
예제 #2
0
        public Solution BFSGraphSearch(int start, int stop)
        {
            List <int>  solution = new List <int>();
            List <Arc>  arcList  = new List <Arc>();
            Queue <int> frontier = new Queue <int>();

            frontier.Enqueue(start);
            List <int> explored = new List <int>();
            bool       isFound  = false;

            Stopwatch sw = new Stopwatch();

            sw.Start();
            //BFS checks if solution available
            while (isFound == false && frontier.Count != 0)
            {
                int leaf = frontier.Dequeue();

                if (stop == leaf)
                {
                    isFound = true;
                }
                explored.Add(leaf);
                for (int j = 0; j < Route_stops.AllStops.Count; j++) //Explore child nodes by looking into StopsGraph
                {
                    if (Route_stops.StopsGraph[leaf, j] != 0 && !explored.Contains(j) && !frontier.Contains(j))
                    {
                        frontier.Enqueue(j);
                        Arc arc = new Arc();
                        arc.parent = leaf;
                        arc.child  = j;
                        arcList.Add(arc);
                    }
                }
            }
            sw.Stop();
            time       = sw.ElapsedMilliseconds;
            memorySize = frontier.Count + explored.Count;
            Console.WriteLine("Memory size:" + memorySize);
            System.IO.File.WriteAllText("output.txt", "Memory size:" + memorySize + "\n");
            Console.WriteLine("Search done in " + sw.ElapsedMilliseconds + " miliseconds = " + sw.Elapsed);
            System.IO.File.AppendAllText("output.txt", "Search done in " + sw.ElapsedMilliseconds + " miliseconds = " + sw.Elapsed);

            //Find Parents
            if (isFound == true)
            {
                Arc arc = arcList.First(a => a.child == stop);
                solution.Add(stop);
                while (arc.parent != start)
                {
                    arc = arcList.First(a => a.child == arc.parent);
                    solution.Add(arc.child);
                }
                solution.Add(start);
                solution.Reverse();
            }
            else
            {
                return new Solution()
                       {
                           isSolved = false
                       }
            };

            return(new Solution(solution, RouteFind.FindRoute(solution)));
        }
예제 #3
0
        public Solution AstarLeastRoute(int start, int stop)
        {
            List <int>  solution = new List <int>();
            List <Arc>  arcList  = new List <Arc>();
            List <Node> frontier = new List <Node>();

            frontier.Add(new Node()
            {
                node = start, distance = Route_stops.StopDistance(start, stop)
            });
            List <int> explored = new List <int>();
            bool       isFound  = false;

            Stopwatch sw = new Stopwatch();

            sw.Start();
            //A* checks if solution available
            while (isFound == false && frontier.Count != 0)
            {
                frontier = frontier.OrderBy(c => c.distance).ToList(); //Select with least f(x)= h(x) + g(x)

                Node leafNode = frontier.First();
                int  leaf     = leafNode.node;
                frontier.Remove(leafNode);
                if (stop == leaf)
                {
                    isFound = true;
                }
                else
                {
                    explored.Add(leaf);
                    List <Node> children = new List <Node>();
                    for (int j = 0; j < Route_stops.AllStops.Count; j++) //Explore child nodes by looking into StopsGraph
                    {
                        if (Route_stops.StopsGraph[leaf, j] != 0 && !explored.Contains(j) && !frontier.Any(f => f.node == j))
                        {
                            double distanceUntilThisNode = 0;

                            if (arcList.Count != 0 && explored.Count > 1)
                            {
                                List <int> tempStopList = new List <int>();

                                tempStopList.Add(j);
                                tempStopList.Add(leaf);
                                Arc arc = arcList.First(a => a.child == leaf);
                                while (arc.parent != start)
                                {
                                    arc = arcList.First(a => a.child == arc.parent);
                                    tempStopList.Add(arc.child);
                                }
                                tempStopList.Add(start);
                                tempStopList.Reverse();
                                distanceUntilThisNode = RouteFind.FindRoute(tempStopList).Count;
                            }
                            frontier.Add(new Node()
                            {
                                node = j, distance = distanceUntilThisNode
                            });                                                                      // = h(n)
                            Arc arcAdd = new Arc();
                            arcAdd.parent = leaf;
                            arcAdd.child  = j;
                            arcList.Add(arcAdd);
                        }
                    }
                }
            }

            sw.Stop();
            time       = sw.ElapsedMilliseconds;
            memorySize = frontier.Count + explored.Count;
            Console.WriteLine("Memory size:" + memorySize);
            System.IO.File.WriteAllText("output.txt", "Memory size:" + memorySize + "\n");
            Console.WriteLine("Search done in " + sw.ElapsedMilliseconds + " miliseconds = " + sw.Elapsed);
            System.IO.File.AppendAllText("output.txt", "Search done in " + sw.ElapsedMilliseconds + " miliseconds = " + sw.Elapsed);

            //Find Parents
            if (isFound == true)
            {
                Arc arc = arcList.First(a => a.child == stop);
                solution.Add(stop);
                while (arc.parent != start)
                {
                    arc = arcList.First(a => a.child == arc.parent);
                    solution.Add(arc.child);
                }
                solution.Add(start);
                solution.Reverse();
            }
            else
            {
                return new Solution()
                       {
                           isSolved = false
                       }
            };

            return(new Solution(solution, RouteFind.FindRoute(solution)));
        }
    }
예제 #4
0
        public Solution DFSTreeSearchNonRecursive(int start, int stop)
        {
            List <int>  solution = new List <int>();
            List <Arc>  arcList  = new List <Arc>();
            Stack <int> frontier = new Stack <int>();

            frontier.Push(start);
            bool isFound = false;

            Stopwatch sw = new Stopwatch();

            sw.Start();
            //DFS checks if solution available
            while (isFound == false && frontier.Count != 0)
            {
                if (frontier.Count > 100000)
                {
                    System.IO.File.WriteAllText("output.txt", "Function ended to prevent infinite loop.");
                    return(new Solution()
                    {
                        isSolved = false
                    });                                         //Probably stuck on infinite loop
                }
                int leaf = frontier.Pop();

                if (stop == leaf)
                {
                    isFound = true;
                }
                for (int j = 0; j < Route_stops.AllStops.Count; j++)
                {
                    if (Route_stops.StopsGraph[leaf, j] != 0)
                    {
                        frontier.Push(j);
                        Arc arc = new Arc();
                        arc.parent = leaf;
                        arc.child  = j;
                        arcList.Add(arc);
                    }
                }
            }
            sw.Stop();
            time       = sw.ElapsedMilliseconds;
            memorySize = frontier.Count;
            Console.WriteLine("Memory size:" + memorySize);
            System.IO.File.WriteAllText("output.txt", "Memory size:" + memorySize + "\n");
            Console.WriteLine("Search done in " + sw.ElapsedMilliseconds + " miliseconds = " + sw.Elapsed);
            System.IO.File.AppendAllText("output.txt", "Search done in " + sw.ElapsedMilliseconds + " miliseconds = " + sw.Elapsed);

            //Find Parents
            if (isFound == true)
            {
                Arc arc = arcList.First(a => a.child == stop);
                solution.Add(stop);
                while (arc.parent != start)
                {
                    arc = arcList.First(a => a.child == arc.parent);
                    solution.Add(arc.child);
                }
                solution.Add(start);
                solution.Reverse();
                //return solution;
            }
            else
            {
                return new Solution()
                       {
                           isSolved = false
                       }
            };

            return(new Solution(solution, RouteFind.FindRoute(solution)));
        }