예제 #1
0
        public static LinkedList<Node> BuildNodePath(Node Start, Node Goal, long NodesCountInSystem)
        {
            int expandedNodesCount = 0;
            int nodesInNowList = 0;
            int depthLimit = 0;
            bool found = false;
            TreeNode start = new TreeNode(Start);
            TreeNode goal = new TreeNode(Goal);
            LinkedList<TreeNode> toExpand = new LinkedList<TreeNode>();
            Dictionary<long, TreeNode> discoveredNodes = new Dictionary<long, TreeNode>();

            toExpand.AddFirst(start);
            discoveredNodes.Add(start.Node.Id, start);
            while (!found)
            {
                toExpand = FringeSearchIteration(goal, toExpand, discoveredNodes, depthLimit, ref found, ref expandedNodesCount);
                nodesInNowList += toExpand.Count;
                depthLimit++;
                if (discoveredNodes.Count == NodesCountInSystem)
                    throw new PathNotFoundException("Path not found!");
            }

            Console.WriteLine("Expanded {0} nodes", expandedNodesCount);
            Console.WriteLine("Nodes in now list: {0}", nodesInNowList);

            return createPath(goal, start);
        }
예제 #2
0
        public static Dictionary<AbstractState, Node> CreateGraph(Node root, params AbstractAction[] actions)
        {
            Dictionary<AbstractState, Node> uniqeNodes = new Dictionary<AbstractState, Node>();
            uniqeNodes.Add(root.State, root);
            Queue<Node> queue = new Queue<Node>();
            queue.Enqueue(root);

            while (queue.Count > 0)
            {
                Node curNode = queue.Dequeue();
                foreach (var action in actions)
                {
                    AbstractState state = action.execute(curNode);
                    if (state != null)
                        if (!uniqeNodes.ContainsKey(state))
                        {
                            Node newNode = new Node(state);
                            curNode.Children.Add(new Tuple<Node, AbstractAction>(newNode, action));
                            uniqeNodes.Add(state, newNode);
                            queue.Enqueue(newNode);
                        }
                        else
                        {
                            curNode.Children.Add(new Tuple<Node, AbstractAction>(uniqeNodes[state], action));
                        }
                }
            }

            return uniqeNodes;
        }
 public override IList<AbstractState> GetNextStates(Node node)
 {
     IList<AbstractState> states = new List<AbstractState>();
     foreach (var action in actions)
     {
         AbstractState state = action.execute(node);
         if (state != null && state is TilesState)
             states.Add(state);
     }
     return states;
 }
예제 #4
0
        public static LinkedList<Node> BuildNodePathUsingAStarWithoutSystem(Node Start, Node Goal, HeuristicCalculator Calculator, NextStatesProvider Provider)
        {
            int expandedNodesCount = 0;
            bool found = false;
            TreeNode start = new TreeNode(Start);
            TreeNode goal = new TreeNode(null);
            var toExpand = new C5.IntervalHeap<TreeNode>(new TreeNodeCostComparer());
            var discoveredTreeNodes = new Dictionary<int, TreeNode>();

            start.Cost = Calculator.Calculate(Start.State, Goal.State);
            toExpand.Add(start);
            discoveredTreeNodes.Add(start.Node.State.GetHashCode(), start);

            while (toExpand.Count > 0)
            {
                TreeNode Current = toExpand.FindMin();
                toExpand.DeleteMin();

                if (Current.Node.State.Equals(Goal.State))
                {
                    goal = Current;
                    found = true;
                    break;
                }

                expandedNodesCount++;
                foreach (var nextNode in Provider.GetNodesWithNextStates(Current.Node))
                {
                    double Cost = Calculator.Calculate(nextNode.State, Goal.State);
                    if (!discoveredTreeNodes.ContainsKey(nextNode.State.GetHashCode()))
                    {
                        TreeNode Tem = new TreeNode(nextNode, Current, Current.Cost + Cost);
                        Current.Children.AddLast(Tem);
                        discoveredTreeNodes.Add(nextNode.State.GetHashCode(), Tem);
                        toExpand.Add(Tem);
                    }
                    else
                    {
                        CheckIfBetterPathWasFound(discoveredTreeNodes, Current, nextNode, Cost);
                    }
                }
            }

            Console.WriteLine("Expanded {0} nodes", expandedNodesCount);

            if (!found)
                throw new PathNotFoundException("Path not found!");

            return createPath(goal, start);
        }
        public override IList<Node> GetNodesWithNextStates(Node node)
        {
            IList<Node> nodes = new List<Node>();
            foreach (var action in actions)
            {
                AbstractState state = action.execute(node);
                if (state != null && state is TilesState)
                {
                    Node newNode = new Node(state);
                    node.Children.Add(Tuple.Create(newNode, action));
                    nodes.Add(newNode);
                }
            }

            return nodes;
        }
예제 #6
0
        private static Dictionary<long, Node> createNodes(string FileName, char separator)
        {
            var nodes = new Dictionary<long, Node>();
            var lines = readLines(FileName);

            foreach (var line in lines)
            {
                var coords = line.Split(separator);
                var val = Convert.ToDouble(coords[0]);
                var state = new MazeState(Convert.ToDouble(coords[0]), Convert.ToDouble(coords[1]));
                var node = new Node(state);
                nodes.Add(node.Id, node);
            }

            return nodes;
        }
 public override AbstractState execute(Node node)
 {
     if (!(node.State is TilesState))
         throw new InvalidCastException();
     TilesState state = node.State as TilesState;
     int[,] tiles = state.GetTiles();
     int rowCoord = state.EmptyTileRowCoord;
     int colCoord = state.EmptyTileColumnCoord;
     if (CanShift(ShiftDirectionEnum.DOWN, rowCoord, colCoord))
     {
         tiles[rowCoord, colCoord] = tiles[rowCoord + 1, colCoord];
         tiles[rowCoord + 1, colCoord] = 0;
         state = new TilesState(tiles);
         return state;
     }
     return null;
 }
예제 #8
0
        private static void CheckIfBetterPathWasFound(Dictionary<int, TreeNode> discoveredTreeNodes, TreeNode Current, Node nextNode, double Cost)
        {
            var updateCandidate = discoveredTreeNodes[nextNode.State.GetHashCode()];
            if (updateCandidate.Cost > Current.Cost + Cost)
            {
                var costDifference = updateCandidate.Cost - (Current.Cost + Cost);

                Current.Children.AddLast(updateCandidate);
                updateCandidate.Parent.Children.Remove(updateCandidate);
                updateCandidate.Parent = Current;
                updateCandidate.Cost = Current.Cost + Cost;

                var candidateChildren = new LinkedList<TreeNode>(updateCandidate.Children);
                while (candidateChildren.Count > 0)
                {
                    var child = candidateChildren.First();
                    candidateChildren.RemoveFirst();

                    foreach (var item in child.Children)
                        candidateChildren.AddLast(item);

                    child.Cost -= costDifference;
                }
            }
        }
 public abstract override AbstractState execute(Node node);
예제 #10
0
 public abstract AbstractState execute(Node node);
예제 #11
0
        static void Main(string[] args)
        {
            int[,] rootMatrix = new int[,] {{7, 2, 4 },{5, 0, 6 },{8, 3, 1 }};
            TilesState rootState = new TilesState(rootMatrix);

            IList<AbstractAction> actions = new List<AbstractAction>();
            actions.Add(new ShiftEmptyTileLeftAction());
            actions.Add(new ShiftEmptyTileRightAction());
            actions.Add(new ShiftEmptyTileUpAction());
            actions.Add(new ShiftEmptyTileDownAction());

            Node root = new Node(rootState);

            HeuristicCalculator calculator = new TilesHeuristicCalculator();
            NextStatesProvider provider = new NextTileStatesProvider(actions);

            // Console.WriteLine(calculator.Calculate(rootState, new TilesState(new int[,] { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 } })));

            try
            {
                var path = PathBuilder.BuildNodePathUsingAStarWithoutSystem(root, new Node(new TilesState(new int[,] { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 } })), calculator, provider);

                //foreach (var node in path)
                 //   Console.WriteLine(node);

                foreach (var action in PathTransformer.TransformNodePathToActionPath(path))
                    Console.WriteLine(action);
            }
            catch (PathNotFoundException ex) { Console.WriteLine(ex.Message); }

            /*

            var uniqueNodes = NodeGraphFactory.CreateGraph(root, actions.ToArray());

            //Console.WriteLine(uniqueNodes.Count);

            try
            {
                var path = PathBuilder.BuildNodePath(root, new Node(new TilesState(new int[,] { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 } })), uniqueNodes.Count);

                foreach (var action in PathTransformer.TransformNodePathToActionPath(path))
                    Console.WriteLine(action);
            }
            catch (PathNotFoundException ex) { Console.WriteLine(ex.Message); }
            Console.WriteLine("\n-----------\n");
            try
            {
                var path = PathBuilder.BuildNodePath(root, new Node(new TilesState(new int[,] { { 8, 0, 6 }, { 5, 4, 7 }, { 2, 3, 1 } })), uniqueNodes.Count);

                foreach (var action in PathTransformer.TransformNodePathToActionPath(path))
                    Console.WriteLine(action);
            }
            catch (PathNotFoundException ex) { Console.WriteLine(ex.Message); }

            Console.WriteLine("\n-----------\n");

            try
            {
                var path = PathBuilder.BuildNodePath(root, new Node(new TilesState(new int[,] { { 1, 2, 3 }, { 8, 0, 4 }, { 7, 6, 5 } })), uniqueNodes.Count);

                foreach (var action in PathTransformer.TransformNodePathToActionPath(path))
                    Console.WriteLine(action);
            }
            catch (PathNotFoundException ex) { Console.WriteLine(ex.Message); }
            */
            Console.ReadKey();
        }
 public override AbstractState execute(Node node)
 {
     throw new NotImplementedException();
 }
 public abstract IList<Node> GetNodesWithNextStates(Node node);
 public abstract IList<AbstractState> GetNextStates(Node node);