-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Copy patharc_flow_cutting_stock_sat.py
430 lines (379 loc) · 10.4 KB
/
arc_flow_cutting_stock_sat.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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
#!/usr/bin/env python3
# Copyright 2010-2025 Google LLC
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Cutting stock problem with the objective to minimize wasted space."""
import collections
import time
from absl import app
from absl import flags
import numpy as np
from google.protobuf import text_format
from ortools.linear_solver.python import model_builder as mb
from ortools.sat.python import cp_model
_OUTPUT_PROTO = flags.DEFINE_string(
"output_proto", "", "Output file to write the cp_model proto to."
)
_PARAMS = flags.DEFINE_string(
"params",
"num_search_workers:8,log_search_progress:true,max_time_in_seconds:10",
"Sat solver parameters.",
)
_SOLVER = flags.DEFINE_string("solver", "sat", "Method used to solve: sat, mip.")
DESIRED_LENGTHS = [
2490,
3980,
2490,
3980,
2391,
2391,
2391,
596,
596,
596,
2456,
2456,
3018,
938,
3018,
938,
943,
3018,
943,
3018,
2490,
3980,
2490,
3980,
2391,
2391,
2391,
596,
596,
596,
2456,
2456,
3018,
938,
3018,
938,
943,
3018,
943,
3018,
2890,
3980,
2890,
3980,
2391,
2391,
2391,
596,
596,
596,
2856,
2856,
3018,
938,
3018,
938,
943,
3018,
943,
3018,
3290,
3980,
3290,
3980,
2391,
2391,
2391,
596,
596,
596,
3256,
3256,
3018,
938,
3018,
938,
943,
3018,
943,
3018,
3690,
3980,
3690,
3980,
2391,
2391,
2391,
596,
596,
596,
3656,
3656,
3018,
938,
3018,
938,
943,
3018,
943,
3018,
2790,
3980,
2790,
3980,
2391,
2391,
2391,
596,
596,
596,
2756,
2756,
3018,
938,
3018,
938,
943,
3018,
943,
3018,
2790,
3980,
2790,
3980,
2391,
2391,
2391,
596,
596,
596,
2756,
2756,
3018,
938,
3018,
938,
943,
]
POSSIBLE_CAPACITIES = [4000, 5000, 6000, 7000, 8000]
# Toy problem
# DESIRED_LENGTHS = [12, 12, 8, 8, 8]
# POSSIBLE_CAPACITIES = [10, 20]
def regroup_and_count(raw_input):
"""Regroup all equal capacities in a multiset."""
grouped = collections.defaultdict(int)
for i in raw_input:
grouped[i] += 1
output = []
for size, count in grouped.items():
output.append([size, count])
output.sort(reverse=False)
return output
def price_usage(usage, capacities):
"""Compute the best price for a given usage and possible capacities."""
price = max(capacities)
for capacity in capacities:
if capacity < usage:
continue
price = min(capacity - usage, price)
return price
def create_state_graph(items, max_capacity):
"""Create a state graph from a multiset of items, and a maximum capacity."""
states = []
state_to_index = {}
states.append(0)
state_to_index[0] = 0
transitions = []
for item_index, size_and_count in enumerate(items):
size, count = size_and_count
num_states = len(states)
for state_index in range(num_states):
current_state = states[state_index]
current_state_index = state_index
for card in range(count):
new_state = current_state + size * (card + 1)
if new_state > max_capacity:
break
if new_state in state_to_index:
new_state_index = state_to_index[new_state]
else:
new_state_index = len(states)
states.append(new_state)
state_to_index[new_state] = new_state_index
# Add the transition
transitions.append(
[current_state_index, new_state_index, item_index, card + 1]
)
return states, transitions
def solve_cutting_stock_with_arc_flow_and_sat(output_proto_file: str, params: str):
"""Solve the cutting stock with arc-flow and the CP-SAT solver."""
items = regroup_and_count(DESIRED_LENGTHS)
print("Items:", items)
num_items = len(DESIRED_LENGTHS)
max_capacity = max(POSSIBLE_CAPACITIES)
states, transitions = create_state_graph(items, max_capacity)
print(
"Dynamic programming has generated",
len(states),
"states and",
len(transitions),
"transitions",
)
incoming_vars = collections.defaultdict(list)
outgoing_vars = collections.defaultdict(list)
incoming_sink_vars = []
item_vars = collections.defaultdict(list)
item_coeffs = collections.defaultdict(list)
transition_vars = []
model = cp_model.CpModel()
objective_vars = []
objective_coeffs = []
for outgoing, incoming, item_index, card in transitions:
count = items[item_index][1]
max_count = count // card
count_var = model.NewIntVar(
0, max_count, "i%i_f%i_t%i_C%s" % (item_index, incoming, outgoing, card)
)
incoming_vars[incoming].append(count_var)
outgoing_vars[outgoing].append(count_var)
item_vars[item_index].append(count_var)
item_coeffs[item_index].append(card)
transition_vars.append(count_var)
for state_index, state in enumerate(states):
if state_index == 0:
continue
exit_var = model.NewIntVar(0, num_items, "e%i" % state_index)
outgoing_vars[state_index].append(exit_var)
incoming_sink_vars.append(exit_var)
price = price_usage(state, POSSIBLE_CAPACITIES)
objective_vars.append(exit_var)
objective_coeffs.append(price)
# Flow conservation
for state_index in range(1, len(states)):
model.Add(sum(incoming_vars[state_index]) == sum(outgoing_vars[state_index]))
# Flow going out of the source must go in the sink
model.Add(sum(outgoing_vars[0]) == sum(incoming_sink_vars))
# Items must be placed
for item_index, size_and_count in enumerate(items):
num_arcs = len(item_vars[item_index])
model.Add(
sum(
item_vars[item_index][i] * item_coeffs[item_index][i]
for i in range(num_arcs)
)
== size_and_count[1]
)
# Objective is the sum of waste
model.Minimize(
sum(objective_vars[i] * objective_coeffs[i] for i in range(len(objective_vars)))
)
# Output model proto to file.
if output_proto_file:
model.ExportToFile(output_proto_file)
# Solve model.
solver = cp_model.CpSolver()
if params:
text_format.Parse(params, solver.parameters)
solver.parameters.log_search_progress = True
solver.Solve(model)
def solve_cutting_stock_with_arc_flow_and_mip():
"""Solve the cutting stock with arc-flow and a MIP solver."""
items = regroup_and_count(DESIRED_LENGTHS)
print("Items:", items)
num_items = len(DESIRED_LENGTHS)
max_capacity = max(POSSIBLE_CAPACITIES)
states, transitions = create_state_graph(items, max_capacity)
print(
"Dynamic programming has generated",
len(states),
"states and",
len(transitions),
"transitions",
)
incoming_vars = collections.defaultdict(list)
outgoing_vars = collections.defaultdict(list)
incoming_sink_vars = []
item_vars = collections.defaultdict(list)
item_coeffs = collections.defaultdict(list)
start_time = time.time()
model = mb.ModelBuilder()
objective_vars = []
objective_coeffs = []
var_index = 0
for outgoing, incoming, item_index, card in transitions:
count = items[item_index][1]
count_var = model.new_int_var(
0,
count,
"a%i_i%i_f%i_t%i_c%i" % (var_index, item_index, incoming, outgoing, card),
)
var_index += 1
incoming_vars[incoming].append(count_var)
outgoing_vars[outgoing].append(count_var)
item_vars[item_index].append(count_var)
item_coeffs[item_index].append(card)
for state_index, state in enumerate(states):
if state_index == 0:
continue
exit_var = model.new_int_var(0, num_items, "e%i" % state_index)
outgoing_vars[state_index].append(exit_var)
incoming_sink_vars.append(exit_var)
price = price_usage(state, POSSIBLE_CAPACITIES)
objective_vars.append(exit_var)
objective_coeffs.append(price)
# Flow conservation
for state_index in range(1, len(states)):
model.add(
mb.LinearExpr.sum(incoming_vars[state_index])
== mb.LinearExpr.sum(outgoing_vars[state_index])
)
# Flow going out of the source must go in the sink
model.add(
mb.LinearExpr.sum(outgoing_vars[0]) == mb.LinearExpr.sum(incoming_sink_vars)
)
# Items must be placed
for item_index, size_and_count in enumerate(items):
num_arcs = len(item_vars[item_index])
model.add(
mb.LinearExpr.sum(
[
item_vars[item_index][i] * item_coeffs[item_index][i]
for i in range(num_arcs)
]
)
== size_and_count[1]
)
# Objective is the sum of waste
model.minimize(np.dot(objective_vars, objective_coeffs))
solver = mb.ModelSolver("scip")
solver.enable_output(True)
status = solver.solve(model)
### Output the solution.
if status == mb.SolveStatus.OPTIMAL or status == mb.SolveStatus.FEASIBLE:
print(
"Objective value = %f found in %.2f s"
% (solver.objective_value, time.time() - start_time)
)
else:
print("No solution")
def main(_):
"""Main function."""
if _SOLVER.value == "sat":
solve_cutting_stock_with_arc_flow_and_sat(_OUTPUT_PROTO.value, _PARAMS.value)
else: # 'mip'
solve_cutting_stock_with_arc_flow_and_mip()
if __name__ == "__main__":
app.run(main)