当前位置: 首页>>代码示例>>Python>>正文


Python LP.solve方法代码示例

本文整理汇总了Python中pylpsolve.LP.solve方法的典型用法代码示例。如果您正苦于以下问题:Python LP.solve方法的具体用法?Python LP.solve怎么用?Python LP.solve使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在pylpsolve.LP的用法示例。


在下文中一共展示了LP.solve方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: checkLBUBMix

# 需要导入模块: from pylpsolve import LP [as 别名]
# 或者: from pylpsolve.LP import solve [as 别名]
    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)
开发者ID:preo,项目名称:pylpsolve,代码行数:52,代码来源:test_bounds.py

示例2: testBasicBasis

# 需要导入模块: from pylpsolve import LP [as 别名]
# 或者: from pylpsolve.LP import solve [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"))
开发者ID:hoytak,项目名称:pylpsolve,代码行数:18,代码来源:test_basis.py

示例3: checkUB

# 需要导入模块: from pylpsolve import LP [as 别名]
# 或者: from pylpsolve.LP import solve [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)
开发者ID:preo,项目名称:pylpsolve,代码行数:44,代码来源:test_bounds.py

示例4: checkLB

# 需要导入模块: from pylpsolve import LP [as 别名]
# 或者: from pylpsolve.LP import solve [as 别名]
    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)
开发者ID:preo,项目名称:pylpsolve,代码行数:41,代码来源:test_bounds.py

示例5: checkBindSandwich

# 需要导入模块: from pylpsolve import LP [as 别名]
# 或者: from pylpsolve.LP import solve [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)
开发者ID:preo,项目名称:pylpsolve,代码行数:66,代码来源:test_convenience.py

示例6: checkBindEach

# 需要导入模块: from pylpsolve import LP [as 别名]
# 或者: from pylpsolve.LP import solve [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)
开发者ID:preo,项目名称:pylpsolve,代码行数:77,代码来源:test_convenience.py


注:本文中的pylpsolve.LP.solve方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。