//A* search algorithm
        public void Astar(Node_1 Start, Node_1 Goal, Hashtable h)
        {
            //creat empty PriorityQueue
            PriorityQueue Astar_Pq = new PriorityQueue();

            //insert start point to PriorityQueue
            Astar_Pq.Enqueue(Start);
            //list of visited nodes
            List <Node_1> RepeatedStates = new List <Node_1>();

            //loop on graph
            while (Astar_Pq.Count != 0)
            {
                //remove first point in the PriorityQueue
                Node_1 Current = (Node_1)Astar_Pq.Dequeue();
                //display visited nodes
                Console.WriteLine(Current.State + "\t visited");
                //add visited nodes in list
                RepeatedStates.Add(Current);
                //check goal
                if (Current.State == Goal.State)
                {
                    //return goal
                    //list to get parent or path
                    List <string> SolutionGoal = new List <string>();
                    //tn insert parent or path in list
                    while (Current != null)
                    {
                        SolutionGoal.Insert(0, Current.State);
                        Current = Current.Parent;
                    }
                    //display parent or path
                    Console.WriteLine("Goal");
                    foreach (var item in SolutionGoal)
                    {
                        Console.WriteLine(item);
                    }
                    //To stop loop
                    break;
                }
                //add neighbors for removed point
                else
                {
                    foreach (var item in Current.Neighbors)
                    {
                        //check on visited node from list to Prevents repate
                        if (RepeatedStates.Contains(item))
                        {
                            continue;
                        }
                        //get g(n) from hashtable to calc f(n)
                        item.Cost += Current.Cost;
                        //get h(n) from hashtable and calc f(n)
                        item.Fn = item.Cost + (double)h[item];
                        //insert itenm in PriorityQueue
                        Astar_Pq.Enqueue(item);
                    }
                }
            }
        }
示例#2
0
        static void Main(string[] args)
        {
            //object to initialize start point
            Node_1 Start = new Node_1("S");
            //object to initialize goal point
            Node_1 Goal = new Node_1("G");
            //group of objects to creat graph
            Node_1 a = new Node_1("a");
            Node_1 b = new Node_1("b");
            Node_1 c = new Node_1("c");
            Node_1 d = new Node_1("d");
            Node_1 e = new Node_1("e");

            //

            //add neihbors for each node
            Start.AddNeihbors(a, 1.5);
            Start.AddNeihbors(d, 2);
            a.AddNeihbors(b, 2);
            b.AddNeihbors(c, 3);
            c.AddNeihbors(Goal, 4);
            d.AddNeihbors(e, 3);
            e.AddNeihbors(Goal, 2);
            //

            //object of hashtable
            Hashtable H = new Hashtable
                          //object add node and cost to calc f(n)
            {
                { (Node_1)a, (double)4 },
                { (Node_1)b, (double)2 },
                { (Node_1)c, (double)4 },
                { (Node_1)d, (double)4.5 },
                { (Node_1)e, (double)2 },
                { (Node_1)Goal, (double)0 },
                { (Node_1)Start, (double)0 }
            };
            //

            //object from hueristic search algorithm
            HeuristicSearch hs = new HeuristicSearch();

            //object use greedy algorithm
            //hs.Greedy(Start, Goal, H);
            //object use A* algorithm
            //hs.Astar(Start, Goal, H);
            //object use Uniform Cost Search algorithm (uninformed search strategy)
            hs.UCS(Start, Goal, H);

            Console.ReadKey();
        }
示例#3
0
 //method to add neighbors for each state
 public void AddNeihbors(Node_1 n, double Cost)
 {
     //add neighbors for each object call this method to list
     this.Neighbors.Add(n);
     n.Cost = Cost;
 }