forked from MilesCranmer/DataDrivenDiffEq.jl
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcache.jl
More file actions
177 lines (147 loc) · 5.5 KB
/
cache.jl
File metadata and controls
177 lines (147 loc) · 5.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
struct SearchCache{ALG, PTYPE, O} <: AbstractAlgorithmCache
alg::ALG
candidates::AbstractVector{Candidate}
ages::AbstractVector{Int}
keeps::AbstractVector{Bool}
sorting::AbstractVector{Int}
p::AbstractVector
dataset::Dataset
optimiser_state::O
end
Base.show(io::IO, cache::SearchCache) = print(io, "SearchCache : $(cache.alg)")
function init_model(x::AbstractDAGSRAlgorithm, basis::Basis, dataset::Dataset, intervals)
(; simplex, n_layers, arities, functions, use_protected, skip) = x.options
# Get the parameter mapping
variable_mask = map(enumerate(equations(basis))) do (i, eq)
return any(ModelingToolkit.isvariable, ModelingToolkit.get_variables(eq.rhs)) &&
IntervalArithmetic.iscommon(intervals[i])
end
variable_mask = Any[variable_mask...]
if use_protected
functions = map(convert_to_safe, functions)
end
return LayeredDAG(length(basis), size(dataset.y, 1), n_layers, arities, functions;
skip = skip, input_functions = variable_mask, simplex = simplex)
end
function init_cache(x::X where {X <: AbstractDAGSRAlgorithm},
basis::Basis, problem::DataDrivenProblem; kwargs...)
(; rng, keep, observed, populationsize, optimizer,
optim_options, optimiser, loss) = x.options
# Derive the model
dataset = Dataset(problem)
TData = eltype(dataset)
rng_ = Lux.replicate(rng)
observed = isa(observed, ObservedModel) ? observed :
ObservedModel(dataset.y, fixed = true)
parameters = ParameterDistributions(basis, TData)
intervals = interval_eval(basis, dataset, get_interval(parameters))
model = init_model(x, basis, dataset, intervals)
ps = ComponentVector(Lux.initialparameters(rng_, model))
# Derive the candidates
candidates = map(1:populationsize) do i
candidate = Candidate(rng_, model, basis, dataset; observed = observed,
parameterdist = parameters, ptype = TData)
optimize_candidate!(
candidate, dataset; optimizer = optimizer, options = optim_options)
return candidate
end
keeps = zeros(Bool, populationsize)
ages = zeros(Int, populationsize)
sorting = zeros(Int, populationsize)
if isa(keep, Int)
sortperm!(sorting, candidates, alg = PartialQuickSort(keep), by = loss)
permute!(candidates, sorting)
keeps[1:keep] .= true
else
losses = filter(!isnan, map(loss, candidates))
# TODO Maybe weight by age or loss here
loss_quantile = quantile(losses, keep)
keeps .= (losses .<= loss_quantile)
end
# Distributed always goes first here
if x.options.distributed
ptype = __PROCESSUSE(3)
elseif x.options.threaded
ptype = __PROCESSUSE(2)
else
ptype = __PROCESSUSE(1)
end
# Setup the optimiser
if isa(optimiser, Optimisers.AbstractRule)
optimiser_state = Optimisers.setup(optimiser, ps[:])
else
optimiser_state = nothing
end
return SearchCache{typeof(x), ptype, typeof(optimiser_state)}(
x, candidates, ages, keeps, sorting, ps, dataset, optimiser_state)
end
function update_cache!(cache::SearchCache)
(; keep, loss) = cache.alg.options
# Update the parameters based on the current results
update_parameters!(cache)
optimize_cache!(cache, cache.p)
cache.keeps .= false
if isa(keep, Int)
sortperm!(cache.sorting, cache.candidates, alg = PartialQuickSort(keep), by = loss)
permute!(cache.candidates, cache.sorting)
cache.keeps[1:keep] .= true
else
losses = map(loss, cache.candidates)
@. losses = ifelse(isnan(losses), Inf, losses)
# TODO Maybe weight by age or loss here
sortperm!(cache.sorting, cache.candidates, by = loss)
permute!(cache.candidates, cache.sorting)
loss_quantile = quantile(losses, keep, sorted = true)
@. cache.keeps = losses ≤ loss_quantile
end
return
end
# Optimizes the cache and returns the loglikelihoods
# Serial
function optimize_cache!(cache::SearchCache{<:Any, __PROCESSUSE(1)}, p = cache.p)
(; optimizer, optim_options) = cache.alg.options
map(enumerate(cache.candidates)) do (i, candidate)
if cache.keeps[i]
cache.ages[i] += 1
return true
else
optimize_candidate!(
candidate, cache.dataset, p; optimizer = optimizer, options = optim_options)
cache.ages[i] = 0
return true
end
end
return
end
# Threaded
function optimize_cache!(cache::SearchCache{<:Any, __PROCESSUSE(2)}, p = cache.p)
(; optimizer, optim_options) = cache.alg.options
# Update all
Threads.@threads for i in 1:length(cache.keeps)
if cache.keeps[i]
cache.ages[i] += 1
else
optimize_candidate!(cache.candidates[i], cache.dataset, p;
optimizer = optimizer, options = optim_options)
cache.ages[i] = 0
end
end
return
end
# Distributed
function optimize_cache!(cache::SearchCache{<:Any, __PROCESSUSE(3)}, p = cache.p)
(; optimizer, optim_options) = cache.alg.options
successes = pmap(1:length(cache.keeps)) do i
if cache.keeps[i]
cache.ages[i] += 1
return true
else
optimize_candidate!(cache.candidates[i], cache.dataset, p;
optimizer = optimizer, options = optim_options)
cache.ages[i] = 0
return true
end
end
return
end
function convert_to_basis(::SearchCache) end