Skip to content

Commit b77c7fb

Browse files
lperronMizux
authored andcommitted
fix #5302
1 parent 92f6aed commit b77c7fb

5 files changed

Lines changed: 40 additions & 12 deletions

File tree

‎ortools/linear_solver/python/model_builder.py‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,7 @@ def new_var_series(
720720
if (
721721
mbn.is_a_number(lower_bounds)
722722
and mbn.is_a_number(upper_bounds)
723-
and lower_bounds > upper_bounds
723+
and lower_bounds > upper_bounds # pyrefly: ignore[unsupported-operation]
724724
):
725725
raise ValueError(
726726
f"lower_bound={lower_bounds} is greater than"
@@ -1000,7 +1000,11 @@ def add_enforced(
10001000
"""
10011001
if isinstance(ct, mbh.BoundedLinearExpression):
10021002
return _add_enforced_linear_constraint_to_helper(
1003-
ct, self.__helper, var, value, name
1003+
ct,
1004+
self.__helper,
1005+
var,
1006+
value,
1007+
name, # pyrefly: ignore[bad-argument-type]
10041008
)
10051009
elif (
10061010
isinstance(ct, bool)
@@ -1241,7 +1245,7 @@ def value(self, expr: LinearExprT) -> np.double:
12411245
if not self.__solve_helper.has_solution():
12421246
return pd.NA
12431247
if mbn.is_a_number(expr):
1244-
return expr
1248+
return expr # pyrefly: ignore[bad-return]
12451249
elif isinstance(expr, LinearExpr):
12461250
return self.__solve_helper.expression_value(expr)
12471251
else:

‎ortools/linear_solver/python/model_builder_numbers.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def assert_is_a_number(x: NumberT) -> np.double:
5858
"""Asserts that x is a number and converts to a np.double."""
5959
if not is_a_number(x):
6060
raise TypeError("Not a number: %s" % x)
61-
return np.double(x)
61+
return np.double(x) # pyrefly: ignore[bad-argument-type]
6262

6363

6464
def assert_is_a_number_array(x: Sequence[NumberT]) -> npt.NDArray[np.double]:

‎ortools/linear_solver/python/model_builder_test.py‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2574,6 +2574,29 @@ def testSimplification12(self):
25742574
prod3 = 2 * x * 0.0
25752575
self.assertEqual(repr(prod3), "FixedValue(0)")
25762576

2577+
def testIssue3502a(self):
2578+
m = mb.Model()
2579+
x = m.new_bool_var("x")
2580+
y = m.new_num_var(0.0, 10.0, "y")
2581+
ct = m.add_enforced_linear_constraint(y, x, True, lb=0.0, ub=3.0, name="enf")
2582+
self.assertEqual(ct.name, "enf")
2583+
self.assertEqual(ct.lower_bound, 0.0)
2584+
self.assertEqual(ct.upper_bound, 3.0)
2585+
self.assertEqual(ct.indicator_variable.index, x.index)
2586+
self.assertTrue(ct.indicator_value)
2587+
2588+
def testIssue3502b(self):
2589+
m = mb.Model()
2590+
yy = m.new_num_var(0.0, 10.0, "y")
2591+
w = m.new_num_var(0.0, 10.0, "w")
2592+
z = m.new_bool_var("z")
2593+
m.add_linear_constraint(yy, ub=2.0, name="cap")
2594+
m.add_enforced_linear_constraint(w, z, True, ub=3.0, name="ind")
2595+
m.maximize(yy)
2596+
s = mb.Solver("sat")
2597+
self.assertEqual(s.solve(m), mb.SolveStatus.OPTIMAL)
2598+
self.assertEqual(s.objective_value, 2.0)
2599+
25772600

25782601
if __name__ == "__main__":
25792602
absltest.main()

‎ortools/linear_solver/python/pywraplp_test.py‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,12 @@ def test_proto(self):
5454
error = solver.LoadModelFromProto(input_proto)
5555
self.assertEqual(error, "")
5656
solver.Solve()
57-
# Fill response
58-
response = linear_solver_pb2.MPSolutionResponse()
59-
solver.FillSolutionResponseProto(response)
60-
self.assertEqual(response.objective_value, 3.0)
61-
self.assertEqual(response.variable_value[0], 1.0)
62-
self.assertEqual(response.variable_value[1], 1.0)
63-
self.assertEqual(response.best_objective_bound, 3.0)
57+
# Fill solution
58+
solution = solver.FillSolutionResponseProto()
59+
self.assertEqual(solution.objective_value, 3.0)
60+
self.assertEqual(solution.variable_value[0], 1.0)
61+
self.assertEqual(solution.variable_value[1], 1.0)
62+
self.assertEqual(solution.best_objective_bound, 3.0)
6463

6564
def test_external_api(self):
6665
solver = pywraplp.Solver.CreateSolver("GLOP")

‎ortools/linear_solver/samples/simple_lp_program_mb.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ def main():
6767
print("x =", solver.value(x))
6868
print("y =", solver.value(y))
6969

70-
print("dual_value(ct) =", solver.dual_value(ct))
70+
print(
71+
"dual_value(ct) =", solver.dual_value(ct)
72+
) # pyrefly: ignore[bad-argument-type]
7173
print("reduced_cost(x) =", solver.reduced_cost(x))
7274
else:
7375
print("The problem does not have an optimal solution.")

0 commit comments

Comments
 (0)