Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def create_mip(solver, J, dur, S, c, r, EST, relax=False, sense=MINIMIZE):
"""Creates a mip model to solve the RCPSP"""
NR = len(c)
mip = Model(solver_name=solver)
sd = sum(dur[j] for j in J)
vt = CONTINUOUS if relax else BINARY
x = [
{
t: mip.add_var("x(%d,%d)" % (j, t), var_type=vt)
for t in range(EST[j], sd + 1)
}
for j in J
]
TJ = [set(x[j].keys()) for j in J]
T = set()
for j in J:
T = T.union(TJ[j])
if sense == MINIMIZE:
mip.objective = minimize(xsum(t * x[J[-1]][t] for t in TJ[-1]))
else:
mip.objective = maximize(xsum(t * x[J[-1]][t] for t in TJ[-1]))
def var_type(self, value: str):
if value not in (mip.BINARY, mip.CONTINUOUS, mip.INTEGER):
raise ValueError(
"Expected one of {}, but got {}".format(
(mip.BINARY, mip.CONTINUOUS, mip.INTEGER), value
)
)
self.__model.solver.var_set_var_type(self, value)
def add_var(
self: "Model",
name: str = "",
lb: numbers.Real = 0.0,
ub: numbers.Real = mip.INF,
obj: numbers.Real = 0.0,
var_type: str = mip.CONTINUOUS,
column: "mip.Column" = None,
) -> "mip.Var":
""" Creates a new variable in the model, returning its reference
Args:
name (str): variable name (optional)
lb (numbers.Real): variable lower bound, default 0.0
ub (numbers.Real): variable upper bound, default infinity
obj (numbers.Real): coefficient of this variable in the objective
function, default 0
var_type (str): CONTINUOUS ("C"), BINARY ("B") or INTEGER ("I")
column (mip.Column): constraints where this variable will appear,
necessary only when constraints are already created in
the model and a new variable will be created.
Examples:
def relax(self):
self.flush_cols()
idxv = [var.idx for var in self.model.vars if var.var_type in [BINARY, INTEGER]]
n = len(idxv)
idxs = ffi.new("int[]", idxv)
cont_char = CONTINUOUS.encode("utf-8")
ccont = ffi.new("char[]", [cont_char for i in range(n)])
attr = "VType".encode("utf-8")
GRBsetcharattrlist(self._model, attr, n, idxs, ccont)
self.__updated = False
self.update()
def add_var(
self,
name: str = "",
obj: numbers.Real = 0,
lb: numbers.Real = 0,
ub: numbers.Real = INF,
var_type: str = CONTINUOUS,
column: "Column" = None,
):
# collecting column data
if column is None:
vind = ffi.NULL
vval = ffi.NULL
numnz = 0
else:
vind = ffi.new("int[]", [c.idx for c in column.constrs])
vval = ffi.new("double[]", [coef for coef in column.coeffs])
numnz = len(column.constrs)
isInt = (
CHAR_ONE if var_type.upper() == "B" or var_type.upper() == "I" else CHAR_ZERO
)
cbclib.Osi_addCol(
def var_set_var_type(self, var: "Var", value: str):
cv = var.var_type
if value == cv:
return
if cv == CONTINUOUS:
if value in (INTEGER, BINARY):
cbclib.Osi_setInteger(self.osi, var.idx)
else:
if value == CONTINUOUS:
cbclib.Osi_setContinuous(self.osi, var.idx)
if value == BINARY:
# checking bounds
if var.lb != 0.0:
var.lb = 0.0
if var.ub != 1.0:
var.ub = 1.0
self.set_int_param("Seed", self.model.seed)
self.set_int_param("PoolSolutions", self.model.sol_pool_size)
# executing Gurobi to solve the formulation
self.__clear_sol()
# if solve only LP relax, saving var types
int_vars = []
if relax:
int_vars = [
(v, v.var_type)
for v in self.model.vars
if v.var_type in [BINARY, INTEGER]
]
for v, _ in int_vars:
v.var_type = CONTINUOUS
self.update()
status = GRBoptimize(self._model)
if int_vars:
for v, vt in int_vars:
v.var_type = vt
self.update()
if status != 0:
if status == 10009:
raise InterfacingError(
"gurobi found but license not accepted, please check it"
)
if status == 10001:
raise MemoryError("out of memory error")
def add(
self,
name: str = "",
lb: numbers.Real = 0.0,
ub: numbers.Real = mip.INF,
obj: numbers.Real = 0.0,
var_type: str = mip.CONTINUOUS,
column: "mip.Column" = None,
) -> "mip.Var":
if not name:
name = "var({})".format(len(self.__vars))
if var_type == mip.BINARY:
lb = 0.0
ub = 1.0
new_var = mip.Var(self.__model, len(self.__vars))
self.__model.solver.add_var(obj, lb, ub, var_type, column, name)
self.__vars.append(new_var)
return new_var
def add_var(
self: "Solver",
name: str = "",
obj: numbers.Real = 0,
lb: numbers.Real = 0,
ub: numbers.Real = mip.INF,
var_type: str = mip.CONTINUOUS,
column: "Column" = None,
):
pass