work on model_builder python; support building and testing it in python on bazel
This commit is contained in:
44
ortools/model_builder/python/BUILD.bazel
Normal file
44
ortools/model_builder/python/BUILD.bazel
Normal file
@@ -0,0 +1,44 @@
|
||||
# Python wrapper for model_builder.
|
||||
|
||||
load("@ortools_deps//:requirements.bzl", "requirement")
|
||||
load("@pybind11_bazel//:build_defs.bzl", "pybind_extension")
|
||||
load("@rules_python//python:defs.bzl", "py_library")
|
||||
|
||||
pybind_extension(
|
||||
name = "pywrap_model_builder_helper",
|
||||
srcs = ["pywrap_model_builder_helper.cc"],
|
||||
visibility = ["//visibility:public"],
|
||||
deps = [
|
||||
"//ortools/linear_solver:linear_solver_cc_proto",
|
||||
"//ortools/linear_solver:model_exporter",
|
||||
"//ortools/model_builder/wrappers:model_builder_helper",
|
||||
"@com_google_absl//absl/strings",
|
||||
"@eigen//:eigen3",
|
||||
],
|
||||
)
|
||||
|
||||
py_library(
|
||||
name = "model_builder_helper",
|
||||
srcs = ["model_builder_helper.py"],
|
||||
data = [
|
||||
":pywrap_model_builder_helper.so",
|
||||
],
|
||||
visibility = ["//visibility:public"],
|
||||
deps = [
|
||||
requirement("numpy"),
|
||||
"//ortools/linear_solver:linear_solver_py_pb2",
|
||||
],
|
||||
)
|
||||
|
||||
py_library(
|
||||
name = "model_builder",
|
||||
srcs = ["model_builder.py"],
|
||||
data = [
|
||||
":pywrap_model_builder_helper.so",
|
||||
],
|
||||
visibility = ["//visibility:public"],
|
||||
deps = [
|
||||
":model_builder_helper",
|
||||
"//ortools/linear_solver:linear_solver_py_pb2",
|
||||
],
|
||||
)
|
||||
@@ -157,7 +157,7 @@ class LinearExpr(object):
|
||||
return _Sum(self, arg)
|
||||
|
||||
def __radd__(self, arg):
|
||||
return self.__add(arg)
|
||||
return self.__add__(arg)
|
||||
|
||||
def __sub__(self, arg):
|
||||
if mbh.is_zero(arg):
|
||||
@@ -219,8 +219,6 @@ class LinearExpr(object):
|
||||
def __eq__(self, arg):
|
||||
if arg is None:
|
||||
return False
|
||||
if isinstance(self, Variable) and isinstance(arg, Variable):
|
||||
return VarCompVar(self, arg, True)
|
||||
if mbh.is_a_number(arg):
|
||||
arg = mbh.assert_is_a_number(arg)
|
||||
return BoundedLinearExpression(self, arg, arg)
|
||||
@@ -242,8 +240,6 @@ class LinearExpr(object):
|
||||
return BoundedLinearExpression(self - arg, -math.inf, 0)
|
||||
|
||||
def __ne__(self, arg):
|
||||
if isinstance(self, Variable) and isinstance(arg, Variable):
|
||||
return VarCompVar(self, arg, False)
|
||||
return NotImplemented
|
||||
|
||||
def __lt__(self, arg):
|
||||
@@ -540,6 +536,25 @@ class Variable(LinearExpr):
|
||||
def objective_coefficient(self, coeff):
|
||||
return self.__helper.set_var_objective_coefficient(self.__index, coeff)
|
||||
|
||||
def __eq__(self, arg):
|
||||
if arg is None:
|
||||
return False
|
||||
if isinstance(arg, Variable):
|
||||
return VarCompVar(self, arg, True)
|
||||
else:
|
||||
if mbh.is_a_number(arg):
|
||||
arg = mbh.assert_is_a_number(arg)
|
||||
return BoundedLinearExpression(self, arg, arg)
|
||||
else:
|
||||
return BoundedLinearExpression(self - arg, 0, 0)
|
||||
|
||||
def __ne__(self, arg):
|
||||
if arg is None:
|
||||
return True
|
||||
if isinstance(arg, Variable):
|
||||
return VarCompVar(self, arg, False)
|
||||
return NotImplemented
|
||||
|
||||
def __hash__(self):
|
||||
return hash((self.__helper, self.__index))
|
||||
|
||||
@@ -559,7 +574,7 @@ class VarCompVar(object):
|
||||
return f'{self.__left} == {self.__right}'
|
||||
|
||||
def __repr__(self):
|
||||
return f'VarEqVar({self.__left}, {self.__right}, {self.__is_equality})'
|
||||
return f'VarCompVar({self.__left}, {self.__right}, {self.__is_equality})'
|
||||
|
||||
@property
|
||||
def left(self):
|
||||
@@ -574,7 +589,7 @@ class VarCompVar(object):
|
||||
return self.__is_equality
|
||||
|
||||
def __bool__(self):
|
||||
return (self.__left == self.__right) == self.__is_equality
|
||||
return (self.__left.index == self.__right.index) == self.__is_equality
|
||||
|
||||
|
||||
class BoundedLinearExpression(object):
|
||||
|
||||
Reference in New Issue
Block a user