Skip to content

Commit a290c63

Browse files
committed
Test GP abstraction
1 parent 9145ab4 commit a290c63

File tree

7 files changed

+144
-14
lines changed

7 files changed

+144
-14
lines changed

src/abstractions/gaussian_process.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ function transition_prob(
1919
collect(enumerate(regions(state_abstraction)))
2020
for (j, input) in enumerate(inputs(input_abstraction))
2121
srcact_idx = (i - 1) * ninputs + j
22-
gp_bounds = bounds(dyn, source_region, input)
22+
bounds = gp_bounds(dyn, source_region, input)
2323

2424
source_action_transition_prob(
2525
dyn,
2626
state_abstraction,
2727
target_model,
28-
gp_bounds,
28+
bounds,
2929
prob_lower,
3030
prob_upper,
3131
srcact_idx,

test/abstractions/abstractions.jl

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11

2-
test_files = ["additive_noise/additive_noise.jl", "mixture.jl"]
3-
for f in test_files
4-
@testset "abstractions/$f" include(f)
5-
end
2+
@testset verbose=true "abstractions/additive_noise" include("additive_noise/additive_noise.jl")
3+
@testset verbose=true "abstractions/gaussian_process.jl" include("gaussian_process.jl")
4+
@testset verbose=true "abstractions/mixture.jl" include("mixture.jl")

test/abstractions/additive_noise/decoupled.jl

+6-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ include("example_systems.jl")
2727
# Dense
2828
mdp_dense, spec_dense = simple_1d_decoupled()
2929
@test num_states(mdp_dense) == 11
30-
@test stateptr(mdp_dense)[end] == 11
30+
@test length(stateptr(mdp_dense)) == 11 # 10 non-sink states
31+
@test stateptr(mdp_dense)[end] == 11 # No control actions
3132

3233
prob_dense = Problem(mdp_dense, spec_dense)
3334

@@ -37,7 +38,8 @@ include("example_systems.jl")
3738
# Sparse
3839
mdp_sparse, spec_sparse = simple_1d_decoupled(; sparse = true)
3940
@test num_states(mdp_sparse) == 11
40-
@test stateptr(mdp_sparse)[end] == 11
41+
@test length(stateptr(mdp_sparse)) == 11 # 10 non-sink states
42+
@test stateptr(mdp_sparse)[end] == 11 # No control actions
4143

4244
prob_sparse = Problem(mdp_sparse, spec_sparse)
4345

@@ -86,6 +88,7 @@ end
8688
# Dense, input grid
8789
mdp_dense, spec_dense = modified_running_example_decoupled()
8890
@test num_states(mdp_dense) == 121 # 11 * 11 total states
91+
@test length(stateptr(mdp_dense)) == 10 * 10 + 1 # 10 * 10 non-sink states
8992
@test stateptr(mdp_dense)[end] == 10 * 10 * 9 + 1 # 10 * 10 non-sink states, 9 actions
9093

9194
prob_dense = Problem(mdp_dense, spec_dense)
@@ -96,6 +99,7 @@ end
9699
# Sparse, input grid
97100
mdp_sparse, spec_sparse = modified_running_example_decoupled(; sparse = true)
98101
@test num_states(mdp_sparse) == 121 # 11 * 11 total states
102+
@test length(stateptr(mdp_sparse)) == 10 * 10 + 1 # 10 * 10 non-sink states
99103
@test stateptr(mdp_sparse)[end] == 10 * 10 * 9 + 1 # 10 * 10 non-sink states, 9 actions
100104

101105
prob_sparse = Problem(mdp_sparse, spec_sparse)

test/abstractions/additive_noise/direct.jl

+8-4
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ include("example_systems.jl")
2828
# Dense
2929
mdp_dense, spec_dense = simple_1d_direct()
3030
@test num_states(mdp_dense) == 11
31-
@test stateptr(mdp_dense)[end] == 11
31+
@test length(stateptr(mdp_dense)) == 11 # 10 non-sink states
32+
@test stateptr(mdp_dense)[end] == 11 # No control actions
3233

3334
prob_dense = Problem(mdp_dense, spec_dense)
3435

@@ -38,7 +39,8 @@ include("example_systems.jl")
3839
# Sparse
3940
mdp_sparse, spec_sparse = simple_1d_direct(; sparse = true)
4041
@test num_states(mdp_sparse) == 11
41-
@test stateptr(mdp_sparse)[end] == 11
42+
@test length(stateptr(mdp_sparse)) == 11 # 10 non-sink states
43+
@test stateptr(mdp_sparse)[end] == 11 # No control actions
4244

4345
prob_sparse = Problem(mdp_sparse, spec_sparse)
4446

@@ -87,7 +89,8 @@ end
8789
# Dense, input grid
8890
mdp_dense, spec_dense = modified_running_example_direct()
8991
@test num_states(mdp_dense) == 101
90-
@test stateptr(mdp_dense)[end] == 10 * 10 * 9 + 1
92+
@test length(stateptr(mdp_dense)) == 10 * 10 + 1 # 10 * 10 non-sink states
93+
@test stateptr(mdp_dense)[end] == 10 * 10 * 9 + 1 # 10 * 10 non-sink states, 9 actions
9194

9295
prob_dense = Problem(mdp_dense, spec_dense)
9396

@@ -97,7 +100,8 @@ end
97100
# Sparse, input grid
98101
mdp_sparse, spec_sparse = modified_running_example_direct(; sparse = true)
99102
@test num_states(mdp_sparse) == 101
100-
@test stateptr(mdp_sparse)[end] == 10 * 10 * 9 + 1
103+
@test length(stateptr(mdp_sparse)) == 10 * 10 + 1 # 10 non-sink states
104+
@test stateptr(mdp_sparse)[end] == 10 * 10 * 9 + 1 # 10 * 10 non-sink states, 9 actions
101105

102106
prob_sparse = Problem(mdp_sparse, spec_sparse)
103107

test/abstractions/gaussian_process.jl

+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
using Revise, Test
2+
using LinearAlgebra, LazySets
3+
using IntervalMDP, IntervalMDPAbstractions
4+
5+
# System definition
6+
7+
# Action 1
8+
gp_region1_action1 = AbstractedGaussianProcessRegion(
9+
Hyperrectangle(low = [-0.5, -0.5], high = [0.0, 0.0]),
10+
[-0.5, 0.5],
11+
[0.0, 0.6],
12+
[0.1, 0.3],
13+
[0.2, 0.4],
14+
)
15+
gp_region2_action1 = AbstractedGaussianProcessRegion(
16+
Hyperrectangle(low = [0.0, -0.5], high = [0.5, 0.0]),
17+
[-0.5, 0.5],
18+
[0.0, 0.6],
19+
[0.1, 0.3],
20+
[0.2, 0.4],
21+
)
22+
gp_region3_action1 = AbstractedGaussianProcessRegion(
23+
Hyperrectangle(low = [-0.5, 0.0], high = [0.0, 0.5]),
24+
[-0.5, 0.5],
25+
[0.0, 0.6],
26+
[0.1, 0.3],
27+
[0.2, 0.4],
28+
)
29+
gp_region4_action1 = AbstractedGaussianProcessRegion(
30+
Hyperrectangle(low = [0.0, 0.0], high = [0.5, 0.5]),
31+
[-0.5, 0.5],
32+
[0.0, 0.6],
33+
[0.1, 0.3],
34+
[0.2, 0.4],
35+
)
36+
37+
gp_action1 = [gp_region1_action1, gp_region2_action1, gp_region3_action1, gp_region4_action1]
38+
39+
# Action 2
40+
gp_region1_action2 = AbstractedGaussianProcessRegion(
41+
Hyperrectangle(low = [-0.5, -0.5], high = [0.0, 0.0]),
42+
[-0.5, 0.5],
43+
[0.0, 0.6],
44+
[0.1, 0.3],
45+
[0.2, 0.4],
46+
)
47+
gp_region2_action2 = AbstractedGaussianProcessRegion(
48+
Hyperrectangle(low = [0.0, -0.5], high = [0.5, 0.0]),
49+
[-0.5, 0.5],
50+
[0.0, 0.6],
51+
[0.1, 0.3],
52+
[0.2, 0.4],
53+
)
54+
gp_region3_action2 = AbstractedGaussianProcessRegion(
55+
Hyperrectangle(low = [-0.5, 0.0], high = [0.0, 0.5]),
56+
[-0.5, 0.5],
57+
[0.0, 0.6],
58+
[0.1, 0.3],
59+
[0.2, 0.4],
60+
)
61+
gp_region4_action2 = AbstractedGaussianProcessRegion(
62+
Hyperrectangle(low = [0.0, 0.0], high = [0.5, 0.5]),
63+
[-0.5, 0.5],
64+
[0.0, 0.6],
65+
[0.1, 0.3],
66+
[0.2, 0.4],
67+
)
68+
69+
gp_action2 = [gp_region1_action2, gp_region2_action2, gp_region3_action2, gp_region4_action2]
70+
71+
# Noise
72+
w_variance = [0.2, 0.2]
73+
w_stddev = sqrt.(w_variance)
74+
w = AdditiveDiagonalGaussianNoise(w_stddev)
75+
76+
dyn = AbstractedGaussianProcess([gp_action1, gp_action2])
77+
initial_region = Hyperrectangle(low = [-0.1, -0.1], high = [0.1, 0.1])
78+
sys = System(dyn, initial_region)
79+
80+
horizon = 10
81+
avoid_region = EmptySet(2)
82+
prop = FiniteTimeRegionSafety(avoid_region, horizon)
83+
spec = Specification(prop, Pessimistic, Maximize)
84+
85+
prob = AbstractionProblem(sys, spec)
86+
87+
X = Hyperrectangle(; low = [-0.5, -0.5], high = [0.5, 0.5])
88+
state_abs = StateUniformGridSplit(X, (2, 2))
89+
input_abs = InputDiscrete([1, 2])
90+
91+
92+
@testset "direct vs decoupled" begin
93+
# Decoupled
94+
target_model = OrthogonalIMDPTarget()
95+
mdp_decoupled, abstract_spec_decoupled = abstraction(prob, state_abs, input_abs, target_model)
96+
97+
@test num_states(mdp_decoupled) == 3 * 3
98+
@test length(stateptr(mdp_decoupled)) == 5 # 4 non-sink states
99+
@test stateptr(mdp_decoupled)[end] == 4 * 2 + 1 # 4 non-sink states, 2 control actions
100+
101+
prob_decoupled = Problem(mdp_decoupled, abstract_spec_decoupled)
102+
103+
V_decoupled, k, res = value_iteration(prob_decoupled)
104+
@test k == 10
105+
106+
# Direct
107+
target_model = IMDPTarget()
108+
mdp_direct, abstract_spec_direct = abstraction(prob, state_abs, input_abs, target_model)
109+
110+
@test num_states(mdp_direct) == 2 * 2 + 1
111+
@test length(stateptr(mdp_direct)) == 5 # 4 non-sink states
112+
@test stateptr(mdp_direct)[end] == 4 * 2 + 1 # 4 non-sink states, 2 control actions
113+
114+
prob_direct = Problem(mdp_direct, abstract_spec_direct)
115+
116+
V_direct, k, res = value_iteration(prob_direct)
117+
@test k == 10
118+
119+
@test all(V_decoupled[1:end-1, 1:end-1] .≥ reshape(V_direct[1:end-1], 2, 2))
120+
end

test/dynamics/stochastical_switched.jl

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
using Revise, Test
2+
using IntervalMDPAbstractions, LazySets
3+
14
A1 = [
25
0.1 0.9
36
0.8 0.2

test/specifications.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ end
3535

3636
target_model = IMDPTarget()
3737

38-
time_horizon = 10
39-
prop = FiniteTimeRegionReachAvoid(reach_region, avoid_region, time_horizon)
38+
horizon = 10
39+
prop = FiniteTimeRegionReachAvoid(reach_region, avoid_region, horizon)
4040
spec = Specification(prop, Pessimistic, Maximize)
4141

4242
prob = AbstractionProblem(sys, spec)

0 commit comments

Comments
 (0)