Exemplo n.º 1
0
    public List <Node> CalculatePath(Satellite start, Satellite end)
    {
        List <Node> path    = new List <Node>();
        List <Node> visited = new List <Node>();

        int targetId = end.GetInstanceID();

        Node current = new Node {
            Weight = 0, Satellite = start
        };

        if (targetId == start.GetInstanceID())
        {
            // no path needed start node is end node
            return(new List <Node>());
        }

        // Hill climb algorithm
        while (true)
        {
            visited.Add(current);
            var possibleNodes = current.Satellite.ReachableSatellites(end);

            // Check if ID is within possible nodes
            Node possibleEnd = possibleNodes.Find(x => x.Satellite.Id == current.Satellite.Id);
            // Reached end
            if (possibleEnd != null)
            {
                current = possibleEnd;
                path.Add(current);
                break;
            }
            else
            {
                // Get best Node depending on current weight
                Node bestNode = possibleNodes.OrderBy(x => x.Weight).Except(visited).First();

                // Path leads nowhere
                if (bestNode == null)
                {
                    break;
                }

                // Set current as new node
                current = bestNode;
                path.Add(current);
            }
        }

        return(path);
    }