From 79b205fa6d88411be9c945f9787941df75a4fed4 Mon Sep 17 00:00:00 2001 From: Luca Framba Date: Wed, 13 Sep 2023 11:02:53 +0200 Subject: [PATCH] Feat(Expression): Every expression defined as a Fraction but that is actually an integer is saved as an integer --- unified_planning/model/expression.py | 3 +++ unified_planning/model/mixins/metrics.py | 2 +- unified_planning/test/test_problem.py | 10 +++++----- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/unified_planning/model/expression.py b/unified_planning/model/expression.py index 1b94a6e7e..38e74901e 100644 --- a/unified_planning/model/expression.py +++ b/unified_planning/model/expression.py @@ -69,6 +69,9 @@ def uniform_numeric_constant(value: NumericConstant) -> Union[Fraction, int]: number = Fraction(value) except ValueError: raise UPValueError(f"Numeric constant {value} can't be converted to a number") + assert isinstance(number, Fraction) + if number.denominator == 1: + return number.numerator return number diff --git a/unified_planning/model/mixins/metrics.py b/unified_planning/model/mixins/metrics.py index 321200b4a..da52a5543 100644 --- a/unified_planning/model/mixins/metrics.py +++ b/unified_planning/model/mixins/metrics.py @@ -17,8 +17,8 @@ import unified_planning as up from unified_planning.model.metrics import PlanQualityMetric -from unified_planning.model.problem_kind import ProblemKind from unified_planning.model.mixins import ActionsSetMixin +from unified_planning.model.problem_kind import ProblemKind from unified_planning.exceptions import UPProblemDefinitionError diff --git a/unified_planning/test/test_problem.py b/unified_planning/test/test_problem.py index 36df223fa..70e6c249d 100644 --- a/unified_planning/test/test_problem.py +++ b/unified_planning/test/test_problem.py @@ -448,12 +448,12 @@ def test_fluents_defaults(self): if j == i + 1: self.assertEqual( problem.initial_value(distance(locations[i], locations[j])), - Real(Fraction(10)), + Int(10), ) else: self.assertEqual( problem.initial_value(distance(locations[i], locations[j])), - Real(Fraction(-1)), + Int(-1), ) def test_problem_defaults(self): @@ -574,11 +574,11 @@ def test_simple_numeric_planning_ad_hoc_1(self): self.assertTrue(grounded_problem.kind.has_simple_numeric_planning()) with self.assertRaises(UPTypeError): - problem.set_initial_value(distance(l2, l1), 2.0) + problem.set_initial_value(distance(l2, l1), 2.1) with self.assertRaises(UPTypeError): - problem.set_initial_value(distance(l2, l1), "2.0") + problem.set_initial_value(distance(l2, l1), "2.1") with self.assertRaises(UPTypeError): - problem.set_initial_value(distance(l2, l1), "4/2") + problem.set_initial_value(distance(l2, l1), "3/2") with self.assertRaises(UPTypeError): problem.set_initial_value(distance(l2, l1), Div(4, 2)) problem.set_initial_value(distance(l2, l1), "20")