Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def get_int_param(self, name: str) -> int:
res = ffi.new("int *")
env = GRBgetenv(self._model)
error = GRBgetintparam(env, name.encode("utf-8"), res)
if error != 0:
raise ParameterNotAvailable(
"Error getting gurobi integer parameter {}".format(name)
)
return res[0]
def get_objective(self) -> LinExpr:
obj = cbclib.Cbc_getObjCoefficients(self._model)
if obj == ffi.NULL:
raise ParameterNotAvailable("Error getting objective function coefficients")
return (
xsum(
obj[j] * self.model.vars[j]
for j in range(self.num_cols())
if abs(obj[j]) >= 1e-15
)
+ self._objconst
)
def constr_get_index(self, name: str) -> int:
GRBupdatemodel(self._model)
idx = ffi.new("int *")
st = GRBgetconstrbyname(self._model, name.encode("utf-8"), idx)
if st != 0:
raise ParameterNotAvailable("Error calling GRBgetconstrbyname")
return idx[0]
st = GRBgetintattr(grb_model, "NumVars".encode("utf-8"), ires)
if st != 0:
raise ParameterNotAvailable("Could not query number of variables")
ncols = ires[0]
self._cb_sol = ffi.new("double[{}]".format(ncols))
res = GRBcbget(cb_data, where, GRB_CB_MIPNODE_REL, self._cb_sol)
if res != 0:
raise ParameterNotAvailable("Error getting fractional solution")
else:
self._cb_sol = ffi.NULL
elif where == GRB_CB_MIPSOL:
self._status = OptimizationStatus.FEASIBLE
ires = ffi.new("int *")
st = GRBgetintattr(grb_model, "NumVars".encode("utf-8"), ires)
if st != 0:
raise ParameterNotAvailable(
"Could not query number of variables in Gurobi callback"
)
ncols = ires[0]
self._cb_sol = ffi.new("double[{}]".format(ncols))
res = GRBcbget(cb_data, where, GRB_CB_MIPSOL_SOL, self._cb_sol)
if res != 0:
raise ParameterNotAvailable(
"Error getting integer solution in gurobi callback"
)
objp = ffi.new("double *")
res = GRBcbget(cb_data, where, GRB_CB_MIPSOL_OBJ, objp)
if res != 0:
raise ParameterNotAvailable("Error getting solution obj in Gurobi")
self._obj_value = objp[0]
def add_sos(self, sos: List[Tuple["Var", float]], sos_type: int):
self.flush_cols()
types = ffi.new("int[]", [sos_type])
beg = ffi.new("int[]", [0, len(sos)])
idx = ffi.new("int[]", [v.idx for (v, f) in sos])
w = ffi.new("double[]", [f for (v, f) in sos])
st = GRBaddsos(self._model, 1, len(sos), types, beg, idx, w)
if st != 0:
raise ParameterNotAvailable("Error adding SOS to the model")
def get_objective(self) -> LinExpr:
self.flush_cols()
attr = "Obj".encode("utf-8")
# st = GRBsetdblattrarray(self._model, attr,
# 0, num_vars, zeros)
obj = ffi.new("double[]", [0.0 for i in range(self.num_cols())])
st = GRBgetdblattrarray(self._model, attr, 0, self.num_cols(), obj)
if st != 0:
raise ParameterNotAvailable("Error getting objective function")
obj_expr = xsum(
obj[i] * self.model.vars[i]
for i in range(self.num_cols())
if abs(obj[i] > 1e-20)
)
obj_expr.sense = self.get_objective_sense
return obj_expr
if st != 0:
raise ParameterNotAvailable(
"Could not query number of variables in Gurobi callback"
)
ncols = ires[0]
self._cb_sol = ffi.new("double[{}]".format(ncols))
res = GRBcbget(cb_data, where, GRB_CB_MIPSOL_SOL, self._cb_sol)
if res != 0:
raise ParameterNotAvailable(
"Error getting integer solution in gurobi callback"
)
objp = ffi.new("double *")
res = GRBcbget(cb_data, where, GRB_CB_MIPSOL_OBJ, objp)
if res != 0:
raise ParameterNotAvailable("Error getting solution obj in Gurobi")
self._obj_value = objp[0]
else:
self._cb_sol = ffi.NULL
def add_lazy_constr(self, lin_expr: "LinExpr"):
if self._where == GRB_CB_MIPSOL:
# collecting linear expression data
nz = len(lin_expr.expr)
cind = ffi.new("int[]", [var.idx for var in lin_expr.expr.keys()])
cval = ffi.new("double[]", [coef for coef in lin_expr.expr.values()])
# constraint sense and rhs
sense = lin_expr.sense.encode("utf-8")
rhs = -lin_expr.const
res = GRBcblazy(self._cb_data, nz, cind, cval, sense, rhs)
if res != 0:
raise ParameterNotAvailable("Error adding lazy constraint in Gurobi.")
elif self._where == GRB_CB_MIPNODE:
# collecting linear expression data
nz = len(lin_expr.expr)
cind = ffi.new("int[]", [var.idx for var in lin_expr.expr.keys()])
cval = ffi.new("double[]", [coef for coef in lin_expr.expr.values()])
# constraint sense and rhs
sense = lin_expr.sense.encode("utf-8")
rhs = -lin_expr.const
res = GRBcbcut(self._cb_data, nz, cind, cval, sense, rhs)
if res != 0:
raise ParameterNotAvailable("Error adding cutting plane in Gurobi.")
else:
raise ProgrammingError(
"Calling add_lazy_constr in unknown callback location"
def get_dbl_param(self, param: str) -> float:
res = ffi.new("double *")
env = GRBgetenv(self._model)
error = GRBgetdblparam(env, param.encode("utf-8"), res)
if error != 0:
raise ParameterNotAvailable(
"Error getting gurobi double parameter {}".format(param)
)
return res[0]