简体   繁体   English

Python,PuLP:将LpVariable转换为整数

[英]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). 我正在尝试打印定义为LpVariable(PuLP 1.6.1,Python 3.5)的变量的值。 The LpVariable has an option to set the category of the parameter to 'Integer'. LpVariable有一个选项可以将参数的类别设置为“ 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. 代码中的最后一个打印状态将打印一个PuLP表达式。 As it's a non-native python-object, it's string representation is defined by PuLP (overloading within class). 由于它是一个非本地的python对象,因此它的字符串表示由PuLP定义(类中的重载)。

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())

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

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