Skip to content

Commit

Permalink
[CP-SAT] test pandas code; typing tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
lperron committed Jul 11, 2023
1 parent 0094784 commit eea10fb
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
12 changes: 10 additions & 2 deletions ortools/sat/python/cp_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ def __str__(self):
)
if not exprs_str:
return "0"
return exprs_str
return f"({exprs_str})"

def __repr__(self):
exprs_str = ", ".join(map(repr, self.__expressions))
Expand Down Expand Up @@ -757,7 +757,9 @@ def __str__(self) -> str:
def __repr__(self) -> str:
return "%s(%s)" % (self.__var.name, DisplayBounds(self.__var.domain))

def Name(self) -> Optional[str]:
def Name(self) -> str:
if not self.__var or not self.__var.name:
return ""
return self.__var.name

def Not(self) -> "_NotBooleanVariable":
Expand Down Expand Up @@ -946,6 +948,8 @@ def WithName(self, name: str) -> "Constraint":

def Name(self) -> str:
"""Returns the name of the constraint."""
if not self.__constraint or not self.__constraint.name:
return ""
return self.__constraint.name

def Index(self) -> int:
Expand Down Expand Up @@ -1036,6 +1040,8 @@ def __repr__(self):
)

def Name(self) -> str:
if not self.__ct or not self.__ct.name:
return ""
return self.__ct.name

def StartExpr(self) -> LinearExprT:
Expand Down Expand Up @@ -1096,6 +1102,8 @@ def __init__(self):
# Naming.
def Name(self) -> str:
"""Returns the name of the model."""
if not self.__model or not self.__model.name:
return ""
return self.__model.name

def SetName(self, name: str):
Expand Down
34 changes: 29 additions & 5 deletions ortools/sat/python/cp_model_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
# limitations under the License.

"""Tests for ortools.sat.python.cp_model."""

from absl.testing import absltest
import pandas as pd

from ortools.sat.python import cp_model


Expand Down Expand Up @@ -61,7 +62,7 @@ def Obj(self):
return self.__obj


class LogToString(object):
class LogToString:
"""Record log in a string."""

def __init__(self):
Expand Down Expand Up @@ -473,7 +474,7 @@ def testWeightedSum(self):
print("testWeightedSum")
model = cp_model.CpModel()
x = [model.NewIntVar(0, 2, "x%i" % i) for i in range(100)]
c = [2 for i in range(100)]
c = [2] * 100
model.Add(cp_model.LinearExpr.WeightedSum(x, c) <= 3)
model.Maximize(x[99])
solver = cp_model.CpSolver()
Expand Down Expand Up @@ -1089,8 +1090,7 @@ def testDisplayBounds(self):
def testShortName(self):
print("testShortName")
model = cp_model.CpModel()
v = model.Proto().variables.add()
v.domain.extend([5, 10])
model.Proto().variables.add(domain=[5, 10])
self.assertEqual("[5..10]", cp_model.ShortName(model.Proto(), 0))

def testIntegerExpressionErrors(self):
Expand Down Expand Up @@ -1420,6 +1420,30 @@ def testModelError(self):
self.assertEqual(cp_model.MODEL_INVALID, solver.Solve(model))
self.assertEqual(solver.SolutionInfo(), 'var #0 has no domain(): name: "x0"')

def testIntVarSeries(self):
print("testIntVarSeries")
df = pd.DataFrame([1, -1, 1], columns=["coeffs"])
model = cp_model.CpModel()
x = model.NewIntVarSeries(
name="x", index=df.index, lower_bounds=0, upper_bounds=5
)
model.Minimize(df.coeffs.dot(x))
solver = cp_model.CpSolver()
self.assertEqual(cp_model.OPTIMAL, solver.Solve(model))
solution = solver.Values(x)
self.assertTrue((solution.values == [0, 5, 0]).all())

def testBoolVarSeries(self):
print("testBoolVarSeries")
df = pd.DataFrame([1, -1, 1], columns=["coeffs"])
model = cp_model.CpModel()
x = model.NewBoolVarSeries(name="x", index=df.index)
model.Minimize(df.coeffs.dot(x))
solver = cp_model.CpSolver()
self.assertEqual(cp_model.OPTIMAL, solver.Solve(model))
solution = solver.BooleanValues(x)
self.assertTrue((solution.values == [False, True, False]).all())


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

0 comments on commit eea10fb

Please sign in to comment.