[英]Change objective in GEKKO optimisation suite python
I am using GEKKO as a MINP-solver.我正在使用 GEKKO 作为 MINP 求解器。 To establish more or less lower and upper bounds, I would like to minimise the sum of variables once, and then maximise it.为了建立或多或少的下限和上限,我想将变量的总和最小化一次,然后将其最大化。 GEKKO adds up the objectives on the second optimisation, however.然而,GEKKO 将第二次优化的目标加起来。 How can I delete the old objective and add a new one?如何删除旧目标并添加新目标? And how can I reset the variables to their default values after the first optimisation?第一次优化后如何将变量重置为默认值?
Thank you for any help感谢您的任何帮助
solver = GEKKO()
solver.Minimize(sum(x))
solver.solve(disp=False)
print(x)
solver.Maximize(sum(x))
solver.solve(disp=False)
print(x)
Thanks for proposing the solution to your own question.感谢您为自己的问题提出解决方案。 You can create an answer to your own question and mark it as the accepted answer.您可以为自己的问题创建一个答案并将其标记为已接受的答案。 The comments section is also a good place to put the solution but then StackOverflow thinks the questions is still unanswered.评论部分也是放置解决方案的好地方,但 StackOverflow 认为这些问题仍然没有答案。
Approach 1方法一
Here are two ways to switch from minimize
to maximimize
.这是从minimize
切换到maximimize
的两种方法。 If it is just Minimize(sum(x))
to Maximize(sum(x))
then just included an extra Maximize(sum(x))
to cancel out the prior Minimize(sum(x))
.如果它只是Minimize(sum(x))
到Maximize(sum(x))
则只需包含一个额外的Maximize(sum(x))
以抵消先前的Minimize(sum(x))
。 Here is a simple script that shows this first approach:这是一个显示第一种方法的简单脚本:
from gekko import GEKKO
m = GEKKO(remote=False)
x = m.Array(m.Var,3,lb=0,ub=1)
m.Minimize(m.sum(x))
m.solve(disp=False)
print('-'*20)
print('x: ', x)
print('obj: ', m.options.OBJFCNVAL)
m.Maximize(2*m.sum(x))
m.solve(disp=False)
print('-'*20)
print('x: ', x)
print('obj: ', m.options.OBJFCNVAL)
Approach 2方法二
A second approach (as you already found) is to clear the _objectives
list with m._objectives.clear()
.第二种方法(正如您已经发现的那样)是使用m._objectives.clear()
清除_objectives
列表。 Here is a sample script with that second approach:这是第二种方法的示例脚本:
from gekko import GEKKO
m = GEKKO(remote=False)
x = m.Array(m.Var,3,lb=0,ub=1)
m.Minimize(m.sum(x))
m.solve(disp=False)
print('-'*20)
print('x: ', x)
print('obj: ', m.options.OBJFCNVAL)
m._objectives.clear()
m.Maximize(m.sum(x))
m.solve(disp=False)
print('-'*20)
print('x: ', x)
print('obj: ', m.options.OBJFCNVAL)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.