kaggle-environments 1.15.3__py2.py3-none-any.whl → 1.16.1__py2.py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of kaggle-environments might be problematic. Click here for more details.

Files changed (31) hide show
  1. kaggle_environments/__init__.py +1 -1
  2. kaggle_environments/envs/chess/chess.js +15 -0
  3. kaggle_environments/envs/chess/chess.json +5 -4
  4. kaggle_environments/envs/chess/chess.py +206 -51
  5. kaggle_environments/envs/chess/test_chess.py +43 -1
  6. kaggle_environments/envs/lux_ai_s3/agents.py +4 -0
  7. kaggle_environments/envs/lux_ai_s3/index.html +42 -0
  8. kaggle_environments/envs/lux_ai_s3/lux_ai_s3.json +47 -0
  9. kaggle_environments/envs/lux_ai_s3/lux_ai_s3.py +138 -0
  10. kaggle_environments/envs/lux_ai_s3/luxai_s3/__init__.py +1 -0
  11. kaggle_environments/envs/lux_ai_s3/luxai_s3/env.py +924 -0
  12. kaggle_environments/envs/lux_ai_s3/luxai_s3/globals.py +13 -0
  13. kaggle_environments/envs/lux_ai_s3/luxai_s3/params.py +101 -0
  14. kaggle_environments/envs/lux_ai_s3/luxai_s3/profiler.py +140 -0
  15. kaggle_environments/envs/lux_ai_s3/luxai_s3/pygame_render.py +270 -0
  16. kaggle_environments/envs/lux_ai_s3/luxai_s3/spaces.py +30 -0
  17. kaggle_environments/envs/lux_ai_s3/luxai_s3/state.py +399 -0
  18. kaggle_environments/envs/lux_ai_s3/luxai_s3/utils.py +12 -0
  19. kaggle_environments/envs/lux_ai_s3/luxai_s3/wrappers.py +187 -0
  20. kaggle_environments/envs/lux_ai_s3/test_agents/python/agent.py +71 -0
  21. kaggle_environments/envs/lux_ai_s3/test_agents/python/lux/__init__.py +0 -0
  22. kaggle_environments/envs/lux_ai_s3/test_agents/python/lux/kit.py +27 -0
  23. kaggle_environments/envs/lux_ai_s3/test_agents/python/lux/utils.py +17 -0
  24. kaggle_environments/envs/lux_ai_s3/test_agents/python/main.py +53 -0
  25. kaggle_environments/envs/lux_ai_s3/test_lux.py +9 -0
  26. {kaggle_environments-1.15.3.dist-info → kaggle_environments-1.16.1.dist-info}/METADATA +2 -2
  27. {kaggle_environments-1.15.3.dist-info → kaggle_environments-1.16.1.dist-info}/RECORD +31 -11
  28. {kaggle_environments-1.15.3.dist-info → kaggle_environments-1.16.1.dist-info}/LICENSE +0 -0
  29. {kaggle_environments-1.15.3.dist-info → kaggle_environments-1.16.1.dist-info}/WHEEL +0 -0
  30. {kaggle_environments-1.15.3.dist-info → kaggle_environments-1.16.1.dist-info}/entry_points.txt +0 -0
  31. {kaggle_environments-1.15.3.dist-info → kaggle_environments-1.16.1.dist-info}/top_level.txt +0 -0
@@ -20,7 +20,7 @@ from .core import *
20
20
  from .main import http_request
21
21
  from . import errors
22
22
 
23
- __version__ = "1.15.3"
23
+ __version__ = "1.16.1"
24
24
 
25
25
  __all__ = ["Agent", "environments", "errors", "evaluate", "http_request",
26
26
  "make", "register", "utils", "__version__",
@@ -37,6 +37,21 @@ async function renderer(context) {
37
37
  c.fillRect(x, y, squareSize, squareSize);
38
38
  }
39
39
  }
40
+ // Draw the team names and game status
41
+ const info = environment.info;
42
+ const agent1 = info?.TeamNames?.[0] || "Agent 1";
43
+ const agent2 = info?.TeamNames?.[1] || "Agent 2";
44
+ const firstGame = environment.steps[step][0].observation.mark == "white"
45
+ const white = firstGame ? agent1 : agent2
46
+ const black = firstGame ? agent2 : agent1
47
+ const fontSize = Math.round(.33 * offset)
48
+ c.font = `${fontSize}px sans-serif`;
49
+ c.fillStyle = "#FFFFFF";
50
+ const agent1Reward = environment.steps[step][0].reward;
51
+ const agent2Reward = environment.steps[step][1].reward;
52
+ const charCount = agent1.length + agent2.length + 12;
53
+ const title = `${firstGame ? "\u25A0" : "\u25A1"}${agent1} (${agent1Reward}) vs ${firstGame ? "\u25A1" : "\u25A0"}${agent2} (${agent2Reward})`
54
+ c.fillText(title, offset + 4 * squareSize - Math.floor(charCount * fontSize / 4), 40)
40
55
 
41
56
  // Draw the Pieces
42
57
  const board = environment.steps[step][0].observation.board;
@@ -6,12 +6,13 @@
6
6
  "agents": [2],
7
7
  "configuration": {
8
8
  "episodeSteps": 1000,
9
- "actTimeout": 1,
9
+ "actTimeout": 0.1,
10
+ "runTimeout": 300,
10
11
  "agentTimeout": {
11
12
  "description": "Obsolete field kept for backwards compatibility, please use observation.remainingOverageTime.",
12
13
  "type": "number",
13
14
  "minimum": 0,
14
- "default": 60
15
+ "default": 20
15
16
  }
16
17
  },
17
18
  "reward": {
@@ -31,7 +32,7 @@
31
32
  "defaults": ["white", "black"],
32
33
  "enum": ["white", "black"]
33
34
  },
34
- "remainingOverageTime": 60
35
+ "remainingOverageTime": 20
35
36
  },
36
37
  "action": {
37
38
  "description": "Move in UCI notation (e.g., e2e4)",
@@ -41,4 +42,4 @@
41
42
  "status": {
42
43
  "defaults": ["ACTIVE", "INACTIVE"]
43
44
  }
44
- }
45
+ }
@@ -1,28 +1,193 @@
1
- import chess
1
+ import math
2
2
  import random
3
3
  import json
4
4
  from os import path
5
+ from collections import defaultdict
6
+
7
+ from Chessnut import Game
5
8
 
6
9
  ERROR = "ERROR"
7
10
  DONE = "DONE"
8
11
  INACTIVE = "INACTIVE"
9
12
  ACTIVE = "ACTIVE"
13
+ WHITE = "white"
14
+
15
+ OPENINGS = [
16
+ "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1",
17
+ "rnbqkbnr/1p2pp1p/p2p2p1/8/2PNP3/8/PP3PPP/RNBQKB1R w KQkq - 0 6",
18
+ "r1b1kb1r/ppppq1pp/2n2n2/1B2p3/4N3/5N2/PPPPQPPP/R1B1K2R w KQkq - 3 7",
19
+ "rnbqkb1r/p2ppppp/5n2/2pP4/2p5/2N5/PP2PPPP/R1BQKBNR w KQkq - 0 5",
20
+ "rnbqk1nr/p1p1bppp/1p2p3/3pP3/3P4/2N5/PPP2PPP/R1BQKBNR w KQkq - 0 5",
21
+ "r2qk1nr/ppp2pp1/2np3p/2b1p3/2B1P1b1/2PP1N2/PP3PPP/RNBQ1RK1 w kq - 0 7",
22
+ "rn1qk1nr/pp2ppbp/3p2p1/2p5/2PP2b1/2N1PN2/PP3PPP/R1BQKB1R w KQkq c6 0 6",
23
+ "rnbqkbnr/1p2pp1p/p2p2p1/8/2PNP3/8/PP3PPP/RNBQKB1R w KQkq - 0 6",
24
+ ]
25
+
26
+
27
+ MOVES = [
28
+ "",
29
+ "e2e4 c7c5 g1f3 d7d6 d2d4 c5d4 f3d4 a7a6 c2c4 g7g6",
30
+ "e2e4 e7e5 g1f3 b8c6 f1b5 f7f5 b1c3 f5e4 c3e4 g8f6 d1e2 d8e7",
31
+ "d2d4 g8f6 c2c4 c7c5 d4d5 b7b5 b1c3 b5c4",
32
+ "e2e4 e7e6 d2d4 d7d5 b1c3 f8e7 e4e5 b7b6",
33
+ "e2e4 e7e5 g1f3 b8c6 f1c4 f8c5 e1g1 d7d6 c2c3 c8g4 d2d3 h7h6",
34
+ "d2d4 g7g6 c2c4 f8g7 b1c3 d7d6 g1f3 c8g4 e2e3 c7c5",
35
+ "e2e4 c7c5 g1f3 d7d6 d2d4 c5d4 f3d4 a7a6 c2c4 g7g6"
36
+ ]
37
+
10
38
 
11
39
  def random_agent(obs):
12
- """
13
- Selects a random legal move from the board.
14
- Returns:
40
+ """
41
+ Selects a random legal move from the board.
42
+ Returns:
15
43
  A string representing the chosen move in UCI notation (e.g., "e2e4").
16
- """
17
- board = obs.board
18
- board_obj = chess.Board(board)
19
- moves = list(board_obj.legal_moves)
20
- return random.choice(moves).uci()
44
+ """
45
+ game = Game(obs.board)
46
+ moves = list(game.get_moves())
47
+ return random.choice(moves) if moves else None
48
+
49
+
50
+ def king_shuffle_agent(obs):
51
+ """Moves the king pawn and then shuffles the king."""
52
+ game = Game(obs.board)
53
+ moves = list(game.get_moves())
54
+
55
+ to_move = ["e7e5", "e2e4", "e8e7", "e7e8", "e1e2", "e2e1"]
56
+ for move in to_move:
57
+ if move in moves:
58
+ return move
59
+
60
+ # If no other moves are possible, pick a random legal move (or return None)
61
+ return random.choice(moves) if moves else None
62
+
63
+
64
+ def board_shuffle_agent(obs):
65
+ """Moves the king panw and then shuffles pieces."""
66
+ game = Game(obs.board)
67
+ moves = list(game.get_moves())
68
+
69
+ to_move = ["e7e5", "e2e4", "e8e7", "e7e6", "e1e2", "e2e3"]
70
+ for move in to_move:
71
+ if move in moves:
72
+ return move
73
+
74
+ # add shuffle moves for knights and bishop
75
+ to_shuffle = [
76
+ "b1c3",
77
+ "c3b1",
78
+ "g1f3",
79
+ "f3g1",
80
+ "b8c6",
81
+ "c6b8",
82
+ "g8f6",
83
+ "f6g8",
84
+ "f1e2",
85
+ "e2f1",
86
+ "f8e7",
87
+ "e7f8",
88
+ ]
89
+ # filter to only shuffle king moves
90
+ for move in moves:
91
+ f1 = move[0]
92
+ f2 = move[2]
93
+ r1 = int(move[1])
94
+ r2 = int(move[3])
95
+ df = abs(ord(f1) - ord(f2))
96
+ dr = abs(r2 - r1)
97
+ if r1 < 3 or r1 > 6:
98
+ continue
99
+ if r2 < 3 or r2 > 6:
100
+ continue
101
+ if dr > 1 or df > 1:
102
+ continue
103
+ if move[2:4] == "e5":
104
+ continue
105
+ if move[2:4] == "e4":
106
+ continue
107
+ to_shuffle.append(move)
108
+ random.shuffle(to_shuffle)
109
+ for move in to_shuffle:
110
+ if move in moves:
111
+ return move
112
+
113
+ # If no other moves are possible, pick a random legal move (or return None)
114
+ return random.choice(moves) if moves else None
115
+
116
+
117
+ agents = {
118
+ "random": random_agent,
119
+ "king_shuffle": king_shuffle_agent,
120
+ "board_shuffle": board_shuffle_agent}
121
+
122
+
123
+ def sufficient_material(pieces):
124
+ """Checks if the given pieces are sufficient for checkmate."""
125
+ if pieces['q'] > 0 or pieces['r'] > 0 or pieces['p'] > 0:
126
+ return True
127
+ # we only have knights and bishops left on this team
128
+ knight_bishop_count = pieces['n'] + pieces['b']
129
+ if knight_bishop_count >= 3:
130
+ return True
131
+ if knight_bishop_count == 2 and pieces['b'] >= 1:
132
+ return True
133
+
134
+ return False
135
+
136
+
137
+ def is_insufficient_material(board):
138
+ white_pieces = defaultdict(int)
139
+ black_pieces = defaultdict(int)
140
+
141
+ for square in range(64):
142
+ piece = board.get_piece(square)
143
+ if piece and piece != " ":
144
+ if piece.isupper():
145
+ white_pieces[piece.lower()] += 1
146
+ else:
147
+ black_pieces[piece.lower()] += 1
148
+
149
+ if not sufficient_material(
150
+ white_pieces) and not sufficient_material(black_pieces):
151
+ return True
152
+
153
+ return False
154
+
155
+
156
+ def square_str_to_int(square_str):
157
+ """Converts a square string (e.g., "a2") to an integer index (0-63)."""
158
+ if len(square_str) != 2 or not (
159
+ 'a' <= square_str[0] <= 'h' and '1' <= square_str[1] <= '8'):
160
+ raise ValueError("Invalid square string")
161
+
162
+ col = ord(square_str[0]) - ord('a') # Get column index (0-7)
163
+ row = int(square_str[1]) - 1 # Get row index (0-7)
164
+ return row * 8 + col
165
+
166
+
167
+ seen_positions = defaultdict(int)
168
+ game_one_complete = False
169
+ game_start_position = math.floor(random.random() * len(OPENINGS))
21
170
 
22
- agents = {"random": random_agent}
23
171
 
24
172
  def interpreter(state, env):
173
+ global seen_positions
174
+ global game_one_complete
175
+ global game_start_position
25
176
  if env.done:
177
+ game_one_complete = False
178
+ seen_positions = defaultdict(int)
179
+ game_start_position = math.floor(random.random() * len(OPENINGS))
180
+ state[0].observation.board = OPENINGS[game_start_position]
181
+ state[1].observation.board = OPENINGS[game_start_position]
182
+ return state
183
+
184
+ if state[0].status == ACTIVE and state[1].status == ACTIVE:
185
+ # set up new game
186
+ state[0].observation.mark, state[1].observation.mark = state[1].observation.mark, state[0].observation.mark
187
+ state[0].observation.board = OPENINGS[game_start_position]
188
+ state[1].observation.board = OPENINGS[game_start_position]
189
+ state[0].status = ACTIVE if state[0].observation.mark == WHITE else INACTIVE
190
+ state[0].status = ACTIVE if state[0].observation.mark == WHITE else INACTIVE
26
191
  return state
27
192
 
28
193
  # Isolate the active and inactive agents.
@@ -36,39 +201,44 @@ def interpreter(state, env):
36
201
  # The board is shared, only update the first state.
37
202
  board = state[0].observation.board
38
203
 
39
- # Create a chess board object from the FEN string
40
- board_obj = chess.Board(board)
204
+ # Create a chessnut game object from the FEN string
205
+ game = Game(board)
41
206
 
42
207
  # Get the action (move) from the agent
43
208
  action = active.action
44
209
 
45
210
  # Check if the move is legal
46
211
  try:
47
- move = chess.Move.from_uci(action)
48
- if not board_obj.is_legal(move):
49
- raise ValueError("Illegal move")
50
- except:
212
+ game.apply_move(action)
213
+ except BaseException:
51
214
  active.status = ERROR
52
215
  active.reward = -1
53
216
  inactive.status = DONE
54
217
  return state
55
-
56
- # Make the move
57
- board_obj.push(move)
218
+ fen = game.get_fen()
219
+ board_str = fen.split(" ", maxsplit=1)[0]
220
+ seen_positions[board_str] += 1
58
221
 
59
222
  # Update the board in the observation
60
- state[0].observation.board = board_obj.fen()
61
- state[1].observation.board = board_obj.fen()
223
+ state[0].observation.board = fen
224
+ state[1].observation.board = fen
62
225
 
226
+ terminal_state = DONE if game_one_complete else ACTIVE
227
+ pawn_or_capture_move_count = int(
228
+ fen.split(" ")[4]) # fen keeps track of this
63
229
  # Check for game end conditions
64
- if board_obj.is_checkmate():
65
- active.reward = 1
66
- active.status = DONE
67
- inactive.reward = -1
68
- inactive.status = DONE
69
- elif board_obj.is_stalemate() or board_obj.is_insufficient_material() or board_obj.is_game_over():
70
- active.status = DONE
71
- inactive.status = DONE
230
+ if pawn_or_capture_move_count == 100 or is_insufficient_material(
231
+ game.board) or seen_positions[board_str] >= 3 or game.status == Game.STALEMATE:
232
+ active.reward += .5
233
+ inactive.reward += .5
234
+ active.status = terminal_state
235
+ inactive.status = terminal_state
236
+ game_one_complete = True
237
+ elif game.status == Game.CHECKMATE:
238
+ active.reward += 1
239
+ active.status = terminal_state
240
+ inactive.status = terminal_state
241
+ game_one_complete = True
72
242
  else:
73
243
  # Switch turns
74
244
  active.status = INACTIVE
@@ -76,34 +246,19 @@ def interpreter(state, env):
76
246
 
77
247
  return state
78
248
 
249
+
79
250
  def renderer(state, env):
80
- board_str = state[0].observation.board
81
- board_obj = chess.Board(board_str)
82
-
83
- # Unicode characters for chess pieces
84
- piece_symbols = {
85
- 'P': '♙', 'R': '♖', 'N': '♘', 'B': '♗', 'Q': '♕', 'K': '♔',
86
- 'p': '♟', 'r': '♜', 'n': '♞', 'b': '♝', 'q': '♛', 'k': '♚',
87
- '.': ' ' # Empty square
88
- }
89
-
90
- board_repr = ""
91
- for square in chess.SQUARES:
92
- piece = board_obj.piece_at(square)
93
- if piece:
94
- board_repr += piece_symbols[piece.symbol()]
95
- else:
96
- board_repr += piece_symbols['.']
97
- if chess.square_file(square) == 7: # End of a rank
98
- board_repr += "\n"
251
+ board_fen = state[0].observation.board
252
+ game = Game(board_fen)
253
+ return game.board
99
254
 
100
- return board_repr
101
255
 
102
256
  jsonpath = path.abspath(path.join(path.dirname(__file__), "chess.json"))
103
257
  with open(jsonpath) as f:
104
258
  specification = json.load(f)
105
259
 
260
+
106
261
  def html_renderer():
107
262
  jspath = path.abspath(path.join(path.dirname(__file__), "chess.js"))
108
- with open(jspath) as f:
109
- return f.read()
263
+ with open(jspath) as g:
264
+ return g.read()
@@ -1,8 +1,50 @@
1
1
  from kaggle_environments import make
2
+ from Chessnut import Game
3
+ from chess import is_insufficient_material
2
4
 
3
5
  def test_chess_inits():
4
6
  env = make("chess", debug=True)
5
7
  env.run(["random", "random"])
6
8
  json = env.toJSON()
7
9
  assert json["name"] == "chess"
8
- assert json["statuses"] == ["ERROR", "DONE"]
10
+ assert json["statuses"] == ["DONE", "DONE"]
11
+
12
+ def test_chess_three_fold():
13
+ env = make("chess", debug=True)
14
+ env.run(["king_shuffle", "king_shuffle"])
15
+ json = env.toJSON()
16
+ assert json["name"] == "chess"
17
+ assert json["statuses"] == ["DONE", "DONE"]
18
+ assert json["rewards"] == [0, 0]
19
+
20
+ def test_chess_100_move_rule():
21
+ env = make("chess", debug=True)
22
+ env.run(["board_shuffle", "board_shuffle"])
23
+ json = env.toJSON()
24
+ assert json["name"] == "chess"
25
+ assert json["statuses"] == ["DONE", "DONE"]
26
+ assert json["rewards"] == [0, 0]
27
+
28
+ def test_sufficient_material():
29
+ game = Game()
30
+ assert not is_insufficient_material(game.board)
31
+
32
+ def test_insufficient_material_with_two_kings():
33
+ game = Game('8/8/K7/8/8/3k4/8/8 w - - 58 282')
34
+ assert is_insufficient_material(game.board)
35
+
36
+ def test_insufficient_material_with_two_kings_and_bishop():
37
+ game = Game('6k1/8/7B/8/8/8/8/2K5 b - - 90 250')
38
+ assert is_insufficient_material(game.board)
39
+
40
+ def test_insufficient_material_with_two_kings_and_two_knights():
41
+ game = Game('6k1/8/6NN/8/8/8/8/2K5 b - - 90 250')
42
+ assert is_insufficient_material(game.board)
43
+
44
+ def test_sufficient_material_with_king_knight_and_bishop():
45
+ game = Game('6k1/8/6NB/8/8/8/8/2K5 b - - 90 250')
46
+ assert not is_insufficient_material(game.board)
47
+
48
+ def test_sufficient_material_with_king_bishop_and_bishop():
49
+ game = Game('6k1/8/6BB/8/8/8/8/2K5 b - - 90 250')
50
+ assert not is_insufficient_material(game.board)
@@ -0,0 +1,4 @@
1
+ from .test_agents.python.main import agent_fn as random_agent
2
+ all_agents = {
3
+ "random_agent": random_agent,
4
+ }
@@ -0,0 +1,42 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8" />
5
+ <link rel="icon" type="image/svg+xml" href="https://s3vis.lux-ai.org/eye.svg" />
6
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
7
+
8
+ <title>Lux Eye S3</title>
9
+
10
+ <!-- Start Single Page Apps for GitHub Pages -->
11
+ <script type="text/javascript">
12
+ // Single Page Apps for GitHub Pages
13
+ // MIT License
14
+ // https://github.com/rafgraph/spa-github-pages
15
+ // This script checks to see if a redirect is present in the query string,
16
+ // converts it back into the correct url and adds it to the
17
+ // browser's history using window.history.replaceState(...),
18
+ // which won't cause the browser to attempt to load the new url.
19
+ // When the single page app is loaded further down in this file,
20
+ // the correct url will be waiting in the browser's history for
21
+ // the single page app to route accordingly.
22
+ (function (l) {
23
+ if (l.search[1] === '/') {
24
+ const decoded = l.search
25
+ .slice(1)
26
+ .split('&')
27
+ .map(function (s) {
28
+ return s.replace(/~and~/g, '&');
29
+ })
30
+ .join('?');
31
+ window.history.replaceState(null, null, l.pathname.slice(0, -1) + decoded + l.hash);
32
+ }
33
+ })(window.location);
34
+ </script>
35
+ <!-- End Single Page Apps for GitHub Pages -->
36
+ <script type="module" crossorigin src="https://s3vis.lux-ai.org/index.js"></script>
37
+ </head>
38
+ <body>
39
+ <div id="root"></div>
40
+
41
+ </body>
42
+ </html>
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "lux_ai_s3",
3
+ "title": "Lux AI Challenge Season 3",
4
+ "description": "A Novel AI Programming Challenge about Lux",
5
+ "version": "0.0.1",
6
+ "agents": [2],
7
+ "configuration": {
8
+ "episodeSteps": 506,
9
+ "seed": {
10
+ "description": "Seed to use for episodes",
11
+ "type": "integer"
12
+ },
13
+ "actTimeout": 3,
14
+ "runTimeout": 60,
15
+ "env_cfg": {
16
+ "description": "Environment configuration. Not to be filled out by user as it is randomly generated.",
17
+ "type": "object"
18
+ }
19
+ },
20
+ "reward": {
21
+ "description": "Reward of the agent. Equal to number of games won.",
22
+ "type": "integer",
23
+ "default": 0
24
+ },
25
+ "observation": {
26
+ "remainingOverageTime": 60,
27
+ "reward": {
28
+ "description": "Current reward of the agent. Equal to amount of lichen grown.",
29
+ "type": "integer",
30
+ "default": 0
31
+ },
32
+ "obs": {
33
+ "description": "String containing the observations",
34
+ "type": "string",
35
+ "shared": false
36
+ },
37
+ "player": {
38
+ "description": "Current player's index / team id",
39
+ "type": "string",
40
+ "defaults": "player_0"
41
+ }
42
+ },
43
+ "action": {
44
+ "description": "Actions",
45
+ "type": "object"
46
+ }
47
+ }
@@ -0,0 +1,138 @@
1
+ import dataclasses
2
+ import json
3
+ import math
4
+ import os
5
+ import random
6
+ import sys
7
+ from os import path
8
+ from .agents import all_agents
9
+ from sys import path as syspath
10
+ from os import path as osp
11
+
12
+ # next two lines enables importing local packages e.g. luxai_s3
13
+ __dir__ = osp.dirname(__file__)
14
+ syspath.append(__dir__)
15
+
16
+ from luxai_s3.wrappers import LuxAIS3GymEnv, RecordEpisode
17
+ import numpy as np
18
+
19
+ import copy
20
+ import json
21
+ def to_json(state):
22
+ if isinstance(state, np.ndarray):
23
+ return state.tolist()
24
+ elif isinstance(state, np.int64):
25
+ return state.tolist()
26
+ elif isinstance(state, list):
27
+ return [to_json(s) for s in state]
28
+ elif isinstance(state, dict):
29
+ out = {}
30
+ for k in state:
31
+ out[k] = to_json(state[k])
32
+ return out
33
+ else:
34
+ return state
35
+ prev_step = 0
36
+ luxenv: RecordEpisode = None
37
+ prev_obs = None
38
+ state_obs = None
39
+ default_env_cfg = None
40
+ def enqueue_output(out, queue):
41
+ for line in iter(out.readline, b''):
42
+ queue.put(line)
43
+ out.close()
44
+
45
+ def interpreter(state, env):
46
+ global luxenv, prev_obs, state_obs, default_env_cfg
47
+ player_0 = state[0]
48
+ player_1 = state[1]
49
+ # filter out actions such as debug annotations so they aren't saved
50
+ # filter_actions(state, env)
51
+
52
+ if env.done:
53
+ if "seed" in env.configuration:
54
+ seed = int(env.configuration["seed"])
55
+ else:
56
+ seed = math.floor(random.random() * 1e9);
57
+ env.configuration["seed"] = seed
58
+
59
+ luxenv = LuxAIS3GymEnv(numpy_output=True)
60
+ luxenv = RecordEpisode(luxenv, save_on_close=False, save_on_reset=False)
61
+ obs, info = luxenv.reset(seed=seed)
62
+
63
+ env_cfg_json = info["params"]
64
+
65
+ env.configuration.env_cfg = env_cfg_json
66
+
67
+ player_0.observation.player = "player_0"
68
+ player_1.observation.player = "player_1"
69
+ player_0.observation.obs = json.dumps(to_json(obs["player_0"]))
70
+ player_1.observation.obs = json.dumps(to_json(obs["player_1"]))
71
+
72
+ replay_frame = luxenv.serialize_episode_data(dict(
73
+ states=[luxenv.episode["states"][-1]],
74
+ metadata=luxenv.episode["metadata"],
75
+ params=luxenv.episode["params"]
76
+ ))
77
+ # don't need to keep metadata/params beyond first step
78
+ player_0.info = dict(replay=replay_frame)
79
+ return state
80
+
81
+ new_state_obs, rewards, terminations, truncations, infos = luxenv.step({
82
+ "player_0": np.array(player_0.action["action"]),
83
+ "player_1": np.array(player_1.action["action"])
84
+ })
85
+
86
+ # cannot store np arrays in replay jsons so must convert to list
87
+ player_0.action = player_0.action["action"]
88
+ player_1.action = player_1.action["action"]
89
+
90
+ dones = dict()
91
+ for k in terminations:
92
+ dones[k] = terminations[k] | truncations[k]
93
+
94
+ player_0.observation.player = "player_0"
95
+ player_1.observation.player = "player_1"
96
+
97
+ player_0.observation.obs = json.dumps(to_json(new_state_obs["player_0"]))
98
+ player_1.observation.obs = json.dumps(to_json(new_state_obs["player_1"]))
99
+
100
+
101
+ player_0.reward = int(rewards["player_0"])
102
+ player_1.reward = int(rewards["player_1"])
103
+
104
+ player_0.observation.reward = int(player_0.reward)
105
+ player_1.observation.reward = int(player_1.reward)
106
+ replay_frame = luxenv.serialize_episode_data(dict(
107
+ states=[luxenv.episode["states"][-1]],
108
+ actions=[luxenv.episode["actions"][-1]],
109
+ metadata=luxenv.episode["metadata"],
110
+ params=luxenv.episode["params"]
111
+ ))
112
+ # don't need to keep metadata/params beyond first step
113
+ del replay_frame["metadata"]
114
+ del replay_frame["params"]
115
+ player_0.info = dict(replay=replay_frame)
116
+
117
+ if np.all([dones[k] for k in dones]):
118
+ if player_0.status == "ACTIVE":
119
+ player_0.status = "DONE"
120
+ if player_1.status == "ACTIVE":
121
+ player_1.status = "DONE"
122
+ return state
123
+
124
+ def renderer(state, env):
125
+ raise NotImplementedError("To render the replay, please set the render mode to json or html")
126
+
127
+
128
+ dir_path = path.dirname(__file__)
129
+ json_path = path.abspath(path.join(dir_path, "lux_ai_s3.json"))
130
+ with open(json_path) as json_file:
131
+ specification = json.load(json_file)
132
+
133
+
134
+ def html_renderer():
135
+ html_path = path.abspath(path.join(dir_path, "index.html"))
136
+ return ("html_path", html_path)
137
+
138
+ agents = all_agents
@@ -0,0 +1 @@
1
+ from .env import LuxAIS3Env