Skip to content

Commit

Permalink
fix: utility now calculated of properly
Browse files Browse the repository at this point in the history
  • Loading branch information
Eduard-Prokhorikhin committed Apr 23, 2024
1 parent a187860 commit 409fd66
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/agents/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def playGameDemoStepByStep(agent: Agent, board: Tetris) -> Tetris:
if isinstance(result, list):
for action in result:
board.doAction(action)
sleep(0.2)
sleep(0.3)
# board.printBoard()
else:
board.doAction(result)
Expand Down
18 changes: 8 additions & 10 deletions src/agents/heuristic_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,19 @@ def result(self, board: Tetris) -> list[Action]:
# Get all possible boards
possible_boards = board.getPossibleBoards()

best_board = possible_boards[0]
best_utility = utility(best_board, -0.8, -1.2, 3, -0.3,-3)
best_board = None
best_utility = float('-inf')
# Check which board has the best outcome based on the heuristic
for candidate_board in possible_boards[1:]:
current_utility = utility(candidate_board, -0.8, -1.2, 4, -0.3,-0.6)
for candidate_board in possible_boards:
# current_utility = utility(candidate_board, -0.8, -1.2, 4, -0.3,-0.6)
current_utility = utility(candidate_board, -0.510066, 0, 0.760666, -0.184483, -0.3566)


if current_utility > best_utility:
best_board = candidate_board
best_utility = current_utility

# Find the actions needed to transform the current board to the new board
actions = []
try:
actions = transition_model(board, best_board)
return actions
except:
return actions
actions = transition_model(board, best_board)
return actions

1 change: 1 addition & 0 deletions src/game/tetris.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ def getPossibleBoards(self) -> list["Tetris"]:
if not moveBoard.isValidBlockPosition(moveBoard.block):
continue

moveBoard.prevBoard = copy.deepcopy(moveBoard.board)
if moveBoard not in possibleMoves:
possibleMoves.append(moveBoard)

Expand Down

0 comments on commit 409fd66

Please sign in to comment.