Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

intermediate results skeleton #33

Closed
wants to merge 1 commit into from
Closed
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
43 changes: 38 additions & 5 deletions src/SciMLOperations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,24 @@ Simulate a scenario from a PetriNet
function simulate(; model::AbstractPetriNet,
params::Dict{String,Float64},
initials::Dict{String,Float64},
tspan=(0.0, 100.0)::Tuple{Float64,Float64}
tspan=(0.0, 100.0)::Tuple{Float64,Float64},
f
)::DataFrame
sol = solve(_to_prob(model, params, initials, tspan); progress = true, progress_steps = 1)
DataFrame(sol)
prob = _to_prob(model, params, initials, tspan)
# integ.sol.prob.f.sys
sys = prob.f.sys
sts, ps = states(sys), parameters(sys)

integ = init(prob; progress = true, progress_steps = 1)
for x in integ
# i += 1
# if i % 500 == 0
# push to rabbitmq
# JSON3.write(Dict(string.(sts) .=> integ.u))
# end
f(x, job_id)
end
DataFrame(integ.sol)
end

"
Expand All @@ -84,7 +98,8 @@ function calibrate(; model::AbstractPetriNet,
initials::Dict{String,Float64},
dataset::DataFrame,
feature_mappings::Dict{String, String},
timesteps_column::String = "timestamp"
timesteps_column::String = "timestamp",
# f
)
timesteps, data = _select_data(dataset, feature_mappings, timesteps_column)
prob = _to_prob(model, params, initials, extrema(timesteps))
Expand All @@ -94,12 +109,30 @@ function calibrate(; model::AbstractPetriNet,
ks, vs = unzip(collect(p))
p = Num.(ks) .=> vs
data = SciMLOperations._symbolize_args(data, states(sys))
fitp = EasyModelAnalysis.datafit(prob, p, timesteps, data)
opt_step_count = 0

function mycallback(p, l) # p::Vector{Float64}, l::Float64
ppairs = Pair.(ks, p)
f(ppairs, l)
return false
end

fitp = mydatafit(prob, p, timesteps, data; callback=mycallback)
@info fitp
# DataFrame(fitp)
fitp
end

function mydatafit(prob, p::Vector{Pair{Num, Float64}}, t, data; loss = l2loss, callback=nothing)
pvals = getfield.(p, :second)
pkeys = getfield.(p, :first)
oprob = OptimizationProblem(loss, pvals,
lb = fill(-Inf, length(p)),
ub = fill(Inf, length(p)), (prob, pkeys, t, data))
res = solve(oprob, NLopt.LN_SBPLX(); callback)
Pair.(pkeys, res.u)
end

"long running functions like global_datafit and sensitivity wrappers will need to be refactored to share callback info incrementally"
function _global_datafit(; model::LabelledPetriNet,
parameter_bounds::Dict{String,Tuple{Float64,Float64}},
Expand Down
2 changes: 1 addition & 1 deletion src/SimulationService.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Interface for relevant ASKEM simulation libraries
"""
module SimulationService

__precompile__(false)
# __precompile__(false)

import AlgebraicPetri: LabelledPetriNet
import Symbolics
Expand Down
2 changes: 2 additions & 0 deletions test/sciml.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,5 @@ data = [states(sys)[1] => df[:, 2]]

l = EasyModelAnalysis.l2loss(pvals, (prob, pkeys, timesteps, data))
ForwardDiff.gradient(p -> EasyModelAnalysis.l2loss(p, (prob, pkeys, timesteps, data)), last.(fitp))