Пример #1
0
        private static void AStar_0()
        {
            Console.WriteLine("AStar_0");

            Node a = new Node("A");
            Node b = new Node("B");
            Node c = new Node("C");
            Node d = new Node("D");
            Node e = new Node("E");
            Node f = new Node("F");
            Node g = new Node("G");
            Node h = new Node("H");

            a.Edges.Add(new Edge(b, 2.0d));

            b.Edges.Add(new Edge(h, 4.0d));
            b.Edges.Add(new Edge(d, 1.0d));
            b.Edges.Add(new Edge(c, 2.0d));
            b.Edges.Add(new Edge(a, 2.0d));

            c.Edges.Add(new Edge(b, 2.0d));

            d.Edges.Add(new Edge(e, 2.5d));
            d.Edges.Add(new Edge(f, 2.0d));
            d.Edges.Add(new Edge(g, 1.5d));

            GraphProblem problem = new GraphProblem(a, new Node[] { f, h });
            AStarSearch  search  = new AStarSearch();

            State[] results = search.Search(problem);

            Console.WriteLine("Expanded States:");

            for (int index = 0; index < problem.ExpandedStates.Count; index++)
            {
                GraphState state = problem.ExpandedStates[index] as GraphState;
                Console.Write(state.Label);

                if (index != problem.ExpandedStates.Count - 1)
                {
                    Console.Write("->");
                }
            }

            Console.WriteLine("\n");
            Console.WriteLine("Solution from starting state:");

            for (int index = 0; index < results.Length; index++)
            {
                GraphState state = results[index] as GraphState;
                Console.Write(state.Label);

                if (index != results.Length - 1)
                {
                    Console.Write("->");
                }
            }

            Console.WriteLine("\n");
        }
Пример #2
0
        public AStarSearch()
        {
            Board  board      = new Board(5);
            Vector startState = new Vector(0, 4);
            Vector goalState  = new Vector(4, 0);

            _problem = new GraphProblem <Vector>(board, startState, goalState);
        }
        public UninformedSearch()
        {
            UndirectedSparseGraph <String> graph = new UndirectedSparseGraph <String>();

            string[] vertices = new string[] { "a", "b", "c", "d", "e", "f", "g" };

            graph.AddVertices(vertices);

            graph.SetEdge("a", "c", 1);
            graph.SetEdge("a", "d", 1);
            graph.SetEdge("b", "c", 1);
            graph.SetEdge("b", "e", 1);
            graph.SetEdge("d", "g", 1);
            graph.SetEdge("e", "f", 1);
            graph.SetEdge("f", "g", 1);

            _problem = new GraphProblem <string>(graph, "a", "e");

            List <EdgeAction <String> > actionsFromA = _problem.Actions("a").Cast <EdgeAction <String> >().ToList();
            List <EdgeAction <String> > actionsFromB = _problem.Actions("b").Cast <EdgeAction <String> >().ToList();
            List <EdgeAction <String> > actionsFromC = _problem.Actions("c").Cast <EdgeAction <String> >().ToList();
            List <EdgeAction <String> > actionsFromD = _problem.Actions("d").Cast <EdgeAction <String> >().ToList();
            List <EdgeAction <String> > actionsFromE = _problem.Actions("e").Cast <EdgeAction <String> >().ToList();
            List <EdgeAction <String> > actionsFromF = _problem.Actions("f").Cast <EdgeAction <String> >().ToList();
            List <EdgeAction <String> > actionsFromG = _problem.Actions("g").Cast <EdgeAction <String> >().ToList();

            Assert.Equal(actionsFromA, new List <EdgeAction <String> > {
                new EdgeAction <String>(new Edge <String>("a", "c", 1)),
                new EdgeAction <String>(new Edge <String>("a", "d", 1))
            });
            Assert.Equal(actionsFromB, new List <EdgeAction <String> > {
                new EdgeAction <String>(new Edge <String>("b", "c", 1)),
                new EdgeAction <String>(new Edge <String>("b", "e", 1))
            });
            Assert.Equal(actionsFromC, new List <EdgeAction <String> > {
                new EdgeAction <String>(new Edge <String>("c", "a", 1)),
                new EdgeAction <String>(new Edge <String>("c", "b", 1))
            });
            Assert.Equal(actionsFromD, new List <EdgeAction <String> > {
                new EdgeAction <String>(new Edge <String>("d", "a", 1)),
                new EdgeAction <String>(new Edge <String>("d", "g", 1))
            });
            Assert.Equal(actionsFromE, new List <EdgeAction <String> > {
                new EdgeAction <String>(new Edge <String>("e", "b", 1)),
                new EdgeAction <String>(new Edge <String>("e", "f", 1))
            });
            Assert.Equal(actionsFromF, new List <EdgeAction <String> > {
                new EdgeAction <String>(new Edge <String>("f", "e", 1)),
                new EdgeAction <String>(new Edge <String>("f", "g", 1))
            });
            Assert.Equal(actionsFromG, new List <EdgeAction <String> > {
                new EdgeAction <String>(new Edge <String>("g", "d", 1)),
                new EdgeAction <String>(new Edge <String>("g", "f", 1))
            });
        }
Пример #4
0
        private static void AStar_1_Graph_Heuristic()
        {
            Console.WriteLine("AStar_1_Graph_Heuristic");

            Node s = new Node("S");
            Node a = new Node("A");
            Node b = new Node("B");
            Node c = new Node("C");
            Node d = new Node("D");
            Node g = new Node("G");

            s.Edges.Add(new Edge(a, 2.0d));
            s.Edges.Add(new Edge(b, 3.0d));
            s.Edges.Add(new Edge(d, 5.0d));

            a.Edges.Add(new Edge(c, 3.0d));
            a.Edges.Add(new Edge(s, 2.0d));

            b.Edges.Add(new Edge(d, 4.0d));
            b.Edges.Add(new Edge(s, 3.0d));

            c.Edges.Add(new Edge(a, 3.0d));
            c.Edges.Add(new Edge(d, 1.0d));
            c.Edges.Add(new Edge(g, 2.0d));

            d.Edges.Add(new Edge(b, 4.0d));
            d.Edges.Add(new Edge(c, 1.0d));
            d.Edges.Add(new Edge(g, 5.0d));
            d.Edges.Add(new Edge(s, 5.0d));

            Dictionary <int, double> piecewise = new Dictionary <int, double> {
                { s.GetHashCode(), 6.0d },
                { a.GetHashCode(), 2.5d },
                { b.GetHashCode(), 5.25d },
                { c.GetHashCode(), 1.125d },
                { d.GetHashCode(), 1.0625d },
                { g.GetHashCode(), 0d }
            };

            GraphProblem problem = new GraphProblem(s, new Node[] { g });
            AStarSearch  search  = new AStarSearch();

            State[] results = search.Search(problem, new PiecewiseHeuristic <Node>(piecewise));

            Console.WriteLine("Expanded States:");

            for (int index = 0; index < problem.ExpandedStates.Count; index++)
            {
                GraphState state = problem.ExpandedStates[index] as GraphState;
                Console.Write(state.Label);

                if (index != problem.ExpandedStates.Count - 1)
                {
                    Console.Write("->");
                }
            }

            Console.WriteLine("\n");
            Console.WriteLine("Solution from starting state:");

            for (int index = 0; index < results.Length; index++)
            {
                GraphState state = results[index] as GraphState;
                Console.Write(state.Label);

                if (index != results.Length - 1)
                {
                    Console.Write("->");
                }
            }

            Console.WriteLine("\n");
        }