First. Never use eval
. It is always the wrong thing to do.
Either use variable_by_name
, or, if the variable is registered, convert the string to a symbol and do model[Symbol(k)]
:
using JuMP, Ipopt
function foo()
fvars = Dict{String,Float64}("c" => 5)
m = Model(Ipopt.Optimizer)
@variable(m, c)
for (k, v) in fvars
# Option 1
x = variable_by_name(m, k)
fix(x, v; force = true)
# Option 2
x = m[Symbol(k)]
fix(x, v; force = true)
end
end
More generally, consider using some sort of data structure that you control:
using JuMP, Ipopt
function foo()
fvars = Dict{String,Float64}("c" => 5)
my_variables = Dict{String,VariableRef}()
m = Model(Ipopt.Optimizer)
my_variables["c"] = @variable(m, c)
for (k, v) in fvars
fix(my_variables[k], v; force = true)
end
end