|
32 | 32 | from pddl.logic.predicates import DerivedPredicate, Predicate |
33 | 33 | from pylogics.syntax.base import Formula, Logic |
34 | 34 | from pylogics.syntax.pltl import Atomic as PLTLAtomic |
35 | | -from pylogics.syntax.pltl import FalseFormula |
36 | | -from pylogics.utils.to_string import to_string |
| 35 | +from pylogics.syntax.pltl import Before, FalseFormula |
37 | 36 |
|
38 | 37 | from plan4past.constants import ( |
39 | 38 | ACHIEVE_GOAL_ACTION, |
|
45 | 44 | TRUE_PREDICATE, |
46 | 45 | ) |
47 | 46 | from plan4past.exceptions import ProblemUnsolvableException |
48 | | -from plan4past.helpers.compilation_helper import CompilationManager, YesterdayAtom |
| 47 | +from plan4past.helpers.compilation_helper import ( |
| 48 | + CompilationManager, |
| 49 | + PredicateMapping, |
| 50 | + YesterdayAtom, |
| 51 | +) |
49 | 52 | from plan4past.helpers.utils import ( |
50 | 53 | add_val_prefix, |
51 | 54 | check_, |
52 | 55 | default_mapping, |
53 | 56 | remove_yesterday_prefix, |
54 | | - replace_symbols, |
55 | 57 | ) |
56 | 58 | from plan4past.helpers.yesterday_atom_helper import QUOTED_ATOM |
57 | 59 | from plan4past.utils.atoms_visitor import find_atoms |
58 | | -from plan4past.utils.derived_visitor import derived_predicates |
| 60 | +from plan4past.utils.derived_visitor import DerivedPredicatesVisitor |
59 | 61 | from plan4past.utils.dnf_visitor import dnf |
60 | 62 | from plan4past.utils.nnf_visitor import nnf |
61 | | -from plan4past.utils.predicates_visitor import predicates |
| 63 | +from plan4past.utils.predicates_visitor import PredicatesVisitor |
62 | 64 | from plan4past.utils.pylogics2pddl import Pylogics2PddlTranslator |
63 | 65 | from plan4past.utils.rewrite_formula_visitor import rewrite |
64 | | -from plan4past.utils.val_predicates_visitor import val_predicates |
| 66 | +from plan4past.utils.val_predicates_visitor import ValPredicatesVisitor |
65 | 67 |
|
66 | 68 |
|
67 | 69 | class Compiler: |
@@ -93,6 +95,7 @@ def __init__( |
93 | 95 |
|
94 | 96 | check_(self.formula.logic == Logic.PLTL, "only PPLTL is supported!") |
95 | 97 |
|
| 98 | + self._predicate_mapping = PredicateMapping() |
96 | 99 | self._executed: bool = False |
97 | 100 | self._result_domain: Optional[Domain] = None |
98 | 101 | self._result_problem: Optional[Problem] = None |
@@ -134,18 +137,27 @@ def result(self) -> Tuple[Domain, Problem]: |
134 | 137 |
|
135 | 138 | def compile(self): |
136 | 139 | """Compute the new domain and the new problem.""" |
137 | | - if not self._executed: |
138 | | - self._compile_domain() |
139 | | - self._compile_problem() |
140 | | - self._executed = True |
| 140 | + if self._executed: |
| 141 | + return |
| 142 | + |
| 143 | + self._compile_domain() |
| 144 | + self._compile_problem() |
| 145 | + |
| 146 | + self._executed = True |
141 | 147 |
|
142 | 148 | def _compile_domain(self): |
143 | 149 | """Compute the new domain.""" |
144 | | - new_predicates = predicates(self.formula).union(val_predicates(self.formula)) |
145 | | - new_derived_predicates = derived_predicates( |
146 | | - self.formula, self.from_atoms_to_fluent |
| 150 | + subformula_predicates_set = PredicatesVisitor(self._predicate_mapping).visit( |
| 151 | + self.formula |
| 152 | + ) |
| 153 | + val_predicates_set = ValPredicatesVisitor(self._predicate_mapping).visit( |
| 154 | + self.formula |
147 | 155 | ) |
148 | | - new_whens = _compute_whens(self.formula) |
| 156 | + new_predicates = subformula_predicates_set.union(val_predicates_set) |
| 157 | + new_derived_predicates = DerivedPredicatesVisitor( |
| 158 | + self._predicate_mapping, self.from_atoms_to_fluent |
| 159 | + ).visit(self.formula) |
| 160 | + new_whens = _compute_whens(subformula_predicates_set, self._predicate_mapping) |
149 | 161 | domain_actions = _update_domain_actions_det(self.domain.actions, new_whens) |
150 | 162 |
|
151 | 163 | self._result_domain = Domain( |
@@ -174,27 +186,40 @@ def _compile_problem(self): |
174 | 186 | else set(self.problem.init) |
175 | 187 | ) |
176 | 188 |
|
| 189 | + goal_predicate = self._predicate_mapping.get_predicate(self.formula) |
177 | 190 | self._result_problem = Problem( |
178 | 191 | name=self.problem.name, |
179 | 192 | domain_name=self.domain.name, |
180 | 193 | requirements=self.problem.requirements, |
181 | 194 | objects=[*self.problem.objects], |
182 | 195 | init=new_init, |
183 | | - goal=And( |
184 | | - Predicate(add_val_prefix(replace_symbols(to_string(self.formula)))) |
185 | | - ), |
| 196 | + goal=And(Predicate(add_val_prefix(goal_predicate.name))), |
186 | 197 | ) |
187 | 198 |
|
188 | 199 |
|
189 | | -def _compute_whens(formula: Formula) -> Set[When]: |
| 200 | +def _compute_whens(predicates: Set[Predicate], mapping: PredicateMapping) -> Set[When]: |
190 | 201 | """Compute conditional effects for formula progression.""" |
191 | | - return { |
192 | | - When(Predicate(add_val_prefix(remove_yesterday_prefix(p.name))), p) |
193 | | - for p in predicates(formula) |
194 | | - }.union( |
195 | | - When(Not(Predicate(add_val_prefix(remove_yesterday_prefix(p.name)))), Not(p)) |
196 | | - for p in predicates(formula) |
197 | | - ) |
| 202 | + result: Set[When] = set() |
| 203 | + for p in predicates: |
| 204 | + if p.name.startswith("Y-"): |
| 205 | + present_predicate = Predicate( |
| 206 | + add_val_prefix(remove_yesterday_prefix(p.name)) |
| 207 | + ) |
| 208 | + past_predicate = p |
| 209 | + else: |
| 210 | + formula = mapping.inverse_mapping[p] |
| 211 | + if isinstance(formula, Before): |
| 212 | + arg_predicate = mapping.mapping[formula.argument] |
| 213 | + present_predicate = Predicate(add_val_prefix(arg_predicate.name)) |
| 214 | + past_predicate = p |
| 215 | + else: |
| 216 | + present_predicate = Predicate(add_val_prefix(p.name)) |
| 217 | + past_predicate = p |
| 218 | + |
| 219 | + positive_when = When(present_predicate, past_predicate) |
| 220 | + negative_when = When(Not(present_predicate), Not(past_predicate)) |
| 221 | + result.update({positive_when, negative_when}) |
| 222 | + return result |
198 | 223 |
|
199 | 224 |
|
200 | 225 | def _update_domain_actions_det( |
|
0 commit comments