-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathproblem.jl
More file actions
199 lines (158 loc) · 5.36 KB
/
problem.jl
File metadata and controls
199 lines (158 loc) · 5.36 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
abstract type AbstractIntervalMDPProblem end
################
# Verification #
#################
# Problem
"""
VerificationProblem{S <: StochasticProcess, F <: Specification, C <: AbstractStrategy}
A verification problem is a tuple of an interval Markov process and a specification.
### Fields
- `system::S`: interval Markov process.
- `spec::F`: specification (either temporal logic or reachability-like).
- `strategy::C`: strategy to be used for verification, which can be a given strategy or a no strategy,
i.e. select (but do not store! see [`ControlSynthesisProblem`]) optimal action for every state at every timestep.
"""
struct VerificationProblem{
S <: StochasticProcess,
F <: Specification,
C <: AbstractStrategy,
} <: AbstractIntervalMDPProblem
system::S
spec::F
strategy::C
function VerificationProblem(
system::S,
spec::F,
strategy::C,
) where {S <: StochasticProcess, F <: Specification, C <: AbstractStrategy}
checkspecification(spec, system, strategy)
checkstrategy(strategy, system, system_property(spec))
return new{S, F, C}(system, spec, strategy)
end
end
VerificationProblem(system::StochasticProcess, spec::Specification) =
VerificationProblem(system, spec, NoStrategy())
"""
system(prob::VerificationProblem)
Return the system of a problem.
"""
system(prob::VerificationProblem) = prob.system
"""
specification(prob::VerificationProblem)
Return the specification of a problem.
"""
specification(prob::VerificationProblem) = prob.spec
"""
strategy(prob::VerificationProblem)
Return the strategy of a problem, if provided.
"""
strategy(prob::VerificationProblem) = prob.strategy
function Base.show(io::IO, mime::MIME"text/plain", prob::VerificationProblem)
println(io, styled"{code:VerificationProblem}")
showsystem(io, "├─ ", "│ ", system(prob))
showspecification(io, "├─ ", "│ ", specification(prob))
if !(prob.strategy isa NoStrategy)
showstrategy(io, "└─ ", " ", strategy(prob))
else
println(
io,
"└─ ",
styled"No strategy provided (selecting optimal actions at every step)",
)
end
end
# Solution
struct VerificationSolution{R, MR <: AbstractArray{R}, D}
value_function::MR
residual::MR
num_iterations::Int
additional_data::D
end
VerificationSolution(V, res, k) = VerificationSolution(V, res, k, nothing)
"""
value_function(s::VerificationSolution)
Return the value function of a verification solution.
"""
value_function(s::VerificationSolution) = s.value_function
"""
residual(s::VerificationSolution)
Return the residual of a verification solution.
"""
residual(s::VerificationSolution) = s.residual
"""
num_iterations(s::VerificationSolution)
Return the number of iterations of a verification solution.
"""
num_iterations(s::VerificationSolution) = s.num_iterations
Base.iterate(s::VerificationSolution, args...) =
iterate((s.value_function, s.num_iterations, s.residual), args...)
#####################
# Control synthesis #
#####################
# Problem
"""
ControlSynthesisProblem{S <: StochasticProcess, F <: Specification}
A verification problem is a tuple of an interval Markov process and a specification.
### Fields
- `system::S`: interval Markov process.
- `spec::F`: specification (either temporal logic or reachability-like).
"""
struct ControlSynthesisProblem{S <: StochasticProcess, F <: Specification} <:
AbstractIntervalMDPProblem
system::S
spec::F
function ControlSynthesisProblem(
system::S,
spec::F,
) where {S <: StochasticProcess, F <: Specification}
checkspecification(spec, system)
return new{S, F}(system, spec)
end
end
"""
system(prob::ControlSynthesisProblem)
Return the system of a problem.
"""
system(prob::ControlSynthesisProblem) = prob.system
"""
specification(prob::ControlSynthesisProblem)
Return the specification of a problem.
"""
specification(prob::ControlSynthesisProblem) = prob.spec
# Solution
struct ControlSynthesisSolution{C <: AbstractStrategy, R, MR <: AbstractArray{R}, D}
strategy::C
value_function::MR
residual::MR
num_iterations::Int
additional_data::D
end
ControlSynthesisSolution(strategy, V, res, k) =
ControlSynthesisSolution(strategy, V, res, k, nothing)
"""
strategy(s::ControlSynthesisSolution)
Return the strategy of a control synthesis solution.
"""
strategy(s::ControlSynthesisSolution) = s.strategy
"""
value_function(s::ControlSynthesisSolution)
Return the value function of a control synthesis solution.
"""
value_function(s::ControlSynthesisSolution) = s.value_function
"""
residual(s::ControlSynthesisSolution)
Return the residual of a control synthesis solution.
"""
residual(s::ControlSynthesisSolution) = s.residual
"""
num_iterations(s::ControlSynthesisSolution)
Return the number of iterations of a control synthesis solution.
"""
num_iterations(s::ControlSynthesisSolution) = s.num_iterations
Base.iterate(s::ControlSynthesisSolution, args...) =
iterate((s.strategy, s.value_function, s.num_iterations, s.residual), args...)
function Base.show(io::IO, mime::MIME"text/plain", prob::ControlSynthesisProblem)
println(io, styled"{code:ControlSynthesisProblem}")
showsystem(io, "├─ ", "│ ", system(prob))
showspecification(io, "└─ ", " ", specification(prob))
end