本文整理汇总了Python中cylp.cy.CyClpSimplex.primal方法的典型用法代码示例。如果您正苦于以下问题:Python CyClpSimplex.primal方法的具体用法?Python CyClpSimplex.primal怎么用?Python CyClpSimplex.primal使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cylp.cy.CyClpSimplex
的用法示例。
在下文中一共展示了CyClpSimplex.primal方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_removeVar
# 需要导入模块: from cylp.cy import CyClpSimplex [as 别名]
# 或者: from cylp.cy.CyClpSimplex import primal [as 别名]
def test_removeVar(self):
m = self.model
x = self.x
A = self.A
B = self.B
D = self.D
b = self.b
y = m.addVariable('y', 4)
z = m.addVariable('z', 5)
m.addConstraint(x >= 0)
m.addConstraint(y >= -10)
m.addConstraint(z >= -10)
m.addConstraint(A * x + D * y + B * z <= b)
m += x[0] + y[0] + z[0] >= 1.12
m.objective = x.sum() + y.sum() + z.sum()
s = CyClpSimplex(m)
s.primal()
self.assertTrue('y' in s.primalVariableSolution.keys())
m.removeVariable('y')
s = CyClpSimplex(m)
s.primal()
self.assertTrue('y' not in s.primalVariableSolution.keys())
示例2: test_Sparse
# 需要导入模块: from cylp.cy import CyClpSimplex [as 别名]
# 或者: from cylp.cy.CyClpSimplex import primal [as 别名]
def test_Sparse(self):
model = CyLPModel()
x = model.addVariable('x', 3)
y = model.addVariable('y', 2)
A = csc_matrixPlus(([1, 2, 1, 1], ([0, 0, 1, 1], [0, 1, 0, 2])), shape=(2, 3))
B = csc_matrixPlus(([1, 1], ([0, 1], [0, 2])), shape=(2, 3))
D = np.matrix([[1., 2.],[0, 1]])
a = CyLPArray([5, 2.5])
b = CyLPArray([4.2, 3])
x_u= CyLPArray([2., 3.5])
model.addConstraint(A*x <= a)
model.addConstraint(2 <= B * x + D * y <= b)
model.addConstraint(y >= 0)
model.addConstraint(1.1 <= x[1:3] <= x_u)
c = CyLPArray([1., -2., 3.])
model.objective = c * x + 2 * y[0] + 2 * y[1]
s = CyClpSimplex(model)
s.primal()
sol = np.concatenate((s.primalVariableSolution['x'],
s.primalVariableSolution['y']))
self.assertTrue((abs(sol -
np.array([0.2, 2, 1.1, 0, 0.9]) ) <= 10**-6).all())
示例3: test
# 需要导入模块: from cylp.cy import CyClpSimplex [as 别名]
# 或者: from cylp.cy.CyClpSimplex import primal [as 别名]
def test(self):
m = CyCoinModel()
m.addVariable(3, np.array(
[0, 1, 2], np.int32),
np.array([1., 1., 1.], np.double), 0, 10, 5)
m.addVariable(2, np.array(
[1,2], np.int32),
np.array([5, 2.], np.double), 0, 10, 2)
# Add bound for the three constraints (we have two variables)
m.setConstraintLower(0, 2.3)
m.setConstraintLower(1, 4.5)
m.setConstraintLower(0, 1.5)
# Add a 4th constraint
m.addConstraint(2,
np.array([0, 1], np.int32),
np.array([1., 1.], np.double), 2, 7)
s = CyClpSimplex()
# Load the problem from the CyCoinModel
s.loadProblemFromCyCoinModel(m)
s.primal()
self.assertAlmostEqual(s.objectiveValue, 8.7, 7)
示例4: solve
# 需要导入模块: from cylp.cy import CyClpSimplex [as 别名]
# 或者: from cylp.cy.CyClpSimplex import primal [as 别名]
def solve(filename, method):
s = CyClpSimplex()
s.readMps(filename)
s.preSolve(feasibilityTolerance=10 ** -8)
#s.useCustomPrimal(1)
if method == 'd':
pivot = DantzigPivot(s)
elif method == 'l':
pivot = LIFOPivot(s)
elif method == 'm':
pivot = MostFrequentPivot(s)
elif method == 'p':
pivot = PositiveEdgePivot(s)
else:
print 'Unkown solution method.'
sys.exit(1)
s.setPivotMethod(pivot)
#s.setPerturbation(50)
start = clock()
s.primal()
print 'Problem solved in %g seconds.' % (clock() - start)
return s.objectiveValue
示例5: test_removeVar2
# 需要导入模块: from cylp.cy import CyClpSimplex [as 别名]
# 或者: from cylp.cy.CyClpSimplex import primal [as 别名]
def test_removeVar2(self):
s = CyClpSimplex()
fp = os.path.join(currentFilePath, '../../input/p0033.mps')
s.extractCyLPModel(fp)
y = s.addVariable('y', 3)
s.primal()
x = s.getVarByName('x')
s.addConstraint(x[1] + y[1] >= 1.2)
#s.primal()
s.removeVariable('x')
s.primal()
s = s.primalVariableSolution
self.assertTrue((s['y'] - np.array([0, 1.2, 0]) <= 10**-6).all())
示例6: test_1
# 需要导入模块: from cylp.cy import CyClpSimplex [as 别名]
# 或者: from cylp.cy.CyClpSimplex import primal [as 别名]
def test_1(self):
"""simplest QP test"""
s = CyClpSimplex()
s.readMps(join(currentFilePath, '../input/hs35.qps'))
#self.assertTrue(abs(cbcModel.objectiveValue - 3089.0) < 10 ** -6)
#print s.Hessian.todense()
p = WolfePivot(s)
s.setPivotMethod(p)
s.primal()
print s.primalVariableSolution
print s.objectiveValue
示例7: test_multiDim
# 需要导入模块: from cylp.cy import CyClpSimplex [as 别名]
# 或者: from cylp.cy.CyClpSimplex import primal [as 别名]
def test_multiDim(self):
from cylp.cy import CyClpSimplex
from cylp.py.modeling.CyLPModel import CyLPArray
s = CyClpSimplex()
x = s.addVariable('x', (5, 3, 6))
s += 2 * x[2, :, 3].sum() + 3 * x[0, 1, :].sum() >= 5
s += 0 <= x <= 1
c = CyLPArray(range(18))
s.objective = c * x[2, :, :] + c * x[0, :, :]
s.primal()
sol = s.primalVariableSolution['x']
self.assertTrue(abs(sol[0, 1, 0] - 1) <= 10**-6)
self.assertTrue(abs(sol[2, 0, 3] - 1) <= 10**-6)
示例8: test_ArrayIndexing
# 需要导入模块: from cylp.cy import CyClpSimplex [as 别名]
# 或者: from cylp.cy.CyClpSimplex import primal [as 别名]
def test_ArrayIndexing(self):
from cylp.cy import CyClpSimplex
from cylp.py.modeling.CyLPModel import CyLPArray
s = CyClpSimplex()
x = s.addVariable('x', (5, 3, 6))
s += 2 * x[2, :, 3].sum() + 3 * x[0, 1, :].sum() >= 5
s += x[1, 2, [0, 3, 5]] - x[2, 1, np.array([1, 2, 4])] == 1
s += 0 <= x <= 1
c = CyLPArray(range(18))
s.objective = c * x[2, :, :] + c * x[0, :, :]
s.primal()
sol = s.primalVariableSolution['x']
self.assertTrue(abs(sol[1, 2, 0] - 1) <= 10**-6)
self.assertTrue(abs(sol[1, 2, 3] - 1) <= 10**-6)
self.assertTrue(abs(sol[1, 2, 5] - 1) <= 10**-6)
示例9: test_onlyBounds2
# 需要导入模块: from cylp.cy import CyClpSimplex [as 别名]
# 或者: from cylp.cy.CyClpSimplex import primal [as 别名]
def test_onlyBounds2(self):
s = CyClpSimplex()
x = s.addVariable('x', 3)
y = s.addVariable('y', 2)
s += y >= 1
s += 2 <= x <= 4
c = CyLPArray([1., -2., 3.])
s.objective = c * x + 2 * y[0] + 2 * y[1]
s.primal()
sol = np.concatenate((s.primalVariableSolution['x'],
s.primalVariableSolution['y']))
self.assertTrue((abs(sol -
np.array([2, 4, 2, 1, 1]) ) <= 10**-6).all())
示例10: test2
# 需要导入模块: from cylp.cy import CyClpSimplex [as 别名]
# 或者: from cylp.cy.CyClpSimplex import primal [as 别名]
def test2(self):
'Same as test1, but use cylp indirectly.'
s = CyClpSimplex()
x = s.addVariable('x', 3)
A = np.matrix([[1,2,3], [1,1,1]])
b = CyLPArray([5, 3])
s += A * x == b
s += x >= 0
s.objective = 1 * x[0] + 1 * x[1] + 1.1 * x[2]
# Solve it a first time
s.primal()
sol = s.primalVariableSolution['x']
self.assertTrue((abs(sol - np.array([1,2,0]) ) <= 10**-6).all())
# Add a cut
s.addConstraint(x[0] >= 1.1)
s.primal()
sol = s.primalVariableSolution['x']
self.assertTrue((abs(sol - np.array([1.1, 1.8, 0.1]) ) <= 10**-6).all())
# Change the objective function
c = csr_matrixPlus([[1, 10, 1.1]]).T
s.objective = c.T * x
s.primal()
sol = s.primalVariableSolution['x']
self.assertTrue((abs(sol - np.array([2, 0, 1]) ) <= 10**-6).all())
示例11: test
# 需要导入模块: from cylp.cy import CyClpSimplex [as 别名]
# 或者: from cylp.cy.CyClpSimplex import primal [as 别名]
def test(self):
model = CyLPModel()
x = model.addVariable('x', 3)
A = np.matrix([[1,2,3], [1,1,1]])
b = CyLPArray([5, 3])
model.addConstraint(A * x == b)
model.addConstraint(x >= 0)
model.objective = 1*x[0] + 1*x[1] + 1.1 * x[2]
# Solve it a first time
s = CyClpSimplex(model)
s.primal()
sol = s.primalVariableSolution['x']
self.assertTrue((abs(sol - np.array([1,2,0]) ) <= 10**-6).all())
# Add a cut
s.addConstraint(x[0] >= 1.1)
s.primal()
sol = s.primalVariableSolution['x']
self.assertTrue((abs(sol - np.array([1.1, 1.8, 0.1]) ) <= 10**-6).all())
# Change the objective function
c = csr_matrixPlus([[1, 10, 1.1]]).T
s.objective = c.T * x
s.primal()
sol = s.primalVariableSolution['x']
self.assertTrue((abs(sol - np.array([2, 0, 1]) ) <= 10**-6).all())
示例12: test_removeConst
# 需要导入模块: from cylp.cy import CyClpSimplex [as 别名]
# 或者: from cylp.cy.CyClpSimplex import primal [as 别名]
def test_removeConst(self):
m = self.model
x = self.x
A = self.A
b = self.b
m.addConstraint(x >= 0)
m.addConstraint(A * x == b)
m.addConstraint(x[1:3].sum() >= 1, 'rr')
m.objective = x.sum()
s = CyClpSimplex(m)
s.primal()
self.assertAlmostEqual(s.primalVariableSolution['x'][1], 1, 7)
s.removeConstraint('rr')
s.primal()
self.assertAlmostEqual(s.primalVariableSolution['x'][1], 0, 7)
示例13: TestCyClpSimplex
# 需要导入模块: from cylp.cy import CyClpSimplex [as 别名]
# 或者: from cylp.cy.CyClpSimplex import primal [as 别名]
class TestCyClpSimplex(unittest.TestCase):
def setUp(self):
self.s = CyClpSimplex()
self.s.readMps(join(currentFilePath, '../input/p0033.mps'))
def test_PE(self):
#pivot = PositiveEdgePivot(self.s)
self.s.setPivotMethod(PositiveEdgePivot(self.s))
self.s.primal()
self.assertEqual(round(self.s.objectiveValue, 4), 2520.5717)
def test_Dantzig(self):
#pivot = DantzigPivot(self.s)
self.s.setPivotMethod(DantzigPivot(self.s))
self.s.primal()
self.assertEqual(round(self.s.objectiveValue, 4), 2520.5717)
def test_LIFO(self):
#pivot = LIFOPivot(self.s)
self.s.setPivotMethod(LIFOPivot(self.s))
self.s.primal()
self.assertEqual(round(self.s.objectiveValue, 4), 2520.5717)
def test_MostFrequent(self):
#pivot = MostFrequentPivot(self.s)
self.s.setPivotMethod(MostFrequentPivot(self.s))
self.s.primal()
self.assertEqual(round(self.s.objectiveValue, 4), 2520.5717)
def test_initialSolve(self):
self.s.initialSolve()
self.assertEqual(round(self.s.objectiveValue, 4), 2520.5717)
def test_initialPrimalSolve(self):
self.s.initialPrimalSolve()
self.assertEqual(round(self.s.objectiveValue, 4), 2520.5717)
def test_initialDualSolve(self):
self.s.initialDualSolve()
self.assertEqual(round(self.s.objectiveValue, 4), 2520.5717)
def test_direction(self):
self.assertEqual(self.s.optimizationDirection, 'min')
self.s.optimizationDirection = 'max'
self.assertEqual(self.s.optimizationDirection, 'max')
示例14: saveWeights
# 需要导入模块: from cylp.cy import CyClpSimplex [as 别名]
# 或者: from cylp.cy.CyClpSimplex import primal [as 别名]
#del rc2
return indicesToConsider[ind]
return -1
def saveWeights(self, model, mode):
self.clpModel = model
def isPivotAcceptable(self):
return True
def getMpsExample():
import os
import inspect
cylpDir = os.environ['CYLP_SOURCE_DIR']
return os.path.join(cylpDir, 'cylp', 'input', 'p0033.mps')
if __name__ == "__main__":
if len(sys.argv) == 1:
import doctest
doctest.testmod()
else:
from cylp.cy import CyClpSimplex
from cylp.py.pivots import DantzigPivot
s = CyClpSimplex()
s.readMps(sys.argv[1])
pivot = DantzigPivot(s)
s.setPivotMethod(pivot)
s.primal()
示例15: solve
# 需要导入模块: from cylp.cy import CyClpSimplex [as 别名]
# 或者: from cylp.cy.CyClpSimplex import primal [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
#.........这里部分代码省略.........