// Use this for initialization
    void Start()
    {
        // Testing graph functions
        // Reading
        Graph g = Graph.readFromFile("Assets/Graphs/graph-salesman.txt");

        // Showing
        Debug.Log(g.toString());

        /*
         * // Dijkstra
         * PathVertexInfo dijResult = g.dijkstra( 0, 3 );
         * // Distance obtained
         * Debug.Log( "Distance to target: "+dijResult.DistanceToVertex );
         * // Showing the path found
         * List<PathVertexInfo> path = dijResult.pathTo();
         */

        // Floyd Warshall
        g.floydWarshall();
        float floResult = g.getFloydWarshallDistance(2, 3);
        List <PathVertexInfo> floPathResult = g.pathToFloydWarshall(2, 3);

        // Distance obtained
        Debug.Log("Distance to target: " + floResult);
        // Showing the path found
        Debug.Log(PathVertexInfo.pathToString(floPathResult));
    }
Example #2
0
    // Calculates the smallest path between Origin and Target
    public PathVertexInfo dijkstra(int vertexOriginIndex, int vertexTargetIndex)
    {
        //Debug.Log(this.toString());
        // Check if the arguments are valid vertices
        if (this.isValidVertexIndex(vertexOriginIndex) || this.isValidVertexIndex(vertexTargetIndex))
        {
            // Create list of information necessary to the algorithm
            Dictionary <int, PathVertexInfo> info = new Dictionary <int, PathVertexInfo>();
            foreach (VertexInfo vertex in this.vertices)
            {
                info.Add(vertex.VertexIndex, new PathVertexInfo(vertex, float.MaxValue));
            }
            // Initialize the list of neighbors
            List <PathVertexInfo> open = new List <PathVertexInfo>();

            // First Element
            info[vertexOriginIndex].DistanceToVertex = this.vertices[vertexOriginIndex].VertexCost;
            open.Add(info[vertexOriginIndex]);

            PathVertexInfo currentVertex = this.getSmallest(open);

            // Calculate the neighbors
            while (currentVertex != null && currentVertex.VertexIndex != vertexTargetIndex)
            {
                // Informing that this vertex was processed
                currentVertex.Visited = true;

                // Inserting Neighbors
                foreach (Adjacent neighbor in this.adjacency[currentVertex.VertexIndex])
                {
                    if (!info[neighbor.index].Visited)
                    {
                        PathVertexInfo neighborVertex = info[neighbor.index];
                        float          newDistance    = currentVertex.DistanceToVertex + neighbor.edgeWeight + neighborVertex.VertexCost;
                        if (neighborVertex.DistanceToVertex > newDistance)
                        {
                            neighborVertex.DistanceToVertex = newDistance;
                            neighborVertex.PreviousVertex   = currentVertex;
                            //neighborVertex.CostFromPrevious = neighbor.edgeWeight;
                        }
                        // Add neighbor
                        if (!vertexAlreadyInList(neighbor.index, open))
                        {
                            open.Add(neighborVertex);
                        }
                    }
                }

                // Updating current
                currentVertex = this.getSmallest(open);
            }

            // Result
            if (currentVertex != null && currentVertex.VertexIndex == vertexTargetIndex)
            {
                return(info[vertexTargetIndex]);
            }
        }
        return(null);
    }
    // Use this for initialization
    void Start()
    {
        // Testing graph functions
        // Reading
        Graph g = Graph.readFromFile("Assets/Graphs/graph2.txt");

        // Showing
        Debug.Log(g.toString());
        // A*
        PathVertexInfo aResult = g.aStar(0, 3);

        // Distance obtained by the A*
        Debug.Log("Distance to target: " + aResult.DistanceToVertex);
        // Showing the path found by A*
        List <PathVertexInfo> path = aResult.pathTo();
        string resPath             = "";

        foreach (PathVertexInfo v in path)
        {
            resPath += v.VertexIndex + " -> ";
        }
        Debug.Log(resPath);

        Map m = Map.readFromFile("Assets/Maps/map1.txt");

        Debug.Log(m.toString());
        //Debug.Log (m.getGraph.print());
    }
Example #4
0
    private bool visited;                       // Indicates if the vertex was processed by the algorithms

    public PathVertexInfo(VertexInfo vertex, float distanceToVertex = 0)
    {
        this.vertex         = vertex;
        this.previousVertex = null;
        //this.costFromPrevious = 0;
        this.distanceToVertex = distanceToVertex;
        this.visited          = false;
    }
Example #5
0
    // Obtains the path to achieve the vertex
    public List <PathVertexInfo> pathTo()
    {
        List <PathVertexInfo> result  = new List <PathVertexInfo>();
        PathVertexInfo        current = this;

        while (current != null)
        {
            result.Add(current);
            current = current.previousVertex;
        }

        return(result);
    }
Example #6
0
 public List <PathVertexInfo> findPathDijkstra(Character enemy)
 {
     if (this.players.Count > 0)
     {
         PathVertexInfo targetPathInfo = this.graph.dijkstra(
             this.graphIndexFromTile(this.getCharacterPositionX(enemy), this.getCharacterPositionY(enemy)),
             this.graphIndexFromTile(this.getCharacterPositionX(this.players[0]), this.getCharacterPositionY(this.players[0])));
         if (targetPathInfo != null)
         {
             return(targetPathInfo.pathTo());
         }
     }
     return(null);
 }
Example #7
0
 // Dijkstra : Obtains the smallest distance in the list (remove it from the list)
 private PathVertexInfo getSmallest(List <PathVertexInfo> list)
 {
     if (list.Count > 0)
     {
         int resultIndex = -1;
         for (int i = 0; i < list.Count; i++)
         {
             if (resultIndex == -1 || list[resultIndex].DistanceToVertex > list[i].DistanceToVertex)
             {
                 resultIndex = i;
             }
         }
         if (resultIndex > -1)
         {
             PathVertexInfo result = list[resultIndex];
             list.RemoveAt(resultIndex);
             return(result);
         }
     }
     return(null);
 }
Example #8
0
    // Obtains the path to achieve the vertex (based on FloydWarshall)
    public List <PathVertexInfo> pathToFloydWarshall(int indexStart, int indexEnd)
    {
        List <PathVertexInfo> result = new List <PathVertexInfo>();

        if (this.allDistances != null)
        {
            PathVertexInfo current = this.allDistances [indexStart, indexEnd];

            while (current != null)
            {
                result.Add(current);
                if (current.PreviousVertex != null)
                {
                    current = allDistances [indexStart, current.PreviousVertex.Vertex.VertexIndex];
                }
                else
                {
                    current = null;
                }
            }
        }

        return(result);
    }