本文整理汇总了Python中pylpsolve.LP类的典型用法代码示例。如果您正苦于以下问题:Python LP类的具体用法?Python LP怎么用?Python LP使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了LP类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: getLP
def getLP():
lp = LP()
for c, t, b in constraint_arg_list:
lp.addConstraint(c,t,b)
lp.setObjective(objective)
return lp
示例2: testOptionRetrieval_BadValues_01
def testOptionRetrieval_BadValues_01(self):
lp = LP()
self.assert_(lp.getOption("presolve_rows") == False)
self.assertRaises(ValueError,
lambda: lp.setOption("presolve_rows", True, bad_option = None))
self.assert_(lp.getOption("presolve_rows") == False)
self.assert_(lp.getOptionDict()["presolve_rows"] == False)
示例3: testOptionRetrieval_BadValues_02
def testOptionRetrieval_BadValues_02(self):
lp = LP()
self.assert_(lp.getOption("presolve_rows") == False)
self.assertRaises(TypeError,
lambda: lp.setOption(None, True, presolve_rows = True))
self.assert_(lp.getOption("presolve_rows") == False)
self.assert_(lp.getOptionDict()["presolve_rows"] == False)
示例4: checkInconsistentSubarrays
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))
示例5: testOptionRetrieval03
def testOptionRetrieval03(self):
lp = LP()
self.assert_(lp.getOption("presolve_rows") == False)
lp.setOption("presolve_cols", True, presolve_rows = True)
self.assert_(lp.getOption("presolve_rows") == True)
self.assert_(lp.getOptionDict()["presolve_rows"] == True)
self.assert_(lp.getOption("presolve_cols") == True)
self.assert_(lp.getOptionDict()["presolve_cols"] == True)
示例6: test04_bad_size_04
def test04_bad_size_04(self):
lp = LP()
self.assertRaises(ValueError, lambda: lp.getIndexBlock("a1", "a2"))
示例7: test03_bad_recall
def test03_bad_recall(self):
lp = LP()
self.assert_(lp.getIndexBlock(2, "a1") == (0,2))
self.assert_(lp.getIndexBlock(4, "a2") == (2,6))
self.assertRaises(ValueError, lambda: lp.getIndexBlock(3, "a1"))
示例8: test02_reverse_order_mixed
def test02_reverse_order_mixed(self):
lp = LP()
self.assert_(lp.getIndexBlock("a1", 2) == (0,2))
self.assert_(lp.getIndexBlock(4, "a2") == (2,6))
self.assert_(lp.getIndexBlock("a1", 2) == (0,2))
示例9: test02
def test02(self):
lp = LP()
self.assert_(lp.getIndexBlock(2, "a1") == (0,2))
self.assert_(lp.getIndexBlock(4, "a2") == (2,6))
self.assert_(lp.getIndexBlock("a1") == (0,2))
示例10: checkBadSizingTooLarge
def checkBadSizingTooLarge(self, opts):
lp = LP()
def run_test(c_arg, o_arg):
if opts[-1] == "c":
self.assertRaises(ValueError, lambda: lp.addConstraint(c_arg, ">", 1))
elif opts[-1] == "o":
self.assertRaises(ValueError, lambda: lp.setObjective(o_arg))
else:
assert False
indices = {}
indices["t"] = (0,3)
indices["N"] = "a"
indices["l"] = [0,1,2]
indices["a"] = ar([0,1,2])
indices["f"] = ar([0,1,2],dtype=float64)
weights = {}
weights["l"] = [1,1,1,1]
weights["a"] = ar([1,1,1,1])
weights["f"] = ar([1,1,1,1])
obj_func = {}
obj_func["l"] = [1,2,3,4]
obj_func["a"] = ar([1,2,3,4])
obj_func["f"] = ar([1,2,3,4],dtype=float64)
# Some ones used in the dict's case
il = indices["l"]
assert len(il) == 3
wl = weights["l"]
assert len(wl) == 4
ol = obj_func["l"]
assert len(ol) == 4
if opts[0] == "d" or opts[0] == "T":
if opts[1] == "2":
lp.getIndexBlock("b", 1)
cd = [ ("a", wl[:2]), ("b", wl[2:])]
od = [ ("a", ol[:2]), ("b", ol[2:])]
elif opts[1] == "3":
cd = [((0,2), wl[:2]), (2, wl[2:])]
od = [((0,2), ol[:2]), (2, ol[2:])]
elif opts[1] == "4":
cd = [((0,2), wl[:2]), ( (2,3), wl[2:])]
od = [((0,2), ol[:2]), ( (2,3), ol[2:])]
elif opts[1] == "5": # bad for out of order
cd = [("a", wl[:2]), ( (2,3), wl[2:])]
od = [("a", ol[:2]), ( (2,3), ol[2:])]
elif opts[1] in indices.keys() and opts[2] in weights.keys():
if "N" in opts:
lp.getIndexBlock(indices["N"], 3)
cd = [(indices[opts[1]], weights[opts[2]])]
od = [(indices[opts[1]], obj_func[opts[2]])]
else:
assert False
if opts[0] == "d":
run_test(dict(cd), dict(od))
return
elif opts[0] == "T":
run_test(cd, od)
return
else:
assert False
else:
assert len(opts) == 3
# No little n option here
if "N" in opts:
lp.getIndexBlock(indices["N"], 3)
run_test( (indices[opts[0]], weights[opts[1]]),
(indices[opts[0]], obj_func[opts[1]]))
return
示例11: checkBindEach
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)
示例12: checkLBUBMix
def checkLBUBMix(self, opts, lb, ub):
# these are indices to bound
lbindices = (0,3)
ubindices = {}
ubindices["t"] = (3,6)
ubindices["n"] = "a"
ubindices["N"] = "a"
ubindices["l"] = [3,4,5]
ubindices["a"] = ar([3,4,5],dtype=uint)
ubindices["f"] = ar([3,4,5],dtype=float64)
ubvalues = {}
ubvalues["s"] = ub
ubvalues["l"] = [ub, ub, ub]
ubvalues["a"] = ar([ub, ub, ub])
lbvalues = {}
lbvalues["s"] = lb
lbvalues["l"] = [lb, lb, lb]
lbvalues["a"] = ar([lb, lb, lb])
lp = LP()
lp.setLowerBound(lbindices, lbvalues[opts[1]])
if opts[0] == "N":
lp.getIndexBlock(ubindices["N"], 3)
lp.setUpperBound(ubindices[opts[0]], ubvalues[opts[1]])
lp.setObjective([1,1,1,-1,-1,-1])
for num_times in range(2): # make sure it's same anser second time
lp.solve()
self.assertAlmostEqual(lp.getObjectiveValue(), lb*3 - ub*3)
v = lp.getSolution()
self.assert_(len(v) == 6)
self.assertAlmostEqual(v[0], lb)
self.assertAlmostEqual(v[1], lb)
self.assertAlmostEqual(v[2], lb)
self.assertAlmostEqual(v[3], ub)
self.assertAlmostEqual(v[4], ub)
self.assertAlmostEqual(v[5], ub)
示例13: test01_constraint_rejects_float_idx
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))
示例14: test04_bad_size_05
def test04_bad_size_05(self):
lp = LP()
self.assertRaises(ValueError, lambda: lp.getIndexBlock(0, 2))
示例15: checkLB
def checkLB(self, opts, lb):
# 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)
lbvalues = {}
lbvalues["s"] = lb
lbvalues["l"] = [lb, lb, lb]
lbvalues["a"] = ar([lb, lb, lb])
lp = LP()
if opts[0] == "N":
lp.getIndexBlock(indices["N"], 3)
lp.setObjective([1,1,1,1,1,1])
lp.setLowerBound(indices[opts[0]], lbvalues[opts[1]])
for num_times in range(2): # make sure it's same anser second time
lp.solve()
self.assertAlmostEqual(lp.getObjectiveValue(), lb*3)
v = lp.getSolution()
self.assert_(len(v) == 6)
self.assertAlmostEqual(v[0], lb)
self.assertAlmostEqual(v[1], lb)
self.assertAlmostEqual(v[2], lb)
self.assertAlmostEqual(v[3], 0)
self.assertAlmostEqual(v[4], 0)
self.assertAlmostEqual(v[5], 0)