本文整理汇总了Python中cylp.cy.CyClpSimplex.solve方法的典型用法代码示例。如果您正苦于以下问题:Python CyClpSimplex.solve方法的具体用法?Python CyClpSimplex.solve怎么用?Python CyClpSimplex.solve使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cylp.cy.CyClpSimplex
的用法示例。
在下文中一共展示了CyClpSimplex.solve方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: disjunctionToCut
# 需要导入模块: from cylp.cy import CyClpSimplex [as 别名]
# 或者: from cylp.cy.CyClpSimplex import solve [as 别名]
#.........这里部分代码省略.........
if use_cylp:
sp = CyLPModel()
u = sp.addVariable('u', A.shape[0], isInt = False)
v = sp.addVariable('v', A.shape[0], isInt = False)
u0 = sp.addVariable('u0', 1, isInt = False)
v0 = sp.addVariable('v0', 1, isInt = False)
alpha = sp.addVariable('alpha', lp.nVariables, isInt = False)
beta = sp.addVariable('beta', 1, isInt = False)
for i in range(A.shape[1]):
sp += alpha[i] - sum(Atran[i,j]*u[j] for j in range(A.shape[0])) + pi[i]*u0 == 0
for i in range(A.shape[1]):
sp += alpha[i] - sum(Atran[i,j]*v[j] for j in range(A.shape[0])) - pi[i]*v0 == 0
sp += beta - b*u + pi0*u0 <= 0
sp += beta - b*v - (pi0 + 1)*v0 <= 0
sp += u0 + v0 == 1
if sense == '<=':
sp += u >= 0
sp += v >= 0
sp += u0 >= 0
sp += v0 >= 0
else:
#TODO this direction is not debugged
# Is this all we need?
sp += u <= 0
sp += v <= 0
sp += u0 <= 0
sp += v0 <= 0
sp.objective = sum(sol[i]*alpha[i] for i in range(A.shape[1])) - beta
cbcModel = CyClpSimplex(sp).getCbcModel()
cbcModel.logLevel = 0
#cbcModel.maximumSeconds = 5
cbcModel.solve()
beta = cbcModel.primalVariableSolution['beta'][0]
alpha = cbcModel.primalVariableSolution['alpha']
u = cbcModel.primalVariableSolution['u']
v = cbcModel.primalVariableSolution['v']
u0 = cbcModel.primalVariableSolution['u0'][0]
v0 = cbcModel.primalVariableSolution['v0'][0]
if debug_print:
print('Objective Value: ', cbcModel.objectiveValue)
print('alpha: ', alpha, 'alpha*sol: ', np.dot(alpha, sol))
print('beta: ', beta)
print('Violation of cut: ', np.dot(alpha, sol) - beta)
else:
CG = AbstractModel()
CG.u = Var(list(range(A.shape[0])), domain=NonNegativeReals,
bounds = (0.0, None))
CG.v = Var(list(range(A.shape[0])), domain=NonNegativeReals,
bounds = (0.0, None))
CG.u0 = Var(domain=NonNegativeReals, bounds = (0.0, None))
CG.v0 = Var(domain=NonNegativeReals, bounds = (0.0, None))
CG.alpha = Var(list(range(A.shape[0])), domain=Reals,
bounds = (None, None))
CG.beta = Var(domain=Reals, bounds = (None, None))
## Constraints
def pi_rule_left(CG, i):
x = float(pi[i])
return(sum(Atran[i, j]*CG.u[j] for j in range(A.shape[0])) -
x*CG.u0 - CG.alpha[i] == 0.0)
CG.pi_rule_left = Constraint(list(range(A.shape[1])), rule=pi_rule_left)
def pi_rule_right(CG, i):
x = float(pi[i])
示例2: splitCuts
# 需要导入模块: from cylp.cy import CyClpSimplex [as 别名]
# 或者: from cylp.cy.CyClpSimplex import solve [as 别名]
def splitCuts(lp, integerIndices = None, sense = '>=', sol = None,
max_coeff = 1):
A = lp.coefMatrix
b = CyLPArray(lp.constraintsUpper)
if integerIndices is None:
integerIndices = range(lp.nVariables)
if sol is None:
sol = lp.primalVariableSolution['x']
s = A*sol - b
best = lp.getCoinInfinity()
best_theta = None
for theta in [0.1, 0.2, 0.3, 0.4, 0.5]:
sp = CyLPModel()
u = sp.addVariable('u', lp.nConstraints, isInt = False)
v = sp.addVariable('v', lp.nConstraints, isInt = False)
pi = sp.addVariable('pi', lp.nVariables, isInt = True)
pi0 = sp.addVariable('pi0', 1, isInt = True)
sp += pi + A.transpose()*u - A.transpose()*v == 0
sp += pi0 + b*u - b*v == theta - 1
if sense == '<=':
sp += u >= 0
sp += v >= 0
else:
#TODO this direction is not debugged
# Is this all we need?
sp += u <= 0
sp += v <= 0
sp.objective = (theta-1)*s*u - theta*s*v
for i in xrange(lp.nVariables):
if i in integerIndices:
sp += -max_coeff <= pi[i] <= max_coeff
else:
sp[i] += pi[i] == 0
cbcModel = CyClpSimplex(sp).getCbcModel()
cbcModel.logLevel = 0
#cbcModel.maximumSeconds = 5
cbcModel.solve()
if debug_print:
print theta, cbcModel.objectiveValue
print cbcModel.primalVariableSolution['pi'],
print cbcModel.primalVariableSolution['pi0']
if cbcModel.objectiveValue < best:
best = cbcModel.objectiveValue
multu = cbcModel.primalVariableSolution['u']
disjunction = cbcModel.primalVariableSolution['pi']
rhs = cbcModel.primalVariableSolution['pi0']
best_theta = theta
if best_theta is not None:
alpha = A.transpose()*multu + best_theta*disjunction
if sense == '<=':
beta = np.dot(lp.constraintsUpper, multu) + best_theta*rhs
else:
beta = np.dot(lp.constraintsLower, multu) + best_theta*rhs
if (abs(alpha) > 1e-6).any():
return [(alpha, beta)]
return []
示例3: maxViolationSplitCuts
# 需要导入模块: from cylp.cy import CyClpSimplex [as 别名]
# 或者: from cylp.cy.CyClpSimplex import solve [as 别名]
def maxViolationSplitCuts(lp, integerIndices = None, sense = '>=', sol = None,
max_coeff = 1):
#Warning: At the moment, you must put bound constraints in explicitly for split cuts
A = lp.coefMatrix
if sense == '<=':
b = CyLPArray(lp.constraintsUpper)
else:
b = CyLPArray(lp.constraintsLower)
if integerIndices is None:
integerIndices = range(lp.nVariables)
if sol is None:
sol = lp.primalVariableSolution['x']
s = A*sol - b
best = lp.getCoinInfinity()
best_theta = None
for theta in [0.01, 0.02, 0.03, 0.04, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.46, 0.47, 0.48, 0.49, 0.5]:
sp = CyLPModel()
u = sp.addVariable('u', lp.nConstraints, isInt = False)
v = sp.addVariable('v', lp.nConstraints, isInt = False)
pi = sp.addVariable('pi', lp.nVariables, isInt = True)
pi0 = sp.addVariable('pi0', 1, isInt = True)
sp += pi + A.transpose()*u - A.transpose()*v == 0
sp += pi0 + b*u - b*v == theta - 1
if sense == '<=':
sp += u >= 0
sp += v >= 0
else:
#TODO this direction is not debugged
# Is this all we need?
sp += u <= 0
sp += v <= 0
sp.objective = (theta-1)*s*u - theta*s*v
for i in xrange(lp.nVariables):
if i in integerIndices:
sp += -max_coeff <= pi[i] <= max_coeff
else:
sp[i] += pi[i] == 0
cbcModel = CyClpSimplex(sp).getCbcModel()
cbcModel.logLevel = 0
#cbcModel.maximumSeconds = 5
cbcModel.solve()
if debug_print:
#print 'Theta: ', theta,
#print 'Objective Value: ', cbcModel.objectiveValue - theta*(1-theta)
#print 'pi: ', cbcModel.primalVariableSolution['pi']
#print 'pi0: ', cbcModel.primalVariableSolution['pi0']
multu = cbcModel.primalVariableSolution['u']
disjunction = cbcModel.primalVariableSolution['pi']
rhs = cbcModel.primalVariableSolution['pi0']
alpha = A.transpose()*multu + theta*disjunction
beta = np.dot(b, multu) + theta*rhs
#print 'alpha: ', alpha, 'alpha*sol: ', np.dot(alpha, sol)
#print 'beta: ', beta
#print 'Violation of cut: ', np.dot(alpha, sol) - beta
if cbcModel.objectiveValue - theta*(1-theta) < best:
best = cbcModel.objectiveValue - theta*(1-theta)
best_multu = cbcModel.primalVariableSolution['u']
best_multv = cbcModel.primalVariableSolution['v']
best_disjunction = cbcModel.primalVariableSolution['pi']
best_rhs = cbcModel.primalVariableSolution['pi0']
best_theta = theta
if best_theta is not None:
alpha = A.transpose()*best_multu + best_theta*best_disjunction
beta = np.dot(b, best_multu) + best_theta*best_rhs
if debug_print:
print 'Violation of cut: ', np.dot(alpha, sol) - beta
print 'pi: ', best_disjunction
print 'pi0: ', best_rhs
print 'theta: ', best_theta
if (abs(alpha) > 1e-6).any():
return [(alpha, beta)]
return []