Example #1
0
        public override void Execute()
        {
            openset.Enqueue(new PriorityQueueState(CurrentState), 0);
            cost_so_far.Add(CurrentState.Key, 0);
            Console.WriteLine("Initial State:");

            CurrentState.Format();

            watch = new Stopwatch();
            watch.Start();
            while (openset.Count > 0)
            {
                var currentnode = openset.Dequeue();
                CurrentState = currentnode.NodeState;
                if (CurrentState.IsEqualToGoal())
                {
                    watch.Stop();

                    SolutionFound(CurrentState);

                    Console.WriteLine("Elapsed time: {0} ms", watch.Elapsed.Milliseconds);
                    return;
                }
                else
                {
                    var children = CurrentState.BuildChildren();

                    foreach (var child in children)
                    {
                        var new_cost = cost_so_far[CurrentState.Key] + 1; //1 refers to the distance between the current node and its childrens
                        if (!cost_so_far.ContainsKey(child.Key) || new_cost < cost_so_far[child.Key])
                        {
                            cost_so_far[child.Key] = new_cost;

                            int priority = 0;

                            if (use_manhattan)
                            {
                                priority = new_cost + ManVal(child.StateArray);
                            }
                            else
                            {
                                priority = new_cost + HammingDistance(child.StateArray);
                            }

                            openset.Enqueue(new PriorityQueueState(child), (double)priority);

                            if (!parents.ContainsKey(child.Key))
                            {
                                parents[child.Key] = CurrentState;
                            }
                        }
                    }
                }
            }
        }
Example #2
0
        public override void Execute()
        {
            openset.Enqueue(new PriorityQueueState(CurrentState), 0);
            Console.WriteLine("Initial State:");

            CurrentState.Format();

            watch = new Stopwatch();
            watch.Start();
            while (openset.Count > 0)
            {
                var currentnode = openset.Dequeue();
                CurrentState = currentnode.NodeState;
                if (CurrentState.IsEqualToGoal())
                {
                    watch.Stop();

                    SolutionFound(CurrentState);

                    Console.WriteLine("Elapsed time: {0} ms", watch.Elapsed.Milliseconds);
                    return;
                }
                else
                {
                    var children = CurrentState.BuildChildren();

                    foreach (var child in children)
                    {
                        if (!parents.ContainsKey(child.Key))
                        {
                            int priority = ManVal(child.StateArray);
                            openset.Enqueue(new PriorityQueueState(child), (double)priority);
                            parents[child.Key] = CurrentState;
                        }
                    }
                }
            }
        }
Example #3
0
        public override void Execute()
        {
            Console.WriteLine("Beginning execution for BFS");
            Console.WriteLine("Initial State:");
            CurrentState.Format();
            watch = new Stopwatch();
            watch.Start();

            OpenSet.Enqueue(CurrentState);
            try{
                while (OpenSet.Count > 0) //while openset is not empty

                //grab a state from the open set
                {
                    CurrentState = OpenSet.Dequeue();

#if DEBUG
                    Console.WriteLine("Removed from queue: {0}", CurrentState);
#endif

                    //put current node into the closed set
                    if (ClosedSet.ContainsKey(CurrentState.Key) == false)
                    {
                        ClosedSet.Add(CurrentState.Key, CurrentState);
                    }

                    //check if current node is goal state
                    if (CurrentState.IsEqualToGoal())
                    {
                        watch.Stop();

                        Console.WriteLine("Solution found");
                        Console.WriteLine("Move list: ");
                        SolutionFound(CurrentState);

                        break;
                    }
                    else
                    {
                        //if not goal state, find each state accessible from current state (children)
                        //check if they are already present in the closed set if so ignore them

                        var list = CurrentState.BuildChildren();

#if DEBUG
                        Console.WriteLine("Children of node: {0}", CurrentState);
                        foreach (var item in list)
                        {
                            item.Format();
                            Console.WriteLine();
                        }
#endif
                        foreach (var item in list)
                        {
                            if (ClosedSet.ContainsKey(item.Key))
                            {
#if DEBUG
                                Console.WriteLine("ClosedSet already contains key {0}", item.Key);
#endif
                                continue;
                            }
                            OpenSet.Enqueue(item); // if they aren't present add them to the queue.

                            if (!parents.ContainsKey(item.Key))
                            {
                                parents.Add(item.Key, CurrentState);
                            }
                        }
                    }

                    //if openset is empty and we never found the goal, there is no solution.
                    if (OpenSet.Count == 0)
                    {
                        throw new Exception("no solution");
                    }
                }
            }catch (ArgumentException ex) {
                Console.WriteLine(ex.Message + ex.StackTrace);
                Console.WriteLine("Count: {0}", moves.Count);
            }

            Console.WriteLine("Elapsed time: {0} ms", watch.Elapsed.Milliseconds);
        }