def is_empty(self): """ Returns true if the table is empty (or contains only a default assignment), false otherwise :return: true if empty, false otherwise """ if len(self._table) == 0: return True if len(self._table) > 1: return False return list(self._table.keys())[0] == Assignment.create_default( self._head_vars)
def __init__(self, init_state, system, paused): from dialogue_system import DialogueSystem if not isinstance(init_state, DialogueState) or not isinstance( system, DialogueSystem) or not isinstance(paused, bool): raise NotImplementedError("UNDEFINED PARAMETERS") """ Creates the planning process. Timeout is set to twice the maximum sampling ime. Then, runs the planner until the horizon has been reached, or the planner has run out of time. Adds the best action to the dialogue state. :param init_state: initial dialogue state """ self.system = system self.paused = paused self.is_terminated = False settings = system.get_settings() # setting the timeout for the planning timeout = Settings.max_sampling_time * 2 # if the speech stream is not finished, only allow fast, reactive responses if init_state.has_chance_node(settings.user_speech): timeout = timeout / 5.0 # TODO: ScheduledExecutorService # service.schedule(() -> isTerminated = true, timeout, TimeUnit.MILLISECONDS); # step 1: extract the Q-values eval_actions = self.get_q_values(init_state, settings.horizon) # step 2: find the action with highest utility best_action = eval_actions.get_best()[0] if eval_actions.get_util(best_action) < 0.001: best_action = Assignment.create_default( best_action.get_variables()) # step 3: remove the action and utility nodes init_state.remove_nodes(init_state.get_utility_node_ids()) action_vars = init_state.get_action_node_ids() init_state.remove_nodes(action_vars) # step 4: add the selection action to the dialogue state init_state.add_to_state(best_action.remove_primes()) self.is_terminated = True
def get_best(self): """ Returns the most likely assignment of values in the table. If none could be found, returns an empty assignment. :return: the assignment with highest probability """ if len(self._table) == 0: self.log.warning("table is empty, cannot extract best value") raise ValueError() max_prob = -np.inf max_assignment = None for assignment in self._table.keys(): prob = self._table[assignment] if prob > max_prob: max_prob = prob max_assignment = assignment # TODO: check refactor > there is no case of max_assignment is None return max_assignment if max_assignment is not None else Assignment.create_default( self._head_vars)