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

2024-05-23 21:07:53 发布

您现在位置:Python中文网/ 问答频道 /正文

m.optionsm.solver_options在Python Gekko中设置解算器选项有两种方法。哪种方法优先?什么时候应该使用哪种方法?你知道吗

例如,我想为解算器设置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在用哪种方法。你知道吗


Tags: 方法valuevartoleranceoptionsprintx1x2
1条回答
网友
1楼 · 发布于 2024-05-23 21:07:53

如果同时设置了m.optionsm.solver_options,则APOPT或IPOPT等解算器将使用m.solver_options值。Gekko ^{} 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选项。你知道吗

相关问题 更多 >