简体   繁体   English

如何在 Python Gekko 中最大化目标 function?

[英]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.当我使用 Gekko m.Obj function 时,它总是使利润最小化。

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 .报告的最佳解决方案是profit=1 How can I switch to maximizing an objective function with Python Gekko so that the optimal solution is profit=10 ?如何切换到最大化目标 function 和 Python Gekko 以便最佳解决方案是 Profit 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.您可以在您的目标 function 上加一个负号以实现最大化。

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 .从版本 0.2.4 开始,Python GEKKO中提供了两个新函数m.Maximize()m.Minimize() ,如GEKKO 更改日志中所示。 In addition to the accepted answer m.Obj(-profit**2) , another option for maximizing the profit is to use:除了公认的答案m.Obj(-profit**2) ,最大化利润的另一个选项是使用:

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: Python GEKKO 将所有客观项组合成一个值:

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 .您可以在使用 m.options.OBJFCNVAL 成功m.solve()后检索最终目标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 .如果您需要报告最大形式,则可以将目标 function 结果乘以-1

print(-m.options.OBJFCNVAL)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM