diff --git a/examples/model_coupling/external-linearmodel+template+instruction+path/external-linearmodel+template+instruction+path-eigenmatrix.svg b/examples/model_coupling/external-linearmodel+template+instruction+path/external-linearmodel+template+instruction+path-eigenmatrix.svg index 2aec3bbc..f9f31b54 100644 --- a/examples/model_coupling/external-linearmodel+template+instruction+path/external-linearmodel+template+instruction+path-eigenmatrix.svg +++ b/examples/model_coupling/external-linearmodel+template+instruction+path/external-linearmodel+template+instruction+path-eigenmatrix.svg @@ -14,15 +14,15 @@ - - + + Eigenvectors - + 1 @@ -34,8 +34,8 @@ - - + + 1.0 @@ -62,7 +62,7 @@ - + @@ -183,7 +183,7 @@ - + @@ -201,7 +201,7 @@ - + Color @@ -209,59 +209,59 @@ - - - - + + + + - - + + - - + + - + - - + + - + - + - + - - + + a - + b - - + + Parameters @@ -269,7 +269,7 @@ - + diff --git a/examples/model_coupling/external-linearmodel+template+instruction+path/external-linearmodel+template+instruction+path-eigenvalues.svg b/examples/model_coupling/external-linearmodel+template+instruction+path/external-linearmodel+template+instruction+path-eigenvalues.svg index 83447a1d..25180fb4 100644 --- a/examples/model_coupling/external-linearmodel+template+instruction+path/external-linearmodel+template+instruction+path-eigenvalues.svg +++ b/examples/model_coupling/external-linearmodel+template+instruction+path/external-linearmodel+template+instruction+path-eigenvalues.svg @@ -14,15 +14,15 @@ - - + + Eigenvectors - + 1 @@ -34,46 +34,46 @@ - - - - + + + + - - + + - + - + - + - + - + - - + + - + - - - + + + @@ -81,40 +81,40 @@ - - + + 10-4.0 - + 10-3.5 - + 10-3.0 - + 10-2.5 - + 10-2.0 - + 10-1.5 - - + + Eigenvalues @@ -122,7 +122,7 @@ - + diff --git a/examples/model_coupling/external-linearmodel+template+instruction+path/external-linearmodel+template+instruction+path-jacobian.svg b/examples/model_coupling/external-linearmodel+template+instruction+path/external-linearmodel+template+instruction+path-jacobian.svg index beb9183f..a5b89124 100644 --- a/examples/model_coupling/external-linearmodel+template+instruction+path/external-linearmodel+template+instruction+path-jacobian.svg +++ b/examples/model_coupling/external-linearmodel+template+instruction+path/external-linearmodel+template+instruction+path-jacobian.svg @@ -14,15 +14,15 @@ - - + + Parameters - + a @@ -34,8 +34,8 @@ - - + + 40 @@ -62,7 +62,7 @@ - + @@ -183,7 +183,7 @@ - + @@ -201,7 +201,7 @@ - + Color @@ -209,87 +209,87 @@ - - - - + + + + - - + + - + - + - - + + - + - - + + - + - + - + - + - + - + - + - - + + o1 - + o2 - + o3 - + o4 - - + + Observations @@ -297,7 +297,7 @@ - + diff --git a/examples/models/internal-linear-model/internal-linearmodel-5-spaghetti.svg b/examples/models/internal-linear-model/internal-linearmodel-5-spaghetti.svg index 371f99e7..f42d8d06 100644 --- a/examples/models/internal-linear-model/internal-linearmodel-5-spaghetti.svg +++ b/examples/models/internal-linear-model/internal-linearmodel-5-spaghetti.svg @@ -14,8 +14,8 @@ - - + + 0 @@ -42,126 +42,126 @@ - - - - + + + + - - + + - + - + - + - + - + - + - - + + - + - + - + - + - + - - + + - + - + - + - + - - - - + + + + - - + + 4.0,9.0 - - - + + + - - + + 3.0,5.0 - - - + + + - - + + 2.0,1.0 - - - + + + - - + + 1.0,-3.0 @@ -172,38 +172,38 @@ - - + + -50 - + -40 - + -30 - + -20 - + -10 - + 0 - + 10 @@ -211,7 +211,7 @@ - + diff --git a/examples/models/internal-linear-model/internal-linearmodel-a-5-spaghetti.svg b/examples/models/internal-linear-model/internal-linearmodel-a-5-spaghetti.svg index 88b0d1e5..f5ff798b 100644 --- a/examples/models/internal-linear-model/internal-linearmodel-a-5-spaghetti.svg +++ b/examples/models/internal-linear-model/internal-linearmodel-a-5-spaghetti.svg @@ -14,8 +14,8 @@ - - + + 0 @@ -42,144 +42,144 @@ - - - - + + + + - - + + - + - + - + - + - + - + - - + + - + - + - + - + - + - - - + + + - - - + + + - - - + + + - - - + + + - - - + + + - - - - + + + + - - + + 4.0,9.0 - - - + + + - - + + 3.0,5.0 - - - + + + - - + + 2.0,1.0 - - - + + + - - + + 1.0,-3.0 @@ -190,38 +190,38 @@ - - + + -40 - + -30 - + -20 - + -10 - + 0 - + 10 - + 20 @@ -229,7 +229,7 @@ - + diff --git a/examples/models/internal-linear-model/internal-linearmodel-b-5-spaghetti.svg b/examples/models/internal-linear-model/internal-linearmodel-b-5-spaghetti.svg index b6fe68fc..7ab4125b 100644 --- a/examples/models/internal-linear-model/internal-linearmodel-b-5-spaghetti.svg +++ b/examples/models/internal-linear-model/internal-linearmodel-b-5-spaghetti.svg @@ -14,8 +14,8 @@ - - + + 0 @@ -42,138 +42,138 @@ - - - - + + + + - - + + - + - + - + - + - - + + - + - + - + - + - + - - - + + + - - - + + + - - - + + + - - - + + + - - - + + + - - - - + + + + - - + + 4.0,9.0 - - - + + + - - + + 3.0,5.0 - - - + + + - - + + 2.0,1.0 - - - + + + - - + + 1.0,-3.0 @@ -184,28 +184,28 @@ - - + + -10 - + -5 - + 0 - + 5 - + 10 @@ -213,7 +213,7 @@ - + diff --git a/examples/models/internal-linear-model/internal-linearmodel-efast-385.jld2 b/examples/models/internal-linear-model/internal-linearmodel-efast-385.jld2 index 4f3ee3f2..a17af9ea 100644 Binary files a/examples/models/internal-linear-model/internal-linearmodel-efast-385.jld2 and b/examples/models/internal-linear-model/internal-linearmodel-efast-385.jld2 differ diff --git a/examples/models/internal-polynomial-model/internal-polynomial-efast-385.jld2 b/examples/models/internal-polynomial-model/internal-polynomial-efast-385.jld2 index a94f1b80..b3b55523 100644 Binary files a/examples/models/internal-polynomial-model/internal-polynomial-efast-385.jld2 and b/examples/models/internal-polynomial-model/internal-polynomial-efast-385.jld2 differ diff --git a/src/MadsCalibrate.jl b/src/MadsCalibrate.jl index 4b2076c0..ad0267c0 100644 --- a/src/MadsCalibrate.jl +++ b/src/MadsCalibrate.jl @@ -26,7 +26,7 @@ keytext=Dict("tolX"=>"parameter space tolerance [default=`1e-4`]", "usenaive"=>"use naive Levenberg-Marquardt solver [default=`false`]", "seed"=>"random seed [default=`0`]", "quiet"=>"[default=`true`]", - "all"=>"all model results are returned [default=`false`]", + "all_results"=>"all model results are returned [default=`false`]", "save_results"=>"save intermediate results [default=`true`]"))) Returns: @@ -41,7 +41,7 @@ Mads.calibraterandom(madsdata; tolX=1e-3, tolG=1e-6, maxEval=1000, maxIter=100, Mads.calibraterandom(madsdata, numberofsamples; tolX=1e-3, tolG=1e-6, maxEval=1000, maxIter=100, maxJacobians=100, lambda=100.0, lambda_mu=10.0, np_lambda=10, show_trace=false, usenaive=false) ``` """ -function calibraterandom(madsdata::AbstractDict, numberofsamples::Integer=1; tolX::Number=1e-4, tolG::Number=1e-6, tolOF::Number=1e-3, tolOFcount::Integer=5, minOF::Number=1e-3, maxEval::Integer=1000, maxIter::Integer=100, maxJacobians::Integer=100, lambda::Number=100.0, lambda_mu::Number=10.0, np_lambda::Integer=10, show_trace::Bool=false, usenaive::Bool=false, seed::Integer=-1, rng::Union{Nothing,Random.AbstractRNG,DataType}=nothing, quiet::Bool=true, all::Bool=false, save_results::Bool=true, first_init::Bool=true) +function calibraterandom(madsdata::AbstractDict, numberofsamples::Integer=1; tolX::Number=1e-4, tolG::Number=1e-6, tolOF::Number=1e-3, tolOFcount::Integer=5, minOF::Number=1e-3, maxEval::Integer=1000, maxIter::Integer=100, maxJacobians::Integer=100, lambda::Number=100.0, lambda_mu::Number=10.0, np_lambda::Integer=10, show_trace::Bool=false, usenaive::Bool=false, seed::Integer=-1, rng::Union{Nothing,Random.AbstractRNG,DataType}=nothing, quiet::Bool=true, all_results::Bool=false, save_results::Bool=true, first_init::Bool=true) if numberofsamples < 1 numberofsamples = 1 end @@ -49,7 +49,10 @@ function calibraterandom(madsdata::AbstractDict, numberofsamples::Integer=1; tol paramdict = Mads.getparamdict(madsdata) paramsoptdict = copy(paramdict) paramoptvalues = Mads.getparamrandom(madsdata, numberofsamples; init_dist=Mads.haskeyword(madsdata, "init_dist")) - allresults = Vector{Float64}(undef, 0) + + allphi = Vector{Float64}(undef, numberofsamples) + allconverged = Vector{Bool}(undef, numberofsamples) + allparameters = Matrix{Float64}(undef, numberofsamples, length(keys(paramoptvalues))) local bestparameters local bestresult bestphi = Inf @@ -63,25 +66,21 @@ function calibraterandom(madsdata::AbstractDict, numberofsamples::Integer=1; tol Mads.setparamsinit!(madsdata, paramsoptdict) end parameters, results = Mads.calibrate(madsdata; tolX=tolX, tolG=tolG, tolOF=tolOF, tolOFcount=tolOFcount, minOF=minOF, maxEval=maxEval, maxIter=maxIter, maxJacobians=maxJacobians, lambda=lambda, lambda_mu=lambda_mu, np_lambda=np_lambda, show_trace=show_trace, usenaive=usenaive, save_results=save_results) - phi = results.minimum - converged = results.x_converged | results.g_converged # f_converged => of_conferged - !quiet && @info("Random initial guess #$(i): OF = $(phi) (converged=$(converged))") - if phi < bestphi || i == 1 + allphi[i] = results.minimum + allconverged[i] = results.x_converged | results.g_converged # f_converged => of_conferged + !quiet && @info("Random initial guess #$(i): OF = $(allphi[i]) (converged=$(allconverged[i]))") + if allphi[i] < bestphi || i == 1 bestparameters = parameters bestresult = results - bestphi = phi + bestphi = allphi[i] end - if all - if sizeof(allresults) == 0 - allresults = [phi converged parameters] - else - allresults = [allresults; phi converged parameters] - end + if all_results + allparameters[i,:] = [parameters[paramkey] for paramkey in keys(paramoptvalues)] end end Mads.setparamsinit!(madsdata, paramdict) # restore the original initial values - if all - return allresults + if all_results + return bestparameters, bestresult, allphi, allparameters else return bestparameters, bestresult end @@ -152,16 +151,17 @@ function calibraterandom_parallel(madsdata::AbstractDict, numberofsamples::Integ end Mads.setparamsinit!(madsdata, paramdict) # restore the original initial values if all(isnan.(allphi)) - @warn("Something is wrong! All the objective function estimates are NaN!") + @warn("Something is very wrong! All the objective function estimates are NaN!") + end + ibest = first(sortperm(allphi)) + for (j, paramkey) in enumerate(keys(paramoptvalues)) + paramdict[paramkey] = allparameters[ibest, j] end + bestresult = Mads.forward(madsdata, paramdict) if all_results - return allphi, allconverged, allparameters + return paramdict, bestresult, allphi, allparameters else - isort = sortperm(allphi) - for (j, paramkey) in enumerate(keys(paramoptvalues)) - paramdict[paramkey] = allparameters[isort[1],j] - end - return paramdict + return paramdict, bestresult end end