2
votes

I’m trying to be able to use values put in a Dictionary to weight the variables that are going to be minimized in a JuMP objective function.

The dictionnary is implemented as follow :

Dicopond = Dict(0=> 1, 1 => 2, 2 => 4, 3 => 8, 4 => 16, 5 => 32, 6 => 64, 7 => 128)

The variable :

@variable(model, absolute[y=1:10])

And the objective function :

@objective(model, Min, sum((absolute[y])*Dicopond[(absolute[y])] for y=1:10))

Absolute[y] will only take the values between 0 and 7.

However, I get the Error “LoadError: KeyError: key absolute[1] not found in expression starting at…”

Is there a way to make the dictionnary get the actual value that will be assigned to absolute[y] ?

I’ve tried different ways to be able to put a weight to the decision variable and this was the one that seemed to be the most likely to work.

Thank you for your help!

EDIT : Cross-posted on : https://discourse.julialang.org/t/use-dictionary-value-in-objective-function/59119

1

1 Answers

1
votes

Since your absolute variable for each y=1:10 can take values from 0 to 7. You need to convert it into a binary indicator variable (and hence for each absolute[y] you need to have 8 levels).

Hence your model needs to be rewritten to (node that for each y=1:10 the indicator variable needs to sum to 1):

model = Model(optimizer_with_attributes(Cbc.Optimizer, "Sec"=>480.0))
discopond = Dict(0=> 1, 1 => 2, 2 => 4, 3 => 8, 4 => 16, 5 => 32, 6 => 64, 7 => 128)
@variable(model, absolute[y=1:10, d=0:7],Bin)
@constraint(model,[y=1:10], sum(absolute[y, :])==1)
@objective(model, Min, sum( sum(d*absolute[y,d]*discopond[d] for d in 0:7) for y=1:10))
optimize!(model)