简体   繁体   English

如果我的目标函数是非线性(也是指数解释)函数,我应该使用什么求解器? Python GEKKO

[英]What solver should I use if my objective function is an nonlinear (also exponential explanation) function? Python GEKKO

I'm trying to optimise an exponential objective function with GEKKO, but I don't know if the selected solver is the best one for these kind of problems. 我正在尝试使用GEKKO优化指数目标函数,但我不知道所选择的求解器是否是针对这类问题的最佳解算器。

Is the selected one a valid choice?? 被选中的一个是有效的选择吗?

import numpy as np

'GEKKO MODELING'
from gekko import GEKKO
m = GEKKO()
m.options.SOLVER=1  # APOPT is an MINLP solver

# Initialize variables
x = []
x1 = m.Var(value=20,lb=20, ub=6555)  #integer=True
x2 = m.Var(value=0,lb=0,ub=10000)  #integer=True
x3 = m.sos1([30, 42, 45, 55])

x = [x1, x2, x3]
# Equations
m.Equation((x1 * x2* x3) * 10 ** (-6)>=50)

def fun(x):
    return 44440 + ((np.pi * x[0] * x[1] * x[2]) * 10 ** (-4))**0.613

x = [400,300,19]

'GEKKO Optimization'
m.Obj(fun(x))

m.solve(disp=False) # Solve
print('Results')
print('x1: ' + str(x1.value))
print('x2: ' + str(x2.value))
print('x3: ' + str(x3.value))

print('Objective: ' + str(m.options.objfcnval))

A problem with your script is that you are redefining the value of x = [400,300,19] before you call your objective function. 您的脚本存在的问题是,在调用目标函数之前,您要重新定义x = [400,300,19]的值。 The objective function should be called with your original definition x = [x1, x2, x3] so that it can optimize these variables. 应使用原始定义x = [x1, x2, x3]调用目标函数x = [x1, x2, x3]以便它可以优化这些变量。 One other change is that the value of x3 is by default equal to zero. 另一个变化是x3的值默认等于零。 Setting it away from zero x3.value=1.0 allows APOPT and IPOPT solvers to converge because you previously started right on the border of an imaginary number objective with x3<0 . 将其设置为远离零x3.value=1.0允许APOPT和IPOPT求解器收敛,因为您之前在x3<0的虚数目标的边界处开始。

import numpy as np
from gekko import GEKKO
m = GEKKO()
x = []
x1 = m.Var(value=20,lb=20, ub=6555)  #integer=True
x2 = m.Var(value=1,lb=1,ub=10000)  #integer=True
x3 = m.sos1([30, 42, 45, 55])
x3.value = 1.0
x = [x1, x2, x3]
m.Equation((x1 * x2* x3) * 1e-6 >= 50)
def fun(x):
    return 44440 + ((np.pi * x[0] * x[1] * x[2]) * 1e-4)**0.613
m.Obj(fun(x))

# Change to True to initialize with IPOPT
init = False
if init:
    m.options.SOLVER=3  
    m.solve(disp=False) # Solve

m.options.SOLVER=1
m.solve(disp=True) # Solve

print('Results')
print('x1: ' + str(x1.value))
print('x2: ' + str(x2.value))
print('x3: ' + str(x3.value))
print('Objective: ' + str(m.options.objfcnval))

For the solver suggestion, here is a list of publicly available solvers in Gekko . 对于求解器建议,以下是Gekko中公开可用的求解器列表 There are additional commercially available solver options in Gekko but I'll stick with just the publicly accessible ones (APOPT, BPOPT, and IPOPT) for this response. Gekko还有其他商用解算器选项,但我会坚持使用可公开访问的解决方案(APOPT,BPOPT和IPOPT)进行此响应。 Any nonlinear programming solver should be able to handle a nonlinear objective such as x**0.613 . 任何非线性编程求解器都应该能够处理非线性目标,例如x**0.613 Your problem also includes a Special Ordered Set, Type 1 (m.sos1) and so your problem is not just a Nonlinear Programming (NLP) problem but also includes binary variables for the sos1 . 您的问题还包括一个特殊有序集,类型1(m.sos1) ,因此您的问题不仅仅是非线性编程(NLP)问题,还包括sos1二进制变量。 This means that you'll need to use a Mixed Integer Nonlinear Programming (MINLP) solver. 这意味着您需要使用混合整数非线性规划(MINLP)求解器。 The APOPT solver is the only MINLP solver publicly available in Gekko and it is automatically selected for you when you create an sos1 object. APOPT求解器是Gekko中唯一公开的MINLP解算器,在您创建sos1对象时会自动为您选择。 If you would like to try to solve the MINLP problem with an NLP solver (such as IPOPT), then you'll need to specify the solver after you create the m.sos1 object. 如果您想尝试使用NLP求解器(例如IPOPT)解决MINLP问题,则需要创建m.sos1对象指定求解器。

m.options.SOLVER = 3

This could lead to an incorrect solution because x3 can only be one of the following: 30, 42, 45, 55 . 这可能导致错误的解决方案,因为x3只能是以下之一: 30, 42, 45, 55 IPOPT finds a minimum solution of x3==47.079550873 so in this case, it did not return an integer solution. IPOPT找到x3==47.079550873的最小解,所以在这种情况下,它没有返回整数解。 If you want to guarantee an integer solution, you'll need to use APOPT. 如果您想保证整数解决方案,则需要使用APOPT。

 Successful solution

 ---------------------------------------------------
 Solver         :  APOPT (v1.0)
 Solution time  :   4.279999999562278E-002 sec
 Objective      :    44813.4405591393     
 Successful solution
 ---------------------------------------------------

Results
x1: [677.59896405]
x2: [2459.665311]
x3: [30.0]
Objective: 44813.440559

If you need to change some of the tuning parameters of the MINLP APOPT solver then you can use something like the following: 如果您需要更改MINLP APOPT解算器的某些调整参数,那么您可以使用以下内容:

m.solver_options = ['minlp_gap_tol 1.0e-2',\
                    'minlp_maximum_iterations 10000',\
                    'minlp_max_iter_with_int_sol 500']

There is additional information on the APOPT solver options . 有关APOPT求解器选项的其他信息

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

相关问题 Python Gekko 中目标函数的约束 - Constraint on objective function in Python Gekko Python GEKKO:如何将函数插入求解器? - Python GEKKO: how to insert a function into the solver? 如何在 Python Gekko 中最大化目标 function? - How to maximize an objective function in Python Gekko? 非线性 function 和 2 个输入的 Gekko 优化错误 - Gekko optimisation error with a nonlinear function and 2 inputs 将求解器 GRG 非线性 function 转换为 PuLP - Transform Solver GRG Nonlinear function into PuLP 如何在没有已知目标函数(比如一些随机函数)和已知变量和约束的情况下使用 gekko 优化器? - how can I use gekko optimizer without a known objective function(let say some random function) and with known variables and constraints? 我可以在 Gekko 的设计优化过程中使用隐式目标 function。 这个隐式 function 是一个子程序,它创建数字 model - Can I use implicit objective function in a design optimization process in Gekko. this implicit function is a subroutine that creates numerical model 用 python 和 gekko 拟合非线性 model - Fit a nonlinear model with python and gekko 如何在Gekko获得目标函数的价值 - How to get value for objective function in Gekko 目标 function 不使用 Gekko 收敛 - Objective function does not converge using Gekko
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM