Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring autonomous processes events #646

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
2135f27
separating events and processes from actions - work in progress
Ezpeon Nov 29, 2024
fcbf894
separating events and processes from actions - work in progress
Ezpeon Nov 29, 2024
f9300e3
black fix, some mypy fixes - work in progress
Ezpeon Nov 29, 2024
2439020
mypy fixes - work in progress
Ezpeon Nov 29, 2024
60756fc
mypy fixes - work in progress
Ezpeon Nov 29, 2024
a008444
refactoring: added new file for natural transitions
Dec 2, 2024
dd896db
refactoring: mypy fixes and new set mixin for natural transitions
Dec 2, 2024
3a55007
refactoring: minor changes and edited comments
Dec 3, 2024
3e8b551
refactoring: comments
Dec 3, 2024
f7f6820
refactoring: correctly rewriting of _get_static_and_unused_fluents me…
Dec 3, 2024
e66a811
refactoring: some fixes, separated events and processes sets
Dec 3, 2024
1ec433c
refactoring: mypy
Dec 3, 2024
c770833
refactoring: fixed natural_transition set property
Dec 3, 2024
11be2f1
refactoring: removed simulated effects from processes and events
Dec 3, 2024
5583f04
refactoring: work in progress - mixin for InstantaneousAction, Event,…
Dec 3, 2024
162427f
testing some functions
Dec 3, 2024
8a8a44b
refactoring: UntimedEffect and Precondition mixins
Dec 4, 2024
7bd6be1
refactoring: separated events and processes in pddl writer
Dec 5, 2024
5238ea5
new test case in processes examples
Dec 5, 2024
5ffee71
refactoring: removed duplicate code in pddl writer
Dec 13, 2024
e83b326
updated kinds and documentation, separated functions for increase/dec…
Jan 10, 2025
6a40e89
updated kinds and documentation, separated functions for increase/dec…
Jan 10, 2025
cb4be06
moved checks for nonlinear effects on function for processes instead …
Jan 10, 2025
788343e
fix: typo
Jan 10, 2025
41aa4c0
refactoring: changed kind names and related functions, fixed some bugs
Jan 10, 2025
64fa4d8
updated contributors
Jan 10, 2025
2c49ac9
refactoring: changed names
Jan 10, 2025
5fb2ab2
test: updated test_natural_transitions in test_problem
Jan 14, 2025
029bee4
test: updated test_natural_transitions in test_problem
Jan 14, 2025
ed3a2cb
chore: removed TODO comments
Jan 14, 2025
b1b18f1
test: added some coverage
Jan 14, 2025
1b9eae8
fix: added missing time requirement in pddl writer and reader
Jan 15, 2025
19f1e1e
chore: fixed contributors alphabetic order and added myself to contri…
Jan 16, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CONTRIBUTORS
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Luca Framba <[email protected]>
Luigi Bonassi <[email protected]>
Miguel Ramirez <[email protected]>
Roland Godet <[email protected]>
Samuel Gobbi <[email protected]>
Sebastian Stock <[email protected]>
Selvakumar Hastham Sathiya Satchi Sadanandam <[email protected]>
Srajan Goyal <[email protected]>
Expand Down
3 changes: 2 additions & 1 deletion docs/contributions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ Contributors to the repository include:
- Luca Framba <[email protected]>
- Luigi Bonassi <[email protected]>
- Roland Godet <[email protected]>
- Samuel Gobbi <[email protected]>
- Selvakumar Hastham Sathiya Satchi Sadanandam <[email protected]>
- Srajan Goyal <[email protected]>
- Uwe Köckemann <[email protected]>

Alternate source for contributions:
https://github.com/aiplan4eu/unified-planning/graphs/contributors
https://github.com/aiplan4eu/unified-planning/graphs/contributors
15 changes: 15 additions & 0 deletions docs/problem_representation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ Problem Kinds
* -
- SELF_OVERLAPPING
- The temporal planning problem allows actions self overlapping.
* -
- PROCESSES
- The problem contains processes, natural transitions that occur automatically over time as their pre-conditions are met.
* -
- EVENTS
- The problem contains events, natural transitions that occur automatically at a single time point when their pre-conditions are met.
* - EXPRESSION_DURATION
- STATIC_FLUENTS_IN_DURATION
- The duration of at least one action uses static fluents (that may never change).
Expand Down Expand Up @@ -111,6 +117,15 @@ Problem Kinds
* -
- DECREASE_EFFECTS
- At least one effect uses the numeric decrement operator.
* -
- INCREASE_CONTINUOUS_EFFECTS
- At least one effect uses the continuous numeric increment operator.
* -
- DECREASE_CONTINUOUS_EFFECTS
- At least one effect uses the continuous numeric decrement operator.
* -
- NON_LINEAR_CONTINUOUS_EFFECTS
- At least one continuous effect is described by a differential equation that depends on a continuous variable.
* -
- STATIC_FLUENTS_IN_BOOLEAN_ASSIGNMENTS
- At least one effect uses a static fluent in the expression of a boolean assignment.
Expand Down
14 changes: 7 additions & 7 deletions unified_planning/io/pddl_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def __init__(self):
+ ":requirements"
+ OneOrMore(
one_of(
":strips :typing :negative-preconditions :disjunctive-preconditions :equality :existential-preconditions :universal-preconditions :quantified-preconditions :conditional-effects :fluents :numeric-fluents :adl :durative-actions :duration-inequalities :timed-initial-literals :timed-initial-effects :action-costs :hierarchy :method-preconditions :constraints :contingent :preferences"
":strips :typing :negative-preconditions :disjunctive-preconditions :equality :existential-preconditions :universal-preconditions :quantified-preconditions :conditional-effects :fluents :numeric-fluents :adl :durative-actions :duration-inequalities :timed-initial-literals :timed-initial-effects :action-costs :hierarchy :method-preconditions :constraints :contingent :preferences :time"
)
)
+ Suppress(")")
Expand Down Expand Up @@ -413,7 +413,7 @@ def __init__(self, environment: typing.Optional[Environment] = None):
def _parse_exp(
self,
problem: up.model.Problem,
act: typing.Optional[Union[up.model.Action, htn.Method, htn.TaskNetwork]],
act: typing.Optional[Union[up.model.Transition, htn.Method, htn.TaskNetwork]],
types_map: TypesMap,
var: Dict[str, up.model.Variable],
exp: CustomParseResults,
Expand Down Expand Up @@ -655,7 +655,7 @@ def _add_effect(

if isinstance(act, up.model.Process):
eff1 = (eff[0], eff[1].simplify())
act.add_derivative(*eff1)
act.add_increase_continuous_effect(*eff1)
else:
act.add_increase_effect(*eff if timing is None else (timing, *eff)) # type: ignore
elif op == "decrease":
Expand All @@ -669,8 +669,8 @@ def _add_effect(
cond,
)
if isinstance(act, up.model.Process):
eff1 = (eff[0], (eff[1] * (-1)).simplify())
act.add_derivative(*eff1)
eff1 = (eff[0], eff[1].simplify())
act.add_decrease_continuous_effect(*eff1)
else:
act.add_decrease_effect(*eff if timing is None else (timing, *eff)) # type: ignore
elif op == "forall":
Expand Down Expand Up @@ -1292,7 +1292,7 @@ def declare_type(
CustomParseResults(a["eff"][0]),
domain_str,
)
problem.add_action(proc)
problem.add_process(proc)

for a in domain_res.get("events", []):
n = a["name"]
Expand All @@ -1317,7 +1317,7 @@ def declare_type(
CustomParseResults(a["eff"][0]),
domain_str,
)
problem.add_action(evt)
problem.add_event(evt)

for a in domain_res.get("actions", []):
n = a["name"]
Expand Down
138 changes: 73 additions & 65 deletions unified_planning/io/pddl_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@
WithName = Union[
"up.model.Type",
"up.model.Action",
"up.model.NaturalTransition",
"up.model.Fluent",
"up.model.Object",
"up.model.Parameter",
Expand Down Expand Up @@ -443,6 +444,8 @@
out.write(" :hierarchy") # HTN / HDDL
if self.problem_kind.has_method_preconditions():
out.write(" :method-preconditions")
if self.problem_kind.has_processes() or self.problem_kind.has_events():
out.write(" :time")
out.write(")\n")

if self.problem_kind.has_hierarchical_typing():
Expand Down Expand Up @@ -530,7 +533,9 @@
converter = ConverterToPDDLString(
self.problem.environment, self._get_mangled_name
)
costs = {}
costs: Dict[
Union[up.model.NaturalTransition, up.model.Action], Optional[up.model.FNode]
] = {}
metrics = self.problem.quality_metrics
if len(metrics) == 1:
metric = metrics[0]
Expand Down Expand Up @@ -595,74 +600,15 @@
out.write(")\n")

for a in self.problem.actions:
if isinstance(a, up.model.InstantaneousAction) or isinstance(
a, up.model.Event
):
if isinstance(a, up.model.InstantaneousAction):
if any(p.simplify().is_false() for p in a.preconditions):
continue
if isinstance(a, up.model.Event):
out.write(f" (:event {self._get_mangled_name(a)}")
else:
out.write(f" (:action {self._get_mangled_name(a)}")
out.write(f" (:action {self._get_mangled_name(a)}")
out.write(f"\n :parameters (")
self._write_parameters(out, a)
out.write(")")
if len(a.preconditions) > 0:
precond_str = []
for p in (c.simplify() for c in a.preconditions):
if not p.is_true():
if p.is_and():
precond_str.extend(map(converter.convert, p.args))
else:
precond_str.append(converter.convert(p))
out.write(f'\n :precondition (and {" ".join(precond_str)})')
elif len(a.preconditions) == 0 and self.empty_preconditions:
out.write(f"\n :precondition ()")
if len(a.effects) > 0:
out.write("\n :effect (and")
for e in a.effects:
_write_effect(
e,
None,
out,
converter,
self.rewrite_bool_assignments,
self._get_mangled_name,
)

if a in costs:
out.write(
f" (increase (total-cost) {converter.convert(costs[a])})"
)
out.write(")")
out.write(")\n")
elif isinstance(a, up.model.Process):
if any(p.simplify().is_false() for p in a.preconditions):
continue
out.write(f" (:process {self._get_mangled_name(a)}")
out.write(f"\n :parameters (")
self._write_parameters(out, a)
out.write(")")
if len(a.preconditions) > 0:
precond_str = []
for p in (c.simplify() for c in a.preconditions):
if not p.is_true():
if p.is_and():
precond_str.extend(map(converter.convert, p.args))
else:
precond_str.append(converter.convert(p))
out.write(f'\n :precondition (and {" ".join(precond_str)})')
elif len(a.preconditions) == 0 and self.empty_preconditions:
out.write(f"\n :precondition ()")
if len(a.effects) > 0:
out.write("\n :effect (and")
for e in a.effects:
_write_derivative(
e,
out,
converter,
)
out.write(")")
self._write_untimed_preconditions(a, converter, out)
self._write_untimed_effects(a, converter, out, costs)
out.write(")\n")
elif isinstance(a, DurativeAction):
if any(
Expand Down Expand Up @@ -733,6 +679,36 @@
out.write(")\n")
else:
raise NotImplementedError
for proc in self.problem.processes:

if any(p.simplify().is_false() for p in proc.preconditions):
continue

Check warning on line 685 in unified_planning/io/pddl_writer.py

View check run for this annotation

Codecov / codecov/patch

unified_planning/io/pddl_writer.py#L685

Added line #L685 was not covered by tests
out.write(f" (:process {self._get_mangled_name(proc)}")
out.write(f"\n :parameters (")
self._write_parameters(out, proc)
out.write(")")
self._write_untimed_preconditions(proc, converter, out)
if len(proc.effects) > 0:
out.write("\n :effect (and")
for e in proc.effects:
_write_derivative(
e,
out,
converter,
)
out.write(")")
out.write(")\n")
for eve in self.problem.events:

if any(p.simplify().is_false() for p in eve.preconditions):
continue

Check warning on line 704 in unified_planning/io/pddl_writer.py

View check run for this annotation

Codecov / codecov/patch

unified_planning/io/pddl_writer.py#L704

Added line #L704 was not covered by tests
out.write(f" (:event {self._get_mangled_name(eve)}")
out.write(f"\n :parameters (")
self._write_parameters(out, eve)
out.write(")")
self._write_untimed_preconditions(eve, converter, out)
self._write_untimed_effects(eve, converter, out, costs)
out.write(")\n")
out.write(")\n")

def _write_problem(self, out: IO[str]):
Expand Down Expand Up @@ -1066,6 +1042,36 @@
"Task network constraints not supported by HDDL Writer yet"
)

def _write_untimed_preconditions(self, item, converter, out):
if len(item.preconditions) > 0:
precond_str: list[str] = []
for p in (c.simplify() for c in item.preconditions):
if not p.is_true():
if p.is_and():
precond_str.extend(map(converter.convert, p.args))
else:
precond_str.append(converter.convert(p))
out.write(f'\n :precondition (and {" ".join(precond_str)})')
elif len(item.preconditions) == 0 and self.empty_preconditions:
out.write(f"\n :precondition ()")

Check warning on line 1056 in unified_planning/io/pddl_writer.py

View check run for this annotation

Codecov / codecov/patch

unified_planning/io/pddl_writer.py#L1056

Added line #L1056 was not covered by tests

def _write_untimed_effects(self, item, converter, out, costs):
if len(item.effects) > 0:
out.write("\n :effect (and")
for e in item.effects:
_write_effect(
e,
None,
out,
converter,
self.rewrite_bool_assignments,
self._get_mangled_name,
)

if item in costs:
out.write(f" (increase (total-cost) {converter.convert(costs[item])})")
out.write(")")


def _get_pddl_name(item: Union[WithName, "up.model.AbstractProblem"]) -> str:
"""This function returns a pddl name for the chosen item"""
Expand Down Expand Up @@ -1235,8 +1241,10 @@
out.write(f" (increase {fluent} (* #t {converter.convert(simplified_value)} ))")
elif effect.is_decrease():
out.write(f" (decrease {fluent} (* #t {converter.convert(simplified_value)} ))")
elif effect.is_derivative():
elif effect.is_continuous_increase():
out.write(f" (increase {fluent} (* #t {converter.convert(simplified_value)} ))")
elif effect.is_continuous_decrease():
out.write(f" (decrease {fluent} (* #t {converter.convert(simplified_value)} ))")
else:
raise UPProblemDefinitionError(
"Derivative can only be expressed as increase, decrease in processes",
Expand Down
8 changes: 8 additions & 0 deletions unified_planning/model/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,17 @@
InstantaneousAction,
DurativeAction,
SensingAction,
)

from unified_planning.model.natural_transition import (
NaturalTransition,
Process,
Event,
)

from unified_planning.model.transition import (
Transition,
)
from unified_planning.model.effect import Effect, SimulatedEffect, EffectKind
from unified_planning.model.expression import (
BoolExpression,
Expand Down
Loading
Loading