Reputation: 115
This code is supposed to calculate the shortest Manhattan distance between a node state (character position) and the nearest food location.
state = ((x, y), ("some status"))
food_coords = [(x, y), (x, y), (x, y)]
note: (x,y) are some coordinates in a grid
But, when the get_manhattan_distance(pos, food_pos)
executes, I get the following error:
TypeError: GridProblem.get_manhattan_distance() takes 2 positional arguments but 3 were given
note: when this function is called, the character (state position) and the food position are on the same grid location.
# helper function to calculate the manhattan distance
def get_manhattan_distance(p, q):
distance = 0
for p_i,q_i in zip(p,q):
distance += abs(p_i - q_i)
return distance
# heuristic = Manhattan distance
def h(self, node):
if self.is_goal(node.state):
return 0
pos = node.state[0] #current position (x, y)
x_coord = node.state[0][0]
y_coord = node.state[0][1]
distances = []
for food_pos in self.food_coords:
print('pos=',pos)
print('food_pos=',pos)
distances.append(self.get_manhattan_distance(pos, food_pos))
distances.sort()
return distances[0]
Upvotes: 2
Views: 15224
Reputation: 153
Every method of a class, receives its object as input. That is what "self" parameter in the method definition refers to. Just add self parameter to your get_manhattan_distance method definition and the problem will be gone. like this:
get_manhattan_distance(self, p, q):
Upvotes: 4