本文整理匯總了Python中cylp.cy.CyClpSimplex.logLevel方法的典型用法代碼示例。如果您正苦於以下問題:Python CyClpSimplex.logLevel方法的具體用法?Python CyClpSimplex.logLevel怎麽用?Python CyClpSimplex.logLevel使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類cylp.cy.CyClpSimplex
的用法示例。
在下文中一共展示了CyClpSimplex.logLevel方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: splitCuts
# 需要導入模塊: from cylp.cy import CyClpSimplex [as 別名]
# 或者: from cylp.cy.CyClpSimplex import logLevel [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 []
示例2: solve
# 需要導入模塊: from cylp.cy import CyClpSimplex [as 別名]
# 或者: from cylp.cy.CyClpSimplex import logLevel [as 別名]
def solve(self, objective, constraints, cached_data,
warm_start, verbose, solver_opts):
"""Returns the result of the call to the solver.
Parameters
----------
objective : LinOp
The canonicalized objective.
constraints : list
The list of canonicalized cosntraints.
cached_data : dict
A map of solver name to cached problem data.
warm_start : bool
Not used.
verbose : bool
Should the solver print output?
solver_opts : dict
Additional arguments for the solver.
Returns
-------
tuple
(status, optimal value, primal, equality dual, inequality dual)
"""
# Import basic modelling tools of cylp
from cylp.cy import CyClpSimplex
from cylp.py.modeling.CyLPModel import CyLPArray
# Get problem data
data = self.get_problem_data(objective, constraints, cached_data)
c = data[s.C]
b = data[s.B]
A = data[s.A]
dims = data[s.DIMS]
n = c.shape[0]
solver_cache = cached_data[self.name()]
# Problem
model = CyClpSimplex()
# Variables
x = model.addVariable('x', n)
if self.is_mip(data):
for i in data[s.BOOL_IDX]:
model.setInteger(x[i])
for i in data[s.INT_IDX]:
model.setInteger(x[i])
# Constraints
# eq
model += A[0:dims[s.EQ_DIM], :] * x == b[0:dims[s.EQ_DIM]]
# leq
leq_start = dims[s.EQ_DIM]
leq_end = dims[s.EQ_DIM] + dims[s.LEQ_DIM]
model += A[leq_start:leq_end, :] * x <= b[leq_start:leq_end]
# no boolean vars available in cbc -> model as int + restrict to [0,1]
if self.is_mip(data):
for i in data[s.BOOL_IDX]:
model += 0 <= x[i] <= 1
# Objective
model.objective = c
# Build model & solve
status = None
if self.is_mip(data):
cbcModel = model.getCbcModel() # need to convert model
if not verbose:
cbcModel.logLevel = 0
# Add cut-generators (optional)
for cut_name, cut_func in six.iteritems(self.SUPPORTED_CUT_GENERATORS):
if cut_name in solver_opts and solver_opts[cut_name]:
module = importlib.import_module("cylp.cy.CyCgl")
funcToCall = getattr(module, cut_func)
cut_gen = funcToCall()
cbcModel.addCutGenerator(cut_gen, name=cut_name)
# solve
status = cbcModel.branchAndBound()
else:
if not verbose:
model.logLevel = 0
status = model.primal() # solve
results_dict = {}
results_dict["status"] = status
if self.is_mip(data):
results_dict["x"] = cbcModel.primalVariableSolution['x']
results_dict["obj_value"] = cbcModel.objectiveValue
else:
results_dict["x"] = model.primalVariableSolution['x']
results_dict["obj_value"] = model.objectiveValue
#.........這裏部分代碼省略.........
示例3: disjunctionToCut
# 需要導入模塊: from cylp.cy import CyClpSimplex [as 別名]
# 或者: from cylp.cy.CyClpSimplex import logLevel [as 別名]
#.........這裏部分代碼省略.........
Atran = A.transpose()
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)
示例4: CyLPArray
# 需要導入模塊: from cylp.cy import CyClpSimplex [as 別名]
# 或者: from cylp.cy.CyClpSimplex import logLevel [as 別名]
x = lp.addVariable('x', LP.numVars)
if LP.sense[0] == '>=':
lp += A * x >= b
else:
lp += A * x <= b
#lp += x >= 0
c = CyLPArray(LP.c)
# We are maximizing, so negate objective
if LP.sense[1] == 'Min':
lp.objective = c * x
else:
lp.objective = -c * x
lp.logLevel = 0
lp.primal(startFinishOptions = 'x')
np.set_printoptions(precision = 2, linewidth = 200)
print('Basic variables: ', lp.basicVariables)
print("Current tableaux and reduced costs:")
print(lp.reducedCosts)
print(np.around(lp.tableau, decimals = 3))
print('Right-hand side of optimal tableaux:')
#There is a bug in CyLP and this is wrong
#print lp.rhs
if LP.sense[0] == '<=':
print(np.dot(lp.basisInverse, lp.constraintsUpper))
else:
print(np.dot(lp.basisInverse, lp.constraintsLower))
print('Inverse of optimal basis:')
print(np.around(lp.basisInverse, 3))
示例5: maxViolationSplitCuts
# 需要導入模塊: from cylp.cy import CyClpSimplex [as 別名]
# 或者: from cylp.cy.CyClpSimplex import logLevel [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 []