Skip to content

Commit

Permalink
Fix(PK Equals, LE): Added code to correctly calculate == or <= betwee…
Browse files Browse the repository at this point in the history
…n PK with no specified version or with deprecated features
  • Loading branch information
Framba-Luca committed Oct 20, 2023
1 parent 937113e commit 0b15020
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 6 deletions.
45 changes: 39 additions & 6 deletions unified_planning/model/problem_kind.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.
#

from functools import partialmethod, total_ordering
from functools import cache, partialmethod, total_ordering
from itertools import chain
from typing import Dict, Iterable, List, Optional, Set
import unified_planning as up
Expand Down Expand Up @@ -129,6 +129,27 @@
all_features = set(chain(*FEATURES.values()))


@cache
def get_valid_features(version: int) -> Set[str]:
"""
Returns the set of features that are present and not deprecated in the
given ProblemKind's version.
:param version: The ProblemKind's version to extract the valid features from.
:return: the set of features that are present and not deprecated in the
given ProblemKind's version.
"""
valid_features: Set[str] = set()
for f in all_features:
added_version, deprecated_version = FEATURES_VERSIONS.get(f, (1, None))
if added_version > version:
continue
if deprecated_version is not None and deprecated_version <= version:
continue
valid_features.add(f)
return valid_features


class ProblemKindMeta(type):
"""Meta class used to interpret the nodehandler decorator."""

Expand Down Expand Up @@ -205,20 +226,32 @@ def __str__(self) -> str:
return "\n".join(result_str)

def __eq__(self, oth: object) -> bool:
if isinstance(oth, ProblemKind) and self._version == oth._version:
return self._features == oth._features
else:
return False
if isinstance(oth, ProblemKind):
if (
self._version is None
or oth._version is None
or self._version == oth._version
):
if self.version != oth.version:
return False
valid_features = get_valid_features(self.version)
self_feat = self._features.intersection(valid_features)
oth_feat = oth._features.intersection(valid_features)
return self_feat == oth_feat
return False

def __hash__(self) -> int:
return sum(map(hash, self._features))

def __le__(self, oth: object):
if not isinstance(oth, ProblemKind):
raise ValueError(f"Unable to compare a ProblemKind with a {type(oth)}")
self_feat, oth_feat, _ = equalize_versions(
self_feat, oth_feat, version = equalize_versions(
self._features, oth._features, self.version, oth.version
)
valid_version_features = get_valid_features(version)
self_feat.intersection_update(valid_version_features)
oth_feat.intersection_update(valid_version_features)
return self_feat.issubset(oth_feat)

def clone(self) -> "ProblemKind":
Expand Down
108 changes: 108 additions & 0 deletions unified_planning/test/test_problem_kind_versioning.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# 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.


import unified_planning as up
from unified_planning.shortcuts import *
from unified_planning.test import TestCase, main, examples
from unified_planning.test.examples import get_example_problems
from unified_planning.exceptions import UPTypeError


class TestProblemKindVersioning(TestCase):
def setUp(self):
TestCase.setUp(self)
self.problems = get_example_problems()

def test_version_2_1(self):
equals_expected_result = {
(
ProblemKind(
("CONTINUOUS_NUMBERS", "DISCRETE_NUMBERS", "NUMERIC_FLUENTS")
),
ProblemKind(("REAL_FLUENTS", "INT_FLUENTS")),
): False,
(
ProblemKind(("CONTINUOUS_NUMBERS", "NUMERIC_FLUENTS"), 1),
ProblemKind(("REAL_FLUENTS",)),
): False,
(
ProblemKind(("CONTINUOUS_NUMBERS", "NUMERIC_FLUENTS"), 1),
ProblemKind(("CONTINUOUS_NUMBERS", "NUMERIC_FLUENTS")),
): True,
(
ProblemKind(("CONTINUOUS_NUMBERS",), 1),
ProblemKind(("REAL_FLUENTS",)),
): False,
(
ProblemKind(("CONTINUOUS_NUMBERS", "DISCRETE_NUMBERS"), 2),
ProblemKind(("REAL_FLUENTS", "INT_FLUENTS")),
): False,
(
ProblemKind(
(
"CONTINUOUS_NUMBERS",
"DISCRETE_NUMBERS",
"REAL_FLUENTS",
"INT_FLUENTS",
),
2,
),
ProblemKind(("REAL_FLUENTS", "INT_FLUENTS")),
): True,
(
ProblemKind(("REAL_FLUENTS", "INT_FLUENTS")),
ProblemKind(("CONTINUOUS_NUMBERS", "DISCRETE_NUMBERS"), 2),
): False,
}

le_expected_results = {
(
ProblemKind(("CONTINUOUS_NUMBERS", "DISCRETE_NUMBERS")),
ProblemKind(("REAL_FLUENTS", "INT_FLUENTS")),
): True,
(
ProblemKind(("CONTINUOUS_NUMBERS", "DISCRETE_NUMBERS"), 1),
ProblemKind(("REAL_FLUENTS", "INT_FLUENTS")),
): True,
(
ProblemKind(("CONTINUOUS_NUMBERS", "DISCRETE_NUMBERS"), 2),
ProblemKind(("REAL_FLUENTS", "INT_FLUENTS")),
): True,
(
ProblemKind(("REAL_FLUENTS", "INT_FLUENTS")),
ProblemKind(("CONTINUOUS_NUMBERS", "DISCRETE_NUMBERS"), 2),
): False,
(
ProblemKind(("NUMERIC_FLUENTS", "DISCRETE_NUMBERS")),
ProblemKind(("UNBOUNDED_INT_ACTION_PARAMETERS", "INT_FLUENTS")),
): True,
}

for i, ((left_pk, right_pk), res) in enumerate(equals_expected_result.items()):
self.assertEqual(
left_pk == right_pk,
res,
f"{i}) {left_pk} == {right_pk} expected {res} but returned {left_pk == right_pk}",
)
assert (right_pk == left_pk) == (
right_pk == left_pk
), f"{i}) Error, equality does not work both ways"

for i, ((left_pk, right_pk), res) in enumerate(le_expected_results.items()):
self.assertEqual(
left_pk <= right_pk,
res,
f"{i}) {left_pk} <= {right_pk} expected {res} but returned {left_pk <= right_pk}",
)

0 comments on commit 0b15020

Please sign in to comment.