Compare commits

...

1 Commits

Author SHA1 Message Date
d693ff4518 Update 'board' 2022-04-29 07:27:14 +02:00

70
board
View File

@ -159,6 +159,7 @@ def kb_listen(objectArray, gridLength):
#moje #moje
def manhattan(node1, node2): def manhattan(node1, node2):
x1, y1 = node1.state[0], node1.state[1] x1, y1 = node1.state[0], node1.state[1]
x2, y2 = node2.state[0], node2.state[1] x2, y2 = node2.state[0], node2.state[1]
@ -166,6 +167,19 @@ def manhattan(node1, node2):
return distance return distance
class Node: #prawie jak Field w bfs
def __init__(self, state, parent, action):
self.state = state #position - (x, y, direction)
self.parent = parent
self.action = action
def __eq__(self, other):
return True
def __lt__(self, other):
return True
def f(state):#tablica z losowymi wagami(kosztami) pól, w astar trzeba zsumować wagę pola z heurystyką - f + manhattan def f(state):#tablica z losowymi wagami(kosztami) pól, w astar trzeba zsumować wagę pola z heurystyką - f + manhattan
weights = np.array([ weights = np.array([
[1, 2, 1, 4, 5, 2, 7, 8, 1, 4, 1, 3, 4, 5, 8], [1, 2, 1, 4, 5, 2, 7, 8, 1, 4, 1, 3, 4, 5, 8],
@ -176,7 +190,7 @@ def f(state):#tablica z losowymi wagami(kosztami) pól, w astar trzeba zsumować
[1, 2, 1, 4, 5, 2, 7, 8, 1, 4, 1, 3, 4, 5, 6], [1, 2, 1, 4, 5, 2, 7, 8, 1, 4, 1, 3, 4, 5, 6],
[1, 2, 1, 4, 5, 2, 7, 8, 1, 4, 1, 3, 4, 5, 7], [1, 2, 1, 4, 5, 2, 7, 8, 1, 4, 1, 3, 4, 5, 7],
[1, 2, 1, 4, 5, 2, 7, 8, 1, 4, 1, 3, 4, 5, 1], [1, 2, 1, 4, 5, 2, 7, 8, 1, 4, 1, 3, 4, 5, 1],
[1, 2, 1, 4, 5, 2, 7, 8, 1, 4, 1, 3, 4, 5, 3], [9, 2, 1, 4, 5, 2, 7, 8, 1, 4, 1, 3, 4, 5, 3],
[1, 2, 1, 4, 5, 2, 7, 8, 1, 4, 1, 3, 4, 5, 2], [1, 2, 1, 4, 5, 2, 7, 8, 1, 4, 1, 3, 4, 5, 2],
[1, 2, 1, 4, 5, 2, 7, 8, 1, 4, 1, 3, 4, 5, 6], [1, 2, 1, 4, 5, 2, 7, 8, 1, 4, 1, 3, 4, 5, 6],
[1, 2, 1, 4, 5, 2, 7, 8, 1, 4, 1, 3, 4, 5, 7], [1, 2, 1, 4, 5, 2, 7, 8, 1, 4, 1, 3, 4, 5, 7],
@ -186,29 +200,22 @@ def f(state):#tablica z losowymi wagami(kosztami) pól, w astar trzeba zsumować
]) ])
pos_x = state[0] pos_x = state[0]
pos_y = state[1] pos_y = state[1]
#print(weights[pos_x][pos_y])
return weights[pos_x][pos_y] return weights[pos_x][pos_y]
class Node: #prawie jak Field w bfs
def __init__(self, state, parent, action):
self.state = state #position - (x, y, direction)
self.parent = parent
self.action = action
def succ1(state): def succ1(state):
successors = []#-90 obrót w lewo, +90 obrót w prawo successors = []#-90 obrót w lewo, +90 obrót w prawo
print(state[0])#operujemy na 0, 90, 180, 270 print(state[0])#operujemy na 0, 90, 180, 270
print(state[1]) print(state[1])
right = state[2] + 90 right = state[2] + 90
successors.append((("turn", "right"), (state[0], state[1], right)))
left = state[2] - 90 left = state[2] - 90
successors.append((("turn", "left"), (state[0], state[1], left))) if right == 360:
if state[2] == 360: right = 0
state[2] = 0 successors.append((("turn right"), (state[0], state[1], right)))
if state[2] == -90: if left == -90:
state[2] = 270 left = 270
successors.append((("turn left"), (state[0], state[1], left)))
if (state[0], state[1]) not in black_list:#działa if (state[0], state[1]) not in black_list:#działa
if state[2] == 0 and state[0] < 14: if state[2] == 0 and state[0] < 14:
new_x = state[0]+1 new_x = state[0]+1
@ -228,8 +235,10 @@ def succ1(state):
def algorithm(): def algorithm():
opened = PriorityQueue()#może być też lista opened = PriorityQueue()#może być też lista
closed = []#już odwiedzone, odrzucone wierzchołki closed = []#już odwiedzone, odrzucone wierzchołki
first_state = (0, 0, "Right")#x, y, kierunek list_of_actions = []
final_state = (14, 14, "Right")
first_state = (0, 0, 0)#x, y, kierunek
final_state = (14, 14, 0)
starting_point = Node(first_state, False, False) starting_point = Node(first_state, False, False)
ending_point = Node(final_state, False, False) ending_point = Node(final_state, False, False)
@ -238,16 +247,30 @@ def algorithm():
opened.put((1, starting_point)) opened.put((1, starting_point))
a = final_state[0] while not opened.empty():
b = final_state[1] elem = opened.get()[1]#[1] bo inaczej elem nie ma state
#hole = Hole("astar", Position(a, b))#narysowana dziura w miejscu mety(celu), by sprawdzić, czy działa if elem.state[0] == ending_point.state[0] and elem.state[1] == ending_point.state[1]:
#objectArray.append(hole) while elem.action is not False:
list_of_actions.insert(0, elem.action)
elem = elem.parent
return list_of_actions
if elem.state not in closed:
closed.append(elem.state)
for (action, state) in succ1(elem.state):
point = Node(state, elem, action)
score = manhattan(point, ending_point) + f(point.state)
print(score)
if state not in opened.queue:
opened.put((score, point))
if state in opened.queue:
opened.queue.remove(elem)
opened.put((score, point))
if __name__ == '__main__': if __name__ == '__main__':
f((1, 1, 0))
pygame.init() # inicjalizacja modułów, na razie niepotrzebna pygame.init() # inicjalizacja modułów, na razie niepotrzebna
gridSize = 15 gridSize = 15
astar()
# Tworzymy nowego playera, czy tam agenta # Tworzymy nowego playera, czy tam agenta
agent = Agent("smieciarka", Position(0, 0)) agent = Agent("smieciarka", Position(0, 0))
@ -268,7 +291,8 @@ if __name__ == '__main__':
collisionsMap[object.pos.x][object.pos.y] = True collisionsMap[object.pos.x][object.pos.y] = True
black_list = [(10, 10), (7, 4), (3, 10), (8, 10), (4, 5), (1, 2), (10, 4), (13, 14), (6, 9), (4, 9), (5, 11), (11, 7), (13, 8)] black_list = [(10, 10), (7, 4), (3, 10), (8, 10), (4, 5), (1, 2), (10, 4), (13, 14), (6, 9), (4, 9), (5, 11), (11, 7), (13, 8)]
#lista obiektów potrzebna do succ1 - na te pole śmieciarka nie wchodzi, więc nie ma ich brać pod uwagę c = algorithm()
print(c)
width = 610 width = 610
height = 530 height = 530