def main_experiments(player: Player = None): logger = logging.getLogger(__name__) logger.setLevel(logging.INFO) colors = list(Color) colors.remove(Color.EMPTY) possible_codes = [p for p in itertools.product(colors, repeat=4)] if player is None: player = SmartPlayer(strategy=SmartStrategy.FIRST) rounds = [] for possible_code in possible_codes: start = datetime.datetime.now() # print(possible_code) code = Code(*possible_code) player.reset() round = player.play_game(50, code) rounds.append(round + 1) end = datetime.datetime.now() print(f"Code: {possible_code}, time: {end - start}, rounds: {round + 1}") print(f"Number of games: {len(rounds)}") print(f"Mean: {np.mean(rounds)}") print(f"Max: {max(rounds)}") print(f"Min: {min(rounds)}") return np.mean(rounds), max(rounds)
def __init__(self): self.possible_codes = [] colors = list(Color) colors.remove(Color.EMPTY) self.possible_codes = [p for p in itertools.product(colors, repeat=4)] self.possible_codes = [Code(*code) for code in self.possible_codes] self.code = None
def next_code(self, round=0): colors = list(Color) colors.remove(Color.EMPTY) code = Code(random.choice(colors), random.choice(colors), random.choice(colors), random.choice(colors)) self.code = code return self.code
def __init__(self, code: Code = None): if code is None: colors = list(Color) colors.remove(Color.EMPTY) self.code = Code(random.choice(colors), random.choice(colors), random.choice(colors), random.choice(colors)) else: self.code = code logger.info(self.code)
def main_single(player: Player, code: Code = None): logging.basicConfig(level=os.environ.get("LOGLEVEL", "INFO")) print("ready") if code is None: code = Code(Color.ORANGE, Color.PURPLE, Color.ORANGE, Color.YELLOW) if player is None: player = SmartPlayer() round = player.play_game(50, code) print(round + 1)
def next_code(self, round=0): if len(self.history) == 0: self.code = Code(Color.WHITE, Color.WHITE, Color.YELLOW, Color.YELLOW) return self.code self.possible_codes.update(self.history[-1]) self.code = self.possible_codes.get(0) if self.strategy == SmartStrategy.RANDOM: self.code = self.possible_codes.get_random() elif self.strategy == SmartStrategy.LAST: self.code = self.possible_codes.get(len(self.possible_codes) - 1) return self.code
def code_node_depth(player_old, parent_node, code, same_color_and_spot, same_color): player = deepcopy(player_old) if update_player(player, parent_node, code, same_color_and_spot, same_color): node = AnyNode(same_color_and_spot=same_color_and_spot, same_color=same_color, num_possibilites=len(player.possible_codes), parent=parent_node) for possible_code in player.possible_codes: code = Code(*possible_code) player.code = code result_node_depth(player, node, code) del player
def next_code(self, round=0): print(""" WHITE = 0 YELLOW = 1 ORANGE = 2 RED = 3 PINK = 4 PURPLE = 5 BLUE = 6 GREEN = 7""") value = "" while len(value) != 4: value = input("Choose 4 colours for this round: ") code_list = [] for spot in value: code_list.append(Color(int(spot))) code = Code(*code_list) self.code = code return self.code
def next_code(self, round=0): if len(self.history) == 0: # top 10 codes for 100 000 simulations for both of the strategies # (20 codes total) return random.choice([ Code(Color.ORANGE, Color.PINK, Color.WHITE, Color.YELLOW), Code(Color.PURPLE, Color.WHITE, Color.PINK, Color.PINK), Code(Color.PURPLE, Color.ORANGE, Color.RED, Color.ORANGE), Code(Color.RED, Color.PINK, Color.YELLOW, Color.RED), Code(Color.WHITE, Color.PINK, Color.ORANGE, Color.ORANGE), Code(Color.YELLOW, Color.RED, Color.ORANGE, Color.YELLOW), Code(Color.WHITE, Color.YELLOW, Color.YELLOW, Color.ORANGE), Code(Color.WHITE, Color.ORANGE, Color.PINK, Color.RED), Code(Color.RED, Color.RED, Color.RED, Color.PURPLE), Code(Color.WHITE, Color.ORANGE, Color.YELLOW, Color.YELLOW), Code(Color.RED, Color.PURPLE, Color.PINK, Color.PURPLE), Code(Color.WHITE, Color.WHITE, Color.ORANGE, Color.ORANGE), Code(Color.PINK, Color.PINK, Color.YELLOW, Color.RED), Code(Color.RED, Color.PINK, Color.RED, Color.RED), Code(Color.PINK, Color.WHITE, Color.PINK, Color.RED), Code(Color.PINK, Color.PURPLE, Color.PINK, Color.YELLOW), Code(Color.PURPLE, Color.PINK, Color.ORANGE, Color.YELLOW), Code(Color.WHITE, Color.PURPLE, Color.PINK, Color.ORANGE), Code(Color.ORANGE, Color.PINK, Color.ORANGE, Color.RED), Code(Color.YELLOW, Color.RED, Color.PINK, Color.PINK), ]) self.possible_codes.update(self.history[-1]) self.tree = MCTSTree(self.code, self.possible_codes) self.tree.build_tree(num_simulations=self.num_simulations) self.code = self.tree.get_best_move(self.strategy) return self.code
class TestCode(unittest.TestCase): @parameterized.expand([ [ "different1", Code(Color.PURPLE, Color.PURPLE, Color.PINK, Color.PURPLE), Code(Color.WHITE, Color.PURPLE, Color.YELLOW, Color.PURPLE), False ], [ "different2", Code(Color.WHITE, Color.PURPLE, Color.YELLOW, Color.PURPLE), Code(Color.PURPLE, Color.WHITE, Color.YELLOW, Color.PURPLE), False ], [ "same", Code(Color.PURPLE, Color.WHITE, Color.YELLOW, Color.PURPLE), Code(Color.PURPLE, Color.WHITE, Color.YELLOW, Color.PURPLE), True ], ]) def test_equals(self, name, c1: Code, c2: Code, result: bool): self.assertEqual(result, c1.equals(c2)) self.assertEqual(result, c2.equals(c1)) @parameterized.expand([ [ "completely_different", Code(Color.PINK, Color.ORANGE, Color.RED, Color.PINK), Code(Color.WHITE, Color.PURPLE, Color.YELLOW, Color.PURPLE), 0 ], [ "different1", Code(Color.PURPLE, Color.RED, Color.PINK, Color.PURPLE), Code(Color.WHITE, Color.PURPLE, Color.YELLOW, Color.PURPLE), 1 ], [ "different2", Code(Color.WHITE, Color.PURPLE, Color.YELLOW, Color.PURPLE), Code(Color.PURPLE, Color.WHITE, Color.YELLOW, Color.PURPLE), 2 ], [ "same", Code(Color.PURPLE, Color.WHITE, Color.YELLOW, Color.PURPLE), Code(Color.PURPLE, Color.WHITE, Color.YELLOW, Color.PURPLE), 4 ], ]) def test_count_same_color_and_spot(self, name, c1: Code, c2: Code, result: int): self.assertEqual(result, c1.count_same_color_and_spot(c2)) @parameterized.expand([ [ "completely_different", Code(Color.PINK, Color.ORANGE, Color.RED, Color.PINK), Code(Color.WHITE, Color.PURPLE, Color.YELLOW, Color.PURPLE), 0 ], [ "different1", Code(Color.PURPLE, Color.ORANGE, Color.PINK, Color.PINK), Code(Color.WHITE, Color.PURPLE, Color.YELLOW, Color.PURPLE), 1 ], [ "different2", Code(Color.WHITE, Color.PURPLE, Color.YELLOW, Color.PURPLE), Code(Color.PURPLE, Color.WHITE, Color.YELLOW, Color.PURPLE), 2 ], [ "different3", Code(Color.WHITE, Color.PINK, Color.WHITE, Color.PURPLE), Code(Color.PURPLE, Color.WHITE, Color.YELLOW, Color.PURPLE), 1 ], [ "same", Code(Color.PURPLE, Color.WHITE, Color.YELLOW, Color.PURPLE), Code(Color.PURPLE, Color.WHITE, Color.YELLOW, Color.PURPLE), 0 ], [ "difficult", Code(Color.WHITE, Color.WHITE, Color.YELLOW, Color.YELLOW), Code(Color.YELLOW, Color.YELLOW, Color.WHITE, Color.WHITE), 4 ], [ "difficult2", Code(Color.YELLOW, Color.YELLOW, Color.WHITE, Color.WHITE), Code(Color.WHITE, Color.WHITE, Color.YELLOW, Color.YELLOW), 4 ], [ "difficult3", Code(Color.YELLOW, Color.WHITE, Color.WHITE, Color.YELLOW), Code(Color.WHITE, Color.YELLOW, Color.PURPLE, Color.WHITE), 3 ], [ "difficult3", Code(Color.WHITE, Color.WHITE, Color.WHITE, Color.WHITE), Code(Color.WHITE, Color.WHITE, Color.YELLOW, Color.YELLOW), 0 ], [ "difficult4", Code(Color.WHITE, Color.WHITE, Color.ORANGE, Color.YELLOW), Code(Color.WHITE, Color.WHITE, Color.WHITE, Color.YELLOW), 0 ], ]) def test_count_same_color(self, name, c1: Code, c2: Code, result: int): self.assertEqual(result, c1.count_same_color(c2)) @parameterized.expand([ [ "completely_different", Code(Color.PINK, Color.ORANGE, Color.RED, Color.PINK), Code(Color.WHITE, Color.PURPLE, Color.YELLOW, Color.PURPLE), 0 ], [ "different1", Code(Color.PURPLE, Color.ORANGE, Color.PINK, Color.PINK), Code(Color.WHITE, Color.PURPLE, Color.YELLOW, Color.PURPLE), 5 ], [ "different2", Code(Color.WHITE, Color.PURPLE, Color.YELLOW, Color.PURPLE), Code(Color.PURPLE, Color.WHITE, Color.YELLOW, Color.PURPLE), 30 ], [ "same", Code(Color.PURPLE, Color.WHITE, Color.YELLOW, Color.PURPLE), Code(Color.PURPLE, Color.WHITE, Color.YELLOW, Color.PURPLE), 1000 ], ]) def test_evaluate(self, name, c1: Code, c2: Code, result: int): self.assertEqual(result, c1.get_points(c2, 0)) @parameterized.expand([ [ "false", Code(Color.PURPLE, Color.ORANGE, Color.PINK, Color.PINK), Color.WHITE, False ], [ "true", Code(Color.WHITE, Color.PURPLE, Color.YELLOW, Color.PURPLE), Color.WHITE, True ], ]) def test_contains(self, name, c1: Code, color: Color, result: bool): self.assertEqual(result, c1.contains(color)) @parameterized.expand([ [ "false", Code(Color.PURPLE, Color.ORANGE, Color.PINK, Color.PINK), Color.WHITE, 0, False ], [ "true", Code(Color.WHITE, Color.PURPLE, Color.YELLOW, Color.PURPLE), Color.WHITE, 0, True ], [ "false", Code(Color.WHITE, Color.PURPLE, Color.YELLOW, Color.PURPLE), Color.WHITE, 1, False ], [ "true", Code(Color.WHITE, Color.PURPLE, Color.YELLOW, Color.PURPLE), Color.PURPLE, 3, True ], ]) def test_contains_in_spot(self, name, c1: Code, color: Color, spot: int, result: bool): self.assertEqual(result, c1.contains_in_spot(color, spot))
def __init__(self): self.code = Code() self.history = []
class TestPossibleCodes(unittest.TestCase): def test_init(self): codes = PossibleCodes() self.assertEqual(6**4, len(codes.possible_codes)) def test_len(self): codes = PossibleCodes() self.assertEqual(len(codes.possible_codes), len(codes)) def test_get(self): codes = PossibleCodes() self.assertEqual( Code(Color.WHITE, Color.WHITE, Color.WHITE, Color.WHITE), codes.get(0)) @parameterized.expand([ [ "0", { 'code': Code(Color.WHITE, Color.WHITE, Color.WHITE, Color.WHITE), 'same_color': 0, 'same_color_and_spot': 3 }, 20 ], [ "1", { 'code': Code(Color.WHITE, Color.WHITE, Color.WHITE, Color.WHITE), 'same_color': 0, 'same_color_and_spot': 2 }, 150 ], [ "2", { 'code': Code(Color.WHITE, Color.WHITE, Color.YELLOW, Color.YELLOW), 'same_color': 0, 'same_color_and_spot': 4 }, 0 ], [ "3", { 'code': Code(Color.WHITE, Color.WHITE, Color.YELLOW, Color.YELLOW), 'same_color': 4, 'same_color_and_spot': 0 }, 1 ], [ "4", { 'code': Code(Color.WHITE, Color.WHITE, Color.YELLOW, Color.YELLOW), 'same_color': 2, 'same_color_and_spot': 0 }, 96 ], [ "5", { 'code': Code(Color.WHITE, Color.WHITE, Color.YELLOW, Color.YELLOW), 'same_color': 0, 'same_color_and_spot': 0 }, 256 ], [ "6", { 'code': Code(Color.PINK, Color.PINK, Color.PINK, Color.PINK), 'same_color': 0, 'same_color_and_spot': 0 }, 625 ], ]) def test_update(self, name, history, result): codes = PossibleCodes() codes.update(history) self.assertEqual(result, len(codes))
def test_get(self): codes = PossibleCodes() self.assertEqual( Code(Color.WHITE, Color.WHITE, Color.WHITE, Color.WHITE), codes.get(0))
# working correctly start = datetime.now() print(start) logger = logging.getLogger(__name__) logger.setLevel(logging.INFO) rounds = 50 colors = list(Color) colors.remove(Color.EMPTY) player = SmartPlayer() root = AnyNode() player.code = Code(Color.WHITE, Color.WHITE, Color.YELLOW, Color.YELLOW) result_node_depth(player, root, player.code) print("end") with open('tree.pkl', 'w') as f: pickle.dump(root, f) # with open('tree.txt', 'w') as f: # f.write(str(RenderTree(root))) end = datetime.now() print(f"Time: {end-start}")