Skip to content

Commit b85fc91

Browse files
committed
removed unused hashdict and hashlist
1 parent 70d0516 commit b85fc91

9 files changed

Lines changed: 27 additions & 40 deletions

File tree

Config.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ class _ConfigAgent:
2121
# hidden_sizes = tuple()
2222
c = 0.1
2323
learning_rate = 1e-4
24-
debug = True
25-
pretrain = False
24+
debug = False
25+
pretrain = True
2626

2727

2828
class Config(_ConfigPaths, _ConfigAgent):
@@ -40,6 +40,6 @@ class Config(_ConfigPaths, _ConfigAgent):
4040

4141

4242
if Config.debug:
43-
random.seed(69)
44-
np.random.seed(69)
45-
torch.random.manual_seed(69)
43+
random.seed(42)
44+
np.random.seed(42)
45+
torch.random.manual_seed(42)

agent/self_play.py

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,30 @@
1+
import random
12
from collections import deque
2-
from itertools import count, cycle, islice
3-
from more_itertools import windowed
3+
from itertools import count
44

55
import numpy as np
66
from tqdm import tqdm
77

88
from Config import Config
9+
from src.Game import Game
910
from .Agent import Agent
1011
from .policy import policy
11-
from src.Game import Game
1212

1313

1414
def self_play(
1515
agents: deque[Agent],
1616
) -> tuple[list[tuple[np.array, np.array, int]], list[Agent]]:
1717
states = []
1818
winners = []
19-
initial_state = Game(n_players=Config.n_players)
19+
game = Game(n_players=Config.n_players)
2020
for agent in agents:
2121
agent.eval()
22-
for agents_in_order in islice(
23-
windowed(cycle(agents), Config.n_players), Config.n_players
24-
):
25-
game = initial_state.copy()
26-
id_to_agent = dict(
27-
(player.id, agent) for agent, player in zip(agents_in_order, game.players)
28-
)
29-
results, winner = _perform_game(game, [], id_to_agent)
30-
states += results
31-
winners.append(winner)
22+
id_to_agent = dict(
23+
(player.id, agent) for agent, player in zip(random.sample(agents, Config.n_players), game.players)
24+
)
25+
results, winner = _perform_game(game, [], id_to_agent)
26+
states += results
27+
winners.append(winner)
3228
return states, winners
3329

3430

@@ -49,10 +45,10 @@ def _perform_game(
4945
list(
5046
(
5147
state[0].get_state(),
52-
(onehot_encoded_action := np.zeros(Config.n_actions), onehot_encoded_action.__setitem__(game.all_moves.index(state[1]), 1))[0],
48+
np.eye(Config.n_actions)[game.all_moves.index(state[1])],
5349
int(result[state[0].current_player.id] == 1),
5450
)
55-
for state in states
51+
for state in states if state[1] != game.null_move
5652
),
5753
id_to_agent[
5854
next(player.id for player in game.players if result[player.id])

hashabledict.py

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/Game.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class Game:
3636
_performed_the_last_move: dict = None
3737
_last_turn: bool = False
3838
_state_extractor: Type[StateExtractor] = StateExtractor
39-
_null_move: NullMove = NullMove()
39+
null_move: NullMove = NullMove()
4040

4141
def __post_init__(self):
4242
if not self.players:
@@ -84,7 +84,8 @@ def next_turn(self) -> None:
8484

8585
def is_terminal(self) -> bool:
8686
return all(self._performed_the_last_move.values()) or (
87-
not any(move for move in self.all_moves if move.is_valid(self))
87+
not self.null_move.is_valid(self)
88+
or not any(move for move in self.all_moves if move.is_valid(self))
8889
)
8990

9091
def get_results(self) -> dict[int, int]:
@@ -150,7 +151,9 @@ def copy(self) -> Self:
150151
return game
151152

152153
def get_possible_actions(self) -> tuple[Move, ...]:
153-
return tuple(move for move in self.all_moves if move.is_valid(self)) or ((self._null_move,) if self._null_move.is_valid(self) else tuple())
154+
return tuple(move for move in self.all_moves if move.is_valid(self)) or (
155+
(self.null_move,) if self.null_move.is_valid(self) else tuple()
156+
)
154157

155158
combos = combinations([{field.name: 1} for field in fields(BasicResources)], 3)
156159
all_moves = list(

src/entities/extended_lists/Aristocrats.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
from .hashablelist import hashablelist
21
from ..Aristocrat import Aristocrat, empty_aristocrat
32

43

5-
class Aristocrats(hashablelist):
4+
class Aristocrats(list):
65
def pop(self, index: int) -> Aristocrat:
76
aristocrat = super().pop(index)
87
self.append(empty_aristocrat)
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
from .hashablelist import hashablelist
2-
3-
4-
class PlayerAristocrats(hashablelist):
1+
class PlayerAristocrats(list):
52
@property
63
def points(self) -> int:
74
return sum(aristocrat.points for aristocrat in self)

src/entities/extended_lists/PlayerCards.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
from .hashablelist import hashablelist
21
from ..BasicResources import BasicResources
32

43

5-
class PlayerCards(hashablelist):
4+
class PlayerCards(list):
65
@property
76
def production(self) -> BasicResources:
87
if not self:

src/entities/extended_lists/PlayerReserve.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
from typing import Iterable
33

44
from ..Card import Card, empty_card
5-
from .hashablelist import hashablelist
65

76

8-
class PlayerReserve(hashablelist):
7+
class PlayerReserve(list):
98
def __init__(self, iterable: Iterable = None):
109
super().__init__(iterable or (empty_card for _ in range(3)))
1110

src/entities/extended_lists/hashablelist.py

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)