Eric Hedengren
Eric Hedengren

Reputation: 515

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. 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. Which one does Gekko use (1e-7 or 1e-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. I'd like to know which option Gekko is using.

Upvotes: 1

Views: 2910

Answers (1)

John Hedengren
John Hedengren

Reputation: 14356

Solvers such as APOPT or IPOPT use m.solver_options values if both m.options and m.solver_options are set. 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. Some of the common options are convergence tolerances (RTOL and OTOL), maximum iterations (MAX_ITER), and maximum time (MAX_TIME). Common solver results are also output such as objective function value (OBJFCNVAL), solve time (SOLVETIME), and solution status (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. There are additional options that are configurable only from m.solver_options such as:

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.

Upvotes: 2

Related Questions