[英]Python Pulp multiply two variables
I would like to minimize the following simple function f(x,y,z) = x*y*z
with some constraints x<=20 + y
and y < z+2
using Pulp. 我想使用Pulp最小化以下简单函数
f(x,y,z) = x*y*z
并具有x<=20 + y
和y < z+2
一些约束。 Does any body know how this can be done please? 请问有人知道如何做到这一点吗?
When trying I was always getting 尝试的时候我总是
TypeError: Non-constant expressions cannot be multiplied
Any help would be very much appreciated. 任何帮助将不胜感激。 below please find the code below
下面请找到下面的代码
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)
I think that PuLP may only solve linear programs. 我认为PuLP可能只解决线性程序。
In your case, you have an objective function which is cubic (hence non-linear) as you have a product between 3 variables. 在您的情况下,由于具有3个变量之间的乘积,因此您具有一个三次方的目标函数(因此是非线性的)。
That's why PuLP raises this error. 这就是PuLP引发此错误的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.