Haakh
Haakh

Reputation: 66

Error in min max optimization formulation using PYOMO

I am formulating my objective function for a min-max problem as written below

obj_func = power[1, i] for i in range(1, time_slots + 1)
model.obj = pyo.Objective(expr=obj_func, sense=pyo.minimize)

but I get the following error

File "C:\NMBU\TEMP/ipykernel_24484/3898218797.py", line 21 obj_func = power[1, i] for i in range(1, time_slots + 1) SyntaxError: invalid syntax

It also throws an error if I use the below code

obj_func = (power[1, i] for i in range(1, time_slots + 1))
model.obj = pyo.Objective(expr=obj_func, sense=pyo.minimize)

ValueError: Generators are not allowed

If I use the below formulation it works fine but then I don't need to optimize for the sum, I just want to optimize it to get the lowest value of power[1,i]

obj_func = sum(power[1, i] for i in range(1, time_slots + 1))
model.obj = pyo.Objective(expr=obj_func, sense=pyo.minimize)

Any ideas how to make it work, since PYOMO does not work with min/max directly?

I have already modeled power >= (power in batteries), as I cant directly use min/max in PYOMO.

Upvotes: 0

Views: 448

Answers (1)

AirSquid
AirSquid

Reputation: 11938

You are going about this the wrong way. The max() and min() functions are non-linear and can't be used in standard linear programming.

If your goal is to minimize the smallest value in a collection of values, you will need to introduce an additional variable, call it t, generate a set of constraints to force this new variable to be larger than each of the collection's elements ( t >= x[i] for each i ) and then just use t as your objective function and minimize it.

Upvotes: 1

Related Questions