-
Notifications
You must be signed in to change notification settings - Fork 44
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
Adding Support for Axioms and Derived Predicates #493
Open
speckdavid
wants to merge
12
commits into
aiplan4eu:master
Choose a base branch
from
speckdavid:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
06800f8
added axioms to problem model and functions to the pddl reader and wr…
speckdavid 2b18f77
Merge branch 'master' of https://github.com/aiplan4eu/unified-planning
speckdavid 8637771
fixed head of axioms to be a fluent
speckdavid c5baf27
Merge branch 'master' of https://github.com/aiplan4eu/unified-planning
speckdavid 70c1013
derived bool type to handle initial state
speckdavid 762d088
Merge branch 'master' of https://github.com/aiplan4eu/unified-planning
speckdavid 2f2c9d7
style
speckdavid 6f6d903
proper integration for axiom but still buggy
speckdavid cc950dc
Support for axioms and derived predicates.
speckdavid 0f30fe9
Merge branch 'master' of https://github.com/aiplan4eu/unified-planning
speckdavid b4b51ed
Merge branch 'david-sprint2'
speckdavid d186157
Update up-symk version
speckdavid File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
# Copyright 2021-2023 AIPlan4EU project | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
""" | ||
This module defines the base class `Axiom'. | ||
An `Axiom' has a `head' which is a single `Parameter' and a `body' which is a single `Condition'. | ||
""" | ||
|
||
from unified_planning.model.action import * | ||
from unified_planning.environment import get_environment, Environment | ||
|
||
|
||
class Axiom(InstantaneousAction): | ||
def __init__( | ||
self, | ||
_name: str, | ||
_parameters: Optional["OrderedDict[str, up.model.types.Type]"] = None, | ||
_env: Optional[Environment] = None, | ||
**kwargs: "up.model.types.Type", | ||
): | ||
InstantaneousAction.__init__(self, _name, _parameters, _env, **kwargs) | ||
|
||
def set_head(self, fluent: Union["up.model.fnode.FNode", "up.model.fluent.Fluent"]): | ||
if not fluent.type.is_derived_bool_type(): | ||
raise UPTypeError("The head of an axiom must be of type DerivedBoolType!") | ||
|
||
self.add_effect(fluent) | ||
|
||
def add_body_condition( | ||
self, | ||
precondition: Union[ | ||
"up.model.fnode.FNode", | ||
"up.model.fluent.Fluent", | ||
"up.model.parameter.Parameter", | ||
bool, | ||
], | ||
): | ||
super().add_precondition(precondition) | ||
|
||
def add_effect( | ||
self, | ||
fluent: Union["up.model.fnode.FNode", "up.model.fluent.Fluent"], | ||
value: "up.model.expression.Expression" = True, | ||
condition: "up.model.expression.BoolExpression" = True, | ||
forall: Iterable["up.model.variable.Variable"] = tuple(), | ||
): | ||
if value != True: | ||
raise UPUsageError("value can only be true for an axiom") | ||
if condition != True: | ||
raise UPUsageError("the effect of an axiom can not include a condition") | ||
if len(forall) > 0: | ||
raise UPUsageError("the effect of an axiom can not a forall") | ||
|
||
( | ||
fluent_exp, | ||
value_exp, | ||
condition_exp, | ||
) = self._environment.expression_manager.auto_promote(fluent, value, condition) | ||
|
||
if not fluent_exp.is_fluent_exp(): | ||
raise UPUsageError("head/effect of an axiom must be a fluent expression") | ||
|
||
fluent_exp_params = [p.parameter() for p in fluent_exp.args] | ||
if self.parameters != fluent_exp_params: | ||
raise UPUsageError( | ||
f"parameters of axiom and this fluent expression do not match: {self.parameters} vs. {fluent_exp_params}" | ||
) | ||
|
||
self.clear_effects() | ||
self._add_effect_instance( | ||
up.model.effect.Effect(fluent_exp, value_exp, condition_exp, forall=forall) | ||
) | ||
|
||
@property | ||
def head(self) -> List["up.model.fluent.Fluent"]: | ||
"""Returns the `head` of the `Axiom`.""" | ||
return self._effects[0] | ||
|
||
@property | ||
def body(self) -> List["up.model.fnode.FNode"]: | ||
"""Returns the `list` of the `Axiom` `body`.""" | ||
return self._preconditions | ||
|
||
def __repr__(self) -> str: | ||
s = [] | ||
s.append(f"axiom {self.name}") | ||
first = True | ||
for p in self.parameters: | ||
if first: | ||
s.append("(") | ||
first = False | ||
else: | ||
s.append(", ") | ||
s.append(str(p)) | ||
if not first: | ||
s.append(")") | ||
s.append("{\n") | ||
s.append(f" head = {self.head}\n") | ||
s.append(f" body = {self.body}\n") | ||
s.append(" }") | ||
return "".join(s) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why deriving from
InstantaneousAction
? This makes it possible to haveAxioms
passed to theadd_action
methods and causes some hacky code below. Why not a dedicated class?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The idea was mainly to reuse the functionality and code of actions, not only when defining an action, but also for PDDL parsing. But I agree that we may have some side effects here. In general, I am open to creating a dedicated class not derived from
InstantaneousAction
, but I am a bit concerned that the PDDL parsing might become a bit more involved and we might get some redundant code.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you have a look at the internal structure of the InstantaneousActiona and see what is that you really need? Maybe this could be a call to a small refactoring aimed at factorising what is needed to both instantaneous actions and axioms.