简体   繁体   中英

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. I have tried.value, just x1 on its own. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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