本文整理汇总了Python中gurobipy.Model.getConstrByName方法的典型用法代码示例。如果您正苦于以下问题:Python Model.getConstrByName方法的具体用法?Python Model.getConstrByName怎么用?Python Model.getConstrByName使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类gurobipy.Model
的用法示例。
在下文中一共展示了Model.getConstrByName方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: GurobiSolver
# 需要导入模块: from gurobipy import Model [as 别名]
# 或者: from gurobipy.Model import getConstrByName [as 别名]
class GurobiSolver(Solver):
""" Implements the solver interface using gurobipy. """
def __init__(self):
Solver.__init__(self)
self.problem = GurobiModel()
def __getstate__(self):
tmp_file = tempfile.mktemp(suffix=".lp")
self.problem.update()
self.problem.write(tmp_file)
cplex_form = open(tmp_file).read()
repr_dict = {'var_ids': self.var_ids, 'constr_ids': self.constr_ids, 'cplex_form': cplex_form}
return repr_dict
def __setstate__(self, repr_dict):
tmp_file = tempfile.mktemp(suffix=".lp")
open(tmp_file, 'w').write(repr_dict['cplex_form'])
self.problem = read(tmp_file)
self.var_ids = repr_dict['var_ids']
self.constr_ids = repr_dict['constr_ids']
def add_variable(self, var_id, lb=None, ub=None, vartype=VarType.CONTINUOUS, persistent=True, update_problem=True):
""" Add a variable to the current problem.
Arguments:
var_id : str -- variable identifier
lb : float -- lower bound
ub : float -- upper bound
vartype : VarType -- variable type (default: CONTINUOUS)
persistent : bool -- if the variable should be reused for multiple calls (default: true)
update_problem : bool -- update problem immediately (default: True)
"""
lb = lb if lb is not None else -GRB.INFINITY
ub = ub if ub is not None else GRB.INFINITY
map_types = {VarType.BINARY: GRB.BINARY,
VarType.INTEGER: GRB.INTEGER,
VarType.CONTINUOUS: GRB.CONTINUOUS}
if var_id in self.var_ids:
var = self.problem.getVarByName(var_id)
var.setAttr('lb', lb)
var.setAttr('ub', ub)
var.setAttr('vtype', map_types[vartype])
else:
self.problem.addVar(name=var_id, lb=lb, ub=ub, vtype=map_types[vartype])
self.var_ids.append(var_id)
if not persistent:
self.temp_vars.add(var_id)
if update_problem:
self.problem.update()
def add_constraint(self, constr_id, lhs, sense='=', rhs=0, persistent=True, update_problem=True):
""" Add a variable to the current problem.
Arguments:
constr_id : str -- constraint identifier
lhs : list [of (str, float)] -- variables and respective coefficients
sense : {'<', '=', '>'} -- default '='
rhs : float -- right-hand side of equation (default: 0)
persistent : bool -- if the variable should be reused for multiple calls (default: True)
update_problem : bool -- update problem immediately (default: True)
"""
grb_sense = {'=': GRB.EQUAL,
'<': GRB.LESS_EQUAL,
'>': GRB.GREATER_EQUAL}
if constr_id in self.constr_ids:
constr = self.problem.getConstrByName(constr_id)
self.problem.remove(constr)
expr = quicksum([coeff * self.problem.getVarByName(r_id) for r_id, coeff in lhs if coeff])
self.problem.addConstr(expr, grb_sense[sense], rhs, constr_id)
self.constr_ids.append(constr_id)
if not persistent:
self.temp_constrs.add(constr_id)
if update_problem:
self.problem.update()
def remove_variable(self, var_id):
""" Remove a variable from the current problem.
Arguments:
var_id : str -- variable identifier
"""
if var_id in self.var_ids:
self.problem.remove(self.problem.getVarByName(var_id))
self.var_ids.remove(var_id)
def remove_constraint(self, constr_id):
""" Remove a constraint from the current problem.
#.........这里部分代码省略.........