Interacting with solvers

A JuMP model keeps a MathOptInterface (MOI) backend of type MOI.ModelLike that stores the optimization problem and acts as the optimization solver. We call it an MOI backend and not optimizer as it can also be a wrapper around an optimization file format such as MPS that writes the JuMP model in a file. From JuMP, the MOI backend can be accessed using the backend function. JuMP can be viewed as a lightweight, user-friendly layer on top of the MOI backend, in the sense that:

While this allows JuMP to be a thin wrapper on top of the solver API, as mentioned in the last point above, this seems rather demanding on the solver. Indeed, while some solvers support incremental building of the model and modifications before and after solve, other solvers only support the model being copied at once before solve. Moreover, it seems to require all solvers to implement all possible reformulations independently which seems both very ambitious and might generate a lot of duplicated code.

These apparent limitations are addressed at level of MOI in a manner that is completely transparent to JuMP. While the MOI API may seem very demanding, it allows MOI models to be a succession of lightweight MOI layers that fill the gap between JuMP requirements and the solver capabilities. The remainder of this section describes how JuMP interacts with the MOI backend.

JuMP models can be created in three different modes: AUTOMATIC, MANUAL and DIRECT.

Automatic and Manual modes

In AUTOMATIC and MANUAL modes, two MOI layers are automatically applied to the optimizer:

See the MOI documentation for more details on these two MOI layers.

To attach an optimizer to a JuMP model, JuMP needs to be able to create a new empty optimizer instance. For this reason, we provide JuMP with a function that creates a new optimizer (i.e., an optimizer factory), instead of a concrete optimizer object.

The factory can be provided either at model construction time by calling set_optimizer. An optimizer must be set before a call to optimize!. The optimizer can be grouped with attributes to be set before optimization with optimizer_with_attributes.

set_optimizer(model::Model, optimizer_factory;

Creates an empty MathOptInterface.AbstractOptimizer instance by calling optimizer_factory() and sets it as the optimizer of model. Specifically, optimizer_factory must be callable with zero arguments and return an empty MathOptInterface.AbstractOptimizer.

If bridge_constraints is true, constraints that are not supported by the optimizer are automatically bridged to equivalent supported constraints when an appropriate transformation is defined in the MathOptInterface.Bridges module or is defined in another module and is explicitly added.

See set_optimizer_attributes and set_optimizer_attribute for setting solver-specific parameters of the optimizer.


model = Model()
set_optimizer(model, GLPK.Optimizer)
optimizer_with_attributes(optimizer_constructor, attrs::Pair...)

Groups an optimizer constructor with the list of attributes attrs. Note that it is equivalent to MOI.OptimizerWithAttributes.

When provided to the Model constructor or to set_optimizer, it creates an optimizer by calling optimizer_constructor(), and then sets the attributes using set_optimizer_attribute.


model = Model(
        Gurobi.Optimizer, "Presolve" => 0, "OutputFlag" => 1
is equivalent to:

julia model = Model(Gurobi.Optimizer) setoptimizerattribute(model, "Presolve", 0) setoptimizerattribute(model, "OutputFlag", 1) ````


The string names of the attributes are specific to each solver. One should consult the solver's documentation to find the attributes of interest.

See also: set_optimizer_attribute, set_optimizer_attributes, get_optimizer_attribute.

struct NoOptimizer <: Exception end

No optimizer is set. The optimizer can be provided to the Model constructor or by calling set_optimizer.

          ignore_optimize_hook=(model.optimize_hook === nothing),

Optimize the model. If an optimizer has not been set yet (see set_optimizer), a NoOptimizer error is thrown.

Keyword arguments kwargs are passed to the optimize_hook. An error is thrown if optimize_hook is nothing and keyword arguments are provided. ```

New JuMP models are created using the Model constructor:

Model(; caching_mode::MOIU.CachingOptimizerMode=MOIU.AUTOMATIC)

Return a new JuMP model without any optimizer; the model is stored the model in a cache. The mode of the CachingOptimizer storing this cache is caching_mode. Use set_optimizer to set the optimizer before calling optimize!.


Return a new JuMP model with the provided optimizer and bridge settings. This function is equivalent to:

    model = Model()
    set_optimizer(model, optimizer_factory,
    return model

See set_optimizer for the description of the optimizer_factory and bridge_constraints arguments.


The following creates a model with the optimizer set to Ipopt:

model = Model(Ipopt.Optimizer)

Direct mode

JuMP models can be created in DIRECT mode using the JuMP.direct_model function.


Return a new JuMP model using backend to store the model and solve it. As opposed to the Model constructor, no cache of the model is stored outside of backend and no bridges are automatically applied to backend. The absence of cache reduces the memory footprint but it is important to bear in mind the following implications of creating models using this direct mode:

  • When backend does not support an operation, such as modifying constraints or adding variables/constraints after solving, an error is thrown. For models created using the Model constructor, such situations can be dealt with by storing the modifications in a cache and loading them into the optimizer when optimize! is called.
  • No constraint bridging is supported by default.
  • The optimizer used cannot be changed the model is constructed.
  • The model created cannot be copied.

Return the lower-level MathOptInterface model that sits underneath JuMP. This model depends on which operating mode JuMP is in (manual, automatic, or direct), and whether there are any bridges in the model.

If JuMP is in direct mode (i.e., the model was created using direct_model), the backend with be the optimizer passed to direct_model. If JuMP is in manual or automatic mode, the backend is a MOI.Utilities.CachingOptimizer.

This function should only be used by advanced users looking to access low-level MathOptInterface or solver-specific functionality.

Solver attributes

Some solver attributes can be queried and set through JuMP models.


If available, returns the SolverName property of the underlying optimizer. Returns "No optimizer attached" in AUTOMATIC or MANUAL modes when no optimizer is attached. Returns "SolverName() attribute not implemented by the optimizer." if the attribute is not implemented.


When in direct mode, return false. When in manual or automatic mode, return a Bool indicating whether the optimizer is set and unsupported constraints are automatically bridged to equivalent supported constraints when an appropriate transformation is available.

get_optimizer_attribute(model, name::String)

Return the value associated with the solver-specific attribute named name.

Note that this is equivalent to get_optimizer_attribute(model, MOI.RawParameter(name)).


julia get_optimizer_attribute(model, "SolverSpecificAttributeName")`

See also: set_optimizer_attribute, set_optimizer_attributes.

    model::Model, attr::MOI.AbstractOptimizerAttribute

Return the value of the solver-specific attribute attr in model.


julia get_optimizer_attribute(model, MOI.Silent())`

See also: set_optimizer_attribute, set_optimizer_attributes.

set_optimizer_attribute(model::Model, name::String, value)

Sets solver-specific attribute identified by name to value.

Note that this is equivalent to set_optimizer_attribute(model, MOI.RawParameter(name), value).


set_optimizer_attribute(model, "SolverSpecificAttributeName", true)

See also: set_optimizer_attributes, get_optimizer_attribute.

    model::Model, attr::MOI.AbstractOptimizerAttribute, value

Set the solver-specific attribute attr in model to value.


set_optimizer_attribute(model, MOI.Silent(), true)

See also: set_optimizer_attributes, get_optimizer_attribute.

set_optimizer_attributes(model::Model, pairs::Pair...)

Given a list of attribute => value pairs, calls set_optimizer_attribute(model, attribute, value) for each pair.


model = Model(Ipopt.Optimizer)
set_optimizer_attributes(model, "tol" => 1e-4, "max_iter" => 100)

is equivalent to:

model = Model(Ipopt.Optimizer)
set_optimizer_attribute(model, "tol", 1e-4)
set_optimizer_attribute(model, "max_iter", 100)

See also: set_optimizer_attribute, get_optimizer_attribute.


Takes precedence over any other attribute controlling verbosity and requires the solver to produce no output.


Neutralize the effect of the set_silent function and let the solver attributes control the verbosity.

set_time_limit_sec(model::Model, limit)

Sets the time limit (in seconds) of the solver. Can be unset using unset_time_limit_sec or with limit set to nothing.


Unsets the time limit of the solver. Can be set using set_time_limit_sec.


Gets the time limit (in seconds) of the model (nothing if unset). Can be set using set_time_limit_sec.

File formats

JuMP can write models to a variety of file-formats using write_to_file and Base.write.

    format::MOI.FileFormats.FileFormat = MOI.FileFormats.FORMAT_AUTOMATIC

Write the JuMP model model to filename in the format format.

If the filename ends in .gz, it will be compressed using Gzip. If the filename ends in .bz2, it will be compressed using BZip2.

    format::MOI.FileFormats.FileFormat = MOI.FileFormats.FORMAT_MOF

Write the JuMP model model to io in the format format.

JuMP models can be created from file formats using read_from_file and

    format::MOI.FileFormats.FileFormat = MOI.FileFormats.FORMAT_AUTOMATIC

Return a JuMP model read from filename in the format format.

If the filename ends in .gz, it will be uncompressed using Gzip. If the filename ends in .bz2, it will be uncompressed using BZip2.

Base.readMethod., ::Type{Model}; format::MOI.FileFormats.FileFormat)

Return a JuMP model read from io in the format format.