def test_sapientino_goal(): """Test sapientino goal.""" fluents = SapientinoFluents({"red", "green", "blue"}) with pytest.raises(ValueError): temporal_goals.SapientinoGoal( colors=["red", "yellow"], fluents=fluents, reward=1.0, ) tg = temporal_goals.SapientinoGoal( colors=["red", "blue"], fluents=fluents, reward=1.0, ) # some constants empty = PLInterpretation({}) red = PLInterpretation({"red"}) blue = PLInterpretation({"blue"}) yellow = PLInterpretation({"yellow"}) assert len(tg.automaton.states) == 4 assert tg.automaton.accepts([red, blue]) assert tg.automaton.accepts([empty, red, empty, blue]) assert not tg.automaton.accepts([empty, red, empty, blue, empty]) assert not tg.automaton.accepts([red, yellow]) assert not tg.automaton.accepts([red]) assert not tg.automaton.accepts([])
def post_process_dfa(dfa): """Add a loop transition to every state.""" alphabet = dfa.alphabet.union({PLInterpretation(set())}) states = dfa.states initial_state = dfa.initial_state accepting_states = dfa.accepting_states transitions = deepcopy(dfa.transition_function) for state in states: if PLInterpretation(set()) not in transitions[state]: transitions[state][PLInterpretation(set())] = state return DFA(states, alphabet, initial_state, accepting_states, transitions)
def extract_breakout_fluents(obs, action) -> PLInterpretation: brick_matrix = obs["bricks_matrix"] # type: np.ndarray previous_brick_matrix = obs["previous_bricks_matrix"] # type: np.ndarray previous_broken_columns = np.all(previous_brick_matrix == 0.0, axis=1) current_broken_columns = np.all(brick_matrix == 0.0, axis=1) compare = (previous_broken_columns == current_broken_columns ) # type: np.ndarray if compare.all(): result = PLInterpretation(set()) return result else: index = np.argmin(compare) fluent = "c" + str(index) result = PLInterpretation({fluent}) return result
def test_fluents_cont_sapientino(): """Test fluents extraction on cont-sapientino.""" # NOTE: this test depends on gym-sapientino color order with pytest.raises(ValueError): fluents = env_cont_sapientino.Fluents({"not a color"}) fluents = env_cont_sapientino.Fluents({"red", "blue"}) # with just 2 fluents assert fluents.evaluate(dict(beep=0, color=1), 0) == PLInterpretation(set()) assert fluents.evaluate(dict(beep=1, color=1), 0) == PLInterpretation({"red"}) assert fluents.evaluate(dict(beep=1, color=3), 0) == PLInterpretation({"blue"}) with pytest.raises(RuntimeError): fluents.evaluate(dict(beep=1, color=2), 0) # green not used
def extract_sapientino_fluents(obs, action) -> PLInterpretation: color_idx = obs["color"] beep = obs["beep"] fluents = set() if 0 < color_idx <= len(colors) and beep: color_string = colors[color_idx - 1] fluents.add(color_string) elif color_idx == 0 and beep: fluents.add("bad_beep") result = PLInterpretation(fluents) return result
def extract_minecraft_fluents(obs, action) -> PLInterpretation: item_idx = obs["item"] item = int2item[item_idx] command = obs["command"] previous_completed_tasks = obs["previous_completed_tasks"] next_completed_tasks = obs["completed_tasks"] fluents = set() if command == 1 and isinstance(item, Resources): fluents.add(item.value) elif command == 2 and isinstance(item, Tools): fluents.add(item.value) result = PLInterpretation(fluents) return result
def from_inferrer_to_pythomata(dfa: inferrer.automaton.dfa.DFAWrapper) -> pythomata.dfa.DFA: """Transfom an inferrer.automaton.dfa.DFAWrapper into pythomata.DFA.""" char2sym = dfa.char2sym unwrapped_dfa = dfa.unwrapped transitions = { state.name: { PLInterpretation({char2sym[char]}): state2.name for char, state2 in unwrapped_dfa._transitions[state].items() } for state in unwrapped_dfa.states } accepting_states = {s.name for s in unwrapped_dfa.accept_states} initial_state = unwrapped_dfa._start_state.name new_dfa = pythomata.dfa.DFA.from_transitions(initial_state, accepting_states, transitions) return new_dfa