-
Notifications
You must be signed in to change notification settings - Fork 0
/
competition_agent.py
executable file
·97 lines (76 loc) · 3.42 KB
/
competition_agent.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
"""Implement your own custom search agent using any combination of techniques
you choose. This agent will compete against other students (and past
champions) in a tournament.
COMPLETING AND SUBMITTING A COMPETITION AGENT IS OPTIONAL
"""
import random
class SearchTimeout(Exception):
"""Subclass base exception for code clarity. """
pass
def custom_score(game, player):
"""Calculate the heuristic value of a game state from the point of view
of the given player.
This should be the best heuristic function for your project submission.
Parameters
----------
game : `isolation.Board`
An instance of `isolation.Board` encoding the current state of the
game (e.g., player locations and blocked cells).
player : object
A player instance in the current game (i.e., an object corresponding to
one of the player objects `game.__player_1__` or `game.__player_2__`.)
Returns
-------
float
The heuristic value of the current game state to the specified player.
"""
raise NotImplementedError
class CustomPlayer:
"""Game-playing agent to use in the optional player vs player Isolation
competition.
You must at least implement the get_move() method and a search function
to complete this class, but you may use any of the techniques discussed
in lecture or elsewhere on the web -- opening books, MCTS, etc.
**************************************************************************
THIS CLASS IS OPTIONAL -- IT IS ONLY USED IN THE ISOLATION PvP
COMPETITION. IT IS NOT REQUIRED FOR THE ISOLATION PROJECT REVIEW.
**************************************************************************
Parameters
----------
data : string
The name of the search method to use in get_move().
timeout : float (optional)
Time remaining (in milliseconds) when search is aborted. Note that
the PvP competition uses more accurate timers that are not cross-
platform compatible, so a limit of 1ms (vs 10ms for the other classes)
is generally sufficient.
"""
def __init__(self, data=None, timeout=1.):
self.score = custom_score
self.time_left = None
self.TIMER_THRESHOLD = timeout
def get_move(self, game, time_left):
"""Search for the best move from the available legal moves and return a
result before the time limit expires.
**********************************************************************
NOTE: If time_left < 0 when this function returns, the agent will
forfeit the game due to timeout. You must return _before_ the
timer reaches 0.
**********************************************************************
Parameters
----------
game : `isolation.Board`
An instance of `isolation.Board` encoding the current state of the
game (e.g., player locations and blocked cells).
time_left : callable
A function that returns the number of milliseconds left in the
current turn. Returning with any less than 0 ms remaining forfeits
the game.
Returns
-------
(int, int)
Board coordinates corresponding to a legal move; may return
(-1, -1) if there are no available legal moves.
"""
# OPTIONAL: Finish this function!
raise NotImplementedError