-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathray_optimizer.py
More file actions
273 lines (245 loc) · 11 KB
/
ray_optimizer.py
File metadata and controls
273 lines (245 loc) · 11 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
'''This code implements two modes of ray potentials from our paper
https://arxiv.org/pdf/1604.02885.pdf: convex and nonconvex.
As our simple example in Section A.5 shows, the convex mode fails even
in very simple cases, taking all-0.5 solution which is impossible to round
to a proper close-to-binary solution. Nonconvex mode adds
a special constraint: y_occ_i <= max(0, y_free_i-1 + x_occ_i - 1.0) -- see
equation (10) in our paper. We call it a visibility consistency constraint.
It prevents the algorithm from taking a bad solution. At every optimization
step, we take the active branch of this constraint -- thus essentially
implementing an approximate majorize-minimize optimization strategy
(the exact majorize-minimize would mean that we estimate what branches are
active at some point, then optimize to convergence, and only then
re-estimate what branches are active again). Empirically, this approximate
strategy works well.
In the code below we use a first-order primal-dual optimization algorithm to
optimize ray potentials.
For details on this algorithm, see equation (18) in Section 3.1 in
"Diagonal preconditioning for first order primal-dual algorithms
in convex optimization" by Pock & Chambolle. We use alpha = 1 in equation (10).
Basically, update to each variable x_i needs to be normalized by absolute
sum of coefficients in K for terms in <Kx, y> where x_i appears.
Extra primal variables (2 * x_k+1 - x_k) are used for updating
dual variables.'''
from functools import reduce
from copy import deepcopy
import random
class RayOptimizer:
def __init__(self,
grid_sizes,
rays,
ray_costs_occ,
ray_costs_free,
nonconvex):
self.grid_sizes = grid_sizes
self.n_cells = reduce((lambda x, y: x * y), self.grid_sizes)
self.rays = rays
self.ray_costs_occ = ray_costs_occ
self.ray_costs_free = ray_costs_free
self.nonconvex = nonconvex
self.init_variables()
def clamp01(self, value):
return max(0.0, min(1.0, value))
def clamp_nonneg(self, value):
return max(0.0, value)
def get_zeros_along_rays(self):
return [[0.0] * len(ray) for ray in self.rays]
def get_ones_along_rays(self):
return [[1.0] * len(ray) for ray in self.rays]
def get_zeros_all_cells(self):
return [0.0] * self.n_cells
def get_random_all_cells(self):
return [random.random() for _ in range(self.n_cells)]
def init_variables(self):
# primal variables
self.y_occ = self.get_zeros_along_rays()
self.y_free = self.get_ones_along_rays()
self.x_occ = self.get_random_all_cells()
# extra primal variables
self.extra_y_occ = deepcopy(self.y_occ)
self.extra_y_free = deepcopy(self.y_free)
self.extra_x_occ = deepcopy(self.x_occ)
# dual variables: correspond to constraints
self.dual_y_occ_y_free = self.get_zeros_along_rays()
self.dual_y_free_y_free = self.get_zeros_along_rays()
self.dual_y_occ_x_occ = self.get_zeros_along_rays()
self.dual_y_free_x_occ = self.get_zeros_along_rays()
# in particular, visibility consistency constraints
if self.nonconvex:
self.dual_vis_con = self.get_zeros_along_rays()
def step(self):
self.primal()
self.dual()
def primal(self):
# deltas for primal variables
self.dy_occ = self.get_zeros_along_rays()
self.dy_free = self.get_zeros_along_rays()
self.dx_occ = self.get_zeros_all_cells()
# preconditioners: we will divide deltas by them
self.pc_y_occ = self.get_zeros_along_rays()
self.pc_y_free = self.get_zeros_along_rays()
self.pc_x_occ = self.get_zeros_all_cells()
for ray_ind, ray in enumerate(self.rays):
for ray_pos, cell in enumerate(ray):
# costs
self.dy_occ[ray_ind][ray_pos] += self.ray_costs_occ[ray_ind][ray_pos]
if ray_pos >= 1:
# y_occ_y_free constraint
self.dy_occ[ray_ind][ray_pos] += (
self.dual_y_occ_y_free[ray_ind][ray_pos])
self.pc_y_occ[ray_ind][ray_pos] += 1.0
self.dy_free[ray_ind][ray_pos - 1] -= (
self.dual_y_occ_y_free[ray_ind][ray_pos])
self.pc_y_free[ray_ind][ray_pos - 1] += 1.0
# y_free_y_free constraint
self.dy_free[ray_ind][ray_pos] += (
self.dual_y_free_y_free[ray_ind][ray_pos])
self.pc_y_free[ray_ind][ray_pos] += 1.0
self.dy_free[ray_ind][ray_pos - 1] -= (
self.dual_y_free_y_free[ray_ind][ray_pos])
self.pc_y_free[ray_ind][ray_pos - 1] += 1.0
# visibility consistency constraint
if self.nonconvex:
linear_branch = (self.y_free[ray_ind][ray_pos - 1] +
self.x_occ[cell] - 1.0)
self.dy_occ[ray_ind][ray_pos] += (
self.dual_vis_con[ray_ind][ray_pos])
self.pc_y_occ[ray_ind][ray_pos] += 1.0
if linear_branch > 0: # linear branch is active in the constraint
self.dy_free[ray_ind][ray_pos - 1] -= (
self.dual_vis_con[ray_ind][ray_pos])
self.pc_y_free[ray_ind][ray_pos - 1] += 1.0
self.dx_occ[cell] -= (
self.dual_vis_con[ray_ind][ray_pos])
self.pc_x_occ[cell] += 1.0
# y_occ_x_occ constraint
self.dy_occ[ray_ind][ray_pos] += self.dual_y_occ_x_occ[ray_ind][ray_pos]
self.pc_y_occ[ray_ind][ray_pos] += 1.0
self.dx_occ[cell] -= self.dual_y_occ_x_occ[ray_ind][ray_pos]
self.pc_x_occ[cell] += 1.0
# y_free_x_occ constraint
self.dy_free[ray_ind][ray_pos] += (
self.dual_y_free_x_occ[ray_ind][ray_pos])
self.pc_y_free[ray_ind][ray_pos] += 1.0
self.dx_occ[cell] += self.dual_y_free_x_occ[ray_ind][ray_pos]
self.pc_x_occ[cell] += 1.0
# costs
self.dy_free[ray_ind][-1] += self.ray_costs_free[ray_ind]
self.divide_deltas_by_preconditioners()
self.backup_primal()
self.update_primal_by_deltas()
self.clamp_primal()
self.compute_extra_primal()
def divide_deltas_by_preconditioners(self):
for ray_ind, ray in enumerate(self.rays):
for ray_pos, cell in enumerate(ray):
self.dy_occ[ray_ind][ray_pos] /= max(1.0,
self.pc_y_occ[ray_ind][ray_pos])
self.dy_free[ray_ind][ray_pos] /= max(1.0,
self.pc_y_free[ray_ind][ray_pos])
for cell in range(self.n_cells):
# avoid division by 0, could happen if no ray hits the cell
self.dx_occ[cell] /= max(1.0, self.pc_x_occ[cell])
def backup_primal(self):
self.prev_y_occ = deepcopy(self.y_occ)
self.prev_y_free = deepcopy(self.y_free)
self.prev_x_occ = deepcopy(self.x_occ)
def update_primal_by_deltas(self):
for ray_ind, ray in enumerate(self.rays):
for ray_pos, cell in enumerate(ray):
self.y_occ[ray_ind][ray_pos] -= self.dy_occ[ray_ind][ray_pos]
self.y_free[ray_ind][ray_pos] -= self.dy_free[ray_ind][ray_pos]
for cell in range(self.n_cells):
self.x_occ[cell] -= self.dx_occ[cell]
def clamp_primal(self):
for ray_ind, ray in enumerate(self.rays):
for ray_pos, cell in enumerate(ray):
self.y_occ[ray_ind][ray_pos] = self.clamp01(
self.y_occ[ray_ind][ray_pos])
self.y_free[ray_ind][ray_pos] = self.clamp01(
self.y_free[ray_ind][ray_pos])
for cell in range(self.n_cells):
self.x_occ[cell] = self.clamp01(self.x_occ[cell])
def compute_extra_primal(self):
for ray_ind, ray in enumerate(self.rays):
for ray_pos, cell in enumerate(ray):
self.extra_y_occ[ray_ind][ray_pos] = (
2.0 * self.y_occ[ray_ind][ray_pos] -
self.prev_y_occ[ray_ind][ray_pos])
self.extra_y_free[ray_ind][ray_pos] = (
2.0 * self.y_free[ray_ind][ray_pos] -
self.prev_y_free[ray_ind][ray_pos])
for cell in range(self.n_cells):
self.extra_x_occ[cell] = (
2.0 * self.x_occ[cell] - self.prev_x_occ[cell])
def dual(self):
# inline division by preconditioners because it's easier than for primal
for ray_ind, ray in enumerate(self.rays):
for ray_pos, cell in enumerate(ray):
if ray_pos >= 1:
self.dual_y_occ_y_free[ray_ind][ray_pos] += (
self.extra_y_occ[ray_ind][ray_pos] -
self.extra_y_free[ray_ind][ray_pos - 1]) / 2.0
self.dual_y_free_y_free[ray_ind][ray_pos] += (
self.extra_y_free[ray_ind][ray_pos] -
self.extra_y_free[ray_ind][ray_pos - 1]) / 2.0
# visibility consistency
if self.nonconvex:
linear_branch = (self.y_free[ray_ind][ray_pos - 1] +
self.x_occ[cell] - 1.0)
if linear_branch > 0: # re-majorize at every step
self.dual_vis_con[ray_ind][ray_pos] += (
self.extra_y_occ[ray_ind][ray_pos] -
self.extra_y_free[ray_ind][ray_pos - 1] -
self.extra_x_occ[cell] + 1.0) / 3.0
else:
self.dual_vis_con[ray_ind][ray_pos] += (
self.extra_y_occ[ray_ind][ray_pos])
self.dual_y_occ_x_occ[ray_ind][ray_pos] += (
self.extra_y_occ[ray_ind][ray_pos] -
self.extra_x_occ[cell]) / 2.0
self.dual_y_free_x_occ[ray_ind][ray_pos] += (
self.extra_y_free[ray_ind][ray_pos] +
self.extra_x_occ[cell] - 1.0) / 2.0
self.clamp_duals()
def get_cost(self):
cost = 0.0
for ray_ind, ray in enumerate(self.rays):
for ray_pos, cell in enumerate(ray):
cost += (self.ray_costs_occ[ray_ind][ray_pos] *
self.y_occ[ray_ind][ray_pos])
cost += self.ray_costs_free[ray_ind] * self.y_free[ray_ind][-1]
return cost
def clamp_duals(self):
for ray_ind, ray in enumerate(self.rays):
for ray_pos, cell in enumerate(ray):
self.dual_y_occ_y_free[ray_ind][ray_pos] = self.clamp_nonneg(
self.dual_y_occ_y_free[ray_ind][ray_pos])
self.dual_y_free_y_free[ray_ind][ray_pos] = self.clamp_nonneg(
self.dual_y_free_y_free[ray_ind][ray_pos])
self.dual_y_occ_x_occ[ray_ind][ray_pos] = self.clamp_nonneg(
self.dual_y_occ_x_occ[ray_ind][ray_pos])
self.dual_y_free_x_occ[ray_ind][ray_pos] = self.clamp_nonneg(
self.dual_y_free_x_occ[ray_ind][ray_pos])
if self.nonconvex:
self.dual_vis_con[ray_ind][ray_pos] = self.clamp_nonneg(
self.dual_vis_con[ray_ind][ray_pos])
def get_solution(self):
return self.x_occ
def print_state(self):
self.print_cost()
self.print_primal()
self.print_dual()
def print_primal(self):
print('y_occ:\n', self.y_occ)
print('y_free:\n', self.y_free)
print('x_occ:\n', self.x_occ)
def print_dual(self):
print('dual_y_occ_y_free:\n', self.dual_y_occ_y_free)
print('dual_y_free_y_free:\n', self.dual_y_free_y_free)
print('dual_y_occ_x_occ:\n', self.dual_y_occ_x_occ)
print('dual_y_free_x_occ:\n', self.dual_y_free_x_occ)
if self.nonconvex:
print('dual_vis_con:\n', self.dual_vis_con)
def print_cost(self):
print('cost:\n', self.get_cost())