1
votes

I'm relatively new to Julia and have been trying to learn it. So I've come across an example of unit commitment problem; however, it doesn't work for me, since I'm getting this error:

    LoadError: MethodError: no method matching 
    value(::Array{VariableRef,1})
    Closest candidates are:
    value(!Matched::NonlinearExpression) at ***\packages\JuMP\jnmGG\src\nlp.jl:1126
    value(!Matched::NonlinearParameter) at ***\packages\JuMP\jnmGG\src\nlp.jl:125
    value(!Matched::VariableRef) at ***\packages\JuMP\jnmGG\src\variables.jl:721
    ...
in expression starting at untitled-c2a2b8253aafb31b0a191c03db8d0489:41
solve_uc(::Array{Int64,1}, ::Array{Int64,1}, ::Array{Int64,1}, ::Int64, 
::Int64, ::Int64) at untitled-c2a2b8253aafb31b0a191c03db8d0489:38
top-level scope at none:0

The code itself is here (I've taken it from the tutorial):

    using JuMP 
    using GLPK
    using MathOptInterface
    const MOI = MathOptInterface
    using Interact

    const g_max = [1000,1000];
    const g_min = [0,300];
    const c_g = [50,100];
    const c_g0 = [1000,0]
    const c_w = 50;
    const d = 1500;
    const w_f = 200;

    function solve_uc(g_max, g_min, c_g, c_w, d, w_f)
uc=Model(with_optimizer(GLPK.Optimizer)) 

@variable(uc, 0 <= g[i=1:2] <= g_max[i]) 
@variable(uc, u[i=1:2], Bin)
@variable(uc, 0 <= w  <= w_f )

@objective(uc,Min,sum(c_g[i] * g[i] for i in 1:2) + c_w * w)


for i in 1:2
    @constraint(uc,  g[i] <= g_max[i] * u[i]) 
    @constraint(uc,  g[i] >= g_min[i] * u[i]) 
end

@constraint(uc, w <= w_f)

    @constraint(uc, sum(g[i] for i in 1:2) + w == d)

    status = optimize!(myModel)


    return status, value(g), value(w), w_f-value(w), value(u), objective_value(uc)
end

    status,g_opt,w_opt,ws_opt,u_opt,obj=solve_uc(g_max, g_min, c_g, c_w, d, w_f);

Thank you.

1

1 Answers

0
votes

Welcome to SO!

As g and u are arrays of variables, you need to broadcast the call to value over g and u with a single dot (.) after the function name. Changing

return status, value(g), value(w), w_f-value(w), value(u), objective_value(uc)

to

return status, value.(g), value(w), w_f-value(w), value.(u), objective_value(uc)

should fix the error.

Note that this is a common style in Julia. The functions are generally written only for single elements and to apply the functions to an array (or in general to a collection) of elements, you can easily broadcast the call over the array with the dot-syntax. (i.e. f.(A)).

Although appears not relevant to the error you get, the line status = optimize!(myModel) refers to the variable myModel which is not defined in your function's scope. You should probably change it to status = optimize!(uc).