Model API

More information can be found in the Interacting with solvers section of the manual.

JuMP.direct_modelFunction
direct_model(backend::MOI.ModelLike)

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.
source
JuMP.modeFunction
mode(model::Model)

Return mode (DIRECT, AUTOMATIC, MANUAL) of model.

source
JuMP.set_optimizerFunction
set_optimizer(model::Model, optimizer_factory;
              bridge_constraints::Bool=true)

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.

Examples

model = Model()
set_optimizer(model, GLPK.Optimizer)
source
JuMP.optimizer_with_attributesFunction
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.

Example

model = Model(
    optimizer_with_attributes(
        Gurobi.Optimizer, "Presolve" => 0, "OutputFlag" => 1
    )
)

is equivalent to:

model = Model(Gurobi.Optimizer)
set_optimizer_attribute(model, "Presolve", 0)
set_optimizer_attribute(model, "OutputFlag", 1)

Note

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.

source
JuMP.object_dictionaryFunction
object_dictionary(model::Model)

Return the dictionary that maps the symbol name of a variable, constraint, or expression to the corresponding object.

Objects are registered to a specific symbol in the macros. For example, @variable(model, x[1:2, 1:2]) registers the array of variables x to the symbol :x.

This method should be defined for any subtype of AbstractModel.

source
JuMP.unregisterFunction
unregister(model::Model, key::Symbol)

Unregister the name key from model so that a new variable, constraint, or expression can be created with the same key.

Note that this will not delete the object model[key]; it will just remove the reference at model[key]. To delete the object, use

delete(model, model[key])
unregister(model, key)

Examples

```jldoctest; setup=:(model = Model()) julia> @variable(model, x) x

julia> @variable(model, x) ERROR: An object of name x is already attached to this model. If this is intended, consider using the anonymous construction syntax, e.g., x = @variable(model, [1:N], ...) where the name of the object does not appear inside the macro.

Alternatively, use unregister(model, :x) to first unregister the existing name from the model. Note that this will not delete the object; it will just remove the reference at model[:x]. [...]

julia> num_variables(model) 1

julia> unregister(model, :x)

julia> @variable(model, x) x

julia> num_variables(model) 2

source
JuMP.set_silentFunction
set_silent(model::Model)

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

source
JuMP.unset_silentFunction
unset_silent(model::Model)

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

source
JuMP.set_time_limit_secFunction
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.

source
JuMP.time_limit_secFunction
time_limit_sec(model::Model)

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

source
JuMP.solver_nameFunction
solver_name(model::Model)

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.

source
JuMP.backendFunction
backend(model::Model)

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.

source
Base.empty!Method
empty!(model::Model) -> model

Empty the model, that is, remove all variables, constraints and model attributes but not optimizer attributes. Always return the argument.

Note: removes extensions data.

source
JuMP.get_optimizer_attributeFunction
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)).

Example

get_optimizer_attribute(model, "SolverSpecificAttributeName")

See also: set_optimizer_attribute, set_optimizer_attributes.

source
get_optimizer_attribute(
    model::Model, attr::MOI.AbstractOptimizerAttribute
)

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

Example

get_optimizer_attribute(model, MOI.Silent())

See also: set_optimizer_attribute, set_optimizer_attributes.

source
JuMP.set_optimizer_attributeFunction
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).

Example

set_optimizer_attribute(model, "SolverSpecificAttributeName", true)

See also: set_optimizer_attributes, get_optimizer_attribute.

source
set_optimizer_attribute(
    model::Model, attr::MOI.AbstractOptimizerAttribute, value
)

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

Example

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

See also: set_optimizer_attributes, get_optimizer_attribute.

source
JuMP.set_optimizer_attributesFunction
set_optimizer_attributes(model::Model, pairs::Pair...)

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

Example

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.

source
JuMP.bridge_constraintsFunction
bridge_constraints(model::Model)

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.

source
JuMP.print_bridge_graphFunction
 print_bridge_graph([io::IO,] model::Model)

Print the hyper-graph containing all variable, constraint, and objective types that could be obtained by bridging the variables, constraints, and objectives that are present in the model.

Each node in the hyper-graph corresponds to a variable, constraint, or objective type.

  • Variable nodes are indicated by [ ]
  • Constraint nodes are indicated by ( )
  • Objective nodes are indicated by | |

The number inside each pair of brackets is an index of the node in the hyper-graph.

Note that this hyper-graph is the full list of possible transformations. When the bridged model is created, we select the shortest hyper-path(s) from this graph, so many nodes may be un-used.

For more information, see Legat, B., Dowson, O., Garcia, J., and Lubin, M. (2020). "MathOptInterface: a data structure for mathematical optimization problems." URL: https://arxiv.org/abs/2002.03447

source
JuMP.write_to_fileFunction
write_to_file(
    model::Model,
    filename::String;
    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.

source
Base.writeMethod
Base.write(
    io::IO,
    model::Model;
    format::MOI.FileFormats.FileFormat = MOI.FileFormats.FORMAT_MOF
)

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

source
JuMP.read_from_fileFunction
read_from_file(
    filename::String;
    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.

source
Base.readMethod
Base.read(io::IO, ::Type{Model}; format::MOI.FileFormats.FileFormat)

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

source
JuMP.ReferenceMapType
ReferenceMap

Mapping between variable and constraint reference of a model and its copy. The reference of the copied model can be obtained by indexing the map with the reference of the corresponding reference of the original model.

source
JuMP.copy_modelFunction
copy_model(model::Model)

Return a copy of the model model and a ReferenceMap that can be used to obtain the variable and constraint reference of the new model corresponding to a given model's reference. A Base.copy(::AbstractModel) method has also been implemented, it is similar to copy_model but does not return the reference map.

Note

Model copy is not supported in DIRECT mode, i.e. when a model is constructed using the direct_model constructor instead of the Model constructor. Moreover, independently on whether an optimizer was provided at model construction, the new model will have no optimizer, i.e., an optimizer will have to be provided to the new model in the optimize! call.

Examples

In the following example, a model model is constructed with a variable x and a constraint cref. It is then copied into a model new_model with the new references assigned to x_new and cref_new.

model = Model()
@variable(model, x)
@constraint(model, cref, x == 2)

new_model, reference_map = copy_model(model)
x_new = reference_map[x]
cref_new = reference_map[cref]
source
JuMP.copy_extension_dataFunction
copy_extension_data(data, new_model::AbstractModel, model::AbstractModel)

Return a copy of the extension data data of the model model to the extension data of the new model new_model. A method should be added for any JuMP extension storing data in the ext field.

source
Base.copyMethod
copy(model::AbstractModel)

Return a copy of the model model. It is similar to copy_model except that it does not return the mapping between the references of model and its copy.

Note

Model copy is not supported in DIRECT mode, i.e. when a model is constructed using the direct_model constructor instead of the Model constructor. Moreover, independently on whether an optimizer was provided at model construction, the new model will have no optimizer, i.e., an optimizer will have to be provided to the new model in the optimize! call.

Examples

In the following example, a model model is constructed with a variable x and a constraint cref. It is then copied into a model new_model with the new references assigned to x_new and cref_new.

model = Model()
@variable(model, x)
@constraint(model, cref, x == 2)

new_model = copy(model)
x_new = model[:x]
cref_new = model[:cref]
source
JuMP.operator_warnFunction
operator_warn(model::AbstractModel)
operator_warn(model::Model)

This function is called on the model whenever two affine expressions are added together without using destructive_add!, and at least one of the two expressions has more than 50 terms.

For the case of Model, if this function is called more than 20,000 times then a warning is generated once.

source
JuMP.error_if_direct_modeFunction
error_if_direct_mode(model::Model, func::Symbol)

Errors if model is in direct mode during a call from the function named func.

Used internally within JuMP, or by JuMP extensions who do not want to support models in direct mode.

source