示例#1
0
 def h(self, node):
     """h function is straight-line distance from a node's state to goal."""
     locs = getattr(self.graph, 'locations', None)
     if locs:
         return int(distance(locs[node.state], locs[self.goal]))
     else:
         return infinity
示例#2
0
def RandomGraph(nodes=list(range(10)),
                min_links=2,
                width=400,
                height=300,
                curvature=lambda: random.uniform(1.1, 1.5)):
    """Construct a random graph, with the specified nodes, and random links.
    The nodes are laid out randomly on a (width x height) rectangle.
    Then each node is connected to the min_links nearest neighbors.
    Because inverse links are added, some nodes will have more connections.
    The distance between nodes is the hypotenuse times curvature(),
    where curvature() defaults to a random number between 1.1 and 1.5."""
    g = UndirectedGraph()
    g.locations = {}
    # Build the cities
    for node in nodes:
        g.locations[node] = (random.randrange(width), random.randrange(height))
    # Build roads from each city to at least min_links nearest neighbors.
    for i in range(min_links):
        for node in nodes:
            if len(g.get(node)) < min_links:
                here = g.locations[node]

                def distance_to_node(n):
                    if n is node or g.get(node, n):
                        return infinity
                    return distance(g.locations[n], here)

                neighbor = argmin(nodes, key=distance_to_node)
                d = distance(g.locations[neighbor], here) * curvature()
                g.connect(node, neighbor, int(d))
    return g
示例#3
0
 def h(self, node):
     "h function is straight-line distance from a node's state to goal."
     locs = getattr(self.graph, 'locations', None)
     if locs:
         return int(distance(locs[node.state], locs[self.goal]))
     else:
         return infinity
示例#4
0
def RandomGraph(nodes=list(range(10)), min_links=2, width=400, height=300,
                curvature=lambda: random.uniform(1.1, 1.5)):
    """Construct a random graph, with the specified nodes, and random links.
    The nodes are laid out randomly on a (width x height) rectangle.
    Then each node is connected to the min_links nearest neighbors.
    Because inverse links are added, some nodes will have more connections.
    The distance between nodes is the hypotenuse times curvature(),
    where curvature() defaults to a random number between 1.1 and 1.5."""
    g = UndirectedGraph()
    g.locations = {}
    # Build the cities
    for node in nodes:
        g.locations[node] = (random.randrange(width), random.randrange(height))
    # Build roads from each city to at least min_links nearest neighbors.
    for i in range(min_links):
        for node in nodes:
            if len(g.get(node)) < min_links:
                here = g.locations[node]

                def distance_to_node(n):
                    if n is node or g.get(node, n):
                        return infinity
                    return distance(g.locations[n], here)
                neighbor = argmin(nodes, key=distance_to_node)
                d = distance(g.locations[neighbor], here) * curvature()
                g.connect(node, neighbor, int(d))
    return g
示例#5
0
 def h(self, node):
     "h function is straight-line distance from a node's state to goal."
     locs = getattr(self.graph, 'locations', None)
     if locs:
         # changed to observe heuristics
         # return int(distance(locs[node.state], locs[self.goal]))
         d2g = distance(locs[node.state], locs[self.goal])
         return d2g
     else:
         return infinity
示例#6
0
 def h(self, node):
     "h function is straight-line distance from a node's state to goal."
     locs = getattr(self.graph, 'locations', None)
     if locs:
         # changed to observe heuristics
         # return int(distance(locs[node.state], locs[self.goal]))
         d2g = distance(locs[node.state], locs[self.goal])
         return d2g
     else:
         return infinity
 def value(self,state):
     #list_temp=self.actions(node)
     #print(list_temp)
     #return list_temp
     "h function is straight-line distance from a node's state to goal."
     locs = getattr(self.graph, 'locations', None)
     if locs:
         #print (int(distance(locs[state], locs[self.goal])))
         #print (state)
         return int(distance(locs[state], locs[self.goal]))
     else:
         return infinity
示例#8
0
 def validate(self, state):
     if "Move" not in self.element["abilities"]:
         logger.debug("%s does not have the ability to Move")
         return False
     if grid.distance(self.target, state.find(self.element)) != 2:
         return False
     if self.target not in grid.VALID_CELLS:
         return False
     if self.target in state:
         logger.debug("%s tried to move to %s, which is occupied by %s",
                      self.element, self.target, state[self.target])
         return False
     return True
示例#9
0
def A(matrix, start, goal, weight, H):
    fringe = []
    closed = set()
    cost = 0
    nodeStart = Node(start, start)
    nodeStart.W = weight
    fringe.append(nodeStart)
    while fringe:
        fringe.sort()
        node = fringe.pop(0)
        if node not in closed:
            closed.add(node.position)
            if node.position == goal:
                path = []
                while node != nodeStart:
                    mem = sys.getsizeof(fringe) + sys.getsizeof(closed)
                    path.append(node.position)
                    mX, mY = node.position
                    node = node.parentNode
                    cost += getCostA(matrix, node, Node(0, (mX, mY)))
                return path, cost, mem, len(closed)
            n = getNeighborsA(node, closed)
            for x in n:
                if x not in closed:
                    mX, mY = x
                    if matrix[mX][mY] != "0":
                        suc = Node(node, x)
                        suc.W = weight
                        nodeCost = getCostA(matrix, node, suc)
                        if H == "D":
                            suc.H = optimalHeuristic(suc, closed, goal)
                        elif H == "N":
                            suc.H = math.trunc(distance((suc.position, goal)))
                        if suc not in fringe:
                            suc.G = sys.maxsize
                        if node.G + nodeCost < suc.G:
                            suc.G = node.G + nodeCost
                            suc.parentNode = node
                            suc.cost = suc.G + suc.W * suc.H
                            if suc in fringe:
                                fringe.remove(suc)
                            fringe.append(suc)
    return None
示例#10
0
 def distance_to_node(n):
     if n is node or g.get(node, n):
         return infinity
     return distance(g.locations[n], here)
示例#11
0
 def h(self, node):
     state = node.state
     coor1 = self.location[state]
     coor2 = self.location[self.goal]
     return distance(coor1, coor2)
示例#12
0
 def h(self, node):
     state = node.state
     loc1 = self.locations[state]
     loc2 = self.locations[self.finish]
     return distance(loc1, loc2)
示例#13
0
 def distance_to_node(n):
     if n is node or g.get(node, n):
         return infinity
     return distance(g.locations[n], here)
示例#14
0
 def h(self, node):
     state = node.state
     coor1 = self.location[state]
     coor2 = self.location[self.goal]
     return distance(coor1,coor2)
示例#15
0
 def h(self, node):
     state = node.state
     loc1 = self.locations[state]
     loc2 = self.locations[self.finish]
     return distance(loc1, loc2)