File size: 5,341 Bytes
07e6790 56f9d69 07e6790 56f9d69 07e6790 56f9d69 07e6790 56f9d69 b661446 07e6790 56f9d69 07e6790 56f9d69 07e6790 56f9d69 07e6790 d153237 07e6790 56f9d69 07e6790 56f9d69 07e6790 56f9d69 07e6790 56f9d69 |
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 |
# from datasets import (
# GeneratorBasedBuilder,
# DatasetInfo,
# SplitGenerator,
# Split,
# DownloadManager,
# Features,
# Value,
# Array2D,
# )
# from adversarial_gym.chess_env import ChessEnv
# class ChessPGNDataset(GeneratorBasedBuilder):
# VERSION = "0.0.1"
# def _info(self):
# return DatasetInfo(
# description="Chess positions + moves + results, streamed from PGN shards",
# features=Features({
# "state": Array2D((8,8), dtype="int8"),
# "action": Value("int16"),
# "result": Value("int8"),
# })
# )
# def _split_generators(self, dl_manager: DownloadManager):
# from pathlib import Path
# data_dir = Path(self.config.data_dir)
# train_shards = sorted(data_dir.joinpath("train").glob("*.pgn"))
# test_shards = sorted(data_dir.joinpath("test").glob("*.pgn"))
# train_paths = dl_manager.download([str(p) for p in train_shards])
# test_paths = dl_manager.download([str(p) for p in test_shards])
# return [
# SplitGenerator(
# name=Split.TRAIN,
# gen_kwargs={"shards": train_paths},
# ),
# SplitGenerator(
# name=Split.TEST,
# gen_kwargs={"shards": test_paths},
# ),
# ]
# def _generate_examples(self, shards):
# import chess.pgn
# uid = 0
# for path in sorted(shards):
# with open(path, "r") as f:
# while (game := chess.pgn.read_game(f)) is not None:
# board = game.board()
# base = {"1-0":1,"0-1":-1}.get(game.headers["Result"], 0)
# for move in game.mainline_moves():
# state = ChessEnv.get_piece_configuration(board)
# state = state if board.turn else -state
# action = ChessEnv.move_to_action(move)
# result = base * (-1 if board.turn == 0 else 1)
# yield uid, {
# "state": state.astype("int8"),
# "action": int(action),
# "result": int(result),
# }
# uid += 1
# board.push(move)
from datasets import GeneratorBasedBuilder, DatasetInfo, SplitGenerator, Split, Features, Value, Array2D, DownloadManager
from pathlib import Path
from datasets import BuilderConfig, Version
class ChessPGNConfig(BuilderConfig):
def __init__( self,
name: str = "default",
version: Version = Version("1.0.0"),
description: str = "PGN chess shards",
data_dir: str = None,
data_files: dict = None,
**kwargs):
super().__init__(
name=name,
version=version,
description=description,
data_dir=data_dir,
data_files=data_files or {},
**kwargs
)
class ChessPGNDataset(GeneratorBasedBuilder):
BUILDER_CONFIG_CLASS = ChessPGNConfig
BUILDER_CONFIGS = [
ChessPGNConfig(
name="default",
data_files={
"train": [str(p) for p in Path("train").glob("*.pgn")],
"test": [str(p) for p in Path("test").glob("*.pgn")],
},
)
]
DEFAULT_WRITER_BATCH_SIZE = 512
def _info(self):
return DatasetInfo(
features=Features({
"state": Array2D((8,8), dtype="int8"),
"action": Value("int16"),
"result": Value("int8"),
}),
)
def _split_generators(self, dl_manager: DownloadManager):
# use config.data_files directly—no data_dir needed
train_files = dl_manager.download(self.config.data_files["train"])
test_files = dl_manager.download(self.config.data_files["test"])
return [
SplitGenerator(name=Split.TRAIN, gen_kwargs={"shards": train_files}),
SplitGenerator(name=Split.TEST, gen_kwargs={"shards": test_files}),
]
def _generate_examples(self, shards):
import chess.pgn, numpy as np
from adversarial_gym.chess_env import ChessEnv
uid = 0
for path in sorted(shards):
with open(path) as f:
while (game := chess.pgn.read_game(f)) is not None:
board = game.board()
base = {"1-0":1,"0-1":-1}.get(game.headers["Result"],0)
for move in game.mainline_moves():
state = ChessEnv.get_piece_configuration(board)
state = state if board.turn else -state
action = ChessEnv.move_to_action(move)
result = base * (-1 if board.turn == 0 else 1)
yield uid, {
"state": state.astype("int8"),
"action": np.int16(action),
"result": np.int8(result),
}
uid += 1
board.push(move)
|