Skip to content

Commit

Permalink
refactoring: changed kind names and related functions, fixed some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Samuel Gobbi committed Jan 10, 2025
1 parent 788343e commit 41aa4c0
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 43 deletions.
4 changes: 2 additions & 2 deletions unified_planning/io/pddl_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,7 @@ def _add_effect(

if isinstance(act, up.model.Process):
eff1 = (eff[0], eff[1].simplify())
act.add_continuous_increase(*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 @@ -670,7 +670,7 @@ def _add_effect(
)
if isinstance(act, up.model.Process):
eff1 = (eff[0], eff[1].simplify())
act.add_continuous_decrease(*eff1)
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
4 changes: 3 additions & 1 deletion unified_planning/io/pddl_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1240,8 +1240,10 @@ def _write_derivative(
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_continuous_increase() or effect.is_continuous_decrease():
elif effect.is_increase_continuous_effect():
out.write(f" (increase {fluent} (* #t {converter.convert(simplified_value)} ))")
elif effect.is_decrease_continuous_effect():
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
24 changes: 14 additions & 10 deletions unified_planning/model/effect.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ class EffectKind(Enum):
`ASSIGN` => `if C then F <= V`
`INCREASE` => `if C then F <= F + V`
`DECREASE` => `if C then F <= F - V`
`CONTINUOUS_INCREASE` => `dF/dt <= V`
`CONTINUOUS_DECREASE` => `dF/dt <= -V`
`INCREASE_CONTINUOUS_EFFECT` => `dF/dt <= V`
`DECREASE_CONTINUOUS_EFFECT` => `dF/dt <= -V`
"""

ASSIGN = auto()
INCREASE = auto()
DECREASE = auto()
CONTINUOUS_INCREASE = auto()
CONTINUOUS_DECREASE = auto()
INCREASE_CONTINUOUS_EFFECT = auto()
DECREASE_CONTINUOUS_EFFECT = auto()


class Effect:
Expand Down Expand Up @@ -113,16 +113,20 @@ def __repr__(self) -> str:
s.append(f"forall {', '.join(str(v) for v in self._forall)}")
if self.is_conditional():
s.append(f"if {str(self._condition)} then")
if not (self.is_continuous_increase() or self.is_continuous_decrease()):
if not (
self.is_increase_continuous_effect() or self.is_decrease_continuous_effect()
):
s.append(f"{str(self._fluent)}")
if self.is_assignment():
s.append(":=")
elif self.is_increase():
s.append("+=")
elif self.is_decrease():
s.append("-=")
elif self.is_continuous_increase() or self.is_continuous_decrease():
elif self.is_increase_continuous_effect():
s.append(f"d{str(self._fluent)}/dt =")
elif self.is_decrease_continuous_effect():
s.append(f"d{str(self._fluent)}/dt = -")
s.append(f"{str(self._value)}")
return " ".join(s)

Expand Down Expand Up @@ -252,13 +256,13 @@ def is_decrease(self) -> bool:
"""Returns `True` if the :func:`kind <unified_planning.model.Effect.kind>` of this `Effect` is a `decrease`, `False` otherwise."""
return self._kind == EffectKind.DECREASE

def is_continuous_increase(self) -> bool:
def is_increase_continuous_effect(self) -> bool:
"""Returns `True` if the :func:`kind <unified_planning.model.Effect.kind>` of this `Effect` is a `continuous increase`, `False` otherwise."""
return self._kind == EffectKind.CONTINUOUS_INCREASE
return self._kind == EffectKind.INCREASE_CONTINUOUS_EFFECT

def is_continuous_decrease(self) -> bool:
def is_decrease_continuous_effect(self) -> bool:
"""Returns `True` if the :func:`kind <unified_planning.model.Effect.kind>` of this `Effect` is a `continuous decrease`, `False` otherwise."""
return self._kind == EffectKind.CONTINUOUS_DECREASE
return self._kind == EffectKind.DECREASE_CONTINUOUS_EFFECT


class SimulatedEffect:
Expand Down
35 changes: 14 additions & 21 deletions unified_planning/model/natural_transition.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,28 +161,21 @@ def _add_continuous_effect(
)
if not fluent_exp.type.is_int_type() and not fluent_exp.type.is_real_type():
raise UPTypeError("Derivative can be created only on numeric types!")

Check warning on line 163 in unified_planning/model/natural_transition.py

View check run for this annotation

Codecov / codecov/patch

unified_planning/model/natural_transition.py#L163

Added line #L163 was not covered by tests
if not negative:
self._add_effect_instance(
up.model.effect.Effect(
fluent_exp,
value_exp,
condition_exp,
kind=up.model.effect.EffectKind.CONTINUOUS_INCREASE,
forall=tuple(),
)
)
else:
self._add_effect_instance(
up.model.effect.Effect(
fluent_exp,
value_exp,
condition_exp,
kind=up.model.effect.EffectKind.CONTINUOUS_DECREASE,
forall=tuple(),
)
e_kind = up.model.effect.EffectKind.INCREASE_CONTINUOUS_EFFECT

if negative:
e_kind = up.model.effect.EffectKind.DECREASE_CONTINUOUS_EFFECT
self._add_effect_instance(
up.model.effect.Effect(
fluent_exp,
value_exp,
condition_exp,
kind=e_kind,
forall=tuple(),
)
)

def add_continuous_increase(
def add_increase_continuous_effect(
self,
fluent: Union["up.model.fnode.FNode", "up.model.fluent.Fluent"],
value: "up.model.expression.Expression",
Expand All @@ -196,7 +189,7 @@ def add_continuous_increase(
"""
self._add_continuous_effect(fluent, value, False)

def add_continuous_decrease(
def add_decrease_continuous_effect(
self,
fluent: Union["up.model.fnode.FNode", "up.model.fluent.Fluent"],
value: "up.model.expression.Expression",
Expand Down
4 changes: 2 additions & 2 deletions unified_planning/model/problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -888,9 +888,9 @@ def update_problem_kind_effect(
self.kind.set_effects_kind("STATIC_FLUENTS_IN_OBJECT_ASSIGNMENTS")
if any(f not in self.static_fluents for f in fluents_in_value):
self.kind.set_effects_kind("FLUENTS_IN_OBJECT_ASSIGNMENTS")
elif e.is_continuous_increase():
elif e.is_increase_continuous_effect():
self.kind.unset_problem_type("SIMPLE_NUMERIC_PLANNING")
elif e.is_continuous_decrease():
elif e.is_decrease_continuous_effect():
self.kind.unset_problem_type("SIMPLE_NUMERIC_PLANNING")

Check warning on line 894 in unified_planning/model/problem.py

View check run for this annotation

Codecov / codecov/patch

unified_planning/model/problem.py#L891-L894

Added lines #L891 - L894 were not covered by tests

def update_problem_kind_expression(
Expand Down
6 changes: 3 additions & 3 deletions unified_planning/test/examples/processes.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def get_example_problems():

b = Process("moving")
b.add_precondition(on)
b.add_continuous_increase(d, 1)
b.add_increase_continuous_effect(d, 1)

problem = Problem("1d_Movement")
problem.add_fluent(on)
Expand Down Expand Up @@ -54,11 +54,11 @@ def get_example_problems():

water_heating = Process("water_heating")
water_heating.add_precondition(And(boiler_on, LE(temperature, 100)))
water_heating.add_continuous_increase(temperature, 1)
water_heating.add_increase_continuous_effect(temperature, 1)

water_boiling = Process("water_boiling")
water_boiling.add_precondition(And(boiler_on, GE(temperature, 100)))
water_boiling.add_continuous_decrease(water_level, 1)
water_boiling.add_decrease_continuous_effect(water_level, 1)

open_chimney_vent_auto = Event("open_chimney_vent_auto")
open_chimney_vent_auto.add_precondition(
Expand Down
4 changes: 2 additions & 2 deletions unified_planning/test/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,12 @@ def test_process(self):
x = Fluent("x", IntType())
move = Process("moving", car=Vehicle)
move.add_precondition(a)
move.add_continuous_increase(x, 1)
move.add_increase_continuous_effect(x, 1)
e = Effect(
FluentExp(x),
Int(1),
TRUE(),
unified_planning.model.EffectKind.CONTINUOUS_INCREASE,
unified_planning.model.EffectKind.INCREASE_CONTINUOUS_EFFECT,
)
self.assertEqual(move.effects[0], e)
self.assertEqual(move.name, "moving")
Expand Down
4 changes: 2 additions & 2 deletions unified_planning/test/test_pddl_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,8 +451,8 @@ def test_non_linear_car(self):
if isinstance(ele, Process):
for e in ele.effects:
self.assertTrue(
(e.kind == EffectKind.CONTINUOUS_INCREASE)
or (e.kind == EffectKind.CONTINUOUS_DECREASE)
(e.kind == EffectKind.INCREASE_CONTINUOUS_EFFECT)
or (e.kind == EffectKind.DECREASE_CONTINUOUS_EFFECT)
)
if ele.name == "drag_ahead":
found_drag_ahead = True
Expand Down

0 comments on commit 41aa4c0

Please sign in to comment.