From 75a04e3780215937d53c9ef4dc6662a821c142b5 Mon Sep 17 00:00:00 2001 From: Huaiming Yu Date: Sun, 26 Feb 2023 20:56:18 +0800 Subject: [PATCH 1/7] revise output formats --- Project.toml | 3 ++- src/Algorithm/DE.jl | 4 ++-- src/OptScenario/OptScenario.jl | 16 ++++++++-------- src/QuanEstimation.jl | 2 +- src/output.jl | 20 ++++++++++++++------ 5 files changed, 27 insertions(+), 18 deletions(-) diff --git a/Project.toml b/Project.toml index 70b62c8..0c055a9 100644 --- a/Project.toml +++ b/Project.toml @@ -11,6 +11,7 @@ Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f" Flux = "587475ba-b771-5e3f-ad9e-33799f191a9c" Interpolations = "a98d9a8b-a2ab-59e6-89dd-64a1c18fca59" IntervalSets = "8197267c-284f-5f27-9208-e0e47529a953" +JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed" Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7" @@ -26,12 +27,12 @@ Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f" [compat] BoundaryValueDiffEq = "2" -OrdinaryDiffEq = "6" Convex = "0.15" Distributions = "0.25" Flux = "0.12, 0.13" Interpolations = "0.13" IntervalSets = "0.5" +OrdinaryDiffEq = "6" QuadGK = "2" ReinforcementLearning = "0.10" SCS = "1" diff --git a/src/Algorithm/DE.jl b/src/Algorithm/DE.jl index fabe12a..e7683c7 100644 --- a/src/Algorithm/DE.jl +++ b/src/Algorithm/DE.jl @@ -94,7 +94,7 @@ function update!(opt::StateOpt, alg::DE, obj, dynamics, output) end set_f!(output, p_out[1]) - set_buffer!(output, transpose(dynamics.data.ψ0)) + set_buffer!(output, dynamics.data.ψ0) set_io!(output, p_out[1]) show(opt, output, obj) @@ -132,7 +132,7 @@ function update!(opt::StateOpt, alg::DE, obj, dynamics, output) end idx = findmax(p_fit)[2] set_f!(output, p_out[idx]) - set_buffer!(output, transpose(populations[idx].data.ψ0)) + set_buffer!(output, populations[idx].data.ψ0) set_io!(output, p_out[idx], ei) show(output, obj) end diff --git a/src/OptScenario/OptScenario.jl b/src/OptScenario/OptScenario.jl index 10bf9fd..fb588e0 100644 --- a/src/OptScenario/OptScenario.jl +++ b/src/OptScenario/OptScenario.jl @@ -202,14 +202,14 @@ result(opt::StateControlMeasurementOpt) = [opt.psi, opt.ctrl, opt.M] result(opt, ::Type{Val{:save_reward}}) = [result(opt)..., [0.0]] const res_file_name = Dict( - :Copt => ["controls.csv"], - :Sopt => ["states.csv"], - :Mopt => ["measurements.csv"], - :Mopt_input => ["measurements.csv"], - :SCopt => ["states.csv", "controls.csv"], - :CMopt => ["controls.csv", "measurements.csv"], - :SMopt => ["states.csv", "measurements.csv"], - :SCMopt => ["states.csv", "controls.csv", "measurements.csv"], + :Copt => ["controls"], + :Sopt => ["states"], + :Mopt => ["measurements"], + :Mopt_input => ["measurements"], + :SCopt => ["states", "controls"], + :CMopt => ["controls", "measurements"], + :SMopt => ["states", "measurements"], + :SCMopt => ["states", "controls", "measurements"], ) res_file(opt::AbstractOpt) = res_file_name[opt_target(opt)] diff --git a/src/QuanEstimation.jl b/src/QuanEstimation.jl index bb39031..0918dbf 100644 --- a/src/QuanEstimation.jl +++ b/src/QuanEstimation.jl @@ -15,7 +15,7 @@ using Interpolations using Distributions using QuadGK using OrdinaryDiffEq - +using JLD2 const pkgpath = @__DIR__ const GLOBAL_RNG = MersenneTwister(1234) diff --git a/src/output.jl b/src/output.jl index db22ecd..f0e0d46 100644 --- a/src/output.jl +++ b/src/output.jl @@ -29,26 +29,34 @@ Output(opt::AbstractOpt; save::Bool=false) = save_type(::Output{savefile}) = :savefile save_type(::Output{no_save}) = :no_save -function SaveFile(output::Output{no_save}) +function SaveFile(output::Output{no_save};suffix::AbstractString=".dat") open("f.csv", "w") do f writedlm(f, output.f_list) end + @show output.opt_buffer|>typeof ## debug for (res, file) in zip(output.opt_buffer, output.res_file) - open(file, "w") do g - writedlm(g, res) + # open(file, "w") do g + # writedlm(g, res) + # end + jldopen(file*suffix, "w") do f + f[file] = res end end end function SaveFile(output::Output{savefile}) end -function SaveCurrent(output::Output{savefile}) +function SaveCurrent(output::Output{savefile};suffix::AbstractString=".dat") open("f.csv", "a") do f writedlm(f, output.f_list[end]) end for (res, file) in zip(output.opt_buffer, output.res_file) - open(file, "a") do g - writedlm(g, res) + # open(file, "a") do g + # writedlm(g, res) + # end + fs = isfile(file*suffix) ? load(file*suffix)[file] : typeof(res)[] + jldopen(file*suffix, "w") do f + f[file] = append!(fs, [res]) end end end From c91fd6522e25ea31b31b4fac957c4d0a84e6c1e2 Mon Sep 17 00:00:00 2001 From: Huaiming Yu Date: Mon, 6 Mar 2023 09:59:36 +0800 Subject: [PATCH 2/7] revise output --- src/Algorithm/PSO.jl | 4 ++-- src/output.jl | 1 - 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Algorithm/PSO.jl b/src/Algorithm/PSO.jl index 578524c..167e862 100644 --- a/src/Algorithm/PSO.jl +++ b/src/Algorithm/PSO.jl @@ -126,7 +126,7 @@ function update!(opt::StateOpt, alg::PSO, obj, dynamics, output) f_ini, f_comp = objective(obj, dynamics) set_f!(output, f_ini) - set_buffer!(output, transpose(dynamics.data.ψ0)) + set_buffer!(output, dynamics.data.ψ0) set_io!(output, f_ini) show(opt, output, obj) @@ -170,7 +170,7 @@ function update!(opt::StateOpt, alg::PSO, obj, dynamics, output) particles = repeat(dynamics, p_num) end set_f!(output, fit_out) - set_buffer!(output, transpose(gbest)) + set_buffer!(output, gbest) set_io!(output, fit_out, ei) show(output, obj) end diff --git a/src/output.jl b/src/output.jl index f0e0d46..94457cc 100644 --- a/src/output.jl +++ b/src/output.jl @@ -33,7 +33,6 @@ function SaveFile(output::Output{no_save};suffix::AbstractString=".dat") open("f.csv", "w") do f writedlm(f, output.f_list) end - @show output.opt_buffer|>typeof ## debug for (res, file) in zip(output.opt_buffer, output.res_file) # open(file, "w") do g # writedlm(g, res) From 9160828925d4c1e9d837046bd28e1b40d48ee570 Mon Sep 17 00:00:00 2001 From: Huaiming Yu Date: Thu, 23 Mar 2023 10:55:11 +0800 Subject: [PATCH 3/7] remove transpose() for Sopt --- src/Algorithm/DDPG.jl | 6 +++--- src/Algorithm/DE.jl | 12 ++++++------ src/Algorithm/NM.jl | 4 ++-- src/Algorithm/PSO.jl | 12 ++++++------ 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/Algorithm/DDPG.jl b/src/Algorithm/DDPG.jl index f926545..4971dfa 100644 --- a/src/Algorithm/DDPG.jl +++ b/src/Algorithm/DDPG.jl @@ -287,7 +287,7 @@ function update!(Sopt::StateOpt, alg::DDPG, obj, dynamics::Lindblad, output) trajectory=CircularArraySARTTrajectory(capacity=400, state=Vector{Float64} => (ns,), action=Vector{Float64} => (na,),),) set_f!(output, f_ini) - set_buffer!(output, transpose(dynamics.data.ψ0)) + set_buffer!(output, dynamics.data.ψ0) set_io!(output, f_ini) show(Sopt, output, obj) @@ -343,7 +343,7 @@ function update!(Sopt::StateOpt, alg::DDPG, obj, dynamics::Kraus, output) trajectory=CircularArraySARTTrajectory(capacity=400, state=Vector{Float64} => (ns,), action=Vector{Float64} => (na,),),) set_f!(output, f_ini) - set_buffer!(output, transpose(dynamics.data.ψ0)) + set_buffer!(output, dynamics.data.ψ0) set_io!(output, f_ini) show(Sopt, output, obj) @@ -383,7 +383,7 @@ function _step!(env::StateEnv, a) env.episode += 1 set_f!(env.output, f_out) - set_buffer!(env.output, transpose(env.dynamics.data.ψ0)) + set_buffer!(env.output, env.dynamics.data.ψ0) set_io!(env.output, f_out, env.episode) show(env.output, env.obj) SaveReward(env.output, env.total_reward) diff --git a/src/Algorithm/DE.jl b/src/Algorithm/DE.jl index e7683c7..a6941d7 100644 --- a/src/Algorithm/DE.jl +++ b/src/Algorithm/DE.jl @@ -423,7 +423,7 @@ function update!(opt::StateControlOpt, alg::DE, obj, dynamics, output) f_noctrl, f_comp = objective(obj, dynamics_copy) set_f!(output, p_out[1]) - set_buffer!(output, transpose(populations[1].data.ψ0), populations[1].data.ctrl) + set_buffer!(output, populations[1].data.ψ0, populations[1].data.ctrl) set_io!(output, f_noctrl, p_out[1]) show(opt, output, obj) @@ -490,7 +490,7 @@ function update!(opt::StateControlOpt, alg::DE, obj, dynamics, output) end idx = findmax(p_fit)[2] set_f!(output, p_out[idx]) - set_buffer!(output, transpose(populations[idx].data.ψ0), populations[idx].data.ctrl) + set_buffer!(output, populations[idx].data.ψ0, populations[idx].data.ctrl) set_io!(output, p_out[idx], ei) show(output, obj) end @@ -522,7 +522,7 @@ function update!(opt::StateMeasurementOpt, alg::DE, obj, dynamics, output) M = [C_all[1][i]*(C_all[1][i])' for i in 1:M_num] set_f!(output, p_out[1]) - set_buffer!(output, transpose(populations[1].data.ψ0), M) + set_buffer!(output, populations[1].data.ψ0, M) set_io!(output, p_out[1]) show(opt, output, obj) @@ -593,7 +593,7 @@ function update!(opt::StateMeasurementOpt, alg::DE, obj, dynamics, output) idx = findmax(p_fit)[2] M = [C_all[idx][i]*(C_all[idx][i])' for i in 1:M_num] set_f!(output, p_out[idx]) - set_buffer!(output, transpose(populations[idx].data.ψ0), M) + set_buffer!(output, populations[idx].data.ψ0, M) set_io!(output, p_out[idx], ei) show(output, obj) end @@ -742,7 +742,7 @@ function update!(opt::StateControlMeasurementOpt, alg::DE, obj, dynamics, output M = [C_all[1][i]*(C_all[1][i])' for i in 1:M_num] set_f!(output, p_out[1]) - set_buffer!(output, transpose(populations[1].data.ψ0), populations[1].data.ctrl, M) + set_buffer!(output, populations[1].data.ψ0, populations[1].data.ctrl, M) set_io!(output, p_out[1]) show(opt, output, obj) @@ -839,7 +839,7 @@ function update!(opt::StateControlMeasurementOpt, alg::DE, obj, dynamics, output idx = findmax(p_fit)[2] M = [C_all[idx][i]*(C_all[idx][i])' for i in 1:M_num] set_f!(output, p_out[idx]) - set_buffer!(output, transpose(populations[idx].data.ψ0), populations[idx].data.ctrl, M) + set_buffer!(output, populations[idx].data.ψ0, populations[idx].data.ctrl, M) set_io!(output, p_out[idx], ei) show(output, obj) end diff --git a/src/Algorithm/NM.jl b/src/Algorithm/NM.jl index d20a17f..fd5a85f 100644 --- a/src/Algorithm/NM.jl +++ b/src/Algorithm/NM.jl @@ -27,7 +27,7 @@ function update!(opt::StateOpt, alg::NM, obj, dynamics, output) sort_ind = sortperm(p_fit, rev=true) set_f!(output, p_out[1]) - set_buffer!(output, transpose(dynamics.data.ψ0)) + set_buffer!(output, dynamics.data.ψ0) set_io!(output, p_out[1]) show(opt, output, obj) @@ -144,7 +144,7 @@ function update!(opt::StateOpt, alg::NM, obj, dynamics, output) end idx = findmax(p_fit)[2] set_f!(output, p_out[idx]) - set_buffer!(output, transpose(nelder_mead[sort_ind[1]].data.ψ0)) + set_buffer!(output,nelder_mead[sort_ind[1]].data.ψ0) set_io!(output, p_out[idx], ei) show(output, obj) end diff --git a/src/Algorithm/PSO.jl b/src/Algorithm/PSO.jl index 167e862..4f89d19 100644 --- a/src/Algorithm/PSO.jl +++ b/src/Algorithm/PSO.jl @@ -503,7 +503,7 @@ function update!(opt::StateControlOpt, alg::PSO, obj, dynamics, output) f_ini, f_comp = objective(obj, dynamics) set_f!(output, f_ini) - set_buffer!(output, transpose(particles[1].data.ψ0), particles[1].data.ctrl) + set_buffer!(output, particles[1].data.ψ0, particles[1].data.ctrl) set_io!(output, f_noctrl, f_ini) show(opt, output, obj) @@ -572,7 +572,7 @@ function update!(opt::StateControlOpt, alg::PSO, obj, dynamics, output) end end set_f!(output, fit_out) - set_buffer!(output, transpose(gbest_state), gbest_ctrl) + set_buffer!(output, gbest_state, gbest_ctrl) set_io!(output, fit_out, ei) show(output, obj) end @@ -614,7 +614,7 @@ function update!(opt::StateMeasurementOpt, alg::PSO, obj, dynamics, output) f_ini, f_comp = objective(obj_copy, dynamics) set_f!(output, f_ini) - set_buffer!(output, transpose(particles[1].data.ψ0), M) + set_buffer!(output, particles[1].data.ψ0, M) set_io!(output, f_ini) show(opt, output, obj) @@ -687,7 +687,7 @@ function update!(opt::StateMeasurementOpt, alg::PSO, obj, dynamics, output) end M = [gbest_meas[i]*(gbest_meas[i])' for i in 1:M_num] set_f!(output, fit_out) - set_buffer!(output, transpose(gbest_state), M) + set_buffer!(output, gbest_state, M) set_io!(output, fit_out, ei) show(output, obj) end @@ -858,7 +858,7 @@ function update!(opt::StateControlMeasurementOpt, alg::PSO, obj, dynamics, outpu f_ini, f_comp = objective(obj_copy, dynamics) set_f!(output, f_ini) - set_buffer!(output, transpose(particles[1].data.ψ0), particles[1].data.ctrl, M) + set_buffer!(output, particles[1].data.ψ0, particles[1].data.ctrl, M) set_io!(output, f_ini) show(opt, output, obj) @@ -956,7 +956,7 @@ function update!(opt::StateControlMeasurementOpt, alg::PSO, obj, dynamics, outpu end M = [gbest_meas[i]*(gbest_meas[i])' for i in 1:M_num] set_f!(output, fit_out) - set_buffer!(output, transpose(gbest_state), gbest_ctrl, M) + set_buffer!(output, gbest_state, gbest_ctrl, M) set_io!(output, fit_out, ei) show(output, obj) end From c126b07e228983ea2dcb134916a3fc94bd719b84 Mon Sep 17 00:00:00 2001 From: Huaiming Yu Date: Thu, 23 Mar 2023 11:15:18 +0800 Subject: [PATCH 4/7] save adaptive --- src/Common/AdaptiveScheme.jl | 32 +++++++++++--------------------- 1 file changed, 11 insertions(+), 21 deletions(-) diff --git a/src/Common/AdaptiveScheme.jl b/src/Common/AdaptiveScheme.jl index e67d3e7..d0be2fc 100755 --- a/src/Common/AdaptiveScheme.jl +++ b/src/Common/AdaptiveScheme.jl @@ -528,26 +528,18 @@ function iter_MI_multipara(p, p_num, para_num, x, x_list, u, rho_all, M, ei) end function savefile_true(p, xout, y) - open("pout.csv","w") do f - writedlm(f, [p]) - end - open("xout.csv","w") do m - writedlm(m, [xout]) - end - open("y.csv","w") do n - writedlm(n, [y]) + jldopen("adaptive.dat", "w") do f + f["p"] = [p] + f["x"] = [xout] + f["y"] = [y] end end function savefile_false(p, xout, y) - open("pout.csv","w") do f - writedlm(f, [p]) - end - open("xout.csv","w") do m - writedlm(m, xout) - end - open("y.csv","w") do n - writedlm(n, y) + jldopen("adaptive.dat", "w") do f + f["p"] = [p] + f["x"] = xout + f["y"] = y end end @@ -679,11 +671,9 @@ function calculate_online{MI}(x, p, pyx, a_res, a, rho0, N, ei, phi_span, exp_ix end function savefile_online(xout, y) - open("xout.csv","w") do m - writedlm(m, xout) - end - open("y.csv","w") do n - writedlm(n, y) + jldopen("adaptive.dat", "w") do f + f["x"] = xout + f["y"] = y end end From 82bf7f9fa8d0ec66e7b63dbed7face86d129b000 Mon Sep 17 00:00:00 2001 From: Huaiming Yu Date: Thu, 23 Mar 2023 11:27:08 +0800 Subject: [PATCH 5/7] bayes save --- src/Common/BayesEstimation.jl | 74 ++++++++++++++++------------------- 1 file changed, 33 insertions(+), 41 deletions(-) diff --git a/src/Common/BayesEstimation.jl b/src/Common/BayesEstimation.jl index 6981bbb..77cb706 100644 --- a/src/Common/BayesEstimation.jl +++ b/src/Common/BayesEstimation.jl @@ -46,12 +46,11 @@ function Bayes(x, p, rho, y; M=missing, estimator="mean", savefile=false) "The input is not a valid value for estimator, supported values are 'mean' and 'MAP'.") end - open("pout.csv","w") do f - writedlm(f, [p]) - end - open("xout.csv","w") do m - writedlm(m, x_out) + jldopen("bayes.dat", "w") do f + f["p"] = [p] + f["x"] = x_out end + return p, x_out[end] else p_out, x_out = [], [] @@ -83,12 +82,11 @@ function Bayes(x, p, rho, y; M=missing, estimator="mean", savefile=false) "The input is not a valid value for estimator, supported values are 'mean' and 'MAP'.") end - open("pout.csv","w") do f - writedlm(f, [p_out]) - end - open("xout.csv","w") do m - writedlm(m, x_out) - end + jldopen("bayes.dat", "w") do f + f["p"] = [p_out] + f["x"] = x_out + end + return p, x_out[end] end else @@ -127,12 +125,11 @@ function Bayes(x, p, rho, y; M=missing, estimator="mean", savefile=false) "The input is not a valid value for estimator, supported values are 'mean' and 'MAP'.") end - open("pout.csv","w") do f - writedlm(f, [p]) - end - open("xout.csv","w") do m - writedlm(m, x_out) + jldopen("bayes.dat", "w") do f + f["p"] = [p] + f["x"] = x_out end + return p, x_out[end] else p_out, x_out = [], [] @@ -166,12 +163,11 @@ function Bayes(x, p, rho, y; M=missing, estimator="mean", savefile=false) "The input is not a valid value for estimator, supported values are 'mean' and 'MAP'.") end - open("pout.csv","w") do f - writedlm(f, p_out) - end - open("xout.csv","w") do m - writedlm(m, x_out) + jldopen("bayes.dat", "w") do f + f["p"] = p_out + f["x"] = x_out end + return p, x_out[end] end end @@ -207,12 +203,11 @@ function MLE(x, rho, y; M=missing, savefile=false) append!(x_out, x[1][indx]) end - open("Lout.csv","w") do f - writedlm(f, [L_out]) - end - open("xout.csv","w") do m - writedlm(m, x_out) + jldopen("MLE.dat", "w") do f + f["L"] = [L_out] + f["x"] = x_out end + return L_out, x_out[end] else L_out, x_out = [], [] @@ -227,12 +222,11 @@ function MLE(x, rho, y; M=missing, savefile=false) append!(x_out, x[1][indx]) end - open("Lout.csv","w") do f - writedlm(f, L_out) - end - open("xout.csv","w") do m - writedlm(m, x_out) + jldopen("MLE.dat", "w") do f + f["L"] = L_out + f["x"] = x_out end + return L_tp, x_out[end] end else @@ -256,12 +250,11 @@ function MLE(x, rho, y; M=missing, savefile=false) indx = findmax(L_out)[2] append!(x_out, [[x[i][indx[i]] for i in 1:para_num]]) end - open("Lout.csv","w") do f - writedlm(f, [L_out]) - end - open("xout.csv","w") do m - writedlm(m, x_out) + jldopen("MLE.dat", "w") do f + f["L"] = [L_out] + f["x"] = x_out end + return L_out, x_out[end] else L_out, x_out = [], [] @@ -275,12 +268,11 @@ function MLE(x, rho, y; M=missing, savefile=false) append!(x_out, [[x[i][indx[i]] for i in 1:para_num]]) end - open("Lout.csv","w") do f - writedlm(f, L_out) - end - open("xout.csv","w") do m - writedlm(m, x_out) + jldopen("MLE.dat", "w") do f + f["L"] = L_out + f["x"] = x_out end + return L_tp, x_out[end] end end From 7066abe9b4f3e2106539c7d7b1c0b9e033026ef4 Mon Sep 17 00:00:00 2001 From: Huaiming Yu Date: Thu, 23 Mar 2023 11:39:01 +0800 Subject: [PATCH 6/7] savefile == true --- src/Common/AdaptiveScheme.jl | 2 +- src/Common/BayesEstimation.jl | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Common/AdaptiveScheme.jl b/src/Common/AdaptiveScheme.jl index d0be2fc..1704574 100755 --- a/src/Common/AdaptiveScheme.jl +++ b/src/Common/AdaptiveScheme.jl @@ -528,7 +528,7 @@ function iter_MI_multipara(p, p_num, para_num, x, x_list, u, rho_all, M, ei) end function savefile_true(p, xout, y) - jldopen("adaptive.dat", "w") do f + jldopen("adaptive.dat", "a") do f f["p"] = [p] f["x"] = [xout] f["y"] = [y] diff --git a/src/Common/BayesEstimation.jl b/src/Common/BayesEstimation.jl index 77cb706..3d7bc46 100644 --- a/src/Common/BayesEstimation.jl +++ b/src/Common/BayesEstimation.jl @@ -82,7 +82,7 @@ function Bayes(x, p, rho, y; M=missing, estimator="mean", savefile=false) "The input is not a valid value for estimator, supported values are 'mean' and 'MAP'.") end - jldopen("bayes.dat", "w") do f + jldopen("bayes.dat", "a") do f f["p"] = [p_out] f["x"] = x_out end @@ -163,7 +163,7 @@ function Bayes(x, p, rho, y; M=missing, estimator="mean", savefile=false) "The input is not a valid value for estimator, supported values are 'mean' and 'MAP'.") end - jldopen("bayes.dat", "w") do f + jldopen("bayes.dat", "a") do f f["p"] = p_out f["x"] = x_out end @@ -222,7 +222,7 @@ function MLE(x, rho, y; M=missing, savefile=false) append!(x_out, x[1][indx]) end - jldopen("MLE.dat", "w") do f + jldopen("MLE.dat", "a") do f f["L"] = L_out f["x"] = x_out end @@ -268,7 +268,7 @@ function MLE(x, rho, y; M=missing, savefile=false) append!(x_out, [[x[i][indx[i]] for i in 1:para_num]]) end - jldopen("MLE.dat", "w") do f + jldopen("MLE.dat", "a") do f f["L"] = L_out f["x"] = x_out end From 8e8f4262d297904327abf9b737598644efa82832 Mon Sep 17 00:00:00 2001 From: Huaiming Yu Date: Thu, 23 Mar 2023 14:38:16 +0800 Subject: [PATCH 7/7] revise savefile --- src/Common/AdaptiveScheme.jl | 11 +++++++---- src/Common/BayesEstimation.jl | 8 ++++---- 2 files changed, 11 insertions(+), 8 deletions(-) mode change 100755 => 100644 src/Common/AdaptiveScheme.jl diff --git a/src/Common/AdaptiveScheme.jl b/src/Common/AdaptiveScheme.jl old mode 100755 new mode 100644 index 1704574..593f72e --- a/src/Common/AdaptiveScheme.jl +++ b/src/Common/AdaptiveScheme.jl @@ -528,10 +528,13 @@ function iter_MI_multipara(p, p_num, para_num, x, x_list, u, rho_all, M, ei) end function savefile_true(p, xout, y) - jldopen("adaptive.dat", "a") do f - f["p"] = [p] - f["x"] = [xout] - f["y"] = [y] + fp = isfile("adaptive.dat") ? load("adaptive.dat")["p"] : [] + fx = isfile("adaptive.dat") ? load("adaptive.dat")["x"] : [] + fy = isfile("adaptive.dat") ? load("adaptive.dat")["y"] : [] + jldopen("adaptive.dat", "w") do f + f["p"] = append!(fp, [p]) + f["x"] = append!(fx, [xout]) + f["y"] = append!(fy, [y]) end end diff --git a/src/Common/BayesEstimation.jl b/src/Common/BayesEstimation.jl index 3d7bc46..77cb706 100644 --- a/src/Common/BayesEstimation.jl +++ b/src/Common/BayesEstimation.jl @@ -82,7 +82,7 @@ function Bayes(x, p, rho, y; M=missing, estimator="mean", savefile=false) "The input is not a valid value for estimator, supported values are 'mean' and 'MAP'.") end - jldopen("bayes.dat", "a") do f + jldopen("bayes.dat", "w") do f f["p"] = [p_out] f["x"] = x_out end @@ -163,7 +163,7 @@ function Bayes(x, p, rho, y; M=missing, estimator="mean", savefile=false) "The input is not a valid value for estimator, supported values are 'mean' and 'MAP'.") end - jldopen("bayes.dat", "a") do f + jldopen("bayes.dat", "w") do f f["p"] = p_out f["x"] = x_out end @@ -222,7 +222,7 @@ function MLE(x, rho, y; M=missing, savefile=false) append!(x_out, x[1][indx]) end - jldopen("MLE.dat", "a") do f + jldopen("MLE.dat", "w") do f f["L"] = L_out f["x"] = x_out end @@ -268,7 +268,7 @@ function MLE(x, rho, y; M=missing, savefile=false) append!(x_out, [[x[i][indx[i]] for i in 1:para_num]]) end - jldopen("MLE.dat", "a") do f + jldopen("MLE.dat", "w") do f f["L"] = L_out f["x"] = x_out end