Hi all,
I have got at JuMP.jl problem. I have the following discrete optimization problem:
using JuMP, Cbc
function best_bundle(;m=10,b=2)
srand(1)
# m = courses available
k = 5 # courses to take
price = collect(linspace(0.1,1,m))
prefs = collect(linspace(0.1,1,m))
println("price = $price")
println("prefs = $prefs")
model = Model(solver=CbcSolver())
@variable(model, x[1:m] >= 0, Bin)
# objective: sum of preferences
@objective(model, Max, dot(prefs,x) )
# constraint: cost is less than budget
@constraint(model, dot(price,x) <= b )
# can only choose k courses out of all m
@constraint(model, sum(x) <= k )
status =solve(model)
println("Objective is: ", getobjectivevalue(model))
println("Solution is:")
println("find(x) = $(find(getvalue(x)))")
println("total cost = $(dot(price,getvalue(x)))")
end
julia> best_bundle(b=3)
price = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]
prefs = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]
Objective is: 3.0
Solution is:
find(x) = [2, 4, 6, 8, 10]
total cost = 3.0
I need to run call this function for many different values of b
, and eventually for many different versions of prefs
, but that should be equivalent. I was wondering if there is a way to set up the problem once in the beginning, parameterized by b
, which would allow evaluating any given problem best_bundle(b)
faster (without having to do the setup tasks of a standard JuMP problem). thanks!