Tutorial SolverParameters.jl
Contents
Example
This example describes a simple parameter set with a real, a categorical and an integer parameter.
using SolverParameters
struct CatMockSolverParamSet{I, F} <: AbstractParameterSet
real_inf::Parameter{F, RealInterval{F}}
real::Parameter{String, CategoricalSet{String}}
int_r::Parameter{I, IntegerRange{I}}
end
function CatMockSolverParamSet()
CatMockSolverParamSet(
Parameter(Float64(42)),
Parameter("A", CategoricalSet(["A", "B", "C", "D"])),
Parameter(Int32(5), IntegerRange(Int32(5), Int32(20))),
)
end
param_set = CatMockSolverParamSet()Main.CatMockSolverParamSet{Int32, Float64}(Parameter{Float64, RealInterval{Float64}}(42.0, RealInterval{Float64}(-Inf, Inf, true, true), ""), Parameter{String, CategoricalSet{String}}("A", CategoricalSet{String}(["A", "B", "C", "D"]), ""), Parameter{Int32, IntegerRange{Int32}}(5, IntegerRange{Int32}(5, 20), ""))Access values
It is straightforward to access the default values of each parameter
values(param_set)3-element Vector{Any}:
42.0
"A"
5or only the numerical parameters
values_num(param_set)2-element Vector{Float64}:
42.0
5.0val = zeros(Float32, 2)
values_num!(param_set, val)2-element Vector{Float32}:
42.0
5.0Getter
The default value can be replaced, for instance with values randomly chosen in the domain of each parameter.
nw = rand(param_set)
nw ∈ param_settrueset_values!(param_set, nw)The number of parameters or numerical parameters is accessible with length.
(length(param_set), length_num(param_set))(3, 2)Lower and upper bounds on the domains can be accessed as well:
(lower_bounds(param_set), upper_bounds(param_set))(Any[-Inf, 5], Any[Inf, 20])Access subset of parameters
Most of the functionalities described above can be used for only a subset of parameters by defining an NTuple.
subset = (:real_inf, :real)
values(subset, param_set)2-element Vector{Any}:
-4.370131607632653e15
"C"The getter are also available in place, which allows to pre-allocate the type of vector.
subset = (:real_inf,)
tmp = zeros(1)
values_num!(subset, param_set, tmp)1-element Vector{Float64}:
-4.370131607632653e15