-
Notifications
You must be signed in to change notification settings - Fork 0
/
loading_problem.py
403 lines (352 loc) · 16 KB
/
loading_problem.py
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
import json
import math
import numpy as np
from dimod import SampleSet, BQM, Binary
from aircraft_data import AircraftData
from utils import get_container_t, get_container_d, get_num_bits
class LoadingProblem:
def __init__(self, aircraft: AircraftData,
container_types: np.ndarray,
container_masses: np.ndarray,
target_cg,
zero_payload_mass,
zero_payload_cg):
if len(container_types) != len(container_masses):
raise ValueError(
f'Mismatching container types and masses lengths ({len(container_types)},{len(container_masses)})')
self.aircraft = aircraft
self.container_types = container_types
self.container_masses = container_masses
self.target_cg = target_cg * aircraft.payload_area_length
self.zero_payload_mass = zero_payload_mass
self.zero_payload_cg = zero_payload_cg * aircraft.payload_area_length
self.container_t = get_container_t(container_types)
self.container_d = get_container_d(container_types)
self.coefficients = {'pl_o': 1.0,
'pl_w': 1.0,
'pl_d': 1.0,
'pl_c': 1.0,
'cl_t': 1.0,
'cl_u': 1.0,
'cl_l': 1.0,
'sl_l': 1.0,
'sl_r': 1.0}
self.num_slack_variables = self.get_num_slack_vars()
self.enabled_constraints = {'pl_o', 'pl_w', 'pl_d', 'pl_c'}
def enable_constraints(self, constraints: set[str]):
self.enabled_constraints.update(constraints)
def disable_constraints(self, constraints: set[str]):
self.enabled_constraints -= constraints
def get_objective_bqm(self) -> BQM:
bqm = BQM.empty('BINARY')
for i, t_i in enumerate(self.container_t):
for pos in range(self.aircraft.num_positions):
bqm += Binary(f'p_{i}_{pos}', -t_i * self.container_masses[i])
return bqm
def get_no_overlaps_bqm(self) -> BQM:
num_slack = self.num_slack_variables['pl_o'] // self.aircraft.num_positions
bqm = BQM.empty('BINARY')
for pos in range(self.aircraft.num_positions):
bqm_pos = BQM.empty('BINARY')
for i, d_i in enumerate(self.container_d):
bqm_pos += Binary(f'p_{i}_{pos}', d_i)
for k in range(num_slack):
bqm_pos += Binary(f'v_o_{pos}_{k}', 2 ** k)
bqm_pos += -1
bqm_pos = bqm_pos ** 2
bqm += bqm_pos
bqm.scale(self.coefficients['pl_o'])
return bqm
def get_no_duplicates_bqm(self) -> BQM:
num_slack = self.num_slack_variables['pl_d'] // len(self.container_types)
bqm = BQM.empty('BINARY')
for c, t_i in enumerate(self.container_t):
cont_bqm = BQM.empty('BINARY')
# Quadratic terms
for pos in range(self.aircraft.num_positions):
cont_bqm += Binary(f'p_{c}_{pos}', t_i)
for k in range(num_slack):
cont_bqm += Binary(f'v_d_{c}_{k}', 2 ** k)
cont_bqm += -1
cont_bqm = cont_bqm ** 2
bqm += cont_bqm
bqm.scale(self.coefficients['pl_d'])
return bqm
def get_contiguity_bqm(self) -> BQM:
bqm = BQM.empty('BINARY')
for i, t_i in enumerate(self.container_t):
cont_bqm = BQM.empty('BINARY')
# Not a type 3 container
if t_i >= 1:
continue
else:
for pos in range(self.aircraft.num_positions - 1):
actual_p = Binary(f'p_{i}_{pos}')
next_p = Binary(f'p_{i}_{pos + 1}')
cont_bqm += 1 / 2 * actual_p - (actual_p * next_p)
cont_bqm += Binary(f'p_{i}_{self.aircraft.num_positions - 1}', 1 / 2)
bqm += cont_bqm
bqm.scale(self.coefficients['pl_c'])
return bqm
def get_max_capacity_bqm(self) -> BQM:
num_slack = self.num_slack_variables['pl_w']
bqm = BQM.empty('BINARY')
for i, t_i in enumerate(self.container_t):
for pos in range(self.aircraft.num_positions):
coef = t_i * self.container_masses[i]
bqm += Binary(f'p_{i}_{pos}', coef)
for k in range(num_slack):
bqm += Binary(f'v_w_{k}', 2 ** k)
bqm += -self.aircraft.max_payload
bqm = bqm ** 2
bqm.scale(self.coefficients['pl_w'])
return bqm
def get_x_cg(self) -> tuple[BQM, BQM]:
x_cg_up = BQM.empty('BINARY')
x_cg_down = BQM.empty('BINARY')
for i, t_i in enumerate(self.container_t):
for pos in range(self.aircraft.num_positions):
x_cg_up += Binary(f'p_{i}_{pos}', t_i * self.container_masses[i] * self.aircraft.locations[pos])
x_cg_down += Binary(f'p_{i}_{pos}', t_i * self.container_masses[i])
x_cg_up += self.zero_payload_mass * self.zero_payload_cg
x_cg_down += self.zero_payload_mass
return x_cg_up, x_cg_down
def get_cg_target_bqm(self) -> BQM:
x_cg_up, x_cg_down = self.get_x_cg()
bqm = x_cg_up - x_cg_down * self.target_cg
bqm = bqm ** 2
bqm *= self.coefficients['cl_t']
return bqm
def get_cg_lower_bqm(self):
x_cg_up, x_cg_down = self.get_x_cg()
bqm = x_cg_up - x_cg_down * self.aircraft.min_cg
for k in range(self.num_slack_variables['cl_l']):
bqm += Binary(f'v_cl_l_{k}', -(2 ** k))
bqm = bqm ** 2
bqm *= self.coefficients['cl_l']
return bqm
def get_cg_upper_bqm(self):
x_cg_up, x_cg_down = self.get_x_cg()
bqm = x_cg_up - x_cg_down * self.aircraft.max_cg
for k in range(self.num_slack_variables['cl_u']):
bqm += Binary(f'v_cl_u_{k}', 2 ** k)
bqm = bqm ** 2
bqm *= self.coefficients['cl_u']
return bqm
def get_left_shear_at_pos_bqm(self, u: int):
bqm = BQM('BINARY')
for i, t_i in enumerate(self.container_t):
for pos in range(u + 1):
bqm += Binary(f'p_{i}_{pos}', t_i * self.container_masses[i])
return bqm
def get_right_shear_at_pos_bqm(self, u: int):
bqm = BQM('BINARY')
for i, t_i in enumerate(self.container_t):
for pos in range(u + 1, self.aircraft.num_positions):
bqm += Binary(f'p_{i}_{pos}', t_i * self.container_masses[i])
return bqm
def get_left_shear_bqm(self):
bqm = BQM('BINARY')
half_pos = int(self.aircraft.num_positions / 2.0)
for u in range(half_pos):
u_bqm = self.get_left_shear_at_pos_bqm(u)
for k in range(self.num_slack_variables['sl'][u]):
u_bqm += Binary(f'v_sl_l_{u}_{k}', 2 ** k)
u_bqm += -self.aircraft.shear_curve[u]
bqm += u_bqm ** 2
if self.aircraft.num_positions % 2 != 0:
zero_bqm = BQM('BINARY')
zero_bqm += self.get_left_shear_at_pos_bqm(half_pos - 1)
for i, t_i in enumerate(self.container_t):
zero_bqm += Binary(f'p_{i}_{half_pos}', t_i * self.container_masses[i] / 2)
for k in range(self.num_slack_variables['sl'][half_pos]):
zero_bqm += Binary(f'v_sl_l_c_0', 2 ** k)
zero_bqm += -self.aircraft.shear_curve[half_pos]
bqm += zero_bqm ** 2
return self.coefficients['sl_l'] * bqm
def get_right_shear_bqm(self):
bqm = BQM('BINARY')
half_pos = int(self.aircraft.num_positions / 2.0)
is_even_pos = self.aircraft.num_positions % 2 == 0
u_min = half_pos - 1 if is_even_pos else half_pos
limit_offset = 1 if is_even_pos else 2
for u in range(u_min, self.aircraft.num_positions - 1):
u_bqm = self.get_right_shear_at_pos_bqm(u)
for k in range(self.num_slack_variables['sl'][u + limit_offset]):
u_bqm += Binary(f'v_sl_r_{u}_{k}', 2 ** k)
u_bqm += -self.aircraft.shear_curve[u + limit_offset]
bqm += u_bqm ** 2
if not is_even_pos:
zero_bqm = BQM('BINARY')
zero_bqm += self.get_right_shear_at_pos_bqm(half_pos)
for i, t_i in enumerate(self.container_t):
zero_bqm += Binary(f'p_{i}_{half_pos}', t_i * self.container_masses[i] / 2)
for k in range(self.num_slack_variables['sl'][half_pos + 1]):
zero_bqm += Binary(f'v_sl_r_c_0', 2 ** k)
zero_bqm += -self.aircraft.shear_curve[half_pos + 1]
bqm += zero_bqm ** 2
return self.coefficients['sl_r'] * bqm
def get_bqm(self) -> BQM:
bqm = self.get_objective_bqm()
if 'pl_o' in self.enabled_constraints:
bqm += self.get_no_overlaps_bqm()
if 'pl_d' in self.enabled_constraints:
bqm += self.get_no_duplicates_bqm()
if 'pl_w' in self.enabled_constraints:
bqm += self.get_max_capacity_bqm()
if 'pl_c' in self.enabled_constraints:
bqm += self.get_contiguity_bqm()
if 'cl_t' in self.enabled_constraints:
bqm += self.get_cg_target_bqm()
if 'cl_l' in self.enabled_constraints:
bqm += self.get_cg_lower_bqm()
if 'cl_u' in self.enabled_constraints:
bqm += self.get_cg_upper_bqm()
if 'sl' in self.enabled_constraints:
bqm += self.get_left_shear_bqm()
bqm += self.get_right_shear_bqm()
return bqm
def parse_solution(self, results: SampleSet) -> np.ndarray:
solutions = []
for r in results.samples():
cont_occ = np.zeros(shape=(len(self.container_types), self.aircraft.num_positions))
for k, v in r.items():
if k[0] == 'p':
i = int(k[2])
j = int(k[4])
cont_occ[i, j] = v
solutions.append(cont_occ)
return np.array(solutions)
def filter_solutions(self, solutions: np.ndarray, verbose=True) -> list:
res = []
violated_constraints = dict.fromkeys(self.enabled_constraints, 0)
for s in solutions:
discard = False
if 'pl_o' in self.enabled_constraints and not self.check_overlap_constraint(s):
violated_constraints['pl_o'] += 1
discard = True
if 'pl_d' in self.enabled_constraints and not self.check_no_duplicates_constraint(s):
violated_constraints['pl_d'] += 1
discard = True
if 'pl_w' in self.enabled_constraints and not self.check_max_weight_constraint(s):
discard = True
violated_constraints['pl_w'] += 1
if 'pl_c' in self.enabled_constraints and not self.check_contiguity_constraint(s):
discard = True
violated_constraints['pl_c'] += 1
if 'cl_u' in self.enabled_constraints and not self.check_cg_upper_bound_constraint(s):
discard = True
violated_constraints['cl_u'] += 1
if 'cl_l' in self.enabled_constraints and not self.check_cg_lower_bound_constraint(s):
discard = True
violated_constraints['cl_l'] += 1
if 'sl' in self.enabled_constraints and not self.check_shear_constraint(s):
discard = True
violated_constraints['sl'] += 1
if not discard:
res.append(s)
if verbose:
print(f'{len(res)} feasible solutions out of {len(solutions)}')
print('Violated constraints:')
print(json.dumps(violated_constraints, indent=1, )[2:-2])
return res
def check_overlap_constraint(self, cont_occ: np.ndarray) -> bool:
for pos in range(self.aircraft.num_positions):
pos_sum = 0
for i, d_i in enumerate(self.container_d):
pos_sum += d_i * cont_occ[i, pos]
if pos_sum > 1:
return False
return True
def check_contiguity_constraint(self, cont_occ: np.ndarray) -> bool:
num_cont = cont_occ.shape[0]
for c in range(num_cont):
if self.container_types[c] != 't3':
continue
row_sum = np.sum(cont_occ[c])
if row_sum == 0:
continue
if row_sum != 2:
return False
s = 0
for pos in range(self.aircraft.num_positions - 1):
s += cont_occ[c, pos] * cont_occ[c, pos + 1]
if s != 1:
return False
return True
def check_max_weight_constraint(self, cont_occ: np.ndarray) -> bool:
weight = 0
for index, v in np.ndenumerate(cont_occ):
i, j = index
weight += self.container_t[i] * self.container_masses[i] * v
return weight <= self.aircraft.max_payload
def check_no_duplicates_constraint(self, cont_occ: np.ndarray) -> bool:
num_cont = cont_occ.shape[0]
for c in range(num_cont):
s = 0
for pos in range(self.aircraft.num_positions):
s += self.container_t[c] * cont_occ[c, pos]
if s > 1:
return False
return True
def check_cg_lower_bound_constraint(self, cont_occ: np.ndarray) -> bool:
return self.get_cg(cont_occ) >= self.aircraft.min_cg
def check_cg_upper_bound_constraint(self, cont_occ: np.ndarray) -> bool:
return self.get_cg(cont_occ) <= self.aircraft.max_cg
def check_shear_constraint(self, cont_occ: np.ndarray) -> bool:
shear = np.concatenate(self.get_shear(cont_occ))[:-1]
return np.all(np.less(shear, self.aircraft.shear_curve[:-1]))
def get_payload_weight(self, cont_occ: np.ndarray) -> float:
pl_weight = 0
for index, v in np.ndenumerate(cont_occ):
c, pos = index
pl_weight += self.container_t[c] * self.container_masses[c] * v
return pl_weight
def get_cg(self, cont_occ: np.ndarray) -> float:
s1 = 0
s2 = 0
for index, p in np.ndenumerate(cont_occ):
i, j = index
s2 += self.container_t[i] * self.container_masses[i] * p
s1 += s2 * self.aircraft.locations[j]
s1 += self.zero_payload_mass * self.zero_payload_cg
s2 += self.zero_payload_mass
return s1 / s2
def get_shear(self, cont_occ: np.ndarray) -> tuple:
shear_l = []
shear_r = []
for u, x_u in enumerate(self.aircraft.location_ends):
if x_u <= 0:
shear_l.append(self.get_shear_left(u, cont_occ))
if x_u >= 0:
shear_r.append(self.get_shear_right(u, cont_occ))
# Odd number of positions
if self.aircraft.num_positions % 2 != 0:
s = 0
j = math.floor(self.aircraft.num_positions / 2.0) + 1
for i, t_i in enumerate(self.container_t):
s += t_i * self.container_masses[i] * cont_occ[i, j] / 2.0
shear_r = [s + shear_r[0]] + shear_r
shear_l += [shear_l[-1] + s]
return np.array(shear_l), np.array(shear_r)
def get_shear_left(self, pos, cont_occ: np.ndarray):
shear = 0
for i, t_i in enumerate(self.container_t):
for j in range(pos + 1):
shear += t_i * self.container_masses[i] * cont_occ[i, j]
return shear
def get_shear_right(self, pos, cont_occ: np.ndarray):
shear = 0
for i, t_i in enumerate(self.container_t):
for j in range(pos + 1, self.aircraft.num_positions):
shear += t_i * self.container_masses[i] * cont_occ[i, j]
return shear
def get_num_slack_vars(self) -> dict:
return {
'pl_o': self.aircraft.num_positions,
'pl_d': len(self.container_types),
'pl_w': get_num_bits(self.aircraft.max_payload),
'cl_l': get_num_bits(self.aircraft.min_cg),
'cl_u': get_num_bits(self.aircraft.max_cg),
'sl': [get_num_bits(s) for s in self.aircraft.shear_curve]
}