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

时间:2019-11-26 14:05:37

标签: python solver ipopt gekko

有两种方法可以在Python Gekko中使用m.optionsm.solver_options来设置求解器选项。哪种方法优先,何时应使用其中一种?

例如,我想为求解器设置objective toleranceOTOL)和equation residual toleranceRTOL)。 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))

这将产生解决方案:

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

有时候问题需要或多或少的准确性,但是我还想将其他选项用于IPOPTAPOPT。我想知道Gekko使用的是哪个选项。

1 个答案:

答案 0 :(得分:1)

如果同时设置了m.solver_optionsm.options,则APOPT或IPOPT之类的求解器将使用m.solver_options值。 Gekko m.options values只是所有求解器选项的子集,而且是一些对于所有求解器均可调的最常见配置参数。一些常见的选项是会聚公差(RTOLOTOL),maximum iterationsMAX_ITER)和maximum timeMAX_TIME)。还会输出常见的求解器结果,例如objective function valueOBJFCNVAL),solve timeSOLVETIME)和solution statusAPPINFO)。

还有一些可以通过求解器类型配置的特定选项。例如,APOPT求解器是混合整数非线性规划(MINLP)求解器。还有其他只能从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']

IPOPT求解器是一个非线性规划(NLP)求解器,因此它不使用MINLP选项。