-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzonotope.sage
202 lines (170 loc) · 6.27 KB
/
zonotope.sage
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
200
201
202
#!/usr/bin/env sage
"""
A collection of functions for working with zonotopes in Sage.
"""
def zonotope_from_segments(segments):
"""
Returns the polyhedron for the zonotope given by the Minkowski sum
of ``segments``, where ``segments`` is a list of generating
line-segment endpoint points pairs ``(s,t)``.
"""
return sum(Polyhedron(vertices=s) for s in segments)
def zonotope_from_vectors(V):
"""
Returns the zonotope constructed from the vectors ``v`` iterated
over ``V``, using the line segments ``[0,v]`` as generators.
"""
return zonotope_from_segments(segments_from_origin(V))
def segments_from_origin(V):
"""
Convert an ``n-by-d`` matrix ``V`` to an iterator of segments of
the form ``(zero_vector(d),v)`` for the rows ``v`` in ``V``.
"""
n, d = V.dimensions()
origin = zero_vector(d)
return ((origin, row) for row in V)
def robust_region(V, z = None):
"""
Computes the robust sub-polytope of the zonotope ``z`` that was
generated from the matrix ``V``.
"""
if z is None:
z = zonotope_from_vectors(V)
ieqs = []
for H in z.Hrepresentation():
# We have H = { x : a*x + b >= 0 } so
#
# H - v = { x - v: a*x + b >= 0 }
# = { y : a*y + (b + a*v) >= 0 }
#
# so we only need the endpoint v minimizing a*v
a = H.A()
b = H.b() + min(a*v for v in V)
ieqs.append([b] + list(a))
return Polyhedron(ieqs=ieqs)
def robust_region_naive(V, z = None):
"""
See ``robust_region(V, z)``.
"""
if z is None:
z = zonotope_from_vectors(V)
n, d = V.dimensions()
origin = zero_vector(d)
return reduce(lambda a,b: a&b, (z-v for v in V))
def zonotope_halfspaces(V, verbose=False):
"""
A proof of concept implementation that generates the
H-representation of the zonotope directly from the generators.
"""
n, d = V.dimensions()
ieqs = set([])
generator_sum = sum(V)
for U in Combinations(V, d-1):
normal = Matrix(ZZ, U).transpose().integer_kernel().gen()
offset_vector = zero_vector(ZZ, d)
for v in V:
normal_proj = normal.dot_product(v)
if normal_proj <= 0:
offset_vector += v
# standardize the normal to the smallest integral vector representing
# the same hyperplane
standardizer = abs(gcd(normal))
if standardizer > 0:
normal /= standardizer
offset = - ( normal.dot_product(offset_vector) )
# { x : normal*x + offset >= 0 } is one of the halfspaces
# comptue the antipodal inequality
antipode_normal = -normal
antipode_offset = ( generator_sum.dot_product(normal) ) + offset
# { x: antipode_normal + antipode_offset >= 0 } is the antipodal halfspace
ieqs.add(tuple(offset) + tuple(normal))
ieqs.add(tuple(antipode_offset) + tuple(antipode_normal))
if verbose == True:
print "n={n}, d={d}, n_ieqs={n_ieqs}".format(\
n=n, d=d, n_ieqs=len(ieqs))
return ieqs
def data_iterator_from_file(f, number_field=QQ):
"""
Converts a data file formatted according to make_data to an iterator over
its numerical values.
"""
return (number_field(i) for i in f.read().split())
def generators_from_iterator(data, number_field=QQ):
"""
Reads a data iterator and returns an iterator over the matrices defined by
the iterator.
"""
n_tests = data.next()
t = 0
while t < n_tests:
n = data.next()
d = data.next()
generators = matrix(number_field, n, d)
for i in xrange(n):
for j in xrange(d):
generators[i,j] = data.next()
yield generators
t += 1
def zonotope_halfspaces_test(f, number_field=QQ):
data_iterator = data_iterator_from_file(f, number_field)
for V in generators_from_iterator(data_iterator):
zonotope_halfspaces(V, verbose=True)
def show_robust_region(z, V, alpha_baseline=0.6, **kwargs):
"""
Returns an object for the visualization of the generalized robust
regions of ``z`` with respect to the generating matrix ``V``.
"""
n, d = V.dimensions()
assert d <= 3
# Init opacity options
alpha = alpha_baseline / n
if d <= 2:
kw_alpha = 'alpha'
if d == 3:
kw_alpha = 'opacity'
kwargs[kw_alpha] = alpha
# Handle scaling of outlines etc for resolution-independence in
# the output.
default_figsize = 5
if 'figsize' not in kwargs:
kwargs['figsize'] = default_figsize
# fig_scale = float(kwargs['figsize']) / default_figsize
# kwargs['thickness'] = 3 * fig_scale
# Overlay all sub-zonotopes when a generator from V is removed to
# the visualization. We limit the opacity of each sub-zonotope,
# such intersections and outlines are easier to discern.
output = z.show(**kwargs)
robust = z
origin = zero_vector(d)
for v in V:
z_v = z - v
output += (z & z_v).show(**kwargs)
robust &= z_v
# The robust region should stand out, so we give it full opacity
kwargs[kw_alpha] = 1
output += robust.show(**kwargs)
# Similarly, the generators need to stand out, so they get greater
# thickness (there is only limited built in support for changing
# the color, so we don't do that for now)
generator_thickness = 6
# kwargs['thickness'] = generator_thickness * fig_scale
if d == 2:
kwargs['color'] = 'red'
for v in V:
segment = Polyhedron(vertices=(origin,v))
output += segment.show(**kwargs)
return output
#if __name__ == '__main__':
# # Set up a 2D demo zonotope. Works pretty much as expected.
# V_2 = matrix(QQ, [[-1,1],[1,0], [1,-2], [2,3], [-0.3,-1]])
# z_2 = zonotope_from_vectors(V_2)
# r_2 = show_robust_region(z_2, V_2)
# r_2.save('robust_2.png', axes=False, figsize=40)
#
# # Set up a 3D demo zonotope. This is still broken -- the robust
# # region graphics object doesn't seem to play well with the
# # raytracer that Sage uses.
# V_3 = matrix(QQ, [[-1,1,1],[1,0,1], [1,-5,1], [2,3,-1], [1,-1,-1]])
# z_3 = zonotope_from_vectors(V_3)
# r_3 = show_robust_region(z_3, V_3, frame=False)
# r_3.save('robust_3.png')