Skip to content

Commit

Permalink
Added exception when replanner remove_{action/goal} fails
Browse files Browse the repository at this point in the history
  • Loading branch information
Framba-Luca committed Aug 16, 2023
1 parent 02f25aa commit fefc2cc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
15 changes: 15 additions & 0 deletions unified_planning/engines/replanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
PlanGenerationResult,
)
from unified_planning.engines.mixins.oneshot_planner import OptimalityGuarantee
from unified_planning.exceptions import UPUsageError
from typing import Type, IO, Callable, Optional, Union, List, Tuple
from fractions import Fraction

Expand Down Expand Up @@ -100,9 +101,16 @@ def _remove_goal(
(goal_exp,) = self._problem.environment.expression_manager.auto_promote(goal)
goals = self._problem.goals
self._problem.clear_goals()
removed = False
for g in goals:
if not g is goal_exp:
self._problem.add_goal(g)
else:
removed = True
if not removed:
raise UPUsageError(
f"goal to remove: {goal_exp} not found inside the problem goals: {goals}"
)

def _add_action(self, action: "up.model.action.Action"):
assert isinstance(self._problem, up.model.Problem)
Expand All @@ -112,6 +120,13 @@ def _remove_action(self, name: str):
assert isinstance(self._problem, up.model.Problem)
actions = self._problem.actions
self._problem.clear_actions()
removed = False
for a in actions:
if a.name != name:
self._problem.add_action(a)
else:
removed = True
if not removed:
raise UPUsageError(
f"action to remove: {name} not found inside the problem actions: {list(map(lambda a: a.name, actions))}"
)
9 changes: 9 additions & 0 deletions unified_planning/test/test_replanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from unified_planning.shortcuts import *
from unified_planning.model.problem_kind import classical_kind
from unified_planning.engines.results import POSITIVE_OUTCOMES, NEGATIVE_OUTCOMES
from unified_planning.exceptions import UPUsageError
from unified_planning.test import TestCase, main
from unified_planning.test import skipIfNoOneshotPlannerForProblemKind
from unified_planning.test.examples import get_example_problems
Expand Down Expand Up @@ -61,3 +62,11 @@ def test_basic(self):
replanner.add_action(a)
res = replanner.resolve()
self.assertIn(res.status, POSITIVE_OUTCOMES)

with self.assertRaises(UPUsageError):
replanner.remove_action("b")

with self.assertRaises(UPUsageError):
y = Fluent("y")
problem.add_fluent(y, default_initial_value=False)
replanner.remove_goal(y)

0 comments on commit fefc2cc

Please sign in to comment.