Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 3 additions & 3 deletions src/Algorithm/DDPG.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand Down
16 changes: 8 additions & 8 deletions src/Algorithm/DE.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/Algorithm/NM.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down
16 changes: 8 additions & 8 deletions src/Algorithm/PSO.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down
35 changes: 14 additions & 21 deletions src/Common/AdaptiveScheme.jl
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -528,26 +528,21 @@ 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])
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

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

Expand Down Expand Up @@ -679,11 +674,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

Expand Down
74 changes: 33 additions & 41 deletions src/Common/BayesEstimation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [], []
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 = [], []
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 = [], []
Expand All @@ -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
Expand All @@ -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 = [], []
Expand All @@ -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
Expand Down
Loading