Skip to content

Commit 202812c

Browse files
committed
fix #5302
1 parent 08a6edb commit 202812c

6 files changed

Lines changed: 69 additions & 33 deletions

File tree

‎ortools/linear_solver/README.md‎

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,39 +29,39 @@ This module provides:
2929

3030
To begin, skim
3131

32-
* [linear_solver.proto](../linear_solver/linear_solver.proto):
32+
* [linear_solver.proto](/ortools/linear_solver/linear_solver.proto):
3333
Specifically, look at `MPModelProto`. This gives a succinct description of
3434
what problems can be solved.
3535

36-
* [solve_mp_model.h](../linear_solver/solve_mp_model.h):
36+
* [solve_mp_model.h](/ortools/linear_solver/solve_mp_model.h):
3737
This file contains the key functions to run various solvers.
3838

3939
## Available solvers
4040

4141
Each *\*_interface.cc* file corresponds to one of the solver accessible
4242
through the wrapper.
4343

44-
* Google's BOP (boolean) solver: [bop_interface.cc](../linear_solver/bop_interface.cc)
44+
* Google's BOP (boolean) solver: [bop_interface.cc](/ortools/linear_solver/bop_interface.cc)
4545

46-
* Google's GLOP (lp) solver: [glop_interface.cc](../linear_solver/glop_interface.cc)
46+
* Google's GLOP (lp) solver: [glop_interface.cc](/ortools/linear_solver/glop_interface.cc)
4747

48-
* Gurobi (MIP) solver: [gurobi_interface.cc](../linear_solver/gurobi_interface.cc)
48+
* Gurobi (MIP) solver: [gurobi_interface.cc](/ortools/linear_solver/gurobi_interface.cc)
4949

50-
* Google's PLDP solver: [pdlp_interface.cc](../linear_solver/pdlp_interface.cc)
50+
* Google's PLDP solver: [pdlp_interface.cc](/ortools/linear_solver/pdlp_interface.cc)
5151

52-
* SCIP (MIP) solver: [scip_interface.cc](../linear_solver/scip_interface.cc)
52+
* SCIP (MIP) solver: [scip_interface.cc](/ortools/linear_solver/scip_interface.cc)
5353

54-
* Google's CP-SAT solver: [sat_interface.cc](../linear_solver/sat_interface.cc)
54+
* Google's CP-SAT solver: [sat_interface.cc](/ortools/linear_solver/sat_interface.cc)
5555

56-
* Coin-OR Cbc (MIP) solver: [cbc_interface.cc](../linear_solver/cbc_interface.cc)
56+
* Coin-OR Cbc (MIP) solver: [cbc_interface.cc](/ortools/linear_solver/cbc_interface.cc)
5757

58-
* Coin-OR Clp (LP) solver: [clp_interface.cc](../linear_solver/clp_interface.cc)
58+
* Coin-OR Clp (LP) solver: [clp_interface.cc](/ortools/linear_solver/clp_interface.cc)
5959

60-
* CPLEX (MIP) solver: [cplex_interface.cc](../linear_solver/cplex_interface.cc)
60+
* CPLEX (MIP) solver: [cplex_interface.cc](/ortools/linear_solver/cplex_interface.cc)
6161

62-
* GLPK (MIP) solver: [glpk_interface.cc](../linear_solver/glpk_interface.cc)
62+
* GLPK (MIP) solver: [glpk_interface.cc](/ortools/linear_solver/glpk_interface.cc)
6363

64-
* Xpress (MIP) solver: [xpress_interface.cc](../linear_solver/xpress_interface.cc)
64+
* Xpress (MIP) solver: [xpress_interface.cc](/ortools/linear_solver/xpress_interface.cc)
6565

6666
## Wrappers
6767

@@ -76,4 +76,4 @@ through the wrapper.
7676

7777
## Samples
7878

79-
You can find some canonical examples in [samples](../linear_solver/samples/)
79+
You can find some canonical examples in [samples](/ortools/linear_solver/samples/)

‎ortools/linear_solver/python/model_builder.py‎

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ def _add_enforced_linear_constraint_to_helper(
172172
helper.set_enforced_constraint_lower_bound(c.index, bounded_expr.lower_bound)
173173
helper.set_enforced_constraint_upper_bound(c.index, bounded_expr.upper_bound)
174174
if name is not None:
175-
helper.set_constraint_name(c.index, name)
175+
helper.set_enforced_constraint_name(c.index, name)
176176
return c
177177

178178
raise TypeError(f"invalid type={type(bounded_expr).__name__!r}")
@@ -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"
@@ -941,16 +941,24 @@ def add_enforced_linear_constraint( # pytype: disable=annotation-type-mismatch
941941
ct.indicator_variable = ivar
942942
ct.indicator_value = ivalue
943943
if name:
944-
self.__helper.set_constraint_name(ct.index, name)
944+
self.__helper.set_enforced_constraint_name(ct.index, name)
945945
if mbn.is_a_number(linear_expr):
946-
self.__helper.set_constraint_lower_bound(ct.index, lb - linear_expr)
947-
self.__helper.set_constraint_upper_bound(ct.index, ub - linear_expr)
946+
self.__helper.set_enforced_constraint_lower_bound(
947+
ct.index, lb - linear_expr
948+
)
949+
self.__helper.set_enforced_constraint_upper_bound(
950+
ct.index, ub - linear_expr
951+
)
948952
elif isinstance(linear_expr, LinearExpr):
949953
flat_expr = mbh.FlatExpr(linear_expr)
950954
# pylint: disable=protected-access
951-
self.__helper.set_constraint_lower_bound(ct.index, lb - flat_expr.offset)
952-
self.__helper.set_constraint_upper_bound(ct.index, ub - flat_expr.offset)
953-
self.__helper.add_terms_to_constraint(
955+
self.__helper.set_enforced_constraint_lower_bound(
956+
ct.index, lb - flat_expr.offset
957+
)
958+
self.__helper.set_enforced_constraint_upper_bound(
959+
ct.index, ub - flat_expr.offset
960+
)
961+
self.__helper.add_terms_to_enforced_constraint(
954962
ct.index, flat_expr.vars, flat_expr.coeffs
955963
)
956964
else:
@@ -992,7 +1000,11 @@ def add_enforced(
9921000
"""
9931001
if isinstance(ct, mbh.BoundedLinearExpression):
9941002
return _add_enforced_linear_constraint_to_helper(
995-
ct, self.__helper, var, value, name
1003+
ct,
1004+
self.__helper,
1005+
var,
1006+
value,
1007+
name, # pyrefly: ignore[bad-argument-type]
9961008
)
9971009
elif (
9981010
isinstance(ct, bool)
@@ -1233,7 +1245,7 @@ def value(self, expr: LinearExprT) -> np.double:
12331245
if not self.__solve_helper.has_solution():
12341246
return pd.NA
12351247
if mbn.is_a_number(expr):
1236-
return expr
1248+
return expr # pyrefly: ignore[bad-return]
12371249
elif isinstance(expr, LinearExpr):
12381250
return self.__solve_helper.expression_value(expr)
12391251
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
@@ -2471,6 +2471,29 @@ def testSimplification12(self):
24712471
prod3 = 2 * x * 0.0
24722472
self.assertEqual(repr(prod3), "FixedValue(0)")
24732473

2474+
def testIssue3502a(self):
2475+
m = mb.Model()
2476+
x = m.new_bool_var("x")
2477+
y = m.new_num_var(0.0, 10.0, "y")
2478+
ct = m.add_enforced_linear_constraint(y, x, True, lb=0.0, ub=3.0, name="enf")
2479+
self.assertEqual(ct.name, "enf")
2480+
self.assertEqual(ct.lower_bound, 0.0)
2481+
self.assertEqual(ct.upper_bound, 3.0)
2482+
self.assertEqual(ct.indicator_variable.index, x.index)
2483+
self.assertTrue(ct.indicator_value)
2484+
2485+
def testIssue3502b(self):
2486+
m = mb.Model()
2487+
yy = m.new_num_var(0.0, 10.0, "y")
2488+
w = m.new_num_var(0.0, 10.0, "w")
2489+
z = m.new_bool_var("z")
2490+
m.add_linear_constraint(yy, ub=2.0, name="cap")
2491+
m.add_enforced_linear_constraint(w, z, True, ub=3.0, name="ind")
2492+
m.maximize(yy)
2493+
s = mb.Solver("sat")
2494+
self.assertEqual(s.solve(m), mb.SolveStatus.OPTIMAL)
2495+
self.assertEqual(s.objective_value, 2.0)
2496+
24742497

24752498
if __name__ == "__main__":
24762499
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)