private Path FindPathToCP(PersonBehavior person, Node CPNode)
    {
        personPath = new List <Element>();
        personPath.Add(new Element(person.GetInitNode(), 0, Vector3.Distance(person.GetInitNode().GetPos(), CPNode.GetPos()), null));
        Element actual;
        int     countdown = 100;

        while (countdown > 0)
        {
            countdown--;
            if (personPath.Count < 1)
            {
                Utils.Print("ERROR. Could not find a path to destination " + CPNode.GetData() + " from " + person.GetInitNode().GetData());
                return(null);
            }

            actual = personPath[0];

            // Goal reached
            if (actual.n == CPNode)
            {
                float       fsol;
                List <Node> sol     = new List <Node>();
                Element     solNode = actual;
                sol.Add(solNode.n);
                fsol = solNode.f;
                while (solNode.parent != null)
                {
                    sol.Add(solNode.parent.n);
                    solNode = solNode.parent;
                }
                sol.Reverse();
                return(new Path(person, sol, fsol));
            }

            // Expand node
            personPath.RemoveAt(0);
            foreach (Edge e in actual.n.GetAdjacentEdges())
            {
                Node childNode = e.GetOtherNode(actual.n);

                if (!(actual.parent != null && childNode == actual.parent.n))
                {
                    float   walkedChild    = actual.walked + e.GetDistance();
                    float   remainingChild = Vector3.Distance(CPNode.GetPos(), childNode.GetPos());
                    Element child          = new Element(childNode, walkedChild, remainingChild, actual);
                    personPath.Add(child);
                }
            }

            // Sort list by f value
            personPath.Sort((a, b) => (a.f.CompareTo(b.f)));
        }

        return(null);
    }
Example #2
0
    // This func goes through graph and return a path when the collection point is found
    private Path FindPathToCP(PersonBehavior person, Node CPNode)
    {
        Path  actualPath = null;
        float maxF       = Mathf.NegativeInfinity;

        personPath = new List <Element>(); // Element list - Node Path
        personPath.Add(new Element(person.GetInitNode(), 0, Vector3.Distance(person.GetInitNode().GetPos(), CPNode.GetPos()), null));
        Element actual;
        int     countdown = 3000;

        while (countdown > 0)
        {
            if (personPath.Count < 1 && actualPath != null)
            {
                return(actualPath);
            }
            else if (personPath.Count < 1) // if personPath
            {
                Utils.Print("ERROR. Could not find a path to destination " + CPNode.GetData() + " from " + person.GetInitNode().GetData());
                return(null);
            }

            actual = personPath[0]; // First Element's List
            personPath.RemoveAt(0); // Delete actual Element

            // Goal Reached
            if (actual.n == CPNode)
            {
                // Assign Path Variables
                List <Node> sol     = new List <Node>();
                Element     solNode = actual;
                float       fsol    = solNode.f;
                sol.Add(solNode.n);
                while (solNode.parent != null)
                {
                    sol.Add(solNode.parent.n);
                    solNode = solNode.parent;
                }
                sol.Reverse();   // Start in CPNode -->  Reverse --> Finish in CPNode --> Path
                if (fsol > maxF) // if remaining length path is > actual max length path
                {
                    actualPath = new Path(person, sol, fsol);
                    maxF       = actualPath.f;
                    UnityEngine.Debug.LogWarning("Far ID N" + actual.n.GetID().ToString());
                }
            }

            // Expand node
            foreach (Edge e in actual.n.GetAdjacentEdges())
            {
                Node childNode = e.GetOtherNode(actual.n); // Associate node to actual node
                if (childNode.GetCurrentCapacity() == 0)
                {
                }
                else // If there is capacity
                {
                    if (!(actual.parent != null && childNode == actual.parent.n)) // Avoid simple loops
                    {
                        float   walkedChild    = actual.walked + e.GetDistance();
                        float   remainingChild = Vector3.Distance(CPNode.GetPos(), childNode.GetPos());
                        Element child          = new Element(childNode, walkedChild, remainingChild, actual);
                        personPath.Add(child);
                    }
                }
            }

            // Sort list by f value
            personPath.Sort((a, b) => a.f.CompareTo(b.f));

            countdown--;
        }
        Debug.Log("countdown expired");
        return(actualPath);
    }
Example #3
0
    //private List<Element> personPath;


    public List <Path> FindPaths(Graph graph_, List <PersonBehavior> people_)
    {
        CPNodes = graph_.GetNodes().FindAll(x => x.GetIsCP()); // Return Collection Points Nodes List
        Path path  = null;
        int  count = 0;

        //for (int count = 0; count < people_.Count; count++)
        //{
        if (!people_[count].GetDependent())
        {
            if (people_[count].GetID() == 0)
            {
                //Path(PersonBehavior p_, List < Node > path_, float f_)
                PersonBehavior person     = people_[count];
                List <Node>    personPath = new List <Node>();
                personPath.Add(person.GetInitNode());
                float fperson = 0;

                personPath.Add(graph_.GetNode(2));
                fperson = fperson + person.GetInitNode().ConnectedTo(graph_.GetNode(2)).GetDistance();

                personPath.Add(graph_.GetNode(3));
                fperson = fperson + graph_.GetNode(2).ConnectedTo(graph_.GetNode(3)).GetDistance();

                //personPath.Add(graph_.GetNode(1));
                //fperson = fperson + graph_.GetNode(3).ConnectedTo(graph_.GetNode(1)).GetDistance();


                path = new Path(person, personPath, fperson);
                if (path != null)
                {
                    foundPaths.Add(path);
                }
                else
                {
                    Utils.Print("PERSON W/O PATH");
                }
            }

            count++;
            if (people_[count].GetID() == 1)
            {
                //Path(PersonBehavior p_, List < Node > path_, float f_)
                PersonBehavior person     = people_[count];
                List <Node>    personPath = new List <Node>();
                personPath.Add(person.GetInitNode());
                float fperson = 0;

                personPath.Add(graph_.GetNode(2));
                fperson = fperson + person.GetInitNode().ConnectedTo(graph_.GetNode(2)).GetDistance();

                personPath.Add(graph_.GetNode(3));
                fperson = fperson + graph_.GetNode(2).ConnectedTo(graph_.GetNode(3)).GetDistance();

                personPath.Add(graph_.GetNode(1));
                fperson = fperson + graph_.GetNode(3).ConnectedTo(graph_.GetNode(1)).GetDistance();


                path = new Path(person, personPath, fperson);
                if (path != null)
                {
                    foundPaths.Add(path);
                }
                else
                {
                    Utils.Print("PERSON W/O PATH");
                }
            }
        }


        //}
        return(foundPaths);
    }
    //private List<Element> personPath;


    public List <Path> FindPaths(Graph graph_, List <PersonBehavior> people_)
    {
        List <Path> foundPaths = new List <Path>();

        CPNodes = graph_.GetNodes().FindAll(x => x.GetIsCP()); // Return Collection Points Nodes List
        Path   path  = null;
        int    count = 0;
        string line;
        List <PersonBehavior> peopleInFile = new List <PersonBehavior>();

        System.IO.StreamReader file = new System.IO.StreamReader(Directory.GetCurrentDirectory() + @"\Assets\SavedData\aimedIndicatePerson.txt");
        while ((line = file.ReadLine()) != null)
        {
            string[] nodesLines = line.Split(' ');
            int      personID   = Convert.ToInt32(nodesLines[0]);
            if ((personID >= 0) && (personID < nodesLines.Length))
            {
                peopleInFile.Add(people_[personID]);
                if (people_[personID].GetDependent())
                {
                    count++;
                }
                else if (count < people_.Count)
                {
                    PersonBehavior person     = people_[personID];
                    List <Node>    personPath = new List <Node>();
                    int            lastNodeID = 0;
                    float          fperson    = 0;
                    for (int j = 1; j < nodesLines.Length; j++)
                    //foreach (string nodeL in nodesLines)
                    {
                        if (nodesLines[j].Equals(person.GetInitNode().GetID())) // We See if the second number is the first node
                        {
                            personPath.Add(person.GetInitNode());
                            lastNodeID = person.GetInitNode().GetID();

                            path = new Path(person, personPath, fperson);
                            if (path != null)
                            {
                                foundPaths.Add(path);
                            }
                            else
                            {
                                Utils.Print("PERSON W/O PATH");
                            }
                        }
                        else
                        {
                            if (!nodesLines[j].Equals(" ") && (!nodesLines[j].Equals("")))
                            {
                                int currentlyNode = Convert.ToInt32(nodesLines[j]);
                                if (graph_.GetNodes().Contains(graph_.GetNode(currentlyNode))) // If the node exists
                                {
                                    //if (graph_.GetAdjacentNodes(graph_.GetNode(lastNodeID)).Contains(graph_.GetNode(currentlyNode)))
                                    //{
                                    personPath.Add(graph_.GetNode(currentlyNode));
                                    fperson    = fperson + graph_.GetNode(lastNodeID).ConnectedTo(graph_.GetNode(currentlyNode)).GetDistance();
                                    lastNodeID = currentlyNode;
                                    path       = new Path(person, personPath, fperson);
                                    if (path != null)
                                    {
                                        foundPaths.Add(path);
                                    }
                                    else
                                    {
                                        Utils.Print("PERSON W/O PATH");
                                    }
                                    //}
                                }
                                else
                                {
                                    Debug.LogError("The Node " + currentlyNode + ", line " + count + ", in the txt doesn't exit");
                                }
                            }
                        }
                    }
                    count++;
                }
                else
                {
                    Debug.LogError("More data that persons in txt");
                }
            }
        }
        file.Close();

        int countAllPeople = 0;

        while (count < people_.Count)
        {
            if (peopleInFile.Contains(people_[countAllPeople]))
            {
                countAllPeople++;
            }
            else
            {
                if (people_[countAllPeople].GetDependent())
                {
                    count++;
                }
                else
                {
                    PersonBehavior person     = people_[countAllPeople];
                    List <Node>    personPath = new List <Node>();
                    float          fperson    = 0;
                    personPath.Add(person.GetInitNode());
                    path = new Path(person, personPath, fperson);
                    if (path != null)
                    {
                        foundPaths.Add(path);
                    }
                    else
                    {
                        Utils.Print("PERSON W/O PATH");
                    }
                    count++;
                }
                peopleInFile.Add(people_[countAllPeople]);
                countAllPeople++;
            }
        }

        return(foundPaths);
    }
Example #5
0
    //private List<Element> personPath;


    public List <Path> FindPaths(Graph graph_, List <PersonBehavior> people_)
    {
        List <Path> foundPaths = new List <Path>();

        CPNodes = graph_.GetNodes().FindAll(x => x.GetIsCP()); // Return Collection Points Nodes List
        Path   path  = null;
        int    count = 0;
        string line;

        System.IO.StreamReader file = new System.IO.StreamReader(Directory.GetCurrentDirectory() + @"\Assets\SavedData\aimed.txt");
        while ((line = file.ReadLine()) != null)
        {
            if (people_[count].GetDependent())
            {
                count++;
            }
            else if (count < people_.Count)
            {
                string[]       nodesLines = line.Split(' ');
                PersonBehavior person     = people_[count];
                List <Node>    personPath = new List <Node>();
                int            lastNodeID = 0;
                float          fperson    = 0;
                foreach (string nodeL in nodesLines)
                {
                    if (nodeL.Equals(person.GetInitNode().GetID()))
                    {
                        personPath.Add(person.GetInitNode());
                        lastNodeID = person.GetInitNode().GetID();

                        path = new Path(person, personPath, fperson);
                        if (path != null)
                        {
                            foundPaths.Add(path);
                        }
                        else
                        {
                            Utils.Print("PERSON W/O PATH");
                        }
                    }
                    else
                    {
                        if (!nodeL.Equals(" ") && (!nodeL.Equals("")))
                        {
                            int currentlyNode = Convert.ToInt32(nodeL);
                            //if (graph_.GetAdjacentNodes(graph_.GetNode(lastNodeID)).Contains(graph_.GetNode(currentlyNode)))
                            //{
                            personPath.Add(graph_.GetNode(currentlyNode));
                            fperson    = fperson + graph_.GetNode(lastNodeID).ConnectedTo(graph_.GetNode(currentlyNode)).GetDistance();
                            lastNodeID = currentlyNode;
                            path       = new Path(person, personPath, fperson);
                            if (path != null)
                            {
                                foundPaths.Add(path);
                            }
                            else
                            {
                                Utils.Print("PERSON W/O PATH");
                            }
                            //}
                        }
                    }
                }
                count++;
            }
            else
            {
                Debug.LogError("More data that persons in txt");
            }
        }
        file.Close();

        while (count < people_.Count)
        {
            if (people_[count].GetDependent())
            {
                count++;
            }
            else
            {
                PersonBehavior person     = people_[count];
                List <Node>    personPath = new List <Node>();
                float          fperson    = 0;
                personPath.Add(person.GetInitNode());
                path = new Path(person, personPath, fperson);
                if (path != null)
                {
                    foundPaths.Add(path);
                }
                else
                {
                    Utils.Print("PERSON W/O PATH");
                }
                count++;
            }
        }

        return(foundPaths);
    }