Warning
This documentation is out of date. JuMP no longer uses readthedocs to host its documentation. See here for the links to the latest release and developer documentation.
Models¶
Constructor¶
Model
is a type defined by JuMP. All variables and constraints are
associated with a Model
object. It has a constructor that has no
required arguments:
m = Model()
The constructor also accepts an optional keyword argument, solver
. You may specify a solver either here or later on by calling setsolver
. JuMP will throw an error if you try to solve a problem without specifying a solver.
solver
must be an AbstractMathProgSolver
object, which is constructed as follows:
solver = solvername(Option1=Value1, Option2=Value2, ...)
where solvername
is one of the supported solvers. See the solver table for the list of available solvers and corresponding parameter names. All options are solver-dependent; see corresponding solver packages for more information.
Note
Be sure that the solver provided supports the problem class of the model. For example ClpSolver
and GLPKSolverLP
support only linear programming problems. CbcSolver
and GLPKSolverMIP
support only mixed-integer programming problems.
As an example, we can create a Model
object that will use GLPK’s
exact solver for LPs as follows:
m = Model(solver = GLPKSolverLP(method=:Exact))
Methods¶
General
MathProgBase.numvar(m::Model)
- returns the number of variables associated with theModel m
.MathProgBase.numlinconstr(m::Model)
- returns the number of linear constraints associated with theModel m
.MathProgBase.numquadconstr(m::Model)
- returns the number of quadratic constraints associated with theModel m
.JuMP.numsocconstr(m::Model)
- returns the number of second order cone constraints associated with theModel m
.JuMP.numsosconstr(m::Model)
- returns the number of sos constraints associated with theModel m
.JuMP.numsdconstr(m::Model)
- returns the number of semi-definite constraints associated with theModel m
.JuMP.numnlconstr(m::Model)
- returns the number of nonlinear constraints associated with theModel m
.MathProgBase.numconstr(m::Model)
- returns the total number of constraints associated with theModel m
.getsolvetime(m::Model)
- returns the solve time reported by the solver if it is implemented.getnodecount(m::Model)
- returns the number of explored branch-and-bound nodes, if it is implemented.getobjbound(m::Model)
- returns the best known bound on the optimal objective value. This is used, for example, when a branch-and-bound method is stopped before finishing.getobjgap(m::Model)
- returns the final relative optimality gap as optimization terminated. That is, it returns \(\frac{|b-f|}{|f|}\), where \(b\) is the best bound and \(f\) is the best feasible objective value.getrawsolver(m::Model)
- returns an object that may be used to access a solver-specific API.getsimplexiter(m::Model)
- returns the cumulative number of simplex iterations during the optimization process. In particular, for a MIP it returns the total simplex iterations for all nodes.getbarrieriter(m::Model)
- returns the cumulative number of barrier iterations during the optimization process.internalmodel(m::Model)
- returns the internal low-levelAbstractMathProgModel
object which can be used to access any functionality that is not exposed by JuMP. See the MathProgBase documentation.solve(m::Model; suppress_warnings=false, relaxation=false)
- solves the model using the selected solver (or a default for the problem class), and takes two optional arguments that are disabled by default. Settingsuppress_warnings
totrue
will suppress all JuMP-specific output (e.g. warnings about infeasibility and lack of dual information) but will not suppress solver output (which should be done by passing options to the solver). Settingrelaxation=true
solves the standard continuous relaxation for the model: that is, integrality is dropped, special ordered set constraints are not enforced, and semi-continuous and semi-integer variables with bounds[l,u]
are replaced with bounds[min(l,0),max(u,0)]
.JuMP.build(m::Model)
- builds the model in memory at the MathProgBase level without optimizing.setsolver(m::Model,s::AbstractMathProgSolver)
- changes the solver which will be used for the next call tosolve()
, discarding the current internal model if present.getindex(m::Model,name::Symbol)
- returns the variable, or group of variables, or constraint, or group of constraints, of the given name which were added to the model. This errors if multiple variables or constraints share the same name.setindex!(m::Model, value, name::Symbol)
- stores the objectvalue
in the modelm
so that it can be accessed viagetindex
.
Objective
getobjective(m::Model)
- returns the objective function as aQuadExpr
.getobjectivesense(m::Model)
- returns objective sense, either:Min
or:Max
.setobjectivesense(m::Model, newSense::Symbol)
- sets the objective sense (newSense
is either:Min
or:Max
).getobjectivevalue(m::Model)
- returns objective value after a call tosolve
.getobjectivebound(m::Model)
- returns the best known bound on the optimal objective value after a call tosolve
.
Output
writeLP(m::Model, filename::AbstractString; genericnames=true)
- write the model tofilename
in the LP file format. Setgenericnames=false
for user-defined variable names.writeMPS(m::Model, filename::AbstractString)
- write the model tofilename
in the MPS file format.
Solve status¶
The call status = solve(m)
returns a symbol recording the status of the optimization process, as reported by the solver. Typical values are listed in the table below, although the code can take solver-dependent values. For instance, certain solvers prove infeasibility or unboundedness during presolve, but do not report which of the two cases holds. See your solver interface documentation (as linked to in the solver table) for more information.
Status | Meaning |
---|---|
:Optimal |
Problem solved to optimality |
:Unbounded |
Problem is unbounded |
:Infeasible |
Problem is infeasible |
:UserLimit |
Iteration limit or timeout |
:Error |
Solver exited with an error |
:NotSolved |
Model built in memory but not optimized |
Quadratic Objectives¶
Quadratic objectives are supported by JuMP using a solver which implements the corresponding extensions of the MathProgBase interface. Add them in the same way you would a linear objective:
using Ipopt
m = Model(solver=IpoptSolver())
@variable(m, 0 <= x <= 2 )
@variable(m, 0 <= y <= 30 )
@objective(m, Min, x*x+ 2x*y + y*y )
@constraint(m, x + y >= 1 )
print(m)
status = solve(m)
Second-order cone constraints¶
Second-order cone constraints of the form \(||Ax-b||_2 + a^Tx + c \le 0\) can be added directly using the norm
function:
@constraint(m, norm(A*x) <= 2w - 1)
You may use generator expressions within norm()
to build up normed expressions with complex indexing operations in much the same way as with sum(...)
:
@constraint(m, norm(2x[i] - i for i=1:n if c[i] == 1) <= 1)
Accessing the low-level model¶
It is possible to construct the internal low-level model before optimizing. To do this,
call the JuMP.build
function. It is then possible
to obtain this model by using the internalmodel
function. This may be useful when
it is necessary to access some functionality that is not exposed by JuMP. When you are ready to optimize,
simply call solve
in the normal fashion.