简体   繁体   中英

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. 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.

In your case, you have an objective function which is cubic (hence non-linear) as you have a product between 3 variables.

That's why PuLP raises this error.

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