SDDP:MethodError: no method matching *(::Float64, ::SDDP.State{VariableRef})

Hi,
If I write

using SDDP,Gurobi
a=2*ones(10)
a[1]=3
a[2]=4
ini_a=ones(10);
c_a=ones(10);
model = SDDP.LinearPolicyGraph(
    stages = 3,
    sense = :Min,
    lower_bound = 0.0,
    optimizer = Gurobi.Optimizer,
) do sp, node
    @variable(sp,0<=a_v[i in 1:10]<=a[i],SDDP.State,initial_value=ini_a[i])  
    @stageobjective(sp,sum(c_a[i].out*a_v[i] for i=1:10))
end

Why it tells me
MethodError: no method matching *(::Float64, ::SDDP.State{VariableRef})
Thanks in advance.

It should be

@stageobjective(sp,sum(c_a[i] * a_v[i].out for i=1:10))

a_v is the state variable, not c_a.

1 Like

Thanks a lot, I make a mistake,it shoult be as follows, but the same has a error,

using SDDP,Gurobi
a=2*ones(10)
a[1]=3
a[2]=4
ini_a=ones(10);
c_a=ones(10);
c_u=[4 3 7 5 1 7];
model = SDDP.LinearPolicyGraph(
    stages = 3,
    sense = :Min,
    lower_bound = 0.0,
    optimizer = Gurobi.Optimizer,
) do sp, node
    @variable(sp,0<=a_v[i in 1:10]<=a[i],SDDP.State,initial_value=ini_a[i])
    @variable(sp,0<=u[1:6]<=1,Bin)
    @stageobjective(sp,sum(c_a[i]*a_v[i].out for i=1:10)+c_u*u)
end

MethodError: no method matching zero(::Type{Array{GenericAffExpr{Float64,VariableRef},1}})

This is a bit of a confusing error, but c_u * u is a 1x1 matrix.

julia> c_u * rand(6)
1-element Vector{Float64}:
 12.597665667602827

SDDP.jl doesn’t know how to deal with stage-objectives that are vector-valued.

Make c_u a vector first, then do c_u' * u.

using SDDP
a = fill(2.0, 10)
a[1] = 3.0
a[2] = 4.0
ini_a = ones(10)
c_a = ones(10)
c_u = [4, 3, 7, 5, 1, 7]
model = SDDP.LinearPolicyGraph(
    stages = 3,
    sense = :Min,
    lower_bound = 0.0,
) do sp, node
    @variable(sp, 0 <= a_v[i=1:10] <= a[i], SDDP.State, initial_value = ini_a[i])
    @variable(sp, 0 <= u[1:6] <= 1, Bin)
    @stageobjective(sp, sum(c_a[i] * a_v[i].out for i=1:10) + c_u' * u)
end

Edit: I’ve opened an issue to improve things: Sanitize stageobjective macro · Issue #404 · odow/SDDP.jl · GitHub

1 Like

Thank you very much!

1 Like