简体   繁体   English

将纸浆变量值添加到矩阵

[英]Adding Pulp Variable Value to Matrix

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

I keep getting 'nan' as the values in the matrix.我不断得到'nan'作为矩阵中的值。 I have tried.value, just x1 on its own.我已经尝试过.value,只是 x1 本身。 How do I fetch the value and insert it into the matrix?如何获取值并将其插入矩阵? Why though.为什么。

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

This line needs to be in the square(n,m) function for x1 to be defined.这条线需要在square(n,m) function 中才能定义 x1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM