Exemple #1
0
        static void Main(string[] args)
        {
            var root = new BinaryTree {
                Left = new BinaryTree {
                    Left = new BinaryTree {
                        Left = null, Right = null, Value = 34
                    },
                    Right = new BinaryTree {
                        Left = null, Right = null, Value = 4
                    }, Value = 18
                },
                Right = new BinaryTree {
                    Left = new BinaryTree {
                        Left = null, Right = null, Value = 66
                    },
                    Right = new BinaryTree {
                        Left = null, Right = null, Value = 71
                    }, Value = 1
                },
                Value = 3
            };

            const int searchFor = 75;
            bool      search    = DepthFirstSearch.DepthFirstSearchMethod(root, searchFor);

            Console.WriteLine("DFS Search is " + search);
            search = DepthFirstSearch.DepthFirstSearchMethod2(root, searchFor);
            Console.WriteLine("DFS2 Search is " + search);
            search = DepthFirstSearch.DepthFirstSearchUsingStacks(root, searchFor);
            Console.WriteLine("DFS Using Stacks Search is " + search);
            search = BreadththFirstSearch.BreadththFirstSearchMethod(root, searchFor);
            Console.WriteLine("BFS Search is " + search);
            Preorder.PreorderTraversal(root);
            Console.WriteLine();
            Inorder.InorderTraversal(root);
            Console.WriteLine();
            Postorder.PostorderTraversal(root);
            Console.ReadLine();
        }
Exemple #2
0
        private static void Main(string[] args)
        {
            Stopwatch stopwatch;

            TextWriter textWriter;

            var initialState = new GridState();

            initialState.Agent = new Position(0, 0);
            initialState.Puzzles.Add(new Tuple <Position, string>(new Position(1, 0), "a"));
            initialState.Puzzles.Add(new Tuple <Position, string>(new Position(1, 1), "b"));
            initialState.Puzzles.Add(new Tuple <Position, string>(new Position(1, 2), "c"));

            var goalState = new GridState();

            goalState.Puzzles.Add(new Tuple <Position, string>(new Position(1, 1), "a"));
            goalState.Puzzles.Add(new Tuple <Position, string>(new Position(2, 1), "b"));
            goalState.Puzzles.Add(new Tuple <Position, string>(new Position(3, 1), "c"));

            var offsets = new List <Tuple <int, int> >
            {
                new Tuple <int, int>(-1, 0),
                new Tuple <int, int>(0, -1),
                new Tuple <int, int>(1, 0),
                new Tuple <int, int>(0, 1)
            };

            var actions = new List <Action>();

            foreach (var offset in offsets)
            {
                var o = offset;

                var action = new Action
                {
                    Cost = s => 1 + s.HeuristicDifference(goalState), //A* heuristic
                    Succ = s => MoveState(s, o)
                };

                actions.Add(action);
            }

            var depthFirstSearch =
                new DepthFirstSearch(initialState, goalState, actions, maxDepth: 16);

            var breadthFirstSearch =
                new BreadthFirstSearch(initialState, goalState, actions, maxDepth: 16);

            var iterativeDeepening1 =
                new IterativeDeepening(initialState, goalState, actions, depthFirstSearch, maxDepth: 16);

            var iterativeDeepening2 =
                new IterativeDeepening(initialState, goalState, actions, breadthFirstSearch, maxDepth: 16);

            var algorythms = new Dictionary <string, Search>()
            {
                { "a-star.txt", new UniformCostSearch(initialState, goalState, actions) },
                //{ "breadth-first-search.txt", breadthFirstSearch },
                //{ "depth-first-search.txt", depthFirstSearch },
                { "iterative-deepening-with-depth-first-search.txt", iterativeDeepening1 },
                //{ "iterative-deepening-with-breadth-first-search.txt", iterativeDeepening2 }
            };

            foreach (var a in algorythms)
            {
                var fileName  = a.Key;
                var algorythm = a.Value;
                //using (var textWriter = Console.Out)
                using (textWriter = File.CreateText(fileName))
                {
                    stopwatch = Stopwatch.StartNew();

                    algorythm.Execute();

                    stopwatch.Stop();

                    Print(textWriter, algorythm.Result);

                    textWriter.WriteLine("Elapsed time: {0}", stopwatch.Elapsed);
                    textWriter.WriteLine("Number of visited states: {0}", algorythm.VisitedStatesCounter);
                    System.Console.WriteLine("the end of " + a.Key);
                }
            }
        }