Skip to content

Commit

Permalink
temp
Browse files Browse the repository at this point in the history
  • Loading branch information
marcofavorito committed Oct 2, 2023
1 parent ce48e1e commit 25bacb0
Show file tree
Hide file tree
Showing 10 changed files with 539 additions and 444 deletions.
49 changes: 31 additions & 18 deletions plan4past/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@
TRUE_PREDICATE,
)
from plan4past.exceptions import ProblemUnsolvableException
from plan4past.helpers.compilation_helper import CompilationManager, YesterdayAtom
from plan4past.helpers.compilation_helper import (
CompilationManager,
PredicateMapping,
YesterdayAtom,
)
from plan4past.helpers.utils import (
add_val_prefix,
check_,
Expand All @@ -55,13 +59,13 @@
)
from plan4past.helpers.yesterday_atom_helper import QUOTED_ATOM
from plan4past.utils.atoms_visitor import find_atoms
from plan4past.utils.derived_visitor import derived_predicates
from plan4past.utils.derived_visitor import DerivedPredicatesVisitor
from plan4past.utils.dnf_visitor import dnf
from plan4past.utils.nnf_visitor import nnf
from plan4past.utils.predicates_visitor import predicates
from plan4past.utils.predicates_visitor import PredicatesVisitor
from plan4past.utils.pylogics2pddl import Pylogics2PddlTranslator
from plan4past.utils.rewrite_formula_visitor import rewrite
from plan4past.utils.val_predicates_visitor import val_predicates
from plan4past.utils.val_predicates_visitor import ValPredicatesVisitor


class Compiler:
Expand Down Expand Up @@ -93,6 +97,7 @@ def __init__(

check_(self.formula.logic == Logic.PLTL, "only PPLTL is supported!")

self._predicate_mapping = PredicateMapping()
self._executed: bool = False
self._result_domain: Optional[Domain] = None
self._result_problem: Optional[Problem] = None
Expand Down Expand Up @@ -134,18 +139,27 @@ def result(self) -> Tuple[Domain, Problem]:

def compile(self):
"""Compute the new domain and the new problem."""
if not self._executed:
self._compile_domain()
self._compile_problem()
self._executed = True
if self._executed:
return

self._compile_domain()
self._compile_problem()

self._executed = True

def _compile_domain(self):
"""Compute the new domain."""
new_predicates = predicates(self.formula).union(val_predicates(self.formula))
new_derived_predicates = derived_predicates(
self.formula, self.from_atoms_to_fluent
subformula_predicates_set = PredicatesVisitor(self._predicate_mapping).visit(
self.formula
)
val_predicates_set = ValPredicatesVisitor(self._predicate_mapping).visit(
self.formula
)
new_whens = _compute_whens(self.formula)
new_predicates = subformula_predicates_set.union(val_predicates_set)
new_derived_predicates = DerivedPredicatesVisitor(
self._predicate_mapping, self.from_atoms_to_fluent
).visit(self.formula)
new_whens = _compute_whens(subformula_predicates_set)
domain_actions = _update_domain_actions_det(self.domain.actions, new_whens)

self._result_domain = Domain(
Expand Down Expand Up @@ -174,26 +188,25 @@ def _compile_problem(self):
else set(self.problem.init)
)

goal_predicate = self._predicate_mapping.get_predicate(self.formula)
self._result_problem = Problem(
name=self.problem.name,
domain_name=self.domain.name,
requirements=self.problem.requirements,
objects=[*self.problem.objects],
init=new_init,
goal=And(
Predicate(add_val_prefix(replace_symbols(to_string(self.formula))))
),
goal=And(Predicate(add_val_prefix(goal_predicate.name))),
)


def _compute_whens(formula: Formula) -> Set[When]:
def _compute_whens(predicates: Set[Predicate]) -> Set[When]:
"""Compute conditional effects for formula progression."""
return {
When(Predicate(add_val_prefix(remove_yesterday_prefix(p.name))), p)
for p in predicates(formula)
for p in predicates
}.union(
When(Not(Predicate(add_val_prefix(remove_yesterday_prefix(p.name)))), Not(p))
for p in predicates(formula)
for p in predicates
)


Expand Down
48 changes: 46 additions & 2 deletions plan4past/helpers/compilation_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
# along with Plan4Past. If not, see <https://www.gnu.org/licenses/>.
#
"""This module contains the class that manages the compilation of a PPLTL formula."""
from typing import List, Tuple
from typing import Dict, List, Tuple

from pddl.logic import Predicate
from pylogics.syntax.base import Formula, Not
from pylogics.syntax.pltl import (
Atomic,
Expand All @@ -33,7 +34,7 @@
)

from plan4past.helpers.utils import check_
from plan4past.helpers.yesterday_atom_helper import YesterdayAtom
from plan4past.helpers.yesterday_atom_helper import QUOTED_ATOM, YesterdayAtom
from plan4past.utils.ppnf_visitor import ppnf
from plan4past.utils.yesterday_generator_visitor import get_quoted_dictionary

Expand Down Expand Up @@ -108,3 +109,46 @@ def get_problem_extension(self) -> Tuple[List[Formula], List, Formula]:
conditional_effects.append((ppnf(yesterday_atom.formula), yesterday_atom))

return fresh_atoms, conditional_effects, goal


class PredicateMapping:
"""Class that manages the mapping of the predicates."""

def __init__(self):
"""Initialize the predicate mapping."""
self.mapping: Dict[Formula, Predicate] = {}
self.inverse_mapping: Dict[Predicate, Formula] = {}
self.id = 0

def add_predicate(self, formula: Formula) -> None:
"""
Add a predicate to the mapping.
:param formula: the formula to be added
"""
if self.mapping.get(formula) is None:
self.mapping[formula] = Predicate(f"{QUOTED_ATOM}_{self.id}")
self.inverse_mapping[self.mapping[formula]] = formula
self.id += 1

def get_predicate(self, formula: Formula) -> Predicate:
"""
Get the predicate from the mapping.
:param formula: the formula to be added
:return: the predicate
"""
result = self.mapping.get(formula)
if result is None:
self.add_predicate(formula)
return self.mapping[formula]

def get_formula(self, predicate: Predicate) -> Formula:
"""
Get the formula from the inverse mapping.
:param predicate: the predicate to be added
:return: the formula
"""
check_(predicate in self.inverse_mapping, f"predicate {predicate} not found")
return self.inverse_mapping[predicate]
Loading

0 comments on commit 25bacb0

Please sign in to comment.