Example #1
0
        public void TwoPathTest()
        {   /*
             * A-B-C-D
             * E     F
             * G-H-I-J
             */
            var graph = new BidirectionalGraph <string, SimpleWeightedEdge>();

            graph.AddNodes("A", "B", "C", "D", "E", "F", "G", "H", "I", "J");
            graph.AddEdge("A", "B", new SimpleWeightedEdge(1));
            graph.AddEdge("B", "C", new SimpleWeightedEdge(1));
            graph.AddEdge("C", "D", new SimpleWeightedEdge(1));
            graph.AddEdge("A", "E", new SimpleWeightedEdge(1));
            graph.AddEdge("E", "G", new SimpleWeightedEdge(1));
            graph.AddEdge("D", "F", new SimpleWeightedEdge(1));
            graph.AddEdge("F", "J", new SimpleWeightedEdge(1));
            graph.AddEdge("G", "H", new SimpleWeightedEdge(1));
            graph.AddEdge("H", "I", new SimpleWeightedEdge(1));
            graph.AddEdge("J", "I", new SimpleWeightedEdge(1));

            var algorithm = new AStar <string, SimpleWeightedEdge>(graph, (u, v) => 1);
            var result    = algorithm.Compute("B", "H").ToList();

            Assert.AreEqual(5, result.Count);
            Assert.AreEqual("B", result[0]);
            Assert.AreEqual("A", result[1]);
            Assert.AreEqual("E", result[2]);
            Assert.AreEqual("G", result[3]);
            Assert.AreEqual("H", result[4]);
        }
Example #2
0
        public void IntGraphTest()
        {
            TestWeightedGraph graph = new TestWeightedGraph(1, 10);

            graph.AddNode(1);
            var algorithm = new AStar <int, SimpleWeightedEdge>(graph, (u, v) => 1);
            var xx        = algorithm.Compute(1, 10).ToList();
        }
Example #3
0
        public void Test()
        {
            var nodeA = new Node(new VecN(0, 0), true, 5);
            var nodeB = new Node(new VecN(30, -30), true, 2);
            var nodeC = new Node(new VecN(-30, -30), true, 3);

            nodeA.ConnectedNodes = new[] { nodeB, nodeC };
            nodeB.ConnectedNodes = new[] { nodeA };
            nodeC.ConnectedNodes = new[] { nodeA };

            var quickestRoute = AStar.Compute(nodeA, nodeB);

            System.Console.WriteLine();
        }
Example #4
0
        public List <TState> Solve()
        {
            var astar = new AStar <TState, TTransition>(_graph, Problem.GetHeuristicDistance);

            ResetStatistics();

            astar.ENodeClosed += OnNodeClosed;

            var result = astar.Compute(InitialNode, Goal);

            Statistics.SolutionSize = result.Count();

            return(result);
        }
Example #5
0
        public static void ComputePathsDangerValues(List <Path> paths, Enemy[] enemies, Vector3 min, float tileSizeX, float tileSizeZ, Cell[][][] fullMap, float[][] dangerCells, float maxDanger)
        {
            AStar astar = new AStar();

            foreach (Path currentPath in paths)
            {
                if (currentPath.length3d == 0)
                {
                    currentPath.length3d = ComputeLength3D(currentPath.points);
                }

                Node cur = currentPath.points [currentPath.points.Count - 1];
                Node par = cur.parent;
                while (cur.parent != null)
                {
                    Vector3 p1 = cur.GetVector3();
                    Vector3 p2 = par.GetVector3();
                    Vector3 pd = p1 - p2;

                    float pt = (cur.t - par.t);

                    // Navigate through time to find the right cells to start from
                    for (int t = 0; t < pt; t++)
                    {
                        float delta = ((float)t) / pt;

                        Vector3 pos = p2 + pd * delta;

                        foreach (Enemy each in enemies)
                        {
                            Vector3 enemy = each.positions [par.t + t];

                            int startX = (int)pos.x;
                            int startY = (int)pos.z;
                            int endX   = (int)((enemy.x - min.x) / tileSizeX);
                            int endY   = (int)((enemy.z - min.z) / tileSizeZ);

                            // Do A* from the player to the enemy to compute the cost
                            List <Node> astarpath = astar.Compute(startX, startY, endX, endY, fullMap [par.t + t], true);
                            if (astarpath.Count > 0)
                            {
                                float l = ComputeLength3D(astarpath);

                                pathMap [currentPath] [(int)Heuristic.Danger] [par.t + t]      = 1 / (l * l);
                                pathMap [currentPath] [(int)Heuristic.Danger3] [par.t + t]     = 1 / (l * l * l);
                                pathMap [currentPath] [(int)Heuristic.Danger3Norm] [par.t + t] = (1 / (l * l * l)) * (dangerCells [startX] [startY] / maxDanger);

                                currentPath.danger      += pathMap [currentPath] [(int)Heuristic.Danger] [par.t + t];
                                currentPath.danger3     += pathMap [currentPath] [(int)Heuristic.Danger3] [par.t + t];
                                currentPath.danger3Norm += pathMap [currentPath] [(int)Heuristic.Danger3Norm] [par.t + t];
                            }
                        }
                    }

                    cur = par;
                    par = par.parent;
                }
                currentPath.danger      /= currentPath.length3d;
                currentPath.danger3     /= currentPath.length3d;
                currentPath.danger3Norm /= currentPath.length3d;
            }
        }