简体   繁体   中英

How to maximize an objective function in Python Gekko?

I'm trying to maximize profit in my optimization problem. When I use the Gekko m.Obj function, it always minimizes the profit.

from gekko import GEKKO
m = GEKKO()
profit = m.Var(lb=1,ub=10)
m.Obj(profit**2)
m.solve(disp=False)
print(profit.value)

The reported optimal solution is profit=1 . How can I switch to maximizing an objective function with Python Gekko so that the optimal solution is profit=10 ? I have multiple objectives in my problem. There are some that I want to minimize (utilities, feed material, operating expense) and others that I want to maximize (profit, production).

You can put a negative sign on your objective function for maximizing.

m.Obj(-profit**2)

Two new functions m.Maximize() and m.Minimize() are available in Python GEKKO starting in version 0.2.4 as shown in the GEKKO Change Log . In addition to the accepted answer m.Obj(-profit**2) , another option for maximizing the profit is to use:

m.Maximize(profit**2)

If you have all of the terms expressed in monetary units then you can minimize and maximize to have optimal tradeoffs to drive overall profitability without using the squared objective.

m.Maximize(revenue)
m.Minimize(operating_cost)
m.Minimize(feed_cost)
m.Minimize(utility_cost)

Python GEKKO combines all of the objective terms into a single value as:

minimize (-revenue + operating_cost + feed_cost + utility_cost)

You can retrieve the final objective function value after a successful m.solve() with m.options.OBJFCNVAL . The reported value is in minimization form. If you need to report the maximum form, then you can multiply the objective function result by -1 .

print(-m.options.OBJFCNVAL)

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