How to optimize an 'expr' which is different depending on the values of x,y,z

686 views
Skip to first unread message

thomas Armstrong

unread,
Nov 21, 2021, 12:06:14 PM11/21/21
to Pyomo Forum
Hi,

Sorry for this naive question.

I'd like to optimize a classical linear production model with classical constraints through Python and pyomo

from pyomo.environ import *
model = ConcreteModel()
model.x = Var(domain=NonNegativeReals)
model.y = Var(domain=NonNegativeReals)
model.z = Var(domain=NonNegativeReals)

model.profit = Objective(expr = 40*model.x + 30*model.y + 50*model.z, sense=maximize)
model.demand = Constraint(expr = model.x <= 40)
model.laborA = Constraint(expr = model.x + model.y <= 80)
model.laborB = Constraint(expr = 2*model.x + model.z <= 100)
model.laborC = Constraint(expr = model.z <= 50)

However, the Objective function is sometimes different, depending on the values of 'y' and 'z':

if (model.y > 20):
expr = 40*model.x + 30*model.y + 50*model.z + 200
if (model.z > 30):
expr = 40*model.x + 30*model.y + 60*model.z + 300

I do not know to implement these conditions into my Python code. If I try with the previous lines of code, I get the "Cannot convert non-constant Pyomo expression (20  <  y) to bool." error message.

I wondered if someone could give me any tip to implement this. How are called this kind of "non continuous" linear functions (just to Google them)?

Thank you very much in advance.

Jeffrey Kantor

unread,
Nov 21, 2021, 1:36:26 PM11/21/21
to pyomo...@googlegroups.com
Hi Thomas,

Just to clarify your example, what is the value of the objective if y > 20 and z > 30?  In other words, do you have some criteria that produces a unique value for the objective given values x, y, and z?

Jeff

--
You received this message because you are subscribed to the Google Groups "Pyomo Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyomo-forum...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyomo-forum/4fd4e1fb-052e-49b0-ac46-45169681c7fdn%40googlegroups.com.

thomas Armstrong

unread,
Nov 22, 2021, 4:55:16 AM11/22/21
to Pyomo Forum
Hi Jeff, 

Thank you very much for your answer. I'm afraid I didn't explain myself properly. I am just starting to use 'pyomo' and may not be using the proper nomenclature.

I'm trying to optimize the incomes of three products (x, y and z), with three initial prices ($40/item, $30/item, $50/item). I have several constraints: x<=40, x+y <=80, 2*x+z <=100, z<=50.

However, I receive some bonus:
  • if I produce more than 20 'y' items, their price will be 50 $/item (sorry, there is a typo on the previous expressions), and a fixed bonus of $200.
  • if I produce more than 30 'z' items, their price will be 60 $/item, and a fixed bonus of $300.

So I'm trying to optimize the following expressions:

if ((model.y <= 20) & (model.z <= 30):
expr = 40*model.x + 30*model.y + 50*model.z
if ((model.y > 20) & (model.z <= 30)):
expr = 40*model.x + 50*model.y + 50*model.z + 200
if ((model.y <= 20) & (model.z > 30)):
expr = 40*model.x + 30*model.y + 60*model.z + 300
However, I'm not able how to implement it with the pyomo nomenclature. Thank you again.

Michael Bynum

unread,
Nov 22, 2021, 7:47:33 AM11/22/21
to Pyomo Forum
It sounds like you need some discrete variables. I would recommend checking out Pyomo.GDP. It can make modeling these logical decisions very natural. Check out the online documentation (https://pyomo.readthedocs.io/en/stable/modeling_extensions/gdp/index.html) or chapter 11 of the 3rd edition of the Pyomo book.

Michael

Jeffrey Kantor

unread,
Nov 22, 2021, 10:16:46 AM11/22/21
to pyomo...@googlegroups.com
Thomas,

OK, that clarifies the model.  Welcome to the wizardry of modeling for optimization.

The key here is recognize that Python “if” statements are not directly translated by Pyomo. If you need an “if” statement in your model, you likely have a logical condition that needs to modeled as either a disjunctive constraint using the Pyomo.GDP as suggested by Michael Bynum, or introducing some binary or discrete variables.  A  “big M” formation, for example,  would use some additional binary and decision variables and should work for this problem.  Consider breaking y and z into sums

y = y0 + y1
z = z0 + z1

Where y1 and z1 is the production in excess of the bonus levels. Variables y0 and z1 are then bounded between 0 and the bonus levels. Next, create binary indicator variables by and bz to reflect the bonus conditions. With big M, these are included in the model

20 >= y - M*by
20 <= y + M*(1-by)

30 >= z - M*bz
30 <= z + M*(1-bz)

Now you’re set to go. You need to revise the objective in terms of these new decision variables

Profit = 40*x + 30*y0 + 50*y1 + 600*bz + 50*z0 + 60*z1 + 600*bz


Jeff


Jeffrey Kantor

unread,
Nov 22, 2021, 11:30:21 AM11/22/21
to pyomo...@googlegroups.com
Hi Thomas,

I realize that I misstated the big M constraints. These should be written instead as



This is actually a nice teaching example, so I took the time to write up a notebook file for the ND Pyomo Cookbook.  Here’s link …


to a working Pyomo model.  There are issues with this formulation, in particular it is not numerically robust. It should be cleaned up, in particular reformulated as disjunctive constraints. But I’ll probably save that as an exercise for a class that I teach in the Fall.

Jeff
Reply all
Reply to author
Forward
0 new messages