Graphsearch A*, heurystyka, action cost

This commit is contained in:
Art-cyber520 2021-05-09 15:25:53 +02:00
parent a5a2820eca
commit 9ebb4a0f15
14 changed files with 174 additions and 28 deletions

View File

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.7 (venv)" project-jdk-type="Python SDK" />
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.8" project-jdk-type="Python SDK" />
</project>

View File

@ -2,7 +2,7 @@
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="jdk" jdkName="Python 3.7 (venv)" jdkType="Python SDK" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View File

@ -74,4 +74,4 @@ class Field(object):
def PuttingLargeImage(self, large_img_name):
self.large_image_canvas.image = large_img_name
self.large_image_canvas.create_image(0, 0, anchor=NW, image=large_img_name)
self.large_image_canvas.create_image(0, 0, anchor=NW, image=large_img_name)

View File

@ -1,11 +1,15 @@
from resources.Globals import *
images_coord_list = []
cell_expense_list = []
class Node:
def __init__(self):
self.state = State()
self.parent = None
self.parent = []
self.action = ""
self.priority = 0
class State:
@ -14,6 +18,14 @@ class State:
self.direction = ""
def init_data(coord_list, expense_list):
global images_coord_list
global cell_expense_list
images_coord_list = coord_list
cell_expense_list = expense_list
def successor(state):
node_state_left = Node()
@ -98,11 +110,86 @@ def successor(state):
return [node_state_left, node_state_right]
def graph_search(fringe, explored, start_state, end_state_coord):
def get_cell_expense(node):
global images_coord_list
global cell_expense_list
for i in range(0, len(images_coord_list)):
if (images_coord_list[i][0] <= node.state.coord[0] and node.state.coord[0] <= images_coord_list[i][0] + IMAGE_SIZE) and (images_coord_list[i][1] <= node.state.coord[1] and node.state.coord[1] <= images_coord_list[i][1] + IMAGE_SIZE):
return cell_expense_list[i]
def heurystyka(node_now):
if node_now.action == "Left" or node_now.action == "Right":
return node_now.parent[2] + (get_cell_expense(node_now) / 2)
elif node_now.action == "Up":
return node_now.parent[2] + (get_cell_expense(node_now) * 2)
elif node_now.action == "":
return get_cell_expense(node_now)
# def graph_search(fringe, explored, start_state, end_state_coord):
#
# node = Node()
# node.state = start_state
# node.parent = node.state
# fringe.append(node)
# iterator = 0
#
# end_loop = True
# while end_loop:
# if len(fringe) == 0:
# end_loop = False
# #return False
#
# elem = fringe[iterator]
#
# if elem.state.coord == end_state_coord:
# return fringe
#
# explored.append(elem)
#
# another_states = successor(elem.state)
# for i in range(0, len(another_states)):
# n = len(fringe)
# for j in range(0, n):
# if another_states[i].state.coord[0] == fringe[j].state.coord[0] and another_states[i].state.coord[1] == fringe[j].state.coord[1]:
# if another_states[i].state.direction == fringe[j].state.direction:
# break
# else:
# states = []
# for k in range(0, len(fringe)):
# new_state = [fringe[k].state.coord, fringe[k].state.direction]
# states.append(new_state)
# now_state = [another_states[i].state.coord, another_states[i].state.direction]
# if now_state in states:
# break
#
# another_states[i].parent = elem.state
# fringe.append(another_states[i])
# else:
# states = []
# for k in range(0, len(fringe)):
# new_state = [fringe[k].state.coord, fringe[k].state.direction]
# states.append(new_state)
# now_state = [another_states[i].state.coord, another_states[i].state.direction]
#
# if now_state in states:
# break
#
# if another_states[i].state.direction == fringe[j].state.direction:
# another_states[i].parent = elem.state
# fringe.append(another_states[i])
# iterator += 1
def graph_search_A(fringe, explored, start_state, end_state_coord):
node = Node()
node.state = start_state
node.parent = node.state
node.priority = heurystyka(node)
node.parent = [node.state.coord, node.state.direction, node.priority]
fringe.append(node)
iterator = 0
@ -110,7 +197,7 @@ def graph_search(fringe, explored, start_state, end_state_coord):
while end_loop:
if len(fringe) == 0:
end_loop = False
#return False
# return False
elem = fringe[iterator]
@ -121,10 +208,15 @@ def graph_search(fringe, explored, start_state, end_state_coord):
another_states = successor(elem.state)
for i in range(0, len(another_states)):
another_states[i].parent = [elem.state.coord, elem.state.direction, elem.priority]
p = heurystyka(another_states[i])
n = len(fringe)
for j in range(0, n):
if another_states[i].state.coord[0] == fringe[j].state.coord[0] and another_states[i].state.coord[1] == fringe[j].state.coord[1]:
if another_states[i].state.direction == fringe[j].state.direction:
if another_states[i].state.direction == fringe[j].state.direction and p < fringe[j].priority:
another_states[i].priority = p
fringe[j] = another_states[i]
break
else:
states = []
@ -133,10 +225,30 @@ def graph_search(fringe, explored, start_state, end_state_coord):
states.append(new_state)
now_state = [another_states[i].state.coord, another_states[i].state.direction]
if now_state in states:
break
index = states.index(now_state)
if p < fringe[index].priority:
another_states[i].priority = p
fringe[index] = another_states[i]
break
else:
break
another_states[i].parent = elem.state
another_states[i].priority = p
fringe.append(another_states[i])
n1 = len(fringe)
while n1 > 1:
change = False
for l in range(0, n1 - 1):
if fringe[l].priority > fringe[l + 1].priority:
fringe[l], fringe[l + 1] = fringe[l + 1], fringe[l]
change = True
n1 -= 1
if not change:
break
else:
states = []
for k in range(0, len(fringe)):
@ -145,9 +257,29 @@ def graph_search(fringe, explored, start_state, end_state_coord):
now_state = [another_states[i].state.coord, another_states[i].state.direction]
if now_state in states:
break
index = states.index(now_state)
if p < fringe[index].priority:
another_states[i].priority = p
fringe[index] = another_states[i]
break
else:
break
if another_states[i].state.direction == fringe[j].state.direction:
another_states[i].parent = elem.state
another_states[i].priority = p
fringe.append(another_states[i])
n2 = len(fringe)
while n2 > 1:
change = False
for h in range(0, n2 - 1):
if fringe[h].priority > fringe[h + 1].priority:
fringe[h], fringe[h + 1] = fringe[h + 1], fringe[h]
change = True
n2 -= 1
if not change:
break
iterator += 1

View File

@ -28,6 +28,7 @@ field = Field()
fringe = []
explored = []
action_list = []
images_coord = []
def Arrow(direction):
@ -46,9 +47,16 @@ def Arrow(direction):
# Putting images
def Fill(bool):
global images_coord
if bool:
field.PuttingSmallImages()
for i in range(0, len(field.canvas_small_images)):
images_coord.append(field.small_field_canvas.coords(field.canvas_small_images[i]))
print("Coords List: ", images_coord)
nd.init_data(images_coord, field.cell_expense)
# Drawing red/green rectangles
for el in field.state_of_cell_array:
if el[0] != 0:
@ -171,7 +179,7 @@ def create_action_list(states, index):
action_list.reverse()
return True
action_list.append(fringe[index].action)
state_parent = [fringe[index].parent.coord, fringe[index].parent.direction]
state_parent = [fringe[index].parent[0], fringe[index].parent[1]]
create_action_list(states, states.index(state_parent))
@ -189,6 +197,7 @@ def MouseClickEvent(event):
img_coords = field.small_field_canvas.coords(field.canvas_small_images[i])
if (img_coords[0] <= event.x and event.x <= img_coords[0] + IMAGE_SIZE) and (img_coords[1] <= event.y and event.y <= img_coords[1] + IMAGE_SIZE):
end_position = img_coords
print("Color cost: ", field.cell_expense[i])
# if len(end_position) == 2:
# print("Koncowa pozycja: {} {}".format(end_position[0], end_position[1]))
@ -202,8 +211,7 @@ def MouseClickEvent(event):
for k in range(0, len(fringe)):
new_state = fringe[k].state.coord
states.append(new_state)
index = states.index(start_position)
start_node = fringe[index]
start_node = fringe[-1]
node.state.coord = start_node.state.coord
node.state.direction = start_node.state.direction
@ -211,21 +219,28 @@ def MouseClickEvent(event):
fringe.clear()
explored.clear()
action_list.clear()
fringe = nd.graph_search(fringe, explored, node.state, end_position)
fringe = nd.graph_search_A(fringe, explored, node.state, end_position)
# fringe = nd.graph_search(fringe, explored, node.state, end_position)
states = []
goal_all = []
for i in range(0, len(fringe)):
new_state = [fringe[i].state.coord, fringe[i].state.direction]
states.append(new_state)
if end_position[0] == fringe[i].state.coord[0] and end_position[1] == fringe[i].state.coord[1]:
fringe = fringe[:i + 1]
break
goal_all.append(fringe[i])
elem_min = goal_all[0]
for i in range(1, len(goal_all)):
if elem_min.priority > goal_all[i].priority:
elem_min = goal_all[i]
index = fringe.index(elem_min)
fringe = fringe[:index + 1]
create_action_list(states, -1)
# for i in range(0, len(fringe)):
# print('Node{} = State: {} {}, Parent: {} {}, Action: {}'.format(i + 1, fringe[i].state.coord, fringe[i].state.direction, fringe[i].parent.coord, fringe[i].parent.direction, fringe[i].action))
# print('Node{} = State: {} {}, Parent: {} {} {}, Action: {}'.format(i + 1, fringe[i].state.coord, fringe[i].state.direction, fringe[i].parent[0], fringe[i].parent[1], fringe[i].parent[2], fringe[i].action))
print(action_list)
@ -310,25 +325,26 @@ def DrawRectangle():
y = 4
color = None
# Chose color for rectangle
for i in range(len(field.cell_expense)):
if field.cell_expense[i] == 10:
color = "None"
elif field.cell_expense[i] == 20:
color = "yellow"
elif field.cell_expense[i] == 30:
color = "dodger blue"
elif field.cell_expense[i] == 40:
color = "dodger blue"
elif field.cell_expense[i] == 80:
color = "green4"
if color != "None":
field.small_field_canvas.create_rectangle(x, y, x + IMAGE_SIZE + 2, y + IMAGE_SIZE + 2, width=2, outline=color)
x += player.step
if i > 0 and i % 10 == 0:
if x + IMAGE_SIZE + 2 > field.width:
x = 4
y += player.step
def AddCostCellsToArray(amount, cost):
counter = 0
while counter < amount:

View File

@ -19,12 +19,10 @@ amount_of_sand_cells = 10
sand_cell_cost = 20
amount_of_water_cells = 10
water_cell_cost = 30
water_cell_cost = 40
amount_of_swamp_cells = 10
swamp_cell_cost = 40
swamp_cell_cost = 80
x_start = 5
y_start = 5

Binary file not shown.

Binary file not shown.