Python Pulp乘以两个变量

时间:2012-08-20 11:09:59

标签: python optimization

我想使用Pulp将一些约束f(x,y,z) = x*y*zx<=20 + y最小化为以下简单函数y < z+2。有没有人知道如何做到这一点?

在尝试的时候我总是得到

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)

1 个答案:

答案 0 :(得分:0)

我认为PuLP可能只能解决线性程序。

在您的情况下,您有一个目标函数,它是立方的(因此是非线性的),因为您有3个变量之间的乘积。

这就是PuLP引发此错误的原因。