Julia Optimization Guide — JuMP, Optim, and Mathematical Programming
In this tutorial, you will learn about Julia Optimization Guide. We cover key concepts, practical examples, and best practices to help you master this topic.
Julia optimization offers JuMP for declarative mathematical modeling (linear, mixed-integer, nonlinear, conic), Optim.jl for local unconstrained optimization (BFGS, Nelder-Mead), and access to high-performance solvers like GLPK, Ipopt, Gurobi, and Mosek.
Linear Programming with JuMP
using JuMP
using GLPK
# Create model
model = Model(GLPK.Optimizer)
# Variables
@variable(model, x >= 0)
@variable(model, y >= 0)
# Objective: maximize 3x + 2y
@objective(model, Max, 3x + 2y)
# Constraints
@constraint(model, x + y <= 4)
@constraint(model, 2x + y <= 5)
# Solve
optimize!(model)
# Results
println("x = ", value(x)) # 1.0
println("y = ", value(y)) # 3.0
println("z = ", objective_value(model)) # 7.0
Mixed-Integer Programming
using JuMP
using Cbc
model = Model(Cbc.Optimizer)
# Integer variables
@variable(model, x >= 0, Int)
@variable(model, y >= 0, Int)
@objective(model, Max, 5x + 3y)
@constraint(model, 2x + y <= 10)
@constraint(model, x + 2y <= 8)
optimize!(model)
println("Optimal: x=$(value(x)), y=$(value(y))")
Unconstrained Optimization
using Optim
# Rosenbrock function
f(x) = (1.0 - x[1])^2 + 100.0 * (x[2] - x[1]^2)^2
# BFGS
result = optimize(f, [0.0, 0.0], BFGS())
println(result.minimizer, result.minimum)
# Nelder-Mead (derivative-free)
result = optimize(f, [0.0, 0.0], NelderMead())
# With gradient
function fg!(F, G, x)
if F !== nothing
F[1] = (1.0 - x[1])^2 + 100.0 * (x[2] - x[1]^2)^2
end
if G !== nothing
G[1] = -2.0 * (1.0 - x[1]) - 400.0 * (x[2] - x[1]^2) * x[1]
G[2] = 200.0 * (x[2] - x[1]^2)
end
end
result = optimize(fg!, [0.0, 0.0], LBFGS())
Nonlinear Programming
using JuMP
using Ipopt
model = Model(Ipopt.Optimizer)
@variable(model, x)
@variable(model, y)
@NLobjective(model, Min, (x - 1)^2 + (y - 2)^2)
@NLconstraint(model, x^2 + y^2 <= 9)
optimize!(model)
println("Minimum: x=$(value(x)), y=$(value(y))")
Common Mistakes
1. Wrong solver for problem type
Not all solvers handle all problem types. GLPK = LP/MILP, Ipopt = NLP, Gurobi = most types. Check documentation.
2. Integer variables without integer solver
Use Int variable type and an integer-capable solver (Cbc, GLPK, Gurobi). Continuous solvers (Ipopt) ignore integrality.
3. Not setting solver options
Solver time limits, tolerances, and output can be configured: set_optimizer_attribute(model, "time_limit", 60.0).
Practice Questions
1. How do you define a linear program in JuMP? Create Model with a solver, add @variable, @objective, @constraint, then optimize!.
2. What is the difference between Optim and JuMP? Optim is for local unconstrained optimization. JuMP is for constrained mathematical programming with multiple solver backends.
3. How do you add integer constraints?
Use @variable(model, x >= 0, Int) for integer, Bin for binary.
FAQ
{{< faq question="What solvers does JuMP support?" >}} GLPK (free), Cbc (free), Ipopt (free), Gurobi (commercial), Mosek (commercial), CPLEX (commercial), and many more. {{< /faq >}}
{{< faq question="Can I solve quadratic programs?" >}}
Yes. JuMP supports QP and QCQP. Use @objective(model, Min, x'*Q*x + c'*x) syntax.
{{< /faq >}}
{{< faq question="How do I get dual values?" >}}
dual(constraint_ref) returns the shadow price. reduced_cost(variable_ref) for reduced costs.
{{< /faq >}}
What's Next
Now learn about time series analysis.
| Topic | Description | Link |
|---|---|---|
| Time Series | Time series analysis | {{< ref "26-time-series" >}} |
| Differential Equations | Solving ODEs and PDEs | {{< ref "27-diff-eq" >}} |
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro