Python 向矩阵添加纸浆变量值

Python 向矩阵添加纸浆变量值,python,matrix,pulp,Python,Matrix,Pulp,我一直把“nan”作为矩阵中的值。我已经试过了。value,只是x1本身。如何获取值并将其插入矩阵? 为什么呢 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='In

我一直把“nan”作为矩阵中的值。我已经试过了。value,只是x1本身。如何获取值并将其插入矩阵? 为什么呢

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

要定义x1,此行需要在
平方(n,m)
函数中。

矩阵在示例代码中未初始化。此外,
x1
未在分配给
矩阵
元素的范围内定义。你能给我一个答案吗?我忘了包括在内。但我发现了问题所在。
Matrix[n-1,m-1]=x1.varValue