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

**URL:** <https://discourse.julialang.org/t/sddp-no-method-matching-float64-sddp-state-variableref/61666>\
**Category:** Optimization (Mathematical)\
**Created:** [May 23, 2021, 2:58am UTC](https://discourse.julialang.org/t/sddp-no-method-matching-float64-sddp-state-variableref/61666 "2021-05-23T02:58:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![BYS](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bys/32/25384_2.png) [@BYS](https://discourse.julialang.org/u/BYS)\
**Post date:** [May 23, 2021, 2:58am UTC](https://discourse.julialang.org/t/sddp-no-method-matching-float64-sddp-state-variableref/61666/1 "2021-05-23T02:58:48Z")

</div>

Hi,  
If I write

```julia
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.

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [May 23, 2021, 3:31am UTC](https://discourse.julialang.org/t/sddp-no-method-matching-float64-sddp-state-variableref/61666/2 "2021-05-23T03:31:34Z")

</div>

It should be

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

```

`a_v` is the state variable, not `c_a`.

---

<div class="post-metadata">

**Author:** ![BYS](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bys/32/25384_2.png) [@BYS](https://discourse.julialang.org/u/BYS)\
**Post date:** [May 23, 2021, 3:58am UTC](https://discourse.julialang.org/t/sddp-no-method-matching-float64-sddp-state-variableref/61666/3 "2021-05-23T03:58:41Z")

</div>

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

```julia
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}})`

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [May 23, 2021, 4:08am UTC](https://discourse.julialang.org/t/sddp-no-method-matching-float64-sddp-state-variableref/61666/4 "2021-05-23T04:08:45Z")

</div>

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

```nohighlight
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`.

```nohighlight
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](https://github.com/odow/SDDP.jl/issues/404)

---

<div class="post-metadata">

**Author:** ![BYS](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bys/32/25384_2.png) [@BYS](https://discourse.julialang.org/u/BYS)\
**Post date:** [May 23, 2021, 4:13am UTC](https://discourse.julialang.org/t/sddp-no-method-matching-float64-sddp-state-variableref/61666/5 "2021-05-23T04:13:36Z")

</div>

Thank you very much!
