Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def set_objective_sense(self, sense: str):
if sense.strip().upper() == MAXIMIZE.strip().upper():
cbclib.Osi_setObjSense(self.osi, -1.0)
elif sense.strip().upper() == MINIMIZE.strip().upper():
cbclib.Osi_setObjSense(self.osi, 1.0)
else:
raise ValueError(
"Unknown sense: {}, use {} or {}".format(sense, MAXIMIZE, MINIMIZE)
)
def set_objective_sense(self, sense: str):
if sense.strip().upper() == MAXIMIZE.strip().upper():
cbclib.Osi_setObjSense(self.osi, -1.0)
elif sense.strip().upper() == MINIMIZE.strip().upper():
cbclib.Osi_setObjSense(self.osi, 1.0)
else:
raise ValueError(
"Unknown sense: {}, use {} or {}".format(sense, MAXIMIZE, MINIMIZE)
)
self._model = cbclib.Cbc_newModel()
cbclib.Cbc_storeNameIndexes(self._model, CHAR_ONE)
self.iidx_space = 4096
self.iidx = ffi.new("int[%d]" % self.iidx_space)
self.dvec = ffi.new("double[%d]" % self.iidx_space)
self._objconst = 0.0
# to not add cut generators twice when reoptimizing
self.added_cut_callback = False
self.added_inc_callback = False
# setting objective sense
if sense == MAXIMIZE:
cbclib.Cbc_setObjSense(self._model, -1.0)
self.emphasis = SearchEmphasis.DEFAULT
self.__threads = 0
self.__verbose = 1
# pre-allocate temporary space to query names
self.__name_space = ffi.new("char[{}]".format(MAX_NAME_SIZE))
# in cut generation
self.__name_spacec = ffi.new("char[{}]".format(MAX_NAME_SIZE))
self.__log = (
[]
) # type: List[Tuple[numbers.Real, Tuple[numbers.Real, numbers.Real]]]
self.set_problem_name(name)
self.__pumpp = DEF_PUMPP
# where solution will be stored
self._env,
self._model,
name.encode("utf-8"),
0,
ffi.NULL,
ffi.NULL,
ffi.NULL,
ffi.NULL,
ffi.NULL,
)
if st != 0:
raise InterfacingError("Could not create Gurobi model")
self._model = self._model[0]
# setting objective sense
if sense == MAXIMIZE:
self.set_int_attr("ModelSense", -1)
else:
self.set_int_attr("ModelSense", 1)
else:
self._ownsModel = False
self._model = modelp
self._env = GRBgetenv(self._model)
# default number of threads
self.__threads = 0
# fine grained control of what is changed
# for selective call on model.update
self.__n_cols_buffer = 0
self.__n_int_buffer = 0
self.__n_rows_buffer = 0
def maximize(objective: Union["mip.LinExpr", "mip.Var"]) -> "mip.LinExpr":
"""
Function that should be used to set the objective function to MAXIMIZE
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.MAXIMIZE
return objective
def set_objective_sense(self, sense: str):
if sense.strip().upper() == MAXIMIZE.strip().upper():
cbclib.Cbc_setObjSense(self._model, -1.0)
elif sense.strip().upper() == MINIMIZE.strip().upper():
cbclib.Cbc_setObjSense(self._model, 1.0)
else:
raise ValueError(
"Unknown sense: {}, use {} or {}".format(sense, MAXIMIZE, MINIMIZE)
)
def __str__(self) -> str:
result = []
if hasattr(self, "__sense"):
if self.__sense == mip.MINIMIZE:
result.append("Minimize ")
elif self.__sense == mip.MAXIMIZE:
result.append("Minimize ")
if self.__expr:
for var, coeff in self.__expr.items():
result.append("+ " if coeff >= 0 else "- ")
result.append(str(abs(coeff)) if abs(coeff) != 1 else "")
result.append("{var} ".format(**locals()))
if hasattr(self, "__sense"):
if self.__sense == mip.EQUAL:
result.append(" = ")
if self.__sense == mip.LESS_OR_EQUAL:
result.append(" <= ")
if self.__sense == mip.GREATER_OR_EQUAL:
result.append(" >= ")
result.append(
def get_objective_sense(self) -> str:
objs = cbclib.Osi_getObjSense(self.osi)
if objs <= -0.5:
return MAXIMIZE
return MINIMIZE
def get_objective_sense(self) -> str:
isense = self.get_int_attr("ModelSense")
if isense == 1:
return MINIMIZE
elif isense == -1:
return MAXIMIZE
else:
raise ValueError("Unknown sense")