Closed SerenaZwww closed 6 months ago
There is a bug in your parameterize
call. It is on the 74th line. But that's not very helpful. So save this as a script and run include("my_script.jl")
and it will give you a proper line number.
Thanks for your reply! I have modified something, and now I think there is a bug in JuMP.fix
:
MethodError: no method matching fix(::JuMP.Containers.DenseAxisArray{VariableRef, 3, Tuple{Base.OneTo{Int64}, UnitRange{Int64}, UnitRange{Int64}}, Tuple{JuMP.Containers._AxisLookup{Base.OneTo{Int64}}, JuMP.Containers._AxisLookup{Tuple{Int64, Int64}}, JuMP.Containers._AxisLookup{Tuple{Int64, Int64}}}}, ::JuMP.Containers.DenseAxisArray{DiscreteNonParametric{Int64, Float64, UnitRange{Int64}, Vector{Float64}}, 3, Tuple{Base.OneTo{Int64}, UnitRange{Int64}, UnitRange{Int64}}, Tuple{JuMP.Containers._AxisLookup{Base.OneTo{Int64}}, JuMP.Containers._AxisLookup{Tuple{Int64, Int64}}, JuMP.Containers._AxisLookup{Tuple{Int64, Int64}}}})
And my code is:
prob = Vector{Vector{Vector{Float64}}}([
[
[0.7, 0.2, 0.1],
[0.9, 0.1],
[1.0]
],
[
[0.5, 0.4, 0.1],
[0.7, 0.3],
[1.0]
],
[
[0.1, 0.7, 0.2],
[0.5, 0.5],
[1.0]
]
])
@variable(subproblem, d[i in 1:14, b in 0:2, l in 0:2])
@variable(subproblem, u[i in 1:14, b in 0:2, l in 0:2, l_ in 0:2])
N = 20
Ω = [
Containers.@container([i in 1:14, b in 0:2, l in 0:2], DiscreteNonParametric(l:2, prob[b + 1][l + 1]))
for _ in 1:N
]
P = fill(1 / N, N)
SDDP.parameterize(subproblem, Ω, P) do ω
JuMP.fix(d, ω) # Note the fix.(
for i in 1:14, b in 0:2, l in 0:2, l_ in 0:2
u[i, b, l, l_] = d[i, b, l] == l_ ? 1 : 0
end
return
end
Note that the distribution which $d$ follows is not related to $i$. I don't know if the error happened here.
The MethodError is a different error.
JuMP.fix(d, ω) # Note the fix.(
You need JuMP.fix.(d, ω)
See https://jump.dev/JuMP.jl/stable/tutorials/getting_started/getting_started_with_julia/#Broadcasting
Sorry, I didn't notice the comment. Now I change it to fix.(
and another error occurred:
MethodError: no method matching fix(::VariableRef, ::DiscreteNonParametric{Int64, Float64, UnitRange{Int64}, Vector{Float64}})
Now you are trying to fix a variable to a value of type DiscreteNonParametric{Int64, Float64, UnitRange{Int64}, Vector{Float64}}
.
You must instead fix the variable to a Float64
.
You probably want (I didn't test) a call to rand
in:
Ω = [
Containers.@container(
[i in 1:14, b in 0:2, l in 0:2],
rand(DiscreteNonParametric(l:2, prob[b + 1][l + 1]))
) for _ in 1:N
]
Thanks for your reply! A call to rand
works! But now I have a new problem:
MethodError: Cannot `convert` an object of type Float64 to an object of type VariableRef
So I change
@variable(subproblem, u[i in 1:14, b in 0:2, l in 0:2, l_ in 0:2])
to
u = Array{Float64}(undef, 14, 3, 3, 3)
And the above error disappears. Yet there's some NaN
in $u$, leading to error Invalid coefficient NaN on variable sa[1,0,0].
. I think each element in $u$ is well-defined, and I can't figure out why. I've attached the complete code below.
code_20240217.zip
I think here u[i, b, l, l_] = d[i, b, l] == l_ ? 1 : 0
you are also trying to fix a decision variable to a value?
You need instead fix(u[i, b, l, l_], d[i, b, l] == l_ ? 1 : 0)
Closing because the discussion seems to have come to an end. Please comment below if you have other questions and I will re-open.
I think I have a model now, because when I run:
I get:
Now I'm trying to train a policy. There's an error message saying "i not defined". I don't know where the error is.