Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def add(
self: "VVarList",
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":
solver = self.__model.solver
if not name:
name = "var({})".format(len(self))
if var_type == mip.BINARY:
lb = 0.0
ub = 1.0
new_var = mip.Var(self.__model, solver.num_cols())
solver.add_var(obj, lb, ub, var_type, column, name)
return new_var
def __getitem__(self: "VVarList", key):
if isinstance(key, str):
return self.__model.var_by_name(key)
if isinstance(key, slice):
return VVarList(self.__model, key.start, key.end)
if isinstance(key, int):
if key < 0:
key = self.__end - key
if key >= self.__end:
raise IndexError
return mip.Var(self.__model, key + self.__start)
raise TypeError("Unknown type {}".format(type(key)))
def conflicting(
self: "SolverCbc", e1: Union["LinExpr", "Var"], e2: Union["LinExpr", "Var"],
) -> bool:
idx1, idx2 = (None, None)
if isinstance(e1, Var):
idx1 = e1.idx
elif isinstance(e1, LinExpr):
if len(e1.expr) == 1 and e1.sense == EQUAL:
v1 = next(iter(e1.expr.keys()))
if abs(e1.const) <= 1e-15:
idx1 = v1.idx + v1.model.num_cols
elif abs(e1.const + 1.0) <= 1e-15:
idx1 = v1.idx
else:
raise InvalidParameter(
"LinExpr should contain an "
"assignment to a binary variable, "
"e.g.: x1 == 1"
)
else:
raise InvalidParameter(
def update_vars(self: "VarList", n_vars: int):
self.__vars = [mip.Var(self.__model, i) for i in range(n_vars)]
objects: Union[mip.Var, mip.Constr, List[Union["mip.Var", "mip.Constr"]]],
):
"""removes variable(s) and/or constraint(s) from the model
Args:
objects (Union[mip.Var, mip.Constr, List[Union[mip.Var, mip.Constr]]]):
can be a :class:`~mip.Var`, a :class:`~mip.Constr` or a list of these objects
"""
if isinstance(objects, (mip.Var, mip.Constr)):
objects = [objects]
if isinstance(objects, list):
vlist = []
clist = []
for o in objects:
if isinstance(o, mip.Var):
vlist.append(o)
elif isinstance(o, mip.Constr):
clist.append(o)
else:
raise TypeError(
"Cannot handle removal of object of type "
"{} from model".format(type(o))
)
if vlist:
self.vars.remove(vlist)
if clist:
self.constrs.remove(clist)
else:
raise TypeError(
"Cannot handle removal of object of type "
+ type(objects)
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 conflicting_nodes(
self: "SolverCbc", v1: Union["Var", "LinExpr"]
) -> Tuple[List["Var"], List["Var"]]:
"""Returns all assignment conflicting with the assignment in v1 in the
conflict graph.
"""
cg = cbclib.Cbc_conflictGraph(self._model)
if cg == ffi.NULL:
return (list(), list())
idx1 = None
if isinstance(v1, Var):
idx1 = v1.idx
elif isinstance(v1, LinExpr):
if len(v1.expr) == 1 and v1.sense == EQUAL:
var = next(iter(v1.expr.keys()))
if abs(v1.const) <= 1e-15:
idx1 = var.idx + var.model.num_cols
elif abs(v1.const + 1.0) <= 1e-15:
idx1 = var.idx
else:
raise InvalidParameter(
"LinExpr should contain an "
"assignment to a binary variable, "
"e.g.: x1 == 1"
)
else:
raise InvalidParameter(
"""Returns from the conflict graph all assignments conflicting with one
specific assignment.
Args:
v (Union[mip.Var, mip.LinExpr]): binary variable, if assignment to be
tested is the assignment to one or a linear expression like x == 0
to indicate the complement.
:rtype: Tuple[List[mip.Var], List[mip.Var]]
Returns:
Returns a tuple with two lists. The first one indicates variables
whose conflict occurs when setting them to one. The second list
includes variable whose conflict occurs when setting them to zero.
"""
if not isinstance(v, (mip.LinExpr, mip.Var)):
raise TypeError("type {} not supported".format(type(v)))
return self.model.solver.conflicting_nodes(v)
def minimize(objective: Union["mip.LinExpr", "mip.Var"]) -> "mip.LinExpr":
"""
Function that should be used to set the objective function to MINIMIZE
a given linear expression (passed as argument).
Args:
objective(Union[mip.LinExpr, Var]): linear expression
:rtype: mip.LinExpr
"""
if isinstance(objective, mip.Var):
objective = mip.LinExpr([objective], [1.0])
objective.sense = mip.MINIMIZE
return objective