A basic example

This example uses the following packages:

using JuMP
import GLPK

Build the model:

model = Model(GLPK.Optimizer)

@variable(model, 0 <= x <= 2)
@variable(model, 0 <= y <= 30)

@objective(model, Max, 5x + 3y)

@constraint(model, 1x + 5y <= 3.0)

print(model)
Max 5 x + 3 y
Subject to
 x + 5 y ≤ 3.0
 x ≥ 0.0
 y ≥ 0.0
 x ≤ 2.0
 y ≤ 30.0

Optimize the model:

optimize!(model)

Check the termination and primal status to see if we have a solution:

println("Termination status : ", termination_status(model))
println("Primal status      : ", primal_status(model))
Termination status : OPTIMAL
Primal status      : FEASIBLE_POINT

Print the solution:

obj_value = objective_value(model)
x_value = value(x)
y_value = value(y)

println("Objective value : ", obj_value)
println("x value         : ", x_value)
println("y value         : ", y_value)
Objective value : 10.6
x value         : 2.0
y value         : 0.2

View this file on Github.


This page was generated using Literate.jl.