65 lines
1.6 KiB
Python
65 lines
1.6 KiB
Python
|
|
import math
|
|
|
|
|
|
class State:
|
|
|
|
def __init__(self, direction, x, y):
|
|
self.direction = direction # kierunek w ktorym "patrzy wozek"
|
|
self.x = x
|
|
self.y = y
|
|
|
|
def get_direction(self):
|
|
return self.direction
|
|
|
|
def get_x(self):
|
|
return self.x
|
|
|
|
def get_y(self):
|
|
return self.y
|
|
|
|
def goal_test(self, goal): # sprawdza czy osiagnelismy cel
|
|
if self.x == goal[0] and self.y == goal[1]:
|
|
return True
|
|
else:
|
|
return False
|
|
|
|
|
|
class Node:
|
|
def __init__(self, state, walkable):
|
|
self.state = state
|
|
self.direction = state.direction
|
|
self.walkable = walkable
|
|
self.g_cost = 0
|
|
self.h_cost = 0
|
|
self.parent = None
|
|
self.heap_index = 0
|
|
|
|
def get_action(self):
|
|
return self.action
|
|
|
|
def get_direction(self):
|
|
return self.direction
|
|
|
|
def get_parent(self):
|
|
return self.parent
|
|
|
|
def f_cost(self):
|
|
if self.walkable:
|
|
return self.g_cost + self.h_cost
|
|
else:
|
|
# return 0
|
|
return math.inf
|
|
# if fringe[i].f_cost() < current_node.f_cost() or (fringe[i].f_cost() == current_node.f_cost() and fringe[i].h_cost < current_node.h_cost):
|
|
def __lt__(self, other):
|
|
if self.f_cost() == other.f_cost():
|
|
return self.h_cost < other.h_cost
|
|
return self.f_cost() < other.f_cost()
|
|
def __gt__(self,other):
|
|
if self.f_cost() == other.f_cost():
|
|
return self.h_cost > other.h_cost
|
|
return self.f_cost() > other.f_cost()
|
|
def __eq__(self,other):
|
|
return self.f_cost() == other.f_cost() and self.h_cost == other.h_cost
|
|
|