def fixbug(): from cchess_alphazero.config import Config from cchess_alphazero.lib.data_helper import get_game_data_filenames, read_game_data_from_file, write_game_data_to_file import cchess_alphazero.environment.static_env as senv c = Config('distribute') files = get_game_data_filenames(c.resource) cnt = 0 fix = 0 draw_cnt = 0 for filename in files: try: data = read_game_data_from_file(filename) except: print(f"error: {filename}") os.remove(filename) continue state = data[0] real_data = [state] need_fix = True draw = False action = None value = None is_red_turn = True for item in data[1:]: action = item[0] value = -item[1] if value == 0: need_fix = False draw = True draw_cnt += 1 break state = senv.step(state, action) is_red_turn = not is_red_turn real_data.append([action, value]) if not draw: game_over, v, final_move = senv.done(state) if final_move: v = -v is_red_turn = not is_red_turn if not is_red_turn: v = -v if not game_over: v = 1 # print(game_over, v, final_move, state) if v == data[1][1]: need_fix = False else: need_fix = True if need_fix: write_game_data_to_file(filename, real_data) # print(filename) fix += 1 cnt += 1 if cnt % 1000 == 0: print(cnt, fix, draw_cnt) print(f"all {cnt}, fix {fix}, draw {draw_cnt}")
def save_play_data(self, idx, data, value, score): rc = self.config.resource game_id = datetime.now().strftime("%Y%m%d-%H%M%S.%f") filename = rc.play_data_filename_tmpl % game_id path = os.path.join(rc.play_data_dir, filename) logger.info(f"Process {self.pid} save play data to {path}") write_game_data_to_file(path, data) logger.info(f"Uploading play data {filename} ...") red, black = data[0], data[1] return self.upload_eval_data(path, filename, red, black, value, score)
def save_play_data(self, idx, data, value, score): rc = self.config.resource game_id = datetime.now().strftime("%Y%m%d-%H%M%S.%f") filename = rc.play_data_filename_tmpl % game_id path = os.path.join(rc.play_data_dir, filename) logger.info("保存博弈数据到 %s" % (path)) write_game_data_to_file(path, data) logger.info(f"上传评测对局 {filename} ...") red, black = data[0], data[1] return self.upload_eval_data(path, filename, red, black, value, score)
def flush_buffer(self): rc = self.config.resource game_id = datetime.now().strftime("%Y%m%d-%H%M%S.%f") filename = rc.play_data_filename_tmpl % game_id path = os.path.join(rc.play_data_dir, filename) logger.info("保存博弈数据到 %s" % (path)) write_game_data_to_file(path, self.buffer) if self.config.internet.distributed: upload_worker = Thread(target=self.upload_play_data, args=(path, filename)) upload_worker.start() self.buffer = []
def save_play_data(self, idx, data): self.buffer += data if not idx % self.config.play_data.nb_game_in_file == 0: return rc = self.config.resource game_id = datetime.now().strftime("%Y%m%d-%H%M%S.%f") path = os.path.join(rc.play_data_dir, rc.play_data_filename_tmpl % game_id) logger.info(f"Process {self.pid} save play data to {path}") write_game_data_to_file(path, self.buffer) self.buffer = []
def save_play_data(self, idx, data): self.buffer += data if not idx % self.config.play_data.nb_game_in_file == 0: return rc = self.config.resource game_id = datetime.now().strftime("%Y%m%d-%H%M%S.%f") filename = rc.play_data_filename_tmpl % game_id path = os.path.join(rc.play_data_dir, filename) logger.info(f"Process {self.pid} save play data to {path}") write_game_data_to_file(path, self.buffer) if self.config.internet.distributed: upload_worker = Thread(target=self.upload_play_data, args=(path, filename), name="upload_worker") upload_worker.daemon = True upload_worker.start() self.buffer = []
def save_play_data(self, idx, data): self.buffer += data if not idx % self.config.play_data.nb_game_in_file == 0: return rc = self.config.resource game_id = datetime.now().strftime("%Y%m%d-%H%M%S.%f") filename = rc.play_data_filename_tmpl % game_id path = os.path.join(rc.play_data_dir, filename) logger.info(f"Process {self.pid} save play data to {path}") write_game_data_to_file(path, self.buffer) if self.config.internet.distributed: upload_worker = Thread(target=self.upload_play_data, args=(path, filename), name="upload_worker") upload_worker.daemon = True upload_worker.start() self.buffer = []
def save_play_data(self, idx): data = [] for i in range(len(self.red.moves)): data.append(self.red.moves[i]) if i < len(self.black.moves): data.append(self.black.moves[i]) self.buffer += data if not idx % self.config.play_data.nb_game_in_file == 0: return rc = self.config.resource game_id = datetime.now().strftime("%Y%m%d-%H%M%S.%f") path = os.path.join(rc.play_data_dir, rc.play_data_filename_tmpl % game_id) logger.info(f"Process {self.pid} save play data to {path}") write_game_data_to_file(path, self.buffer) self.buffer = []