コード例 #1
0
    public List <T> Run(T start, Satisfies satisfies, GetNeighbours getNeighbours, int watchdog = 50)
    {
        //Creamos un linkeo de hijos-padres
        Dictionary <T, T> parents = new Dictionary <T, T>();
        //Creamos una cola (Una fila, en donde el primero que ingresa es el primero en salir)
        Stack <T> pending = new Stack <T>();
        //Creamos coleccion para tener todos los nodos que ya fueron visitados
        HashSet <T> visited = new HashSet <T>();

        //Agregamos el nodo start a pendientes
        pending.Push(start);
        while (pending.Count != 0)
        {
            watchdog--;
            if (watchdog <= 0)
            {
                return(new List <T>());
            }
            //Sacame el primero de pendientes
            T current = pending.Pop();
            //Compurevo si ese nodo me satisface
            if (satisfies(current))
            {
                List <T> path = new List <T>();
                path.Add(current);
                //Mientras que parents contenga la key del ultimo item del path
                while (parents.ContainsKey(path[path.Count - 1]))
                {
                    //se va a agregar al path el padre del ultimo item del path
                    var lastNode = path[path.Count - 1];
                    path.Add(parents[lastNode]);
                }
                //Damos vuelta la coleccion
                path.Reverse();
                //La retornamos
                return(path);
            }
            //Agregamos ese nodo a visitados
            visited.Add(current);
            //Obtenemos sus vecinos
            List <T> neighbours = getNeighbours(current);
            for (int i = 0; i < neighbours.Count; i++)
            {
                T item = neighbours[i];
                if (visited.Contains(item) || pending.Contains(item))
                {
                    continue;
                }
                else
                {
                    //Agregamos sus vecinos en pendientes
                    pending.Push(item);
                    //Realizamos el linkeo de sus vecinos con el nodo
                    parents[item] = current;
                }
            }
        }
        return(new List <T>());
    }
コード例 #2
0
ファイル: Theta.cs プロジェクト: UltraLuke/Final-IA1-AM2
    public List <T> Run(T start, Satisfies satisfies, GetNeighbours getNeighbours, GetCost getcost, Heuristic heuristic, InSight inSight, int watchDong = 500)
    {
        Dictionary <T, float> cost    = new Dictionary <T, float>();
        Dictionary <T, T>     parents = new Dictionary <T, T>();
        PriorityQueue <T>     pending = new PriorityQueue <T>();
        HashSet <T>           visited = new HashSet <T>();

        pending.Enqueue(start, 0);
        cost.Add(start, 0);
        while (!pending.IsEmpty)
        {
            T current = pending.Dequeue();
            watchDong--;
            if (watchDong <= 0)
            {
                return(new List <T>());
            }
            if (satisfies(current))
            {
                return(ConstructPath(current, parents));
            }
            visited.Add(current);
            List <T> neighbours = getNeighbours(current);
            for (int i = 0; i < neighbours.Count; i++)
            {
                T node = neighbours[i];
                if (visited.Contains(node))
                {
                    continue;
                }
                T currParent;
                if (parents.ContainsKey(current) && inSight(parents[current], node))
                {
                    currParent = parents[current];
                }
                else
                {
                    currParent = current;
                }
                float nodeCost  = getcost(currParent, node);
                float totalCost = cost[currParent] + nodeCost;
                if (cost.ContainsKey(node) && cost[node] < totalCost)
                {
                    continue;
                }
                cost[node]    = totalCost;
                parents[node] = currParent;
                pending.Enqueue(node, totalCost + heuristic(node));
            }
        }
        return(new List <T>());
    }
コード例 #3
0
    public List <T> Run(T start, Satisfies satisfies, GetNeighbours getNeighbours, GetCost getCost, Heuristic heuristic, int watchdog = 100)
    {
        Dictionary <T, T>     parents = new Dictionary <T, T>();
        PriorityQueue <T>     pending = new PriorityQueue <T>();
        Dictionary <T, float> cost    = new Dictionary <T, float>();
        HashSet <T>           visited = new HashSet <T>();

        pending.Enqueue(start, 0);
        cost.Add(start, 0);

        while (!pending.IsEmpty)
        {
            watchdog--;
            if (watchdog <= 0)
            {
                return(new List <T>());
            }

            T current = pending.Dequeue();
            if (satisfies(current))
            {
                return(ConstructPath(current, parents));
            }

            visited.Add(current);
            List <T> neighbours = getNeighbours(current);
            for (int i = 0; i < neighbours.Count; i++)
            {
                var item = neighbours[i];
                if (visited.Contains(item))
                {
                    continue;
                }

                float totalCost = cost[current] + getCost(current, item);
                if (cost.ContainsKey(item) && cost[item] < totalCost)
                {
                    continue;
                }

                cost[item]    = totalCost;
                parents[item] = current;
                pending.Enqueue(item, totalCost + heuristic(item));
            }
        }
        return(new List <T>());
    }