Skip to content

Commit 879c113

Browse files
committed
Use new sink state encoding in IntervalMDP
1 parent a7014ab commit 879c113

10 files changed

+117
-150
lines changed

examples/systems/almost_identity.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ function main(n)
9696
@time "value iteration" V, k, res = value_iteration(prob)
9797

9898
# Remove the first state from each axis (the avoid state, whose value is always 0).
99-
V = V[(2:d for d in size(V))...]
99+
V = V[(1:d-1 for d in size(V))...]
100100

101101
return V
102102
end

examples/systems/bas_4d.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ function main()
111111
@time "value iteration" V_safety, k, res = value_iteration(prob)
112112

113113
# Remove the first state from each axis (the avoid state, whose value is always 0).
114-
V_safety = V_safety[(2:d for d in size(V_safety))...]
114+
V_safety = V_safety[(1:d-1 for d in size(V_safety))...]
115115

116116
return V_safety
117117
end

examples/systems/big.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ function small_direct(
133133
end
134134

135135
function main(n)
136-
@time "abstraction" mdp, spec, _ = small_direct(; sparse = false)
136+
@time "abstraction" mdp, spec, _ = big_decoupled(n; sparse = false)
137137

138138
println("Memory usage: $(Base.summarysize(mdp) / 1000^2) MB")
139139

@@ -142,7 +142,7 @@ function main(n)
142142
@time "value iteration" V, k, res = value_iteration(prob)
143143

144144
# Remove the first state from each axis (the avoid state, whose value is always 0).
145-
V = V[(2:d for d in size(V))...]
145+
V = V[(1:d-1 for d in size(V))...]
146146

147147
return V
148148
end

examples/systems/car_parking.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ function main()
110110
@time "value iteration decoupled" V_decoupled, k_decoupled, res_decoupled =
111111
value_iteration(prob_decoupled)
112112

113-
V_diff = V_decoupled[2:end, 2:end] - reshape(V_direct[2:end], 20, 20)
113+
V_diff = V_decoupled[1:end-1, 1:end-1] - reshape(V_direct[1:end-1], 20, 20)
114114

115-
return V_diff, V_decoupled[2:end, 2:end], reshape(V_direct[2:end], 20, 20)
115+
return V_diff, V_decoupled[1:end-1, 1:end-1], reshape(V_direct[1:end-1], 20, 20)
116116
end

examples/systems/linear_stochastically_switched.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ function main()
106106
@time "upper bound" V_upper, _, _ = value_iteration(upper_bound_prob)
107107

108108
# Remove the first state from each axis (the avoid state, whose value is always 0).
109-
V_lower = V_lower[(2:d for d in size(V_lower))...]
110-
V_upper = V_upper[(2:d for d in size(V_upper))...]
109+
V_lower = V_lower[(1:d-1 for d in size(V_lower))...]
110+
V_upper = V_upper[(1:d-1 for d in size(V_upper))...]
111111

112112
return V_lower, V_upper
113113
end

examples/systems/robot_2d.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ function main()
134134

135135
@time "value-iteration reach-avoid" V_reachavoid, k_reachavoid, res_reachavoid =
136136
value_iteration(prob_reachavoid)
137-
V_reachavoid = V_reachavoid[2:end, 2:end]
137+
V_reachavoid = V_reachavoid[1:d-1, 1:d-1]
138138

139139
@time "abstraction reachability" mdp_reachability, spec_reachability, _ =
140140
robot_2d_decoupled(;
@@ -146,7 +146,7 @@ function main()
146146

147147
@time "value-iteration reachability" V_reachability, k_reachability, res_reachability =
148148
value_iteration(prob_reachability)
149-
V_reachability = V_reachability[2:end, 2:end]
149+
V_reachability = V_reachability[1:d-1, 1:d-1]
150150

151151
return V_reachavoid, V_reachability
152152
end

examples/systems/van_der_pol.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,5 +117,5 @@ function main()
117117
prob = Problem(mdp, spec)
118118

119119
@time "value iteration" V, k, res = value_iteration(prob)
120-
return V[2:end, 2:end]
120+
return V[1:d-1, 1:d-1]
121121
end

src/abstractions/abstraction.jl

+57-64
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ function abstraction(
9191

9292
# State pointer
9393
stateptr = Int32[
94-
[1, 2]
95-
(1:numregions(state_abstraction)) .* numinputs(input_abstraction) .+ 2
94+
[1]
95+
(1:numregions(state_abstraction)) .* numinputs(input_abstraction) .+ 1
9696
]
9797

9898
# Transition probabilities
@@ -108,7 +108,7 @@ function abstraction(
108108
initial_states = Int32[]
109109
for (i, source_region) in enumerate(regions(state_abstraction))
110110
if !isdisjoint(initial(sys), source_region)
111-
push!(initial_states, i + 1)
111+
push!(initial_states, i)
112112
end
113113
end
114114

@@ -121,15 +121,17 @@ function abstraction(
121121
end
122122

123123
function initprob(::IMDPTarget, nregions, ninputs)
124-
prob_lower = [zeros(Float64, nregions) for _ = 1:((nregions-1)*ninputs+1)]
125-
prob_upper = [zeros(Float64, nregions) for _ = 1:((nregions-1)*ninputs+1)]
124+
nchoices = nregions * ninputs
125+
prob_lower = zeros(Float64, nregions + 1, nchoices)
126+
prob_upper = zeros(Float64, nregions + 1, nchoices)
126127

127128
return prob_lower, prob_upper
128129
end
129130

130131
function initprob(::SparseIMDPTarget, nregions, ninputs)
131-
prob_lower = [spzeros(Float64, nregions) for _ = 1:((nregions-1)*ninputs+1)]
132-
prob_upper = [spzeros(Float64, nregions) for _ = 1:((nregions-1)*ninputs+1)]
132+
nchoices = nregions * ninputs
133+
prob_lower = spzeros(Float64, nregions + 1, nchoices)
134+
prob_upper = spzeros(Float64, nregions + 1, nchoices)
133135

134136
return prob_lower, prob_upper
135137
end
@@ -142,13 +144,13 @@ function convert_property(
142144
prop = system_property(spec)
143145

144146
reach_states = Int32[]
145-
avoid_states = Int32[1] # Absorbing state
147+
avoid_states = Int32[numregions(state_abstraction)] # Absorbing state
146148

147149
for (i, source_region) in enumerate(regions(state_abstraction))
148150
if ispessimistic(spec) && source_region reach(prop)
149-
push!(reach_states, i + 1)
151+
push!(reach_states, i)
150152
elseif isoptimistic(spec) && !iszeromeasure(reach(prop), source_region)
151-
push!(reach_states, i + 1)
153+
push!(reach_states, i)
152154
end
153155
end
154156

@@ -163,17 +165,17 @@ function convert_property(
163165
prop = system_property(spec)
164166

165167
reach_states = Int32[]
166-
avoid_states = Int32[1] # Absorbing state
168+
avoid_states = Int32[numregions(state_abstraction)] # Absorbing state
167169

168170
for (i, source_region) in enumerate(regions(state_abstraction))
169171
if ispessimistic(spec) && !iszeromeasure(avoid(prop), source_region)
170-
push!(avoid_states, i + 1)
172+
push!(avoid_states, i)
171173
elseif isoptimistic(spec) && source_region avoid(prop)
172-
push!(avoid_states, i + 1)
174+
push!(avoid_states, i)
173175
elseif ispessimistic(spec) && source_region reach(prop)
174-
push!(reach_states, i + 1)
176+
push!(reach_states, i)
175177
elseif isoptimistic(spec) && !iszeromeasure(reach(prop), source_region)
176-
push!(reach_states, i + 1)
178+
push!(reach_states, i)
177179
end
178180
end
179181

@@ -187,13 +189,13 @@ function convert_property(
187189
)
188190
prop = system_property(spec)
189191

190-
avoid_states = Int32[1] # Absorbing state
192+
avoid_states = Int32[numregions(state_abstraction)] # Absorbing state
191193

192194
for (i, source_region) in enumerate(regions(state_abstraction))
193195
if ispessimistic(spec) && !iszeromeasure(avoid(prop), source_region)
194-
push!(avoid_states, i + 1)
196+
push!(avoid_states, i)
195197
elseif isoptimistic(spec) && source_region avoid(prop)
196-
push!(avoid_states, i + 1)
198+
push!(avoid_states, i)
197199
end
198200
end
199201

@@ -220,14 +222,10 @@ function abstraction(
220222

221223
# State pointer
222224
stateptr = Int32[1]
223-
sizehint!(stateptr, prod(splits(state_abstraction) .+ 1))
225+
sizehint!(stateptr, prod(splits(state_abstraction)) + 1)
224226

225-
for I in CartesianIndices(splits(state_abstraction) .+ 1)
226-
if any(Tuple(I) .== 1)
227-
push!(stateptr, stateptr[end] + 1)
228-
else
229-
push!(stateptr, stateptr[end] + numinputs(input_abstraction))
230-
end
227+
for I in CartesianIndices(splits(state_abstraction))
228+
push!(stateptr, stateptr[end] + numinputs(input_abstraction))
231229
end
232230

233231
interval_prob = transition_prob(
@@ -239,11 +237,11 @@ function abstraction(
239237
)
240238

241239
# Initial states
242-
initial_states = NTuple{dimstate(sys),Int32}[]
240+
initial_states = CartesianIndex{dimstate(sys)}[]
243241
for (I, source_region) in
244242
zip(CartesianIndices(splits(state_abstraction)), regions(state_abstraction))
245243
if !isdisjoint(initial(sys), source_region)
246-
push!(initial_states, Tuple(I) .+ 1)
244+
push!(initial_states, I)
247245
end
248246
end
249247

@@ -259,9 +257,7 @@ function initprob(::OrthogonalIMDPTarget, state_abstraction::StateUniformGridSpl
259257
prob_lower = Matrix{Float64}[]
260258
prob_upper = Matrix{Float64}[]
261259

262-
# One action for non-absorbing states is already included in the first term.
263-
nchoices =
264-
prod(splits(state_abstraction) .+ 1) + numregions(state_abstraction) * (ninputs - 1)
260+
nchoices = numregions(state_abstraction) * ninputs
265261

266262
for axisregions in splits(state_abstraction)
267263
local_prob_lower = zeros(Float64, axisregions + 1, nchoices)
@@ -282,9 +278,7 @@ function initprob(
282278
prob_lower = SparseMatrixCSC{Float64, Int32}[]
283279
prob_upper = SparseMatrixCSC{Float64, Int32}[]
284280

285-
# One action for non-absorbing states is already included in the first term.
286-
nchoices =
287-
prod(splits(state_abstraction) .+ 1) + numregions(state_abstraction) * (ninputs - 1)
281+
nchoices = numregions(state_abstraction) * ninputs
288282

289283
for axisregions in splits(state_abstraction)
290284
local_prob_lower = spzeros(Float64, Int32, axisregions + 1, nchoices)
@@ -304,22 +298,23 @@ function convert_property(
304298
)
305299
prop = system_property(spec)
306300

307-
reach_states = NTuple{dim(prop),Int32}[]
308-
avoid_states = NTuple{dim(prop),Int32}[]
301+
reach_states = CartesianIndex{dim(prop)}[]
302+
avoid_states = CartesianIndex{dim(prop)}[]
309303

310304
# Absorbing states
311-
for I in CartesianIndices(splits(state_abstraction) .+ 1)
312-
if any(Tuple(I) .== 1)
313-
push!(avoid_states, Tuple(I))
305+
extended_states = splits(state_abstraction) .+ 1
306+
for I in CartesianIndices(extended_states)
307+
if any(Tuple(I) .== extended_states)
308+
push!(avoid_states, I)
314309
end
315310
end
316311

317312
for (I, source_region) in
318313
zip(CartesianIndices(splits(state_abstraction)), regions(state_abstraction))
319314
if ispessimistic(spec) && source_region reach(prop)
320-
push!(reach_states, Tuple(I) .+ 1)
315+
push!(reach_states, I)
321316
elseif isoptimistic(spec) && !iszeromeasure(reach(prop), source_region)
322-
push!(reach_states, Tuple(I) .+ 1)
317+
push!(reach_states, I)
323318
end
324319
end
325320

@@ -333,26 +328,27 @@ function convert_property(
333328
)
334329
prop = system_property(spec)
335330

336-
reach_states = NTuple{dim(prop),Int32}[]
337-
avoid_states = NTuple{dim(prop),Int32}[]
331+
reach_states = CartesianIndex{dim(prop)}[]
332+
avoid_states = CartesianIndex{dim(prop)}[]
338333

339334
# Absorbing states
340-
for I in CartesianIndices(splits(state_abstraction) .+ 1)
341-
if any(Tuple(I) .== 1)
342-
push!(avoid_states, Tuple(I))
335+
extended_states = splits(state_abstraction) .+ 1
336+
for I in CartesianIndices(extended_states)
337+
if any(Tuple(I) .== extended_states)
338+
push!(avoid_states, I)
343339
end
344340
end
345341

346342
for (I, source_region) in
347343
zip(CartesianIndices(splits(state_abstraction)), regions(state_abstraction))
348344
if ispessimistic(spec) && !iszeromeasure(avoid(prop), source_region)
349-
push!(avoid_states, Tuple(I) .+ 1)
345+
push!(avoid_states, I)
350346
elseif isoptimistic(spec) && source_region avoid(prop)
351-
push!(avoid_states, Tuple(I) .+ 1)
347+
push!(avoid_states, I)
352348
elseif ispessimistic(spec) && source_region reach(prop)
353-
push!(reach_states, Tuple(I) .+ 1)
349+
push!(reach_states, I)
354350
elseif isoptimistic(spec) && !iszeromeasure(reach(prop), source_region)
355-
push!(reach_states, Tuple(I) .+ 1)
351+
push!(reach_states, I)
356352
end
357353
end
358354

@@ -366,21 +362,22 @@ function convert_property(
366362
)
367363
prop = system_property(spec)
368364

369-
avoid_states = NTuple{dim(prop),Int32}[]
365+
avoid_states = CartesianIndex{dim(prop)}[]
370366

371367
# Absorbing states
372-
for I in CartesianIndices(splits(state_abstraction) .+ 1)
373-
if any(Tuple(I) .== 1)
374-
push!(avoid_states, Tuple(I))
368+
extended_states = splits(state_abstraction) .+ 1
369+
for I in CartesianIndices(extended_states)
370+
if any(Tuple(I) .== extended_states)
371+
push!(avoid_states, I)
375372
end
376373
end
377374

378375
for (I, source_region) in
379376
zip(CartesianIndices(splits(state_abstraction)), regions(state_abstraction))
380377
if ispessimistic(spec) && !iszeromeasure(avoid(prop), source_region)
381-
push!(avoid_states, Tuple(I) .+ 1)
378+
push!(avoid_states, I)
382379
elseif isoptimistic(spec) && source_region avoid(prop)
383-
push!(avoid_states, Tuple(I) .+ 1)
380+
push!(avoid_states, I)
384381
end
385382
end
386383

@@ -407,14 +404,10 @@ function abstraction(
407404

408405
# State pointer
409406
stateptr = Int32[1]
410-
sizehint!(stateptr, prod(splits(state_abstraction) .+ 1))
407+
sizehint!(stateptr, prod(splits(state_abstraction)) + 1)
411408

412-
for I in CartesianIndices(splits(state_abstraction) .+ 1)
413-
if any(Tuple(I) .== 1)
414-
push!(stateptr, stateptr[end] + 1)
415-
else
416-
push!(stateptr, stateptr[end] + numinputs(input_abstraction))
417-
end
409+
for I in CartesianIndices(splits(state_abstraction))
410+
push!(stateptr, stateptr[end] + numinputs(input_abstraction))
418411
end
419412

420413
# Transition probabilities
@@ -427,11 +420,11 @@ function abstraction(
427420
)
428421

429422
# Initial states
430-
initial_states = NTuple{dimstate(sys),Int32}[]
423+
initial_states = CartesianIndex{dimstate(sys)}[]
431424
for (I, source_region) in
432425
zip(CartesianIndices(splits(state_abstraction)), regions(state_abstraction))
433426
if !isdisjoint(initial(sys), source_region)
434-
push!(initial_states, Tuple(I) .+ 1)
427+
push!(initial_states, I)
435428
end
436429
end
437430

0 commit comments

Comments
 (0)