This matter is partially discussed here: In Julia using StochasticPrograms.jl package how can I get the @sampler object as a n*m matrix not a vector? But there are significant new issues in this question:
In the julia programming language I use StochasticPrograms.jl package to model a two stage stochastic problem. I use @sampler object to develop scenarios and random values.
I have a two dimensional uncertain variable
@uncertain d[1:2, 1:2]
and I want to define a scenario in which values are sampled from different normal distributions.
@sampler ss = begin
N::MvNormal
ss(µ, Σ) = new(MvNormal(µ, Σ))
@sample Scenario begin
x = rand(sampler.N)
return @scenario d = reshape(x,2,2)
end
end
μ=[4,5,3,4];
Σ= Diagonal([1,2,1,1.5]);
when I run the model by :
sp = instantiate(sm, ss(µ, Σ), 5)
I see this error
At untitled-5f4bcc7b395b3178d0e16e18a0962f0c:16: `@uncertain(d[1:2, 1:2])`: @uncertain declarations of type `@uncertain ξ[i = ..., j = ..., ...]` only support scenarios of type `Scenario` with Array or DenseAxisArray as underlying data, given scenario is:
Scenario with probability 0.2
d: [5.100576528402207 4.89202743661476; 5.050253673984235 5.836340864424617].
Use a matching `@scenario` call to generate scenarios or consider declaring a custom scenario type.
error(::String, ::String) at error.jl:42
_macro_error(macroname::Symbol, args::Vector{Expr}, source::LineNumberNode, str::String) at macros.jl:1390
(::StochasticPrograms.var"#_error#656")(x::Expr, str::String) at stage.jl:93
#396 at untitled-5f4bcc7b395b3178d0e16e18a0962f0c:16 [inlined]
(::var"#396#412")(model::Model, stage::NamedTuple{(), Tuple{}}, scenario::Scenario{NamedTuple{(:d,), Tuple{Matrix{Float64}}}}) at none:0
This is strange because in the error what is presented as d is a two dimensional matrix matching the uncertain.
I put below what I do to solve this problem but it is a silly method.
@uncertain d11 d12 d21 d22
@sampler SimpleSampler = begin
N::MvNormal # Normal distribution
SimpleSampler(µ, Σ) = new(MvNormal(µ, Σ))
@sample Scenario begin
x = rand(sampler.N)
return @scenario d11 = x[1] d12 = x[2] d21 = x[3] d22 = x[4]
end
end
Is there any work around for this?