Skip to content

Commit

Permalink
Feat(Expression): Every expression defined as a Fraction but that is …
Browse files Browse the repository at this point in the history
…actually an integer is saved as an integer
  • Loading branch information
Framba-Luca committed Sep 13, 2023
1 parent 3e2747f commit 79b205f
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
3 changes: 3 additions & 0 deletions unified_planning/model/expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
2 changes: 1 addition & 1 deletion unified_planning/model/mixins/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
10 changes: 5 additions & 5 deletions unified_planning/test/test_problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 79b205f

Please sign in to comment.