def save(self, name: Optional[str] = None) -> None: if not self._save_dir_path.exists(): Utils.create_directory(self._save_dir_path) if name is None: name = f'{type(self).__name__}_{Utils.get_now_as_str()}' name += '.pt' torch.save(self._policy_net.state_dict(), self._save_dir_path.joinpath(name))
def save_model(self, name: Optional[str] = None) -> None: if name is None: now = Utils.get_now_as_str() player_name = self.name.replace(' ', '_').replace('(', '').replace( ')', '').strip() name = f'{player_name}_{now}' self._nn.save(name)
def __init__(self): self._alpha = self.ALPHA # Learning rate self._gamma = self.GAMMA # Future reward discount self._state_interpreter = StateInterpreter() self._device = 'cuda' if torch.cuda.is_available() else 'cpu' self._policy_net = self._create_neural_network() self._loss = nn.MSELoss() self._optim = optim.Adam(self._policy_net.parameters(), self._alpha) if self.LOAD_PATH is not None: self.load(self.LOAD_PATH) self._save_dir_path = Utils.get_base_dir().joinpath(self.SAVE_DIR)
def _create_unique_log_file(self, log_dir_path: Path) -> None: extension = 'txt' log_name = Utils.get_now_as_str() log_path = log_dir_path.joinpath('.'.join([log_name, extension])) cnt = 0 while log_path.exists(): log_path = log_dir_path.joinpath('.'.join( [log_name, '_', str(cnt), extension])) cnt += 1 with open(str(log_path), 'w') as file: file.write('') self._log_path = log_path
def test_verify_bid_7(self): bid1 = [10, 2] bid2 = [10, 1] self.assertTrue(Utils.verify_bet(bid1, bid2))
def test_verify_bid_4(self): bid1 = [3, 4] bid2 = [3, 4] self.assertFalse(Utils.verify_bet(bid1, bid2))
def test_verify_bid_6(self): bid1 = [3, 1] bid2 = [4, 3] self.assertTrue(Utils.verify_bet(bid1, bid2))
def test_verify_bid_8(self): bid1 = [10, 1] bid2 = [1, 3] self.assertFalse(Utils.verify_bet(bid1, bid2))
def test_verify_bid_3(self): bid1 = [3, 3] bid2 = [3, 4] self.assertTrue(Utils.verify_bet(bid1, bid2))
def test_AI_bet_up_quantity(self): dice_number = 36 last_bet = [4, 1] ai_bet = Utils.AI_bet(last_bet, dice_number) self.assertTrue(Utils.verify_bet(last_bet, ai_bet))
def test_AI_bet_call_lie(self): dice_number = 36 last_bet = [7, 1] ai_bet = Utils.AI_bet(last_bet, dice_number) self.assertTrue(Utils.verify_bet(last_bet, ai_bet))
def test_AI_bet_call_lie_decimal(self): dice_number = 32 last_bet = [5, 1] ai_bet = Utils.AI_bet(last_bet, dice_number) self.assertTrue(Utils.verify_bet(last_bet, ai_bet))
def test_AI_bet_up_value_perudo(self): dice_number = 36 last_bet = [6, 6] ai_bet = Utils.AI_bet(last_bet, dice_number) self.assertTrue(Utils.verify_bet(last_bet, ai_bet))
def test_verify_bid_10(self): bid1 = [2, 1] bid2 = [2, 2] self.assertFalse(Utils.verify_bet(bid1, bid2))
def _create_default_log(self) -> None: log_dir_name = 'log' project_path = Utils.get_base_dir() log_dir_path = project_path.joinpath(log_dir_name) Utils.create_directory(log_dir_path) self._create_unique_log_file(log_dir_path)
def test_verify_bid_1(self): bid1 = [0, 0] bid2 = [2, 3] self.assertTrue(Utils.verify_bet(bid1, bid2))
def _generate_log_dir_path(self) -> str: comment = self._generate_comment() return str(Utils.get_base_dir().joinpath('runs').joinpath( f'{Utils.get_now_as_str()}: {comment}'))