简体   繁体   English

在 GEKKO 优化套件 python 中更改目标

[英]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.

相关问题 Python Gekko 中目标函数的约束 - Constraint on objective function in Python Gekko 适用于 Python 的 Gekko 优化套件 - if3 始终 &lt;0 - Gekko Optimization Suite for Python - if3 always <0 如何在 Python Gekko 中最大化目标 function? - How to maximize an objective function in Python Gekko? Python GEKKO:目标 function 未显示正确结果 - Python GEKKO: Objective function not showing correct results GEKKO 中的约束非线性优化,目标 function 与预期解决方案不匹配 - Constrained non-linear optimisation in GEKKO, objective function not matching expected solution 在更改参数、目标 function 和初始条件时使用 Gekko 优化套件出现“未找到解决方案”错误 - “Solution Not Found” error using Gekko optimization suite when changing parameters, objective function, and initial condition 用于Python线性优化的Lambda目标函数 - Lambda objective function for python linear optimisation 如何用 Python Gekko 解决绝对值 abs() 目标? - How to solve Absolute Value abs() objective with Python Gekko? GEKKO if3 变量在优化中给出了意想不到的结果 - GEKKO if3 variable giving unexpected results in optimisation 使用 GEKKO 的两个 pandas 数据帧的优化问题 - Optimisation problem with two pandas dataframes using GEKKO
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM