-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathenv.py
226 lines (184 loc) · 4.81 KB
/
env.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
SHORT_PATH = ""
import sys
from numba import njit
from src.render_template import Render as __Render, import_files
from numba.core.errors import (
NumbaPendingDeprecationWarning as __NumbaPendingDeprecationWarning,
)
import warnings as __warnings
__warnings.simplefilter("ignore", __NumbaPendingDeprecationWarning)
_game_name_ = "RockPaperScissors"
def make(game_name: str = "RockPaperScissors") -> any:
"""
make the environment
Parameters
----------
game_name : str, optional
name of the game, by default "RockPaperScissors"
List game:
'Catan',
'CatanNoExchange',
'Century',
'Durak',
'Exploding_Kitten',
'Fantan',
'GoFish',
'Imploding_Kitten',
'MachiKoro',
'Poker',
'RockPaperScissors',
'Sheriff',
'Splendor',
'Splendor_v2',
'Splendor_v3',
'StoneAge',
'SushiGo',
'TLMN',
'TicketToRide',
'WelcomeToTheDungeon_v1',
'WelcomeToTheDungeon_v2',
"""
global _game_name_, __env__, agent_random
_game_name_ = game_name
add_game_to_syspath()
__env__ = __import__(f"src.Base.{_game_name_}.env", fromlist=["*"])
import_files(_game_name_)
agent_random = __env__.bot_lv0
def add_game_to_syspath():
if len(sys.argv) >= 2:
sys.argv = [sys.argv[0]]
sys.argv.append(_game_name_)
#'------------------------------------------------------------------------------------------------------'
#'------------------------------------------------------------------------------------------------------'
#'------------------------------------------------------------------------------------------------------'
@njit()
def getValidActions(state):
"""
return a array of valid actions
Parameters
----------
state : any
current state of the game
Returns
-------
ValidActions : np.array 1D
np.array of valid actions
"""
return __env__.getValidActions(state)
@njit()
def getActionSize():
"""
return the size of action space
Returns
-------
ActionSize : int
"""
return __env__.getActionSize()
@njit()
def getAgentSize():
"""
return the size of agent space
Returns
-------
AgentSize : int
"""
return __env__.getAgentSize()
@njit()
def getStateSize():
"""
return the size of state space
Returns
-------
StateSize : int
"""
return __env__.getStateSize()
@njit()
def getReward(state):
"""
return the reward of the state
Parameters
----------
state : any
current state of the game
Returns
-------
Reward : int
0 if the game is not end
1 if the game is end and the player win
-1 if the game is end and the player lose
"""
return __env__.getReward(state)
# @njit()
def run(
agent,
num_game: int = 100,
agent_model: any = 1,
level: int = 0,
*args,
):
"""
run the game
Parameters
----------
p0 : any, optional
the agent of environment, this is function,
by default 'bot_lv0'
num_game : int, optional
number of game, by default 100
agent_model : any, optional
model of agent, by default 1
level : int, optional
level of the game, by default 0
0: random mode
1: easy mode
-1: hard mode
Returns
-------
result :
result of the game
agent_model :
model of agent
"""
return __env__.run(agent, num_game, agent_model, level, *args)
def render(
Agent="human",
agent_model: any = [0],
level: int = 0,
*args,
max_temp_frame=100,
):
"""
render the game
Parameters
----------
Agent : str, optional
the agent of environment, by default "human"
agent_model : any, optional
data of agent, by default [0]
level : int, optional
level of the game, by default 0
0: random mode
1: easy mode
-1: hard mode
Returns
-------
result :
result of the game
agent_model :
model of agent
"""
list_agent, list_data = __env__.load_agent(level, *args)
if "__render" not in globals():
global __render
__render = __Render(Agent, agent_model, list_agent, list_data, max_temp_frame)
else:
__render.__init__(Agent, agent_model, list_agent, list_data, max_temp_frame)
return __render.render()
def get_data_from_visualized_match():
if "__render" not in globals():
print("Nothing to get, visualize the match before running this function")
return None
return {
"history_state": __render.history_state,
"history_action": __render.history_action,
}