我想f(x,y,z) = x*y*z
通过一些约束x<=20 + y
和y < z+2
使用 Pulp 来最小化以下简单功能。有没有人知道如何做到这一点?
尝试时我总是得到
TypeError: Non-constant expressions cannot be multiplied
任何帮助将不胜感激。请在下面找到下面的代码
from pulp import *
#pulp.pulpTestAll()
prob = LpProblem("Profit", LpMinimize)
# Variables
x = LpVariable.dicts("x",[0,1,2],0 ,100)
def fun(x):
return x[0]*x[1]*x[2]
# Objective
opt=fun(x)
prob += opt
# Constraints
prob += x[0] <= 20
prob += x[1] <= x[2]+2
#print prob
print prob
status=prob.solve()
print "Status: %s" %LpStatus[status]
#Solution
for v in prob.variables():
print v.name, "=", v.varValue
print "Optimum =", value(prob.objective)