简体   繁体   English

如何在Python Gekko中设置求解器选项(例如容错)?

[英]How to set solver options (such as error tolerance) in Python Gekko?

There are two ways to set solver options in Python Gekko with m.options and m.solver_options . 在Python Gekko中,有两种方法可以使用m.optionsm.solver_options来设置求解器选项。 Which method takes precedence and when should one or the other be used? 哪种方法优先,何时应使用其中一种?

For example, I would like to set the objective tolerance ( OTOL ) and equation residual tolerance ( RTOL ) for the solver. 例如,我想为求解器设置目标公差OTOL )和方程式残余公差RTOL )。 Which one does Gekko use ( 1e-7 or 1e-8 )? Gekko使用哪一个( 1e-71e-8 )?

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

m.options.OTOL = 1.0e-8
m.options.RTOL = 1.0e-8

# solver settings with APOPT
m.solver_options = ['objective_convergence_tolerance 1.0e-7', \
                    'constraint_convergence_tolerance 1.0e-7']

# Initialize variables
x1 = m.Var(value=1,lb=1,ub=5)
x2 = m.Var(value=5,lb=1,ub=5)
x3 = m.Var(value=5,lb=1,ub=5,integer=True)
x4 = m.Var(value=1,lb=1,ub=5)
# Equations
m.Equation(x1*x2*x3*x4>=25)
m.Equation(x1**2+x2**2+x3**2+x4**2==40)
m.Obj(x1*x4*(x1+x2+x3)+x3) # Objective
m.solve(disp=False) # Solve
print('Results')
print('x1: ' + str(x1.value))
print('x2: ' + str(x2.value))
print('x3: ' + str(x3.value))
print('x4: ' + str(x4.value))
print('Objective: ' + str(m.options.objfcnval))

This produces the solution: 这产生了解决方案:

Results
x1: [1.0]
x2: [4.5992789966]
x3: [4.0]
x4: [1.3589086474]
Objective: 17.044543237

Sometimes a problem needs more or less accuracy but there are also other options that I'd like to use for IPOPT or APOPT . 有时候一个问题需要或多或少的准确性,但是我还想为IPOPTAPOPT使用其他选择。 I'd like to know which option Gekko is using. 我想知道Gekko使用的是哪个选项。

Solvers such as APOPT or IPOPT use m.solver_options values if both m.options and m.solver_options are set. 如果同时设置了m.optionsm.solver_options则APOPT或IPOPT之类的求解器将使用m.solver_options值。 The Gekko m.options values are only a subset of all the solver options but also some of the most common configuration parameters that are adjustable for all solvers. Gekko m.options只是所有求解器选项的子集,而且还是一些对于所有求解器均可调的最常见配置参数。 Some of the common options are convergence tolerances ( RTOL and OTOL ), maximum iterations ( MAX_ITER ), and maximum time ( MAX_TIME ). 一些常见的选项是收敛容限( RTOLOTOL ), 最大迭代次数MAX_ITER )和最大时间MAX_TIME )。 Common solver results are also output such as objective function value ( OBJFCNVAL ), solve time ( SOLVETIME ), and solution status ( APPINFO ). 还会输出常见的求解器结果,例如目标函数值OBJFCNVAL ), 求解时间SOLVETIME )和求解状态APPINFO )。

There are also specific options that are configurable by the solver type. 也有可以通过求解器类型配置的特定选项。 For example, the APOPT solver is a Mixed Integer Nonlinear Programming (MINLP) solver. 例如,APOPT求解器是混合整数非线性规划(MINLP)求解器。 There are additional options that are configurable only from m.solver_options such as: 只能从m.solver_options配置其他选项,例如:

m.solver_options = ['minlp_maximum_iterations 500', \
                    # minlp iterations with integer solution
                    'minlp_max_iter_with_int_sol 10', \
                    # treat minlp as nlp
                    'minlp_as_nlp 0', \
                    # nlp sub-problem max iterations
                    'nlp_maximum_iterations 50', \
                    # 1 = depth first, 2 = breadth first
                    'minlp_branch_method 1', \
                    # maximum deviation from whole number
                    'minlp_integer_tol 0.05', \
                    # covergence tolerance
                    'minlp_gap_tol 0.01']

The IPOPT solver is a Nonlinear Programming (NLP) solver so it doesn't use the MINLP options. IPOPT求解器是一个非线性编程(NLP)求解器,因此它不使用MINLP选项。

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

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