try remote strat
Browse files- ChessBot-Dataset.py +37 -117
ChessBot-Dataset.py
CHANGED
|
@@ -1,137 +1,57 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
|
| 13 |
-
# class ChessPGNDataset(GeneratorBasedBuilder):
|
| 14 |
-
# VERSION = "0.0.1"
|
| 15 |
-
|
| 16 |
-
# def _info(self):
|
| 17 |
-
# return DatasetInfo(
|
| 18 |
-
# description="Chess positions + moves + results, streamed from PGN shards",
|
| 19 |
-
# features=Features({
|
| 20 |
-
# "state": Array2D((8,8), dtype="int8"),
|
| 21 |
-
# "action": Value("int16"),
|
| 22 |
-
# "result": Value("int8"),
|
| 23 |
-
# })
|
| 24 |
-
# )
|
| 25 |
-
|
| 26 |
-
# def _split_generators(self, dl_manager: DownloadManager):
|
| 27 |
-
# from pathlib import Path
|
| 28 |
-
|
| 29 |
-
# data_dir = Path(self.config.data_dir)
|
| 30 |
-
|
| 31 |
-
# train_shards = sorted(data_dir.joinpath("train").glob("*.pgn"))
|
| 32 |
-
# test_shards = sorted(data_dir.joinpath("test").glob("*.pgn"))
|
| 33 |
-
# train_paths = dl_manager.download([str(p) for p in train_shards])
|
| 34 |
-
# test_paths = dl_manager.download([str(p) for p in test_shards])
|
| 35 |
-
|
| 36 |
-
# return [
|
| 37 |
-
# SplitGenerator(
|
| 38 |
-
# name=Split.TRAIN,
|
| 39 |
-
# gen_kwargs={"shards": train_paths},
|
| 40 |
-
# ),
|
| 41 |
-
# SplitGenerator(
|
| 42 |
-
# name=Split.TEST,
|
| 43 |
-
# gen_kwargs={"shards": test_paths},
|
| 44 |
-
# ),
|
| 45 |
-
# ]
|
| 46 |
-
# def _generate_examples(self, shards):
|
| 47 |
-
# import chess.pgn
|
| 48 |
-
# uid = 0
|
| 49 |
-
|
| 50 |
-
# for path in sorted(shards):
|
| 51 |
-
# with open(path, "r") as f:
|
| 52 |
-
# while (game := chess.pgn.read_game(f)) is not None:
|
| 53 |
-
# board = game.board()
|
| 54 |
-
# base = {"1-0":1,"0-1":-1}.get(game.headers["Result"], 0)
|
| 55 |
-
# for move in game.mainline_moves():
|
| 56 |
-
# state = ChessEnv.get_piece_configuration(board)
|
| 57 |
-
# state = state if board.turn else -state
|
| 58 |
-
# action = ChessEnv.move_to_action(move)
|
| 59 |
-
# result = base * (-1 if board.turn == 0 else 1)
|
| 60 |
-
# yield uid, {
|
| 61 |
-
# "state": state.astype("int8"),
|
| 62 |
-
# "action": int(action),
|
| 63 |
-
# "result": int(result),
|
| 64 |
-
# }
|
| 65 |
-
# uid += 1
|
| 66 |
-
# board.push(move)
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
from datasets import GeneratorBasedBuilder, DatasetInfo, SplitGenerator, Split, Features, Value, Array2D, DownloadManager
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
from pathlib import Path
|
| 75 |
-
from datasets import BuilderConfig, Version
|
| 76 |
-
|
| 77 |
-
class ChessPGNConfig(BuilderConfig):
|
| 78 |
-
def __init__( self,
|
| 79 |
-
name: str = "default",
|
| 80 |
-
version: Version = Version("1.0.0"),
|
| 81 |
-
description: str = "PGN chess shards",
|
| 82 |
-
data_dir: str = None,
|
| 83 |
-
data_files: dict = None,
|
| 84 |
-
**kwargs):
|
| 85 |
-
super().__init__(
|
| 86 |
-
name=name,
|
| 87 |
-
version=version,
|
| 88 |
-
description=description,
|
| 89 |
-
data_dir=data_dir,
|
| 90 |
-
data_files=data_files or {},
|
| 91 |
-
**kwargs
|
| 92 |
-
)
|
| 93 |
class ChessPGNDataset(GeneratorBasedBuilder):
|
| 94 |
-
|
| 95 |
-
BUILDER_CONFIGS = [
|
| 96 |
-
ChessPGNConfig(
|
| 97 |
-
name="default",
|
| 98 |
-
data_files={
|
| 99 |
-
"train": [str(p) for p in Path("train").glob("*.pgn")],
|
| 100 |
-
"test": [str(p) for p in Path("test").glob("*.pgn")],
|
| 101 |
-
},
|
| 102 |
-
)
|
| 103 |
-
]
|
| 104 |
-
|
| 105 |
-
DEFAULT_WRITER_BATCH_SIZE = 512
|
| 106 |
|
| 107 |
def _info(self):
|
| 108 |
return DatasetInfo(
|
|
|
|
| 109 |
features=Features({
|
| 110 |
"state": Array2D((8,8), dtype="int8"),
|
| 111 |
"action": Value("int16"),
|
| 112 |
"result": Value("int8"),
|
| 113 |
-
})
|
| 114 |
)
|
| 115 |
|
| 116 |
def _split_generators(self, dl_manager: DownloadManager):
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
return [
|
| 121 |
-
SplitGenerator(
|
| 122 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
]
|
| 124 |
-
|
| 125 |
def _generate_examples(self, shards):
|
| 126 |
-
import chess.pgn
|
| 127 |
-
from adversarial_gym.chess_env import ChessEnv
|
| 128 |
-
|
| 129 |
uid = 0
|
|
|
|
| 130 |
for path in sorted(shards):
|
| 131 |
-
with open(path) as f:
|
| 132 |
while (game := chess.pgn.read_game(f)) is not None:
|
| 133 |
board = game.board()
|
| 134 |
-
base = {"1-0":1,"0-1":-1}.get(game.headers["Result"],0)
|
| 135 |
for move in game.mainline_moves():
|
| 136 |
state = ChessEnv.get_piece_configuration(board)
|
| 137 |
state = state if board.turn else -state
|
|
@@ -139,8 +59,8 @@ class ChessPGNDataset(GeneratorBasedBuilder):
|
|
| 139 |
result = base * (-1 if board.turn == 0 else 1)
|
| 140 |
yield uid, {
|
| 141 |
"state": state.astype("int8"),
|
| 142 |
-
"action":
|
| 143 |
-
"result":
|
| 144 |
}
|
| 145 |
uid += 1
|
| 146 |
board.push(move)
|
|
|
|
| 1 |
+
from datasets import (
|
| 2 |
+
GeneratorBasedBuilder,
|
| 3 |
+
DatasetInfo,
|
| 4 |
+
SplitGenerator,
|
| 5 |
+
Split,
|
| 6 |
+
DownloadManager,
|
| 7 |
+
Features,
|
| 8 |
+
Value,
|
| 9 |
+
Array2D,
|
| 10 |
+
)
|
| 11 |
+
from adversarial_gym.chess_env import ChessEnv
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
class ChessPGNDataset(GeneratorBasedBuilder):
|
| 14 |
+
VERSION = "0.0.1"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
def _info(self):
|
| 17 |
return DatasetInfo(
|
| 18 |
+
description="Chess positions + moves + results, streamed from PGN shards",
|
| 19 |
features=Features({
|
| 20 |
"state": Array2D((8,8), dtype="int8"),
|
| 21 |
"action": Value("int16"),
|
| 22 |
"result": Value("int8"),
|
| 23 |
+
})
|
| 24 |
)
|
| 25 |
|
| 26 |
def _split_generators(self, dl_manager: DownloadManager):
|
| 27 |
+
from pathlib import Path
|
| 28 |
+
|
| 29 |
+
data_dir = Path(self.config.data_dir)
|
| 30 |
+
|
| 31 |
+
train_shards = sorted(data_dir.joinpath("train").glob("*.pgn"))
|
| 32 |
+
test_shards = sorted(data_dir.joinpath("test").glob("*.pgn"))
|
| 33 |
+
train_paths = dl_manager.download([str(p) for p in train_shards])
|
| 34 |
+
test_paths = dl_manager.download([str(p) for p in test_shards])
|
| 35 |
+
|
| 36 |
return [
|
| 37 |
+
SplitGenerator(
|
| 38 |
+
name=Split.TRAIN,
|
| 39 |
+
gen_kwargs={"shards": train_paths},
|
| 40 |
+
),
|
| 41 |
+
SplitGenerator(
|
| 42 |
+
name=Split.TEST,
|
| 43 |
+
gen_kwargs={"shards": test_paths},
|
| 44 |
+
),
|
| 45 |
]
|
|
|
|
| 46 |
def _generate_examples(self, shards):
|
| 47 |
+
import chess.pgn
|
|
|
|
|
|
|
| 48 |
uid = 0
|
| 49 |
+
|
| 50 |
for path in sorted(shards):
|
| 51 |
+
with open(path, "r") as f:
|
| 52 |
while (game := chess.pgn.read_game(f)) is not None:
|
| 53 |
board = game.board()
|
| 54 |
+
base = {"1-0":1,"0-1":-1}.get(game.headers["Result"], 0)
|
| 55 |
for move in game.mainline_moves():
|
| 56 |
state = ChessEnv.get_piece_configuration(board)
|
| 57 |
state = state if board.turn else -state
|
|
|
|
| 59 |
result = base * (-1 if board.turn == 0 else 1)
|
| 60 |
yield uid, {
|
| 61 |
"state": state.astype("int8"),
|
| 62 |
+
"action": int(action),
|
| 63 |
+
"result": int(result),
|
| 64 |
}
|
| 65 |
uid += 1
|
| 66 |
board.push(move)
|