-
Notifications
You must be signed in to change notification settings - Fork 5
/
random.jl
68 lines (55 loc) · 1.77 KB
/
random.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
using Plots
include("reach.jl")
# Returns H-rep of various input sets
function input_constraints_random(weights, type::String)
if type == "big box"
in_dim = size(weights[1],2) - 1
Aᵢ_pos = Matrix{Float64}(I, in_dim, in_dim)
Aᵢ_neg = Matrix{Float64}(-I, in_dim, in_dim)
Aᵢ = vcat(Aᵢ_pos, Aᵢ_neg)
bᵢ = 1e2*ones(2*in_dim)
elseif type == "box"
in_dim = size(weights[1],2) - 1
Aᵢ_pos = Matrix{Float64}(I, in_dim, in_dim)
Aᵢ_neg = Matrix{Float64}(-I, in_dim, in_dim)
Aᵢ = vcat(Aᵢ_pos, Aᵢ_neg)
bᵢ = 2*ones(2*in_dim)
elseif type == "hexagon"
Aᵢ = [1. 0.; -1. 0.; 0. 1.; 0. -1.; 1. 1.; -1. 1.; 1. -1.; -1. -1.]
bᵢ = [5., 5., 5., 5., 8., 8., 8., 8.]
else
error("Invalid input constraint specification.")
end
return Aᵢ, bᵢ
end
# Plots all polyhedra
function plot_hrep_random(ap2constraints; limit=Inf)
plt = plot(reuse = false)
for (i,ap) in enumerate(keys(ap2constraints))
A, b = ap2constraints[ap]
reg = HPolytope(constraints_list(A,b))
if isempty(reg)
@show reg
error("Empty polyhedron.")
end
plot!(plt, reg, fontfamily=font(14, "Computer Modern"), tickfont = (12))
i == limit ? (return plt) : nothing
end
return plt
end
###########################
######## SCRIPTING ########
###########################
in_d, out_d, hdim, layers = 2, 2, 10, 5
weights = random_net(in_d, out_d, hdim, layers)
Aᵢ, bᵢ = input_constraints_random(weights, "box")
Aₒ = Matrix{Float64}(undef,0,0)
bₒ = Vector{Float64}()
@time begin
ap2input, ap2output, ap2map, ap2backward = compute_reach(weights, Aᵢ, bᵢ, [Aₒ], [bₒ], reach=true)
end
@show length(ap2input)
# Plot all regions (only 2D input) #
if in_d == 2
plt_in = plot_hrep_random(ap2input, limit=Inf)
end