-
Notifications
You must be signed in to change notification settings - Fork 4
/
solve.jl
246 lines (200 loc) · 8.04 KB
/
solve.jl
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
export solve
"""
default_operator(system::InitialValueProblem)
Return the default continous post operator for the initial value problem of a
discrete or continuous system.
### Input
- `system` -- an initial value problem wrapping a mathematical system (continuous or
discrete) and a set of initial states
### Output
A continuous post operator with default options.
"""
function default_operator(system::InitialValueProblem)
if isaffine(system)
op = BFFPSV18()
else
error("no default reachability algorithm available for system of " *
"type $(typeof(system))")
end
return op
end
"""
solve(system, options) or solve(system, :key1 => val1, [...], keyK => valK)
Solves a reachability problem s.t. the given options.
If some options are not defined, we may fall back to default values.
### Input
- `system` -- a (discrete or continuoues) system specification
- `options` -- algorithm options for solving the problem
- `algorithm` -- (optional, default: dispatched on the system's type) the
reachability algorithm for the computation
### Output
A solution object whose content depends on the input options.
### Notes
To see all available input options, see
`keys(Reachability.available_keywords.dict)`.
"""
function solve(system::InitialValueProblem,
options::Options;
op::ContinuousPost=default_operator(system))
solve!(system, copy(options), op=op)
end
solve(system::AbstractSystem, options::Pair{Symbol,<:Any}...) =
solve(system, Options(Dict{Symbol,Any}(options)))
function solve!(problem::InitialValueProblem,
options::Options;
op::ContinuousPost=default_operator(problem)
)
# normalize system to a canonical form if needed
problem = IVP(normalize(problem.s), problem.x0)
# initialize the algorithm-specific options
options = init!(op, problem, options)
# compute a coordinate transformation if needed
problem, options = transform(problem, options)
# run the continuous-post operator
res = post(op, problem, options)
# undo a coordinate transformation
res = backtransform(res, options)
end
"""
solve(system::InitialValueProblem{<:HybridSystem},
options::Options)
Interface to reachability algorithms for a hybrid system PWA dynamics.
### Input
- `system` -- hybrid system
- `options` -- options for solving the problem
"""
function solve(system::InitialValueProblem{<:HybridSystem, <:LazySet},
options::Options,
opC::ContinuousPost=BFFPSV18(),
opD::DiscretePost=LazyDiscretePost())
return solve!(distribute_initial_set(system), copy(options), opC, opD)
end
# if the initial states are distributed, no need to call distribute_initial_set(system)
function solve(system::InitialValueProblem{<:HybridSystem,
<:Vector{<:Tuple{Int64,<:LazySet}}},
options::Options,
opC::ContinuousPost=BFFPSV18(),
opD::DiscretePost=LazyDiscretePost())
return solve!(system, copy(options), opC, opD)
end
function solve!(system::InitialValueProblem{<:HybridSystem,
<:Vector{<:Tuple{Int64,<:LazySet{N}}}},
options_input::Options,
opC::ContinuousPost,
opD::DiscretePost
) where N<:Real
# update global variable
global discrete_post_operator = opD
HS = system.s
init_sets = system.x0
options = init!(opD, HS, options_input)
time_horizon = options[:T]
max_jumps = options[:max_jumps]
property = options[:mode] == "check" ? options[:property] : nothing
# waiting_list entries:
# - (discrete) location
# - (set of) continuous-time reach sets
# - number of previous jumps
waiting_list = Vector{Tuple{Int, AbstractReachSet{LazySet{N}}, Int}}()
for (loc_id, x0) in init_sets
loc = HS.modes[loc_id]
source_invariant = loc.X
if x0 ⊆ source_invariant
loc_x0set = x0
else
loc_x0set = intersection(source_invariant, x0)
end
if !isempty(loc_x0set)
aux = ReachSet{LazySet{N}}(loc_x0set, zero(N), zero(N))
push!(waiting_list, (loc_id, aux, 0))
end
end
# passed_list maps the (discrete) location to the (set of) continuous-time
# reach sets
passed_list = options[:fixpoint_check] == :none ?
nothing :
Vector{Vector{AbstractReachSet{LazySet{N}}}}(undef, nstates(HS))
Rsets = Vector{AbstractReachSet{<:LazySet{N}}}()
while (!isempty(waiting_list))
loc_id, X0, jumps = pop!(waiting_list)
loc = HS.modes[loc_id]
source_invariant = loc.X
# compute reach tube
options_copy = copy(options)
options_copy.dict[:T] = time_horizon - time_start(X0)
options_copy.dict[:project_reachset] = false
if property != nothing
# TODO temporary hack, to be resolved in #447
options_copy[:mode] = "reach"
end
if opC isa BFFPS19
opC.options.specified[:HS] = HS
opC.options.specified[:loc_id] = loc_id
opC.options.specified[:opD] = opD
end
reach_tube = solve!(IVP(loc, set(X0)), options_copy, op=opC)
# get the property for the current location
property_loc = property isa Dict ?
get(property, loc_id, nothing) :
property
# add the very first initial approximation
if passed_list != nothing &&
(!isassigned(passed_list, loc_id) || isempty(passed_list[loc_id]))
reach_set = reach_tube.Xk[1]
# TODO For lazy X0 the fixpoint check is likely to fail, so we
# currently ignore that. In general, we want to add an
# *underapproximation*, which we currently do not support.
X = set(reach_set)
if !(X isa CartesianProductArray) || !(array(X)[1] isa CH)
Xoa = overapproximate(X)
ti, tf = time_start(reach_set), time_end(reach_set)
passed_list[loc_id] = [ReachSet{LazySet{N}}(Xoa, ti, tf)]
end
end
# count_Rsets counts the number of new reach sets added to Rsets
count_Rsets = tube⋂inv!(opD, reach_tube.Xk, loc.X, Rsets,
[time_start(X0), time_end(X0)])
if property_loc != nothing
if opD isa DecomposedDiscretePost
temp_vars = opD.options[:temp_vars]
n_lowdim = length(temp_vars)
n = dim(set(X0))
property_loc_lowdim = project(property_loc, temp_vars)
for (i, reach_set) in enumerate(reach_tube.Xk)
X = set(reach_set)
if (dim(X) == n_lowdim && n_lowdim < n)
if !check(property_loc_lowdim, X)
return CheckSolution(false, i, options)
end
elseif !check(property_loc, X)
return CheckSolution(false, i, options)
end
end
else
for (i, reach_set) in enumerate(Rsets[length(Rsets) - count_Rsets + 1 : end])
if !check(property_loc, set(reach_set))
return CheckSolution(false, i, options)
end
end
end
end
if jumps == max_jumps
continue
end
post(opD, HS, waiting_list, passed_list, loc_id, Rsets, count_Rsets,
jumps, options)
end
if options[:mode] == "check"
return CheckSolution(true, -1, options)
end
# create vector with concrete set type (needed by ReachSolution)
Rsets = [rs for rs in Rsets]
# Projection
if options[:project_reachset] || options[:projection_matrix] != nothing
info("Projection...")
RsetsProj = @timing project(Rsets, options)
else
RsetsProj = Rsets
end
return ReachSolution(RsetsProj, options)
end