Esempio n. 1
0
    def choose_act(self, mode, info=False):
        current_state_as_dict = StateAsDict(self.env.current_state_of_the_game)
        list_of_actions = self.env.action_space.list_of_actions
        if list_of_actions:
            best_action = None
            best_action_value = -float('inf')
            for action in list_of_actions:
                state_copy = current_state_as_dict.to_state()
                action.execute(state_copy)
                state_copy.change_active_player()
                # print('*******************')
                current_value = self.evaluator.evaluate(state_copy)
                # print(f'State_copy = {StateAsDict(state_copy)}')
                # print(f'Action = {action} val = {current_value}')
                # print('------------------------------------')
                if current_value > best_action_value:
                    best_action_value = current_value
                    best_action = action

            if not info:
                return best_action
            if info:
                return best_action, best_action_value

        else:
            if not info:
                return None
            if info:
                return None, -1
    def choose_act(self, mode, info=False):

        current_state_as_dict = StateAsDict(self.env.current_state_of_the_game)
        list_of_actions = self.env.action_space.list_of_actions
        if list_of_actions:
            best_action = None
            best_action_value = -100
            for action in list_of_actions:
                state_copy = current_state_as_dict.to_state()
                action.execute(state_copy)
                current_value = self.model.get_value(state_copy)
                if current_value > best_action_value:
                    best_action_value = current_value
                    best_action = action

            if not info:
                return best_action
            if info:
                return best_action, best_action_value

        else:
            if not info:
                return None
            if info:
                return None, -1
class DeterministicObservation(SplendorObservation):
    def __init__(self, state: State):
        super().__init__('deterministic')
        self.observation_dict = StateAsDict(state)

    def recreate_state(self):
        return self.observation_dict.to_state()
    def generate_all_tree_data_as_list(self,
                                       confidence_threshold: float = 0.1,
                                       count_threshold: int = 6,
                                       confidence_limit: int = 2):
        self.clean_memory()
        # BFS
        kiu = [self.root]
        confidence_count = 0
        print('Collecting tree data.')
        X = []
        Y = []
        while len(kiu) > 0:
            # take first:
            node_to_eval = kiu.pop(0)
            if node_to_eval.value_acc.count() > 0:
                for child in node_to_eval.children:
                    # if child.value_acc.count() >= count_threshold or child.value_acc.perfect_value is not None:
                    if child.value_acc.get_confidence(
                    ) >= confidence_threshold:
                        confidence_count += 1
                    if (
                            child.value_acc.get_confidence() >= confidence_threshold and confidence_count <= confidence_limit) \
                            or child.value_acc.count() >= count_threshold:
                        kiu.append(child)
                        child_state_as_dict = StateAsDict(child.return_state())
                        current_state = child_state_as_dict.to_state()
                        current_state_inversed = child_state_as_dict.to_state()
                        current_state_inversed.change_active_player()
                        current_value = child.value_acc.get()

                        X.append(current_state)
                        Y.append(current_value)
                        X.append(current_state_inversed)
                        Y.append(-current_value)

        return {'state': X, 'mcts_value': Y}
    def generate_all_tree_data(self):
        self.clean_memory()
        all_code = ''
        # BFS
        kiu = [self.root]

        print('Collecting tree data.')

        while len(kiu) > 0:
            # take first:
            node_to_eval = kiu.pop(0)
            if node_to_eval.value_acc.count() > 0:
                for child in node_to_eval.children:
                    if child.value_acc.count() > 0:
                        kiu.append(child)
                        child_state_as_dict = StateAsDict(child.return_state())
                        self.stats_dataframe = self.stats_dataframe.append(
                            {
                                'state': child_state_as_dict.to_state(),
                                'mcts_value': child.value_acc.get()
                            },
                            ignore_index=True)
        return self.stats_dataframe
Esempio n. 6
0
 def load_state_from_dict(self, state_as_dict: StateAsDict):
     self.current_state_of_the_game = state_as_dict.to_state()
     self.is_done = False
        'name': 'RandomAgent - uniform_on_types '
    },
    'board': {
        'nobles_on_board': {104, 108, 102},
        'cards_on_board': {33, 4, 69, 71, 72, 41, 9, 10, 44, 79, 86, 26},
        'gems_on_board': [2, 4, 3, 3, 4, 3],
        'deck_order': [{
            'Row.CHEAP': [
                38, 0, 76, 18, 55, 19, 3, 40, 7, 43, 23, 39, 37, 42, 77, 75,
                25, 59, 2, 5, 54, 73, 56, 21, 57, 22, 1, 36, 6, 60, 61, 78, 74,
                58, 24
            ]
        }, {
            'Row.MEDIUM': [
                12, 49, 47, 31, 45, 62, 11, 81, 46, 80, 66, 67, 48, 83, 82, 65,
                8, 84, 13, 29, 27, 64, 85, 63, 30
            ]
        }, {
            'Row.EXPENSIVE':
            [35, 53, 87, 88, 15, 14, 50, 17, 16, 68, 70, 32, 52, 51, 89, 34]
        }]
    },
    'active_player_id': 1
}

stanek = StateAsDict()
stanek.load_from_dict(dict)

fufu = SplendorGUI()
fufu.draw_state(stanek.to_state())
fufu.keep_window_open(300)
Esempio n. 8
0
def dict_to_state(s_dict):
    s_as_dict = StateAsDict()
    s_as_dict.load_from_dict(s_dict)
    state_to_return = s_as_dict.to_state()
    state_to_return.active_player_id = 0
    return state_to_return