本文整理汇总了Python中pylpsolve.LP.addConstraint方法的典型用法代码示例。如果您正苦于以下问题:Python LP.addConstraint方法的具体用法?Python LP.addConstraint怎么用?Python LP.addConstraint使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pylpsolve.LP
的用法示例。
在下文中一共展示了LP.addConstraint方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: getLP
# 需要导入模块: from pylpsolve import LP [as 别名]
# 或者: from pylpsolve.LP import addConstraint [as 别名]
def getLP():
lp = LP()
for c, t, b in constraint_arg_list:
lp.addConstraint(c,t,b)
lp.setObjective(objective)
return lp
示例2: testBasicBasis
# 需要导入模块: from pylpsolve import LP [as 别名]
# 或者: from pylpsolve.LP import addConstraint [as 别名]
def testBasicBasis(self):
# this should work as it's in the examples
lp = LP()
lp.addConstraint( (0, 1), "<", 3)
lp.addConstraint( (1, 1), "<", 3)
lp.setMaximize()
lp.setObjective([1,1])
lp.solve(guess = [3,3])
self.assert_(lp.getInfo("Iterations") == 0, lp.getInfo("Iterations"))
示例3: checkInconsistentSubarrays
# 需要导入模块: from pylpsolve import LP [as 别名]
# 或者: from pylpsolve.LP import addConstraint [as 别名]
def checkInconsistentSubarrays(self, opts):
values = {}
indices = {}
indices["t"] = (0,3)
indices["n"] = "a"
indices["N"] = "a"
indices["l"] = [0,1,2]
indices["a"] = ar([0,1,2],dtype=uint)
indices["f"] = ar([0,1,2],dtype=float64)
indices["e"] = None # empty
A = [[1,0, 0],
[0,1], # inconsistent; does this get caught?
[0,0.5,0]]
values = {}
values["L"] = A
values["l"] = [ar(le) for le in A]
values["B"] = [[1, 0, 0], [[1,0,0]], [0,1,1]]
values["C"] = ones((1,3,3) )
values["D"] = [[1, 0, 0], [1,1,[1]], [0,1,1]]
values["E"] = [[1, 0, 0], (1,1,1), [0,1,1]]
targets = {}
targets["s"] = 1
targets["l"] = [1,1,1]
targets["a"] = ar([1,1,1],dtype=uint)
targets["f"] = ar([1,1,1],dtype=float64)
lp = LP()
if opts[0] == "N":
lp.getIndexBlock(indices["N"], 3)
io = indices[opts[0]]
vl = values [opts[1]]
tr = targets[opts[2]]
ob = [1,2,3]
if io is None:
self.assertRaises(ValueError, lambda: lp.addConstraint(vl, ">=", tr))
else:
self.assertRaises(ValueError, lambda: lp.addConstraint( (io, vl), ">=", tr))
示例4: checkUB
# 需要导入模块: from pylpsolve import LP [as 别名]
# 或者: from pylpsolve.LP import addConstraint [as 别名]
def checkUB(self, opts, ub):
# these are indices to bound
indices = {}
indices["t"] = (0,3)
indices["N"] = "a"
indices["l"] = [0,1,2]
indices["a"] = ar([0,1,2],dtype=uint)
indices["f"] = ar([0,1,2],dtype=float64)
ubvalues = {}
ubvalues["s"] = ub
ubvalues["l"] = [ub, ub, ub]
ubvalues["a"] = ar([ub, ub, ub])
lp = LP()
if opts[0] == "N":
lp.getIndexBlock(indices["N"], 3)
lp.setObjective([1,1,1,1,1,1])
lp.addConstraint( ((3,6), [[1,0,0],[0,1,0],[0,0,1]]), "<=", 10)
lp.setMaximize()
lp.setLowerBound(indices[opts[0]], None)
lp.setUpperBound(indices[opts[0]], ubvalues[opts[1]])
for num_times in range(2): # make sure it's same anser second time
lp.solve()
self.assertAlmostEqual(lp.getObjectiveValue(), ub*3 + 10 * 3)
v = lp.getSolution()
self.assert_(len(v) == 6)
self.assertAlmostEqual(v[0], ub)
self.assertAlmostEqual(v[1], ub)
self.assertAlmostEqual(v[2], ub)
self.assertAlmostEqual(v[3], 10)
self.assertAlmostEqual(v[4], 10)
self.assertAlmostEqual(v[5], 10)
示例5: checkBindSandwich
# 需要导入模块: from pylpsolve import LP [as 别名]
# 或者: from pylpsolve.LP import addConstraint [as 别名]
def checkBindSandwich(self, opts):
idxlist = [{}, {}]
idxlist[0]["t"] = (0,3)
idxlist[0]["N"] = "a"
idxlist[0]["l"] = [0,1,2]
idxlist[0]["a"] = ar([0,1,2])
idxlist[0]["r"] = ar([0,0,1,1,2,2])[::2]
idxlist[0]["f"] = ar([0,1,2],dtype=float64)
idxlist[1]["t"] = (3,6)
idxlist[1]["n"] = "b"
idxlist[1]["l"] = [3,4,5]
idxlist[1]["a"] = ar([3,4,5])
idxlist[1]["r"] = ar([3,3,4,4,5,5])[::2]
idxlist[1]["f"] = ar([3,4,5],dtype=float64)
lp = LP()
if opts[0] == "N":
self.assert_(lp.getIndexBlock(idxlist[0]["N"], 3) == (0,3) )
# Now bind the second group
lp.bindSandwich(idxlist[0][opts[0]], idxlist[1][opts[1]])
if opts[2] == "u":
lp.addConstraint( (idxlist[0][opts[0]], 1), ">=", 1)
elif opts[2] == "l":
lp.addConstraint( (idxlist[0][opts[0]], 1), "<=", -1)
lp.setUnbounded(idxlist[0][opts[0]])
else:
assert False
lp.setObjective( (idxlist[1][opts[1]], [1,2,3]) )
lp.setMinimize()
lp.solve()
v = lp.getSolution()
v0 = 1 if opts[2] == "u" else -1
self.assert_(len(v) == 6, "len(v) = %d != 6" % len(v))
self.assertAlmostEqual(v[0], v0)
self.assertAlmostEqual(v[1], 0)
self.assertAlmostEqual(v[2], 0)
self.assertAlmostEqual(v[3], 1)
self.assertAlmostEqual(v[4], 0)
self.assertAlmostEqual(v[5], 0)
if opts[0] in "nN" and opts[1] in "nN":
d = lp.getSolutionDict()
self.assert_(set(d.iterkeys()) == set(["a", "b"]))
self.assertAlmostEqual(d["a"][0], v0)
self.assertAlmostEqual(d["a"][1], 0)
self.assertAlmostEqual(d["a"][2], 0)
self.assertAlmostEqual(d["b"][0], 1)
self.assertAlmostEqual(d["b"][1], 0)
self.assertAlmostEqual(d["b"][2], 0)
示例6: checkBindEach
# 需要导入模块: from pylpsolve import LP [as 别名]
# 或者: from pylpsolve.LP import addConstraint [as 别名]
def checkBindEach(self, opts):
idxlist = [{}, {}]
idxlist[0]["t"] = (0,3)
idxlist[0]["N"] = "a"
idxlist[0]["l"] = [0,1,2]
idxlist[0]["a"] = ar([0,1,2])
idxlist[0]["r"] = ar([0,0,1,1,2,2])[::2]
idxlist[0]["f"] = ar([0,1,2],dtype=float64)
idxlist[1]["t"] = (3,6)
idxlist[1]["n"] = "b"
idxlist[1]["l"] = [3,4,5]
idxlist[1]["a"] = ar([3,4,5])
idxlist[1]["r"] = ar([3,3,4,4,5,5])[::2]
idxlist[1]["f"] = ar([3,4,5],dtype=float64)
lp = LP()
if opts[0] == "N":
self.assert_(lp.getIndexBlock(idxlist[0]["N"], 3) == (0,3) )
# Now bind the second group
if opts[2] == "g":
self.assert_(
lp.bindEach(idxlist[1][opts[1]], ">", idxlist[0][opts[0]])
== [0,1,2])
elif opts[2] == "l":
self.assert_(
lp.bindEach(idxlist[0][opts[0]], "<", idxlist[1][opts[1]])
== [0,1,2])
elif opts[2] == "e":
self.assert_(
lp.bindEach(idxlist[0][opts[0]], "=", idxlist[1][opts[1]])
== [0,1,2])
elif opts[2] == "E":
self.assert_(
lp.bindEach(idxlist[1][opts[1]], "=", idxlist[0][opts[0]])
== [0,1,2])
else:
assert False
# Forces some to be defined implicitly above to catch that case
lp.addConstraint( (idxlist[0][opts[0]], 1), ">=", 1)
lp.setObjective( (idxlist[1][opts[1]], [1,2,3]) )
lp.setMinimize()
lp.solve()
v = lp.getSolution()
self.assert_(len(v) == 6, "len(v) = %d != 6" % len(v))
self.assertAlmostEqual(v[0], 1)
self.assertAlmostEqual(v[1], 0)
self.assertAlmostEqual(v[2], 0)
self.assertAlmostEqual(v[3], 1)
self.assertAlmostEqual(v[4], 0)
self.assertAlmostEqual(v[5], 0)
if opts[0] in "nN" and opts[1] in "nN":
d = lp.getSolutionDict()
self.assert_(set(d.iterkeys()) == set(["a", "b"]))
self.assertAlmostEqual(d["a"][0], 1)
self.assertAlmostEqual(d["a"][1], 0)
self.assertAlmostEqual(d["a"][2], 0)
self.assertAlmostEqual(d["b"][0], 1)
self.assertAlmostEqual(d["b"][1], 0)
self.assertAlmostEqual(d["b"][2], 0)
示例7: test01_constraint_rejects_float_idx
# 需要导入模块: from pylpsolve import LP [as 别名]
# 或者: from pylpsolve.LP import addConstraint [as 别名]
def test01_constraint_rejects_float_idx(self):
lp = LP()
self.assertRaises(ValueError,
lambda: lp.addConstraint( (ar([0, 1.1, 2],dtype=float64), ar([1,1,1],dtype=float64) ), ">=", 1))