本文整理汇总了Python中gurobipy.GurobiError方法的典型用法代码示例。如果您正苦于以下问题:Python gurobipy.GurobiError方法的具体用法?Python gurobipy.GurobiError怎么用?Python gurobipy.GurobiError使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类gurobipy
的用法示例。
在下文中一共展示了gurobipy.GurobiError方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: findSolutionValues
# 需要导入模块: import gurobipy [as 别名]
# 或者: from gurobipy import GurobiError [as 别名]
def findSolutionValues(self):
for var in self.varables.values():
try:
var.varValue = var.solver_var.X
except gurobipy.GurobiError:
pass
GRB = gurobipy.GRB
gurobiLPStatus = {
GRB.OPTIMAL: LpStatusOptimal,
GRB.INFEASIBLE: LpStatusInfeasible,
GRB.INF_OR_UNBD: LpStatusInfeasible,
GRB.UNBOUNDED: LpStatusUnbounded,
GRB.ITERATION_LIMIT: LpStatusNotSolved,
GRB.NODE_LIMIT: LpStatusNotSolved,
GRB.TIME_LIMIT: LpStatusNotSolved,
GRB.SOLUTION_LIMIT: LpStatusNotSolved,
GRB.INTERRUPTED: LpStatusNotSolved,
GRB.NUMERIC: LpStatusNotSolved,
}
self.status = gurobiLPStatus.get(self.gurobi_model.Status, LpStatusUndefined)
return self.status
示例2: _relax_customer_constraints_with_feasRelax
# 需要导入模块: import gurobipy [as 别名]
# 或者: from gurobipy import GurobiError [as 别名]
def _relax_customer_constraints_with_feasRelax(m, customer_cover_constraints, active_ptls, relaxed_ptls):
# relax the model and relax minimal number of customer serving constraints
pens = [1.0]*len(customer_cover_constraints)
m.feasRelax(2, True, None, None, None, customer_cover_constraints, pens)
# TODO: not sure if feasRelax can change Status, test it someday
if m.Status == GRB.INTERRUPTED:
raise KeyboardInterrupt()
m.optimize()
# restore SIGINT callback handler which is changed by gurobipy
signal(SIGINT, default_int_handler)
status = m.Status
if __debug__:
log(DEBUG-2, "Relaxed problem Gurobi runtime = %.2f"%m.Runtime)
if m.Status == GRB.OPTIMAL:
log(DEBUG-3, "Relaxed problem Gurobi objective = %.2f"%
m.getObjective().getValue())
if status == GRB.OPTIMAL:
return _decision_variables_to_petals(m.X, active_ptls, relaxed_ptls), False
elif status == GRB.TIME_LIMIT:
raise GurobiError(10023, "Gurobi timeout reached when attempting to solve relaxed SCPCVRP")
elif m.Status == GRB.INTERRUPTED:
raise KeyboardInterrupt()
return None,False
示例3: copy_solver_configuration
# 需要导入模块: import gurobipy [as 别名]
# 或者: from gurobipy import GurobiError [as 别名]
def copy_solver_configuration(source, target):
"""
Copies the solver configuration from a source model to a target model
:param source:
:param target:
:return:
"""
# LP method
try:
target.solver.configuration.lp_method = source.solver.configuration.lp_method
except AttributeError:
pass
# Presolve
target.solver.configuration.presolve = source.solver.configuration.presolve
# Timeout
target.solver.configuration.timeout = source.solver.configuration.timeout
# Tolerances
for tol_name in dir(source.solver.configuration.tolerances):
tol = getattr(source.solver.configuration.tolerances, tol_name)
setattr(target.solver.configuration.tolerances, tol_name, tol)
# Additionnal solver-specific settings
try:
# Gurobi
if source.solver.interface.__name__ == 'optlang.gurobi_interface':
from gurobipy import GurobiError
for k in dir(source.solver.problem.Params):
if not k.startswith('_'):
try:
v = getattr(source.solver.problem.Params, k)
setattr(target.solver.problem.Params, k, v)
except GurobiError:
pass
except ModuleNotFoundError:
pass
# Verbosity
target.solver.configuration.verbosity = source.solver.configuration.verbosity
示例4: _mmp_solve
# 需要导入模块: import gurobipy [as 别名]
# 或者: from gurobipy import GurobiError [as 别名]
def _mmp_solve(w1_ij, x_ij_keys, n, w2_ij = None):
"""A helper function that solves a weighted maximum matching problem.
"""
m = Model("MBSA")
if __debug__:
log(DEBUG,"")
log(DEBUG,"Solving a weighted maximum matching problem with "+
"%d savings weights." % len(w1_ij))
# build model
x_ij = m.addVars(x_ij_keys, obj=w1_ij, vtype=GRB.BINARY, name='x')
_mmp_add_cnts_sum(m, x_ij, x_ij_keys, w1_ij, n)
m._vars = x_ij
m.modelSense = GRB.MAXIMIZE
m.update()
# disable output
m.setParam('OutputFlag', 0)
m.setParam('TimeLimit', MAX_MIP_SOLVER_RUNTIME)
m.setParam('Threads', MIP_SOLVER_THREADS)
#m.write("out.lp")
m.optimize()
# restore SIGINT callback handler which is changed by gurobipy
signal(SIGINT, default_int_handler)
if __debug__:
log(DEBUG-1, "Gurobi runtime = %.2f"%m.Runtime)
if m.Status == GRB.OPTIMAL:
if w2_ij==None:
max_wt, max_merge = max( (w1_ij[k], x_ij_keys[k])
for k, v in enumerate(m.X) if v )
else:
max_wt, _, max_merge = max( (w1_ij[k], w2_ij[k], x_ij_keys[k])
for k, v in enumerate(m.X) if v )
return max_wt, max_merge[0], max_merge[1]
elif m.Status == GRB.TIME_LIMIT:
raise GurobiError(10023, "Gurobi timeout reached when attempting to solve GAP")
elif m.Status == GRB.INTERRUPTED:
raise KeyboardInterrupt()
return None
示例5: findSolutionValues
# 需要导入模块: import gurobipy [as 别名]
# 或者: from gurobipy import GurobiError [as 别名]
def findSolutionValues(self, lp):
"""
:param lp:
:return:
"""
model = lp.solverModel
solutionStatus = model.Status
GRB = gurobipy.GRB
gurobiLpStatus = {GRB.OPTIMAL: LpStatusOptimal,
GRB.INFEASIBLE: LpStatusInfeasible,
GRB.INF_OR_UNBD: LpStatusInfeasible,
GRB.UNBOUNDED: LpStatusUnbounded,
GRB.ITERATION_LIMIT: LpStatusNotSolved,
GRB.NODE_LIMIT: LpStatusNotSolved,
GRB.TIME_LIMIT: LpStatusNotSolved,
GRB.SOLUTION_LIMIT: LpStatusNotSolved,
GRB.INTERRUPTED: LpStatusNotSolved,
GRB.NUMERIC: LpStatusNotSolved}
# populate pulp solution values
try:
for var, value in zip(lp.variables(), model.getAttr(GRB.Attr.X, model.getVars())):
var.varValue = value
except (gurobipy.GurobiError, AttributeError):
pass
try:
for var, value in zip(lp.variables(), model.getAttr(GRB.Attr.RC, model.getVars())):
var.dj = value
except (gurobipy.GurobiError, AttributeError):
pass
# put pi and slack variables against the constraints
try:
for constr, value in zip(lp.constraints.values(), model.getAttr(GRB.Pi, model.getConstrs())):
constr.pi = value
except (gurobipy.GurobiError, AttributeError):
pass
try:
for constr, value in zip(lp.constraints.values(), model.getAttr(GRB.Slack, model.getConstrs())):
constr.slack = value
except (gurobipy.GurobiError, AttributeError):
pass
if self.msg:
print("Gurobi status=", solutionStatus)
lp.resolveOK = True
for var in lp.variables():
var.isModified = False
lp.status = gurobiLpStatus.get(solutionStatus, LpStatusUndefined)
return lp.status