Skip to content

Commit 890e6be

Browse files
committed
Fix bugs in IMDP comparison
1 parent 31877f8 commit 890e6be

10 files changed

+42
-32
lines changed

examples/compare_imdp_approaches.jl

+10-2
Original file line numberDiff line numberDiff line change
@@ -173,14 +173,20 @@ function benchmark_direct(problem::ComparisonProblem)
173173
reach = []
174174
else
175175
reach_lines = split(chomp(reach), '\n')
176-
reach = map(line -> parse(Int32, line), reach_lines) # Parse each line as an integer
176+
reach = map(reach_lines) do line
177+
line_match = match(r"CartesianIndex\(([0-9]+),\)", line)
178+
return parse(Int32, line_match.captures[1])
179+
end
177180
reach = reach .- 1 # Subtract 1 to match 1-based indexing without the avoid state
178181
reach = map(x -> cartesian_indices[x], reach) # Convert from a linear index to a CartesianIndex
179182
end
180183

181184
# Read avoid states
182185
avoid_lines = split(chomp(avoid), '\n')
183-
avoid = map(line -> parse(Int32, line), avoid_lines) # Parse each line as an integer
186+
avoid = map(avoid_lines) do line
187+
line_match = match(r"CartesianIndex\(([0-9]+),\)", line)
188+
return parse(Int32, line_match.captures[1])
189+
end
184190
avoid = filter(x -> x != 1, avoid) # Remove the absorbing avoid state (we remove this manually later)
185191
avoid = avoid .- 1 # Subtract 1 to match 1-based indexing without the avoid state
186192
avoid = map(x -> cartesian_indices[x], avoid) # Convert from a linear index to a CartesianIndex
@@ -280,6 +286,7 @@ function benchmark_decoupled(problem::ComparisonProblem)
280286
else
281287
reach_lines = split(chomp(reach), '\n')
282288
reach = map(reach_lines) do line # Parse each line as a tuple of indices
289+
line = replace(line, "CartesianIndex" => "")
283290
indices = split(line[2:end - 1], ",")
284291
indices = map(index -> parse(Int32, index), indices)
285292
return Tuple(indices)
@@ -290,6 +297,7 @@ function benchmark_decoupled(problem::ComparisonProblem)
290297
# Read avoid states
291298
avoid_lines = split(chomp(avoid), '\n')
292299
avoid = map(avoid_lines) do line # Parse each line as a tuple of indices
300+
line = replace(line, "CartesianIndex" => "")
293301
indices = split(line[2:end - 1], ",")
294302
indices = map(index -> parse(Int32, index), indices)
295303
return Tuple(indices)

examples/isolated_compare_imdp_approaches.jl

+17-15
Original file line numberDiff line numberDiff line change
@@ -119,27 +119,33 @@ end
119119
function measure_abstraction_time(problem::IntervalSySCoReComparisonProblem, constructor)
120120
BenchmarkTools.gcscrub()
121121
start_time = time_ns()
122-
mdp, spec, upper_bound_spec = constructor(problem.state_split, problem.input_split)
122+
mdp, spec, upper_bound_spec = constructor(problem.state_split, problem.input_split, problem.time_horizon)
123123
end_time = time_ns()
124124
abstraction_time = (end_time - start_time) / 1e9
125125

126126
return abstraction_time, mdp, spec, upper_bound_spec
127127
end
128128

129-
function warmup_certification(prob)
130-
value_iteration(prob)
129+
function warmup_certification(prob, upper_bound_spec)
130+
strategy, _ = control_synthesis(prob)
131131

132-
return nothing
132+
upper_bound_prob = Problem(IntervalMDP.system(prob), upper_bound_spec, strategy)
133+
value_iteration(upper_bound_prob)
134+
135+
return upper_bound_prob
133136
end
134137

135-
function measure_certification_time(prob)
138+
function measure_certification_time(prob, upper_bound_prob)
136139
BenchmarkTools.gcscrub()
137140
start_time = time_ns()
138-
strategy, V, k, res = control_synthesis(prob)
141+
142+
_, V_lower, _ = control_synthesis(prob)
143+
V_upper, _ = value_iteration(upper_bound_prob)
144+
139145
end_time = time_ns()
140146
certification_time = (end_time - start_time) / 1e9
141147

142-
return certification_time, V, strategy
148+
return certification_time, V_lower, V_upper
143149
end
144150

145151
function benchmark_intervalsyscore(name::String, direct=true)
@@ -165,19 +171,15 @@ function benchmark_intervalsyscore(name::String, direct=true)
165171
prob = Problem(mdp, spec)
166172

167173
# Warmup
168-
warmup_certification(prob)
174+
upper_bound_prob = warmup_certification(prob, upper_bound_spec)
169175

170176
# Measure certification time
171-
certification_time, V_lower, strategy = measure_certification_time(prob)
177+
certification_time, V_lower, V_upper = measure_certification_time(prob, upper_bound_prob)
172178
println(("Certification time", certification_time))
173179

174-
# Compute upper bound probabilities
175-
prob = Problem(mdp, upper_bound_spec, strategy)
176-
V_upper, _, _ = value_iteration(prob)
177-
178180
println("reach")
179181
reach_set = if system_property(spec) isa AbstractReachability
180-
reach(system_property(spec))
182+
IntervalMDP.reach(system_property(spec))
181183
else
182184
[]
183185
end
@@ -186,7 +188,7 @@ function benchmark_intervalsyscore(name::String, direct=true)
186188
end
187189

188190
println("avoid")
189-
for a in avoid(system_property(spec))
191+
for a in IntervalMDP.avoid(system_property(spec))
190192
println(a)
191193
end
192194

examples/systems/almost_identity.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ function almost_identity_decoupled(num_dims::Int, time_horizon=10; sparse=false,
4646
mdp, abstract_spec = abstraction(prob, state_abs, input_abs, target_model)
4747

4848
upper_bound_spec = Specification(system_property(spec), !satisfaction_mode(spec))
49-
upper_bound_spec = convert_specification(upper_bound_spec, state_abs, target_model)
49+
upper_bound_spec = IntervalSySCoRe.convert_specification(upper_bound_spec, state_abs, target_model)
5050

5151
return mdp, abstract_spec, upper_bound_spec
5252
end
@@ -69,7 +69,7 @@ function almost_identity_direct(num_dims::Int, time_horizon=10; sparse=false, st
6969
mdp, abstract_spec = abstraction(prob, state_abs, input_abs, target_model)
7070

7171
upper_bound_spec = Specification(system_property(spec), !satisfaction_mode(spec))
72-
upper_bound_spec = convert_specification(upper_bound_spec, state_abs, target_model)
72+
upper_bound_spec = IntervalSySCoRe.convert_specification(upper_bound_spec, state_abs, target_model)
7373

7474
return mdp, abstract_spec, upper_bound_spec
7575
end

examples/systems/bas_4d.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ function building_automation_system_4d_decoupled(time_horizon=10; sparse=false,
5454
mdp, abstract_spec = abstraction(prob, state_abs, input_abs, target_model)
5555

5656
upper_bound_spec = Specification(system_property(spec), !satisfaction_mode(spec))
57-
upper_bound_spec = convert_specification(upper_bound_spec, state_abs, target_model)
57+
upper_bound_spec = IntervalSySCoRe.convert_specification(upper_bound_spec, state_abs, target_model)
5858

5959
return mdp, abstract_spec, upper_bound_spec
6060
end
@@ -78,7 +78,7 @@ function building_automation_system_4d_direct(time_horizon=10; sparse=false, sta
7878
mdp, abstract_spec = abstraction(prob, state_abs, input_abs, target_model)
7979

8080
upper_bound_spec = Specification(system_property(spec), !satisfaction_mode(spec))
81-
upper_bound_spec = convert_specification(upper_bound_spec, state_abs, target_model)
81+
upper_bound_spec = IntervalSySCoRe.convert_specification(upper_bound_spec, state_abs, target_model)
8282

8383
return mdp, abstract_spec, upper_bound_spec
8484
end

examples/systems/bas_7d.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ function building_automation_system_7d_decoupled(time_horizon=10; sparse=false,
4848
mdp, abstract_spec = abstraction(prob, state_abs, input_abs, target_model)
4949

5050
upper_bound_spec = Specification(system_property(spec), !satisfaction_mode(spec))
51-
upper_bound_spec = convert_specification(upper_bound_spec, state_abs, target_model)
51+
upper_bound_spec = IntervalSySCoRe.convert_specification(upper_bound_spec, state_abs, target_model)
5252

5353
return mdp, abstract_spec, upper_bound_spec
5454
end
@@ -71,7 +71,7 @@ function building_automation_system_7d_direct(time_horizon=10; sparse=false, sta
7171
mdp, abstract_spec = abstraction(prob, state_abs, input_abs, target_model)
7272

7373
upper_bound_spec = Specification(system_property(spec), !satisfaction_mode(spec))
74-
upper_bound_spec = convert_specification(upper_bound_spec, state_abs, target_model)
74+
upper_bound_spec = IntervalSySCoRe.convert_specification(upper_bound_spec, state_abs, target_model)
7575

7676
return mdp, abstract_spec, upper_bound_spec
7777
end

examples/systems/integrator_chain.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ function integrator_chain_decoupled(num_dims::Int, time_horizon=5; sparse=false,
5252
mdp, abstract_spec = abstraction(prob, state_abs, input_abs, target_model)
5353

5454
upper_bound_spec = Specification(system_property(spec), !satisfaction_mode(spec))
55-
upper_bound_spec = convert_specification(upper_bound_spec, state_abs, target_model)
55+
upper_bound_spec = IntervalSySCoRe.convert_specification(upper_bound_spec, state_abs, target_model)
5656

5757
return mdp, abstract_spec, upper_bound_spec
5858
end

examples/systems/nndm.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ function action_cartpole_decoupled(time_horizon=10; sparse=false)
280280
mdp, abstract_spec = abstraction(prob, state_abs, input_abs, target_model)
281281

282282
upper_bound_spec = Specification(system_property(spec), !satisfaction_mode(spec))
283-
upper_bound_spec = convert_specification(upper_bound_spec, state_abs, target_model)
283+
upper_bound_spec = IntervalSySCoRe.convert_specification(upper_bound_spec, state_abs, target_model)
284284

285285
return mdp, abstract_spec, upper_bound_spec
286286
end
@@ -304,7 +304,7 @@ function action_cartpole_direct(time_horizon=10; sparse=false)
304304
mdp, abstract_spec = abstraction(prob, state_abs, input_abs, target_model)
305305

306306
upper_bound_spec = Specification(system_property(spec), !satisfaction_mode(spec))
307-
upper_bound_spec = convert_specification(upper_bound_spec, state_abs, target_model)
307+
upper_bound_spec = IntervalSySCoRe.convert_specification(upper_bound_spec, state_abs, target_model)
308308

309309
return mdp, abstract_spec, upper_bound_spec
310310
end

examples/systems/robot_2d.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ function robot_2d_decoupled(time_horizon=10; sparse=true, spec=:reachavoid, stat
8080
mdp, abstract_spec = abstraction(prob, state_abs, input_abs, target_model)
8181

8282
upper_bound_spec = Specification(system_property(spec), !satisfaction_mode(spec))
83-
upper_bound_spec = convert_specification(upper_bound_spec, state_abs, target_model)
83+
upper_bound_spec = IntervalSySCoRe.convert_specification(upper_bound_spec, state_abs, target_model)
8484

8585
return mdp, abstract_spec, upper_bound_spec
8686
end
@@ -104,7 +104,7 @@ function robot_2d_direct(time_horizon=10; sparse=true, spec=:reachavoid, state_s
104104
mdp, abstract_spec = abstraction(prob, state_abs, input_abs, target_model)
105105

106106
upper_bound_spec = Specification(system_property(spec), !satisfaction_mode(spec))
107-
upper_bound_spec = convert_specification(upper_bound_spec, state_abs, target_model)
107+
upper_bound_spec = IntervalSySCoRe.convert_specification(upper_bound_spec, state_abs, target_model)
108108

109109
return mdp, abstract_spec, upper_bound_spec
110110
end

examples/systems/running_example.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ function running_example_decoupled(time_horizon=10; sparse=false, range_vs_grid=
4646
mdp, abstract_spec = abstraction(prob, state_abs, input_abs, target_model)
4747

4848
upper_bound_spec = Specification(system_property(spec), !satisfaction_mode(spec))
49-
upper_bound_spec = convert_specification(upper_bound_spec, state_abs, target_model)
49+
upper_bound_spec = IntervalSySCoRe.convert_specification(upper_bound_spec, state_abs, target_model)
5050

5151
return mdp, abstract_spec, upper_bound_spec
5252
end
@@ -76,7 +76,7 @@ function running_example_direct(time_horizon=10; sparse=false, range_vs_grid=:gr
7676
mdp, abstract_spec = abstraction(prob, state_abs, input_abs, target_model)
7777

7878
upper_bound_spec = Specification(system_property(spec), !satisfaction_mode(spec))
79-
upper_bound_spec = convert_specification(upper_bound_spec, state_abs, target_model)
79+
upper_bound_spec = IntervalSySCoRe.convert_specification(upper_bound_spec, state_abs, target_model)
8080

8181
return mdp, abstract_spec, upper_bound_spec
8282
end

examples/systems/van_der_pol.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ function van_der_pol_decoupled(time_horizon=10; sparse=false, state_split=(50, 5
3939
mdp, abstract_spec = abstraction(prob, state_abs, input_abs, target_model)
4040

4141
upper_bound_spec = Specification(system_property(spec), !satisfaction_mode(spec))
42-
upper_bound_spec = convert_specification(upper_bound_spec, state_abs, target_model)
42+
upper_bound_spec = IntervalSySCoRe.convert_specification(upper_bound_spec, state_abs, target_model)
4343

4444
return mdp, abstract_spec, upper_bound_spec
4545
end
@@ -59,7 +59,7 @@ function van_der_pol_direct(time_horizon=10; state_split=(50, 50), input_split=1
5959
mdp, abstract_spec = abstraction(prob, state_abs, input_abs, target_model)
6060

6161
upper_bound_spec = Specification(system_property(spec), !satisfaction_mode(spec))
62-
upper_bound_spec = convert_specification(upper_bound_spec, state_abs, target_model)
62+
upper_bound_spec = IntervalSySCoRe.convert_specification(upper_bound_spec, state_abs, target_model)
6363

6464
return mdp, abstract_spec, upper_bound_spec
6565
end

0 commit comments

Comments
 (0)