コード例 #1
0
ファイル: BreadthFirst.cs プロジェクト: zr0n/AI_Study
        // Start is called before the first frame update
        void Start()
        {
            jsonParsed = JsonUtility.FromJson <MovieList>(jsonString).movies;

            foreach (Movie movie in jsonParsed)
            {
                BreadthFirstNode movieNode = new BreadthFirstNode(movie.title);
                foreach (string actor in movie.cast)
                {
                    if (!nodesClassified.ContainsKey(actor))
                    {
                        nodesClassified[actor] = new BreadthFirstNode(actor);
                    }
                    nodesClassified[actor].Connect(movieNode);
                }
                nodesClassified[movie.title] = movieNode;
            }
            var    shortestPath = new ShortestPath(nodesClassified);
            string searchResult = shortestPath.SetStart(from).SetEnd(to).Search();

            Debug.Log(searchResult);
        }
コード例 #2
0
 public void Connect(BreadthFirstNode edge)
 {
     edges.Add(edge);
     edge.edges.Add(this);
 }
コード例 #3
0
ファイル: BreadthFirst.cs プロジェクト: zr0n/AI_Study
        public string Search()
        {
            Debug.Log("Searching nearest connection between " + start.value + " and " + end.value);

            string connections = start.value + " - " + end.value;

            List <BreadthFirstNode> queue = new List <BreadthFirstNode> {
                start
            };

            if (start.value == end.value)
            {
                return("Found - " + connections);
            }
            connections = "";
            int i = 0;

            while (queue.Count > 0)
            {
                i++;
                current = queue[0];
                queue.RemoveAt(0);

                connections += current.value + " - ";
                if (current.value == end.value)
                {
                    connections = connections.Substring(0, connections.Length - 3);
                    connections = "Found at iteration #" + i + " " + connections;

                    Debug.Log("Connections: " + connections);
                    string        pathStr = "";
                    List <string> path    = new List <string>();

                    var currentPathValue = end;
                    while (currentPathValue != null)
                    {
                        path.Add(currentPathValue.value);
                        currentPathValue = currentPathValue.parent;
                    }
                    for (int j = path.Count - 1; j >= 0; j--)
                    {
                        pathStr += path[j];
                        if (j > 0)
                        {
                            pathStr += " --> ";
                        }
                    }

                    return(pathStr);
                }

                foreach (var edge in current.edges)
                {
                    if (!edge.wasSearched)
                    {
                        edge.wasSearched = true;
                        edge.parent      = current;
                        queue.Add(edge);
                    }
                }
            }
            return("Not Found " + end.value);
        }