Skip to content

Commit

Permalink
Merge pull request #17 from Team-Shipyard-Shenanigans/final_patches_2
Browse files Browse the repository at this point in the history
FINAL FINAL FINAL CHANGES. I promis
  • Loading branch information
AditiB007 authored Mar 25, 2023
2 parents ada60a2 + 41f0672 commit 0a2f694
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 38 deletions.
18 changes: 9 additions & 9 deletions case3TEMP.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
[06, 10], {00000}, UNUSED
[06, 11], {00000}, UNUSED
[06, 12], {00000}, UNUSED
[05, 01], {00333}, Rat
[05, 01], {00000}, UNUSED
[05, 02], {00000}, UNUSED
[05, 03], {00000}, UNUSED
[05, 04], {00000}, UNUSED
Expand All @@ -46,7 +46,7 @@
[05, 10], {00000}, UNUSED
[05, 11], {00000}, UNUSED
[05, 12], {00000}, UNUSED
[04, 01], {00333}, Bat
[04, 01], {00000}, UNUSED
[04, 02], {00000}, UNUSED
[04, 03], {00000}, UNUSED
[04, 04], {00000}, UNUSED
Expand All @@ -58,7 +58,7 @@
[04, 10], {00000}, UNUSED
[04, 11], {00000}, UNUSED
[04, 12], {00000}, UNUSED
[03, 01], {00010}, Doe
[03, 01], {00000}, UNUSED
[03, 02], {00000}, UNUSED
[03, 03], {00000}, UNUSED
[03, 04], {00000}, UNUSED
Expand All @@ -70,9 +70,9 @@
[03, 10], {00000}, UNUSED
[03, 11], {00000}, UNUSED
[03, 12], {00000}, UNUSED
[02, 01], {09041}, Cat
[02, 02], {00000}, UNUSED
[02, 03], {00000}, UNUSED
[02, 01], {00000}, UNUSED
[02, 02], {09041}, Cat
[02, 03], {00010}, Doe
[02, 04], {00000}, UNUSED
[02, 05], {00000}, UNUSED
[02, 06], {00000}, UNUSED
Expand All @@ -82,13 +82,13 @@
[02, 10], {00000}, UNUSED
[02, 11], {00000}, UNUSED
[02, 12], {00000}, UNUSED
[01, 01], {10001}, Ewe
[01, 02], {00000}, UNUSED
[01, 01], {00000}, UNUSED
[01, 02], {00334}, Bat
[01, 03], {00600}, Dog
[01, 04], {00100}, Rat
[01, 05], {00000}, UNUSED
[01, 06], {00000}, UNUSED
[01, 07], {00000}, UNUSED
[01, 07], {10001}, Ewe
[01, 08], {00000}, UNUSED
[01, 09], {00000}, UNUSED
[01, 10], {00000}, UNUSED
Expand Down
2 changes: 1 addition & 1 deletion gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ def display_animation(self):
str_cont_pos = str((cont_pos[0] + 2, cont_pos[1] + 1))

if cont_pos is not None and cont_to_move is not None:
if cont_to_move in prev_move.get_offload_remaining():
if prev_move is not None and cont_to_move in prev_move.get_offload_remaining():
self.display_move("Step %s: Move Crane from %s to %s, then offload container %s to truck" % (self.current_step + 1, str_init_pos, str_cont_pos, cont_to_move.get_description()))
self.write_to_log("Step %s: Move Crane from %s to %s, then offload container %s to truck" % (self.current_step + 1, str_init_pos, str_cont_pos, cont_to_move.get_description()))
else:
Expand Down
59 changes: 31 additions & 28 deletions optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ def load(self, bay, buffer, container_onload_list, container_offload_list):
# print("Choosing: Move To %s From %s End in Bay? %s, F(N) = %s, H(N) = %s, G(N) = %s" % (min_node[1].get_end_pos(), min_node[1].get_init_pos(), min_node[1].get_in_bay(), f_1, f_1 - min_node[1].get_cost(), min_node[1].get_cost()))

def balance(self, bay, buffer):
nodes = [move.Move(bay, buffer, None, (-1, 0), None, None, True, 0, None, None, None)]
if(self.check_sift(nodes[0])):
nodes = [move.Move(bay, buffer, None, (-1, 0), [], [], True, 0, None, None, None)]

if self.check_sift(nodes[0]):
return self.sift(bay, buffer)

min_index = 0
nodes_expanded = 0
max_nodes_in_queue = 0
Expand All @@ -47,7 +47,7 @@ def balance(self, bay, buffer):
max_nodes_in_queue = max(max_nodes_in_queue, len(nodes))
curr = nodes.pop(min_index) ## Remove the smallest cost node from the queue

if curr.check_balanced()[0]: ## If our popped node is our goal state, we've solved the puzzle!
if curr.check_balanced()[0] and len(curr.get_onload_remaining()) == 0: ## If our popped node is our goal state, we've solved the puzzle!
return curr, nodes_expanded, max_nodes_in_queue ## return search information to main function for output. Note: g_n = depth of the tree for any node)
children = self.apply_balance_operations(curr)
for i in children:
Expand Down Expand Up @@ -98,7 +98,7 @@ def apply_load_operations(self, curr_move):
bay_copy.move_to_column(j, dest_col)
dest_stack = bay_copy.get_stacks(dest_col)
moves.append(move.Move(bay_copy, curr_buffer, prev_pos, ending_loc, curr_move.get_offload_remaining(), curr_move.get_onload_remaining(), True, cost, curr_move, dest_stack.peek(), cont_pos))
for i in curr_buffer.get_stacks(): ## for all destination locations within the bay
for i in curr_buffer.get_stacks(): ## for all destination locations within the bay
dest_col = i.get_column()
dest_height = i.get_max_height() - (i.get_height() + 1)
if i.get_height() < i.get_max_height():
Expand Down Expand Up @@ -141,7 +141,7 @@ def apply_balance_operations(self, curr_move) -> list["move.Move"]:
origin_max_height = origin_stack.get_max_height()
origin_height = origin_stack.get_height()
cont_pos = (origin_max_height - (origin_height + 1), j)
for i in curr_bay.get_stacks(): ## for all destination locations within the bay
for i in curr_bay.get_stacks(): ## for all destination locations within the bay
dest_col = i.get_column()
dest_height = i.get_max_height() - (i.get_height() + 1)
if on_ship:
Expand All @@ -159,9 +159,11 @@ def apply_balance_operations(self, curr_move) -> list["move.Move"]:
ending_loc = (i.get_max_height() - (i.get_height() + 2), dest_col)
bay_copy.move_to_column(-1, dest_col, i)
buffer_copy.move_to_column(j, -1, i)
moves.append(move.Move(bay_copy, buffer_copy, prev_pos, ending_loc, None, None, True, cost, curr_move, i, cont_pos))

for i in curr_buffer.get_stacks(): ## for all destination locations within the bay
onload_copy = copy.deepcopy(curr_move.get_onload_remaining())
onload_copy.remove(origin_stack.peek())
moves.append(move.Move(bay_copy, buffer_copy, prev_pos, ending_loc, curr_move.get_offload_remaining(), onload_copy, True, cost, curr_move, origin_stack.peek(), cont_pos))

for i in curr_buffer.get_stacks(): ## for all destination locations within the bay
dest_col = i.get_column()
dest_height = i.get_max_height() - (i.get_height() + 1)
if not on_ship:
Expand All @@ -170,7 +172,7 @@ def apply_balance_operations(self, curr_move) -> list["move.Move"]:
cost = prev_cost + buffer_copy.manhattan(prev_pos, cont_pos) + curr_move.column_move_cost(j, dest_col, on_ship)
ending_loc = (i.get_max_height() - (i.get_height() + 2), dest_col)
buffer_copy.move_to_column(j, dest_col)
moves.append(move.Move(curr_bay, buffer_copy, prev_pos, ending_loc, None, None, True, cost, curr_move, origin_stack.peek(), cont_pos))
moves.append(move.Move(curr_bay, buffer_copy, prev_pos, ending_loc, curr_move.get_offload_remaining(), curr_move.get_onload_remaining(), True, cost, curr_move, origin_stack.peek(), cont_pos))
else:
if i.get_height() < i.get_max_height():
bay_copy = copy.deepcopy(curr_bay)
Expand All @@ -179,7 +181,9 @@ def apply_balance_operations(self, curr_move) -> list["move.Move"]:
ending_loc = (i.get_max_height() - (i.get_height() + 2), dest_col)
buffer_copy.move_to_column(-1, dest_col, origin_stack.peek())
bay_copy.move_to_column(j, -1, origin_stack.peek())
moves.append(move.Move(bay_copy, buffer_copy, prev_pos, ending_loc, None, None, True, cost, curr_move, origin_stack.peek(), cont_pos))
onload_copy = copy.deepcopy(curr_move.get_onload_remaining())
onload_copy.append(origin_stack.peek())
moves.append(move.Move(bay_copy, buffer_copy, prev_pos, ending_loc, curr_move.get_offload_remaining(), onload_copy, True, cost, curr_move, origin_stack.peek(), cont_pos))
if on_ship:
moves.append(move.Move(curr_bay, curr_buffer, prev_pos, (-1, 23), curr_move.get_offload_remaining(), curr_move.get_onload_remaining(), False, prev_cost + curr_move.buffer_move_cost(prev_pos, False), curr_move, None, None))
else:
Expand Down Expand Up @@ -310,8 +314,8 @@ def check_sift(self, move=move.Move):
return False

def sift(self, bay, buffer):
nodes = [move.Move(bay, buffer, None, (-1, 0), None, None, True, 0, None, None, None)]
nodes = [move.Move(bay, buffer, None, (-1, 0), [], [], True, 0, None, None, None)]

min_index = 0
nodes_expanded = 0
max_nodes_in_queue = 0
Expand All @@ -334,12 +338,12 @@ def sift(self, bay, buffer):
for i in enumerate(nodes):
f_1 = self.sift_heuristic(min_node[1], goal) + min_node[1].get_cost()
f_2 = self.sift_heuristic(i[1], goal) + i[1].get_cost()
#print("Move %s To %s From %s End in Bay? %s, F(N) = %s, H(N) = %s, G(N) = %s" % (i[1].get_container(), i[1].get_end_pos(), i[1].get_init_pos(), i[1].get_in_bay(), f_2, f_2 - i[1].get_cost(), i[1].get_cost()))
#print(i[1].get_bay())
# print("Move %s To %s From %s End in Bay? %s, F(N) = %s, H(N) = %s, G(N) = %s" % (i[1].get_container(), i[1].get_end_pos(), i[1].get_init_pos(), i[1].get_in_bay(), f_2, f_2 - i[1].get_cost(), i[1].get_cost()))
# print(i[1].get_bay())
if f_1 >= f_2:
min_node = i
min_index = min_node[0]
#print("Choosing: Move %s To %s From %s End in Bay? %s, F(N) = %s, H(N) = %s, G(N) = %s" % (min_node[1].get_container(), min_node[1].get_end_pos(), min_node[1].get_init_pos(), min_node[1].get_in_bay(), f_1, f_1 - min_node[1].get_cost(), min_node[1].get_cost()))
# print("Choosing: Move %s To %s From %s End in Bay? %s, F(N) = %s, H(N) = %s, G(N) = %s" % (min_node[1].get_container(), min_node[1].get_end_pos(), min_node[1].get_init_pos(), min_node[1].get_in_bay(), f_1, f_1 - min_node[1].get_cost(), min_node[1].get_cost()))

def sift_heuristic(self, curr_move, goal_state):
bay = curr_move.get_bay()
Expand All @@ -352,26 +356,25 @@ def sift_heuristic(self, curr_move, goal_state):
for j in range(curr_move.get_bay().get_columns()):
if grid[i][j] != goal_state[i][j] and grid[i][j] is not None and grid[i][j].get_description() != "NAN":
mismatch.append((grid[i][j]))



for stack in bay.get_stacks():
temp = []
# print(stack)
# print(cost)
while stack.peek() is not None and stack.has(mismatch):
goal_indices = self.get_sift_goal_loc(curr_move, (stack.peek(), stack.get_column()))
goal_indices = self.get_sift_goal_loc(curr_move, (stack.peek(), stack.get_column()))
cost += bay.manhattan(prev_pos, (stack.get_max_height() - (stack.get_height() + 1), stack.get_column()))
dest_stack = bay.get_stacks(goal_indices[1])
dest_row = dest_stack.get_max_height() - (dest_stack.get_height() + 1)
min_cost = curr_move.column_move_cost(stack.get_column(), goal_indices[1], True) + abs(goal_indices[0] - dest_row)
min_cost = curr_move.column_move_cost(stack.get_column(), goal_indices[1], True) + abs(goal_indices[0] - dest_row)
prev_pos = (dest_row, goal_indices[1])
cost += min_cost
temp.append(stack.pop())
temp.append(stack.pop())
for k in reversed(temp):
stack.push(k)

return cost

def get_sift_goal_loc(self, curr_move, container):
containers = curr_move.get_containers_in_section((0, curr_move.get_bay().get_rows()), (0, curr_move.get_bay().get_columns()))
containers.sort(key=lambda x: x[0].get_weight(), reverse=True)
Expand All @@ -387,19 +390,19 @@ def generate_sift_goal(self, curr_move):

for i in range(rows):
for j in range(cols):
if(curr_move.get_bay().get_container(i, j) is not None and curr_move.get_bay().get_container(i, j).get_description() == "NAN"):
if curr_move.get_bay().get_container(i, j) is not None and curr_move.get_bay().get_container(i, j).get_description() == "NAN":
goal[i][j] = curr_move.get_bay().get_grid()[i][j]

containers = curr_move.get_containers_in_section((0, rows), (0, cols))
containers.sort(key=lambda x: x[0].get_weight(), reverse=True)

index_adjustment = 0
for i in enumerate(containers):
goal_loc = self.index_mapper(i[0] + index_adjustment)
if (goal[goal_loc[0]][goal_loc[1]] is None):
if goal[goal_loc[0]][goal_loc[1]] is None:
goal[goal_loc[0]][goal_loc[1]] = i[1][0]
else:
while(goal[goal_loc[0]][goal_loc[1]] is not None):
while goal[goal_loc[0]][goal_loc[1]] is not None:
index_adjustment += 1
goal_loc = self.index_mapper(i[0] + index_adjustment)
goal[goal_loc[0]][goal_loc[1]] = i[1][0]
Expand Down

0 comments on commit 0a2f694

Please sign in to comment.