简体   繁体   中英

Python, PuLP: Turning a LpVariable into an integer

I'm trying to print the value of a variable, defined as an LpVariable (PuLP 1.6.1, Python 3.5). The LpVariable has an option to set the category of the parameter to 'Integer'. However, this does not result into a value when I ask the variable to be printed. Hereby a piece of the problem I am trying to solve:

from pulp import *

prob = LpProblem("Gadget Production", LpMinimize)

# Demand scheme x (Laptops), y (Phones), and z (Tablets)
x = [75, 125, 1000, 1500]
y = [120, 2000, 2000, 2000]
z = [50, 2000, 3000, 2000]

PL1 = LpVariable('Prod Laptop January', cat=int)
PL2 = LpVariable('Prod Laptop February', cat=int)
PL3 = LpVariable('Prod Laptop March', cat=int)

PP1 = LpVariable('Prod Phone January', cat=int)
PP2 = LpVariable('Prod Phone February', cat=int)
PP3 = LpVariable('Prod Phone March', cat=int)

PT1 = LpVariable('Prod Tablet January', cat=int)
PT2 = LpVariable('Prod Tablet February', cat=int)
PT3 = LpVariable('Prod Tablet March', cat=int)

# Inventory (I) of gadget (L, P, T), in month [i]:
IL1 = x[0] + PL1 - x[1]
IL2 = IL1 + PL2 - x[2]
IL3 = IL2 + PL3 - x[3]

IP1 = y[0] + PP1 - y[1]
IP2 = IP1 + PP2 - y[2]
IP3 = IP2 + PP3 - y[3]

IT1 = z[0] + PT1 - z[1]
IT2 = IT1 + PT2 - z[2]
IT3 = IT2 + PT3 - z[3]

# Constraints to meet demand scheme
prob += x[0] + PL1 >= x[1]
prob += IL1 + PL2 >= x[2]
prob += IL2 + PL3 >= x[3]

prob += y[0] + PP1 >= y[1]
prob += IP1 + PP2 >= y[2]
prob += IP2 + PP3 >= y[3]

prob += z[0] + PT1 >= z[1]
prob += IT1 + PT2 >= z[2]
prob += IT2 + PT3 >= z[3]

# Constraints to meet maximal production hours
prob += 5*PL1 + 2*PP1 + 4*PT1 <= 23000
prob += 5*PL2 + 2*PP2 + 4*PT2 <= 23000
prob += 5*PL3 + 2*PP3 + 4*PT3 <= 23000

# Overtime costs, function to be minimized
OT1 = (5*PL1 + 2*PP1 + 4*PT1) - 20000
OT2 = (5*PL2 + 2*PP2 + 4*PT2) - 20000
OT3 = (5*PL3 + 2*PP3 + 4*PT3) - 20000

prob += IL1 + IL2 + IL3 + IP1 + IP2 + IP3 + IT1 + IT2 + IT3 + 10 * (OT1 + OT2 + OT3)

# Solve the problem
prob.solve()

# print solve status
print("Status:", LpStatus[prob.status])

# Print optimum values
for v in prob.variables():
    print(v.name, "=", v.varValue)

print("Total Costs = ", value(prob.objective))
print(OT1)

This gives me the following result:

Status: Optimal
Prod_Laptop_February = 1000.0
Prod_Laptop_January = 50.0
Prod_Laptop_March = 1500.0
Prod_Phone_February = 2000.0
Prod_Phone_January = 1880.0
Prod_Phone_March = 2000.0
Prod_Tablet_February = 3000.0
Prod_Tablet_January = 1950.0
Prod_Tablet_March = 2000.0
Total Costs =  -76900.0
5*Prod_Laptop_January + 2*Prod_Phone_January + 4*Prod_Tablet_January - 20000

The last line I expect to be an integer value, but it is not. Can somebody explain to me how to convert the expression to an integer value?

The last print-state in your code will print a PuLP-expression. As it's a non-native python-object, it's string representation is defined by PuLP (overloading within class).

In this case the expression itself is presented in human-readable form.

If you want to access the value of it, just replace the last line with:

print(OT1.value())

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