本文整理匯總了Python中cylp.cy.CyClpSimplex.addConstraint方法的典型用法代碼示例。如果您正苦於以下問題:Python CyClpSimplex.addConstraint方法的具體用法?Python CyClpSimplex.addConstraint怎麽用?Python CyClpSimplex.addConstraint使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類cylp.cy.CyClpSimplex
的用法示例。
在下文中一共展示了CyClpSimplex.addConstraint方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test
# 需要導入模塊: from cylp.cy import CyClpSimplex [as 別名]
# 或者: from cylp.cy.CyClpSimplex import addConstraint [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())
示例2: test2
# 需要導入模塊: from cylp.cy import CyClpSimplex [as 別名]
# 或者: from cylp.cy.CyClpSimplex import addConstraint [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())
示例3: test_removeVar2
# 需要導入模塊: from cylp.cy import CyClpSimplex [as 別名]
# 或者: from cylp.cy.CyClpSimplex import addConstraint [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())