Skip to content

Commit

Permalink
A* and some QOL
Browse files Browse the repository at this point in the history
  • Loading branch information
atr2718 committed Dec 7, 2021
1 parent dbc9add commit 7ff2cf1
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 3 deletions.
16 changes: 14 additions & 2 deletions code/bot/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,15 @@ class CheeseActions(Enum):
class CheeseGame:
"""Clear all the cheese to win."""

def __init__(self):
def __init__(self, x_dimension=10, y_dimension=20, hole_count=1, cheese_count=9):
self.randomizer = SimpleRandomizer(PIECES)
self.x_dimension = x_dimension
self.y_dimension = y_dimension
self.hole_count = hole_count
self.cheese_count = cheese_count

def get_start_state(self):
return new_cheese_state(x_dimension=10, y_dimension=20)
return new_cheese_state(self.x_dimension, self.y_dimension, self.hole_count, self.cheese_count)

def get_successors(self, state):

Expand Down Expand Up @@ -179,6 +183,7 @@ def is_legal(self):
x < 0
or x >= len(self.grid[0])
or y < 0
or y >= len(self.grid)
or self.grid[y][x] != BLANK_LABEL
):
return False
Expand Down Expand Up @@ -240,6 +245,13 @@ def __repr__(self):
count += 1
return st

def __hash__(self):
return hash((self.piece, self.grid))

def __eq__(self, other):
if isinstance(other, CheeseState):
return (other.piece == self.piece) and (other.grid == self.grid)

def rotate_left(self):
new_piece = []
anchor_x, anchor_y = self.anchor
Expand Down
80 changes: 79 additions & 1 deletion code/bot/search.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python3

import game, queue
import game, queue, heapq


# class SearchNode:
Expand All @@ -22,6 +22,7 @@ def breadth_first_search(game):
while not frontier.empty():
current_state, current_cost, current_path = frontier.get()
explored.add(current_state)
print(current_state)
if game.is_goal(current_state):
return current_path

Expand All @@ -37,3 +38,80 @@ def breadth_first_search(game):

# no path found
return []

def Astar_search(game, heurisitic):

frontier = PriorityQueue()
explored = set()

start_node = (game.get_start_state(), 0, [])

frontier.push(start_node, 0)
#counter = 0

while not frontier.empty():
#counter = counter + 1
current_state, current_cost, current_path = frontier.pop()

#explored.add((current_state.piece, current_state.grid))
explored.add(current_state)
#if counter % 100 == 0:
# print(str(explored))
#print(str(current_state))
if game.is_goal(current_state):
return current_path

for new_state, new_action, action_cost in game.get_successors(current_state):
new_node = (
new_state,
current_cost + action_cost,
current_path + [new_action],
)

if new_state not in explored:
frontier.update(new_node, new_node[1] + heurisitic(new_node[0]))

# no path found
return []

#code from CS188 Berkeley for PriorityQueue with update function modified
class PriorityQueue:
"""
Implements a priority queue data structure. Each inserted item
has a priority associated with it and the client is usually interested
in quick retrieval of the lowest-priority item in the queue. This
data structure allows O(1) access to the lowest-priority item.
"""
def __init__(self):
self.heap = []
self.count = 0

def push(self, item, priority):
entry = (priority, self.count, item)
heapq.heappush(self.heap, entry)
self.count += 1

def pop(self):
(_, _, item) = heapq.heappop(self.heap)
return item

def empty(self):
return len(self.heap) == 0

def update(self, item, priority):
# If item already in priority queue with higher priority, update its priority and rebuild the heap.
# If item already in priority queue with equal or lower priority, do nothing.
# If item not in priority queue, do the same thing as self.push.
for index, (p, c, i) in enumerate(self.heap):
if i[0] == item[0]:
if p <= priority:
break
del self.heap[index]
self.heap.append((priority, c, item))
heapq.heapify(self.heap)
break
else:
self.push(item, priority)

from game import CheeseGame
Astar_search(CheeseGame(), lambda a: 0)

0 comments on commit 7ff2cf1

Please sign in to comment.