将纸浆变量值添加到矩阵

时间:2019-11-09 23:40:40

标签: python matrix pulp

def square(n,m):
    my_lp_problem = pulp.LpProblem("My LP Problem", pulp.LpMinimize)
    x1 = pulp.LpVariable('x1', lowBound=0, cat='Integer')
    x2 = pulp.LpVariable('x2', lowBound=0, cat='Integer')
    x3 = pulp.LpVariable('x3', lowBound=0, cat='Integer')
    x4 = pulp.LpVariable('x4', lowBound=0, cat='Integer')
    x5 = pulp.LpVariable('x5', lowBound=0, cat='Integer')
    x6 = pulp.LpVariable('x6', lowBound=0, cat='Integer')
    x7 = pulp.LpVariable('x7', lowBound=0, cat='Integer')
    x8 = pulp.LpVariable('x8', lowBound=0, cat='Integer')

# Objective function
    Sum = x1+x2+x3+x4+x5+x6+x7+x8
    my_lp_problem += Sum, "Z"

    my_lp_problem += 1+x1-x2+x3-x4+2*x5-2*x6+2*x7-2*x8 == n
    my_lp_problem += 1-(2*x1+2*x2-2*x3-2*x4+x5+x6-x7-x8) == m

    my_lp_problem.solve()
    pulp.LpStatus[my_lp_problem.status]

    for variable in my_lp_problem.variables():
        print( "{} = {}".format(variable.name, variable.varValue))
    return     
for n in range(1,3):
    for m in range(1,3):
        square(n,m)
        Matrix[n-1,m-1]=x1.varValue

我不断得到“ nan”作为矩阵中的值。我尝试过.value,仅靠x1就可以了。如何获取值并将其插入矩阵? 为什么。

1 个答案:

答案 0 :(得分:0)

Matrix[n-1,m-1]=x1.varValue

此行必须在square(n,m)函数中才能定义x1。