odow / SDDP.jl

A JuMP extension for Stochastic Dual Dynamic Programming
https://sddp.dev
Other
309 stars 61 forks source link

SDDP.OutOfSampleMonteCarlo #321

Closed Esnilg closed 4 years ago

Esnilg commented 4 years ago

Hi Oscar

I am trying to run an OutOfSampleMonteCarlo simulation and I have the following error:

ERROR: MethodError: Cannot `convert` an object of type Tuple{Array{SDDP.Noise{Tuple{Int64,Float64}},1},Array{SDDP.Noise{NamedTuple{(:ELECTRICITY, :LIGHTING, :HEAT_HIGH_T, :HEAT_LOW_T_SH, :HEAT_LOW_T_HW, :PASSENGER, :FREIGHT, :C_COAL, :C_NG, :C_ELECT, :C_GASOLINE, :C_DIESEL, :C_LFO, :C_URANIUM, :C_WOOD),NTuple{15,Float64}}},1}} to an object of type Array{SDDP.Noise,1}
Closest candidates are:
  convert(::Type{Array{S,N}}, ::PooledArrays.PooledArray{T,R,N,RA} where RA) where {S, T, R, N} at C:\Users\esnil\.julia\packages\PooledArrays\yiLq3\src\PooledArrays.jl:294
  convert(::Type{Array{T,N}}, ::FillArrays.Zeros{V,N,Axes} where Axes) where {T, V, N} at C:\Users\esnil\.julia\packages\FillArrays\UptEJ\src\FillArrays.jl:339
  convert(::Type{Array{T,N}}, ::FillArrays.Ones{V,N,Axes} where Axes) where {T, V, N} at C:\Users\esnil\.julia\packages\FillArrays\UptEJ\src\FillArrays.jl:339
  ...
Stacktrace:
 [1] SDDP.OutOfSampleMonteCarlo(::var"#813#818", ::SDDP.PolicyGraph{Tuple{Int64,Float64}}; use_insample_transition::Bool, max_depth::Int64, terminate_on_cycle::Bool, terminate_on_dummy_leaf::Bool, rollout_limit::SDDP.var"#83#86") at C:\Users\esnil\.julia\packages\SDDP\tCaKk\src\plugins\sampling_schemes.jl:159
 [2] top-level scope at REPL[739]:1

I'm doing the same thing that you do in https://odow.github.io/SDDP.jl/latest/guides/simulate_using_a_different_sampling_scheme/

the code:

sampling_scheme = SDDP.OutOfSampleMonteCarlo(
    model, use_insample_transition=true
) do node
stage, markov_state = node
    if stage == 0
        # Called from the root node. Transition to (1, 1) with probability 1.0.
        # Only return the list of children, _not_ a list of noise terms.
        return [SDDP.Noise((1, markov_state), 1.0)]
    elseif stage == nb_periods
        # Called from the final node. Return an empty list for the children,
        # and a single, deterministic realization for the noise terms.
        children = SDDP.Noise[]
        noise_terms = [SDDP.Noise((ELECTRICITY=rand(Uniform(quantile(betas_struc.ncenarios[:ELECTRICITY,stage],0),quantile(betas_struc.ncenarios[:ELECTRICITY,stage],1)),1)[1], LIGHTING=rand(Uniform(quantile(betas_struc.ncenarios[:LIGHTING,stage],0),quantile(betas_struc.ncenarios[:LIGHTING,stage],1)),1)[1], HEAT_HIGH_T=rand(Uniform(quantile(betas_struc.ncenarios[:HEAT_HIGH_T,stage],0),quantile(betas_struc.ncenarios[:HEAT_HIGH_T,stage],1)),1)[1], HEAT_LOW_T_SH=rand(Uniform(quantile(M_end_uses_input[:HEAT_LOW_T_SH,stage-1],0),quantile(M_end_uses_input[:HEAT_LOW_T_SH,stage-1],1)),1)[1], HEAT_LOW_T_HW=rand(Uniform(quantile(M_end_uses_input[:HEAT_LOW_T_HW,stage-1],0),quantile(M_end_uses_input[:HEAT_LOW_T_HW,stage-1],1)),1)[1], PASSENGER=rand(Uniform(quantile(M_end_uses_input[:MOBILITY_PASSENGER,stage-1],0),quantile(M_end_uses_input[:MOBILITY_PASSENGER,stage-1],1)),1)[1], FREIGHT=rand(Uniform(quantile(M_end_uses_input[:MOBILITY_FREIGHT,stage-1],0),quantile(M_end_uses_input[:MOBILITY_FREIGHT,stage-1],1)),1)[1],C_COAL =rand(Uniform(quantile(Costo_struc.ncenarios[:COAL,stage],0),quantile(Costo_struc.ncenarios[:COAL,stage],1)),1)[1],C_NG = markov_state,C_ELECT =rand(Uniform(quantile(Costo_struc.ncenarios[:ELECTRICITY,stage],0),quantile(Costo_struc.ncenarios[:ELECTRICITY,stage],1)),1)[1],C_GASOLINE =rand(Uniform(quantile(Costo_struc.ncenarios[:GASOLINE,stage],0),quantile(Costo_struc.ncenarios[:GASOLINE,stage],1)),1)[1],C_DIESEL =rand(Uniform(quantile(Costo_struc.ncenarios[:DIESEL,stage],0),quantile(Costo_struc.ncenarios[:DIESEL,stage],1)),1)[1],C_LFO =rand(Uniform(quantile(Costo_struc.ncenarios[:LFO,stage],0),quantile(Costo_struc.ncenarios[:LFO,stage],1)),1)[1],C_URANIUM =rand(Uniform(quantile(Costo_struc.ncenarios[:URANIUM,stage],0),quantile(Costo_struc.ncenarios[:URANIUM,stage],1)),1)[1],C_WOOD =rand(Uniform(quantile(Costo_struc.ncenarios[:WOOD,stage],0),quantile(Costo_struc.ncenarios[:WOOD,stage],1)),1)[1]), 1.0)]
        return children, noise_terms
    elseif stage == 1
        probability = 1.0
        supports = [(ELECTRICITY=rand(Uniform(quantile(betas_struc.ncenarios[:ELECTRICITY,stage],0),quantile(betas_struc.ncenarios[:ELECTRICITY,stage],1)),1)[1], LIGHTING=rand(Uniform(quantile(betas_struc.ncenarios[:LIGHTING,stage],0),quantile(betas_struc.ncenarios[:LIGHTING,stage],1)),1)[1], HEAT_HIGH_T=rand(Uniform(quantile(betas_struc.ncenarios[:HEAT_HIGH_T,stage],0),quantile(betas_struc.ncenarios[:HEAT_HIGH_T,stage],1)),1)[1], HEAT_LOW_T_SH=end_uses_input_0[:HEAT_LOW_T_SH], HEAT_LOW_T_HW=end_uses_input_0[:HEAT_LOW_T_HW], PASSENGER=end_uses_input_0[:MOBILITY_PASSENGER], FREIGHT=end_uses_input_0[:MOBILITY_FREIGHT],C_COAL =rand(Uniform(quantile(Costo_struc.ncenarios[:COAL,stage],0),quantile(Costo_struc.ncenarios[:COAL,stage],1)),1)[1],C_NG = markov_state,C_ELECT =rand(Uniform(quantile(Costo_struc.ncenarios[:ELECTRICITY,stage],0),quantile(Costo_struc.ncenarios[:ELECTRICITY,stage],1)),1)[1],C_GASOLINE =rand(Uniform(quantile(Costo_struc.ncenarios[:GASOLINE,stage],0),quantile(Costo_struc.ncenarios[:GASOLINE,stage],1)),1)[1],C_DIESEL =rand(Uniform(quantile(Costo_struc.ncenarios[:DIESEL,stage],0),quantile(Costo_struc.ncenarios[:DIESEL,stage],1)),1)[1],C_LFO =rand(Uniform(quantile(Costo_struc.ncenarios[:LFO,stage],0),quantile(Costo_struc.ncenarios[:LFO,stage],1)),1)[1],C_URANIUM =rand(Uniform(quantile(Costo_struc.ncenarios[:URANIUM,stage],0),quantile(Costo_struc.ncenarios[:URANIUM,stage],1)),1)[1],C_WOOD =rand(Uniform(quantile(Costo_struc.ncenarios[:WOOD,stage],0),quantile(Costo_struc.ncenarios[:WOOD,stage],1)),1)[1])]
        noise_terms = [SDDP.Noise(w, p) for (w, p) in zip(supports, probability)]
        children = [SDDP.Noise((stage + 1, 0.039454180273178006 ), 1.0)]
        return children, noise_terms
    elseif stage == 2
        probability = 1.0
        supports = [(ELECTRICITY=rand(Uniform(quantile(betas_struc.ncenarios[:ELECTRICITY,stage],0),quantile(betas_struc.ncenarios[:ELECTRICITY,stage],1)),1)[1], LIGHTING=rand(Uniform(quantile(betas_struc.ncenarios[:LIGHTING,stage],0),quantile(betas_struc.ncenarios[:LIGHTING,stage],1)),1)[1], HEAT_HIGH_T=rand(Uniform(quantile(betas_struc.ncenarios[:HEAT_HIGH_T,stage],0),quantile(betas_struc.ncenarios[:HEAT_HIGH_T,stage],1)),1)[1], HEAT_LOW_T_SH=rand(Uniform(quantile(M_end_uses_input[:HEAT_LOW_T_SH,stage-1],0),quantile(M_end_uses_input[:HEAT_LOW_T_SH,stage-1],1)),1)[1], HEAT_LOW_T_HW=rand(Uniform(quantile(M_end_uses_input[:HEAT_LOW_T_HW,stage-1],0),quantile(M_end_uses_input[:HEAT_LOW_T_HW,stage-1],1)),1)[1], PASSENGER=rand(Uniform(quantile(M_end_uses_input[:MOBILITY_PASSENGER,stage-1],0),quantile(M_end_uses_input[:MOBILITY_PASSENGER,stage-1],1)),1)[1], FREIGHT=rand(Uniform(quantile(M_end_uses_input[:MOBILITY_FREIGHT,stage-1],0),quantile(M_end_uses_input[:MOBILITY_FREIGHT,stage-1],1)),1)[1],C_COAL =rand(Uniform(quantile(Costo_struc.ncenarios[:COAL,stage],0),quantile(Costo_struc.ncenarios[:COAL,stage],1)),1)[1],C_NG = markov_state,C_ELECT =rand(Uniform(quantile(Costo_struc.ncenarios[:ELECTRICITY,stage],0),quantile(Costo_struc.ncenarios[:ELECTRICITY,stage],1)),1)[1],C_GASOLINE =rand(Uniform(quantile(Costo_struc.ncenarios[:GASOLINE,stage],0),quantile(Costo_struc.ncenarios[:GASOLINE,stage],1)),1)[1],C_DIESEL =rand(Uniform(quantile(Costo_struc.ncenarios[:DIESEL,stage],0),quantile(Costo_struc.ncenarios[:DIESEL,stage],1)),1)[1],C_LFO =rand(Uniform(quantile(Costo_struc.ncenarios[:LFO,stage],0),quantile(Costo_struc.ncenarios[:LFO,stage],1)),1)[1],C_URANIUM =rand(Uniform(quantile(Costo_struc.ncenarios[:URANIUM,stage],0),quantile(Costo_struc.ncenarios[:URANIUM,stage],1)),1)[1],C_WOOD =rand(Uniform(quantile(Costo_struc.ncenarios[:WOOD,stage],0),quantile(Costo_struc.ncenarios[:WOOD,stage],1)),1)[1])]
        noise_terms = [SDDP.Noise(w, p) for (w, p) in zip(supports, probability)]
        children = [SDDP.Noise((stage + 1, 0.041970576670158674 ), 0.5), SDDP.Noise((stage + 1, 0.0530710247925964 ), 0.5)]
        return children, noise_terms
    elseif stage == 3
        probability = 1.0
        supports = [(ELECTRICITY=rand(Uniform(quantile(betas_struc.ncenarios[:ELECTRICITY,stage],0),quantile(betas_struc.ncenarios[:ELECTRICITY,stage],1)),1)[1], LIGHTING=rand(Uniform(quantile(betas_struc.ncenarios[:LIGHTING,stage],0),quantile(betas_struc.ncenarios[:LIGHTING,stage],1)),1)[1], HEAT_HIGH_T=rand(Uniform(quantile(betas_struc.ncenarios[:HEAT_HIGH_T,stage],0),quantile(betas_struc.ncenarios[:HEAT_HIGH_T,stage],1)),1)[1], HEAT_LOW_T_SH=rand(Uniform(quantile(M_end_uses_input[:HEAT_LOW_T_SH,stage-1],0),quantile(M_end_uses_input[:HEAT_LOW_T_SH,stage-1],1)),1)[1], HEAT_LOW_T_HW=rand(Uniform(quantile(M_end_uses_input[:HEAT_LOW_T_HW,stage-1],0),quantile(M_end_uses_input[:HEAT_LOW_T_HW,stage-1],1)),1)[1], PASSENGER=rand(Uniform(quantile(M_end_uses_input[:MOBILITY_PASSENGER,stage-1],0),quantile(M_end_uses_input[:MOBILITY_PASSENGER,stage-1],1)),1)[1], FREIGHT=rand(Uniform(quantile(M_end_uses_input[:MOBILITY_FREIGHT,stage-1],0),quantile(M_end_uses_input[:MOBILITY_FREIGHT,stage-1],1)),1)[1],C_COAL =rand(Uniform(quantile(Costo_struc.ncenarios[:COAL,stage],0),quantile(Costo_struc.ncenarios[:COAL,stage],1)),1)[1],C_NG = markov_state,C_ELECT =rand(Uniform(quantile(Costo_struc.ncenarios[:ELECTRICITY,stage],0),quantile(Costo_struc.ncenarios[:ELECTRICITY,stage],1)),1)[1],C_GASOLINE =rand(Uniform(quantile(Costo_struc.ncenarios[:GASOLINE,stage],0),quantile(Costo_struc.ncenarios[:GASOLINE,stage],1)),1)[1],C_DIESEL =rand(Uniform(quantile(Costo_struc.ncenarios[:DIESEL,stage],0),quantile(Costo_struc.ncenarios[:DIESEL,stage],1)),1)[1],C_LFO =rand(Uniform(quantile(Costo_struc.ncenarios[:LFO,stage],0),quantile(Costo_struc.ncenarios[:LFO,stage],1)),1)[1],C_URANIUM =rand(Uniform(quantile(Costo_struc.ncenarios[:URANIUM,stage],0),quantile(Costo_struc.ncenarios[:URANIUM,stage],1)),1)[1],C_WOOD =rand(Uniform(quantile(Costo_struc.ncenarios[:WOOD,stage],0),quantile(Costo_struc.ncenarios[:WOOD,stage],1)),1)[1])]
        noise_terms = [SDDP.Noise(w, p) for (w, p) in zip(supports, probability)]
        children = [SDDP.Noise((stage + 1, 0.0680123090333957 ), 0.35), SDDP.Noise((stage + 1, 0.07632227925138585 ), 0.35), SDDP.Noise((stage + 1, 0.0763461245130928 ), 0.30)]
        return children, noise_terms
    elseif stage == 4
        probability = 1.0
        supports = [(ELECTRICITY=rand(Uniform(quantile(betas_struc.ncenarios[:ELECTRICITY,stage],0),quantile(betas_struc.ncenarios[:ELECTRICITY,stage],1)),1)[1], LIGHTING=rand(Uniform(quantile(betas_struc.ncenarios[:LIGHTING,stage],0),quantile(betas_struc.ncenarios[:LIGHTING,stage],1)),1)[1], HEAT_HIGH_T=rand(Uniform(quantile(betas_struc.ncenarios[:HEAT_HIGH_T,stage],0),quantile(betas_struc.ncenarios[:HEAT_HIGH_T,stage],1)),1)[1], HEAT_LOW_T_SH=rand(Uniform(quantile(M_end_uses_input[:HEAT_LOW_T_SH,stage-1],0),quantile(M_end_uses_input[:HEAT_LOW_T_SH,stage-1],1)),1)[1], HEAT_LOW_T_HW=rand(Uniform(quantile(M_end_uses_input[:HEAT_LOW_T_HW,stage-1],0),quantile(M_end_uses_input[:HEAT_LOW_T_HW,stage-1],1)),1)[1], PASSENGER=rand(Uniform(quantile(M_end_uses_input[:MOBILITY_PASSENGER,stage-1],0),quantile(M_end_uses_input[:MOBILITY_PASSENGER,stage-1],1)),1)[1], FREIGHT=rand(Uniform(quantile(M_end_uses_input[:MOBILITY_FREIGHT,stage-1],0),quantile(M_end_uses_input[:MOBILITY_FREIGHT,stage-1],1)),1)[1],C_COAL =rand(Uniform(quantile(Costo_struc.ncenarios[:COAL,stage],0),quantile(Costo_struc.ncenarios[:COAL,stage],1)),1)[1],C_NG = markov_state,C_ELECT =rand(Uniform(quantile(Costo_struc.ncenarios[:ELECTRICITY,stage],0),quantile(Costo_struc.ncenarios[:ELECTRICITY,stage],1)),1)[1],C_GASOLINE =rand(Uniform(quantile(Costo_struc.ncenarios[:GASOLINE,stage],0),quantile(Costo_struc.ncenarios[:GASOLINE,stage],1)),1)[1],C_DIESEL =rand(Uniform(quantile(Costo_struc.ncenarios[:DIESEL,stage],0),quantile(Costo_struc.ncenarios[:DIESEL,stage],1)),1)[1],C_LFO =rand(Uniform(quantile(Costo_struc.ncenarios[:LFO,stage],0),quantile(Costo_struc.ncenarios[:LFO,stage],1)),1)[1],C_URANIUM =rand(Uniform(quantile(Costo_struc.ncenarios[:URANIUM,stage],0),quantile(Costo_struc.ncenarios[:URANIUM,stage],1)),1)[1],C_WOOD =rand(Uniform(quantile(Costo_struc.ncenarios[:WOOD,stage],0),quantile(Costo_struc.ncenarios[:WOOD,stage],1)),1)[1])]
        noise_terms = [SDDP.Noise(w, p) for (w, p) in zip(supports, probability)]
        children = [SDDP.Noise((stage + 1, 0.08352538879006337 ), 0.35), SDDP.Noise((stage + 1, 0.08468846243792953 ), 0.35), SDDP.Noise((stage + 1, 0.09124435888686733 ), 0.30)]
        return children, noise_terms
    end
end

Using this one it work

sampling_scheme = SDDP.OutOfSampleMonteCarlo(
    model, use_insample_transition = true
) do node
stage, markov_state = node
        probability = 1.0
        if stage == 1
            supports = [(ELECTRICITY=rand(Uniform(quantile(betas_struc.ncenarios[:ELECTRICITY,stage],0),quantile(betas_struc.ncenarios[:ELECTRICITY,stage],1)),1)[1], LIGHTING=rand(Uniform(quantile(betas_struc.ncenarios[:LIGHTING,stage],0),quantile(betas_struc.ncenarios[:LIGHTING,stage],1)),1)[1], HEAT_HIGH_T=rand(Uniform(quantile(betas_struc.ncenarios[:HEAT_HIGH_T,stage],0),quantile(betas_struc.ncenarios[:HEAT_HIGH_T,stage],1)),1)[1], HEAT_LOW_T_SH=end_uses_input_0[:HEAT_LOW_T_SH], HEAT_LOW_T_HW=end_uses_input_0[:HEAT_LOW_T_HW], PASSENGER=end_uses_input_0[:MOBILITY_PASSENGER], FREIGHT=end_uses_input_0[:MOBILITY_FREIGHT],C_COAL =rand(Uniform(quantile(Costo_struc.ncenarios[:COAL,stage],0),quantile(Costo_struc.ncenarios[:COAL,stage],1)),1)[1],C_NG = markov_state,C_ELECT =rand(Uniform(quantile(Costo_struc.ncenarios[:ELECTRICITY,stage],0),quantile(Costo_struc.ncenarios[:ELECTRICITY,stage],1)),1)[1],C_GASOLINE =rand(Uniform(quantile(Costo_struc.ncenarios[:GASOLINE,stage],0),quantile(Costo_struc.ncenarios[:GASOLINE,stage],1)),1)[1],C_DIESEL =rand(Uniform(quantile(Costo_struc.ncenarios[:DIESEL,stage],0),quantile(Costo_struc.ncenarios[:DIESEL,stage],1)),1)[1],C_LFO =rand(Uniform(quantile(Costo_struc.ncenarios[:LFO,stage],0),quantile(Costo_struc.ncenarios[:LFO,stage],1)),1)[1],C_URANIUM =rand(Uniform(quantile(Costo_struc.ncenarios[:URANIUM,stage],0),quantile(Costo_struc.ncenarios[:URANIUM,stage],1)),1)[1],C_WOOD =rand(Uniform(quantile(Costo_struc.ncenarios[:WOOD,stage],0),quantile(Costo_struc.ncenarios[:WOOD,stage],1)),1)[1])]
        else
            supports = [(ELECTRICITY=rand(Uniform(quantile(betas_struc.ncenarios[:ELECTRICITY,stage],0),quantile(betas_struc.ncenarios[:ELECTRICITY,stage],1)),1)[1], LIGHTING=rand(Uniform(quantile(betas_struc.ncenarios[:LIGHTING,stage],0),quantile(betas_struc.ncenarios[:LIGHTING,stage],1)),1)[1], HEAT_HIGH_T=rand(Uniform(quantile(betas_struc.ncenarios[:HEAT_HIGH_T,stage],0),quantile(betas_struc.ncenarios[:HEAT_HIGH_T,stage],1)),1)[1], HEAT_LOW_T_SH=rand(Uniform(quantile(M_end_uses_input[:HEAT_LOW_T_SH,stage-1],0),quantile(M_end_uses_input[:HEAT_LOW_T_SH,stage-1],1)),1)[1], HEAT_LOW_T_HW=rand(Uniform(quantile(M_end_uses_input[:HEAT_LOW_T_HW,stage-1],0),quantile(M_end_uses_input[:HEAT_LOW_T_HW,stage-1],1)),1)[1], PASSENGER=rand(Uniform(quantile(M_end_uses_input[:MOBILITY_PASSENGER,stage-1],0),quantile(M_end_uses_input[:MOBILITY_PASSENGER,stage-1],1)),1)[1], FREIGHT=rand(Uniform(quantile(M_end_uses_input[:MOBILITY_FREIGHT,stage-1],0),quantile(M_end_uses_input[:MOBILITY_FREIGHT,stage-1],1)),1)[1],C_COAL =rand(Uniform(quantile(Costo_struc.ncenarios[:COAL,stage],0),quantile(Costo_struc.ncenarios[:COAL,stage],1)),1)[1],C_NG = markov_state,C_ELECT =rand(Uniform(quantile(Costo_struc.ncenarios[:ELECTRICITY,stage],0),quantile(Costo_struc.ncenarios[:ELECTRICITY,stage],1)),1)[1],C_GASOLINE =rand(Uniform(quantile(Costo_struc.ncenarios[:GASOLINE,stage],0),quantile(Costo_struc.ncenarios[:GASOLINE,stage],1)),1)[1],C_DIESEL =rand(Uniform(quantile(Costo_struc.ncenarios[:DIESEL,stage],0),quantile(Costo_struc.ncenarios[:DIESEL,stage],1)),1)[1],C_LFO =rand(Uniform(quantile(Costo_struc.ncenarios[:LFO,stage],0),quantile(Costo_struc.ncenarios[:LFO,stage],1)),1)[1],C_URANIUM =rand(Uniform(quantile(Costo_struc.ncenarios[:URANIUM,stage],0),quantile(Costo_struc.ncenarios[:URANIUM,stage],1)),1)[1],C_WOOD =rand(Uniform(quantile(Costo_struc.ncenarios[:WOOD,stage],0),quantile(Costo_struc.ncenarios[:WOOD,stage],1)),1)[1])]
        end
    return [SDDP.Noise(w, p) for (w, p) in zip(supports, probability)]
end
odow commented 4 years ago

I think your issue is probably that you're passing use_insample_transition=true to the case where you have return children, noise_terms. The code ties to use the in-sample transition matrix, but then you pass a new one and things gets weird.

You can try:

sampling_scheme = SDDP.OutOfSampleMonteCarlo(
    model; use_insample_transition = false
) do node
Esnilg commented 4 years ago

Thank you very much, it was that was wrong, best regards