线性编程两组约束CPLEX Python API

时间:2018-09-04 08:43:34

标签: python constraints linear-programming cplex

我正在尝试使用IBM的CPLEX Python API解决线性编程问题。它涉及两组平等约束。当我们使用两组约束中的任何一组约束时,下面的代码都可以正常工作,但是当同时使用两组约束时,则找不到解决方案。

约束是: 第一个约束条件:Wx' = c',其中W = [[20,0,0],[0,20,30]]x = [a,b,c]c=[20,30] 第二个约束:Vx' = e',其中V = [[1,1,0],[0,0,1]]x = [a,b,c]c=[1,1]

目标函数:minimize a + c

同时满足两组约束的一种解决方案是a=1b=0c=1

我在Cplex Python中引入两组约束的方式存在错误。我的代码如下。要检查代码本身是否适合任一约束集,请注释掉约束集。

import cplex
from cplex.exceptions import CplexError
import sys



def populatebynonzero(prob):


    my_obj      = [1.0, 0.0, 1.0]
    my_ub       = [1.0] * len(my_obj)
    my_lb       = [0.0] * len(my_obj)
    my_colnames = ["a", "b", "c"]

    prob.objective.set_sense(prob.objective.sense.minimize)
    prob.variables.add(obj = my_obj, ub = my_ub, lb = my_lb ,names = my_colnames)

    # first set of equality constraints: Wx' = c', where W = [[20,0,0],[0,20,30]], x = [a,b,c], c=[20,30]
    my_rhs      = [20.0, 30.0]
    my_rownames = ["c1", "c2"]
    my_sense    = "E" * len(my_rownames)
    rows = [0,1,1] 
    cols = [0,1,2]
    vals = [20.0,20.0,30.0]
    prob.linear_constraints.add(rhs = my_rhs, senses = my_sense,names = my_rownames)
    prob.linear_constraints.set_coefficients(zip(rows, cols, vals))     

    # second set of equality constraints: Vx' = e', where V = [[1,1,0],[0,0,1]], x = [a,b,c], c=[1,1]

    my_rhs      = [1.0, 1.0]
    my_rownames = ["e1", "e2"]
    my_sense    = "E" * len(my_rownames)
    rows = [0,0,1] 
    cols = [0,1,2]
    vals = [1.0,1.0,1.0]
    prob.linear_constraints.add(rhs = my_rhs, senses = my_sense,names = my_rownames)
    prob.linear_constraints.set_coefficients(zip(rows, cols, vals))



def lpex1():
    try:
        my_prob = cplex.Cplex()
        handle = populatebynonzero(my_prob)
        my_prob.solve()
    except CplexError, exc:
        print exc
        return

    numrows = my_prob.linear_constraints.get_num()
    numcols = my_prob.variables.get_num()

    print
    # solution.get_status() returns an integer code
    print "Solution status = " , my_prob.solution.get_status(), ":",
    # the following line prints the corresponding string
    print my_prob.solution.status[my_prob.solution.get_status()]
    print "Solution value  = ", my_prob.solution.get_objective_value()
    slack = my_prob.solution.get_linear_slacks()
    pi    = my_prob.solution.get_dual_values()
    x     = my_prob.solution.get_values()
    dj    = my_prob.solution.get_reduced_costs()
    for i in range(numrows):
        print "Row %d:  Slack = %10f  Pi = %10f" % (i, slack[i], pi[i])
    for j in range(numcols):
        print "Column %d:  Value = %10f Reduced cost = %10f" % (j, x[j], dj[j])

    my_prob.write("lpex1.lp")




    print x, "SOLUTIONS"

lpex1()

2 个答案:

答案 0 :(得分:0)

如果按以下方式将两组约束条件合并到一个矩阵中,则可以工作,尽管找到不必合并的解决方案会很好

xsd:decimal

答案 1 :(得分:0)

当您尝试创建第二组约束时,您为行使用了错误的索引:

# second set of equality constraints: Vx' = e', where V = [[1,1,0],[0,0,1]], x = [a,b,c], c=[1,1]

my_rhs      = [1.0, 1.0]
my_rownames = ["e1", "e2"]
my_sense    = "E" * len(my_rownames)
rows = [0,0,1]  # <- HERE
cols = [0,1,2]
vals = [1.0,1.0,1.0]

也就是说,您正在使用为第一组约束创建的行索引0和1。相反,您应该执行以下操作:

# second set of equality constraints: Vx' = e', where V = [[1,1,0],[0,0,1]], x = [a,b,c], c=[1,1]

my_rhs      = [1.0, 1.0]
my_rownames = ["e1", "e2"]
my_sense    = "E" * len(my_rownames)
cols = [0,1,2]
vals = [1.0,1.0,1.0]
rowindices = list(prob.linear_constraints.add(rhs = my_rhs, senses = my_sense,names = my_rownames))
assert len(rowindices) == 2
rows = [rowindices[0], rowindices[0], rowindices[1]]
prob.linear_constraints.set_coefficients(zip(rowindices, cols, vals))

上面,我们从对prob.linear_constriants.add的调用中获得了新的行索引。进行此更改后,脚本可以正常工作。

此外,最好以LP格式写出该问题以确保它看起来正确,就像这样:

prob.write("model.lp")