@@ -188,69 +188,57 @@ bool BranchingScheme::better(
188
188
Length BranchingScheme::x1_prev (const Node& node, Depth df) const
189
189
{
190
190
switch (df) {
191
- case -2 : {
192
- BinTypeId bin_type_id = instance ().bin_type_id (node.number_of_bins );
193
- return instance ().left_trim (
194
- instance ().bin_type (bin_type_id),
195
- CutOrientation::Horizontal);
196
- } case -1 : {
197
- BinTypeId bin_type_id = instance ().bin_type_id (node.number_of_bins );
198
- return instance ().left_trim (
199
- instance ().bin_type (bin_type_id),
200
- CutOrientation::Vertical);
201
- } case 0 : {
191
+ case 0 : {
202
192
return node.x1_curr + instance ().parameters ().cut_thickness ;
203
193
} case 1 : {
204
194
return node.x1_prev ;
205
195
} case 2 : {
206
196
return node.x1_prev ;
207
197
} default : {
208
- assert (false );
209
- return -1 ;
198
+ BinPos bin_pos = node.number_of_bins + std::ceil (std::fabs (df) / 2 ) - 1 ;
199
+ BinTypeId bin_type_id = instance ().bin_type_id (bin_pos);
200
+ if (std::abs (df) % 2 == 0 ) {
201
+ return instance ().left_trim (
202
+ instance ().bin_type (bin_type_id),
203
+ CutOrientation::Horizontal);
204
+ } else {
205
+ return instance ().left_trim (
206
+ instance ().bin_type (bin_type_id),
207
+ CutOrientation::Vertical);
208
+ }
210
209
}
211
210
}
212
211
}
213
212
214
213
Length BranchingScheme::x3_prev (const Node& node, Depth df) const
215
214
{
216
215
switch (df) {
217
- case -2 : {
218
- BinTypeId bin_type_id = instance ().bin_type_id (node.number_of_bins );
219
- return instance ().left_trim (
220
- instance ().bin_type (bin_type_id),
221
- CutOrientation::Horizontal);
222
- } case -1 : {
223
- BinTypeId bin_type_id = instance ().bin_type_id (node.number_of_bins );
224
- return instance ().left_trim (
225
- instance ().bin_type (bin_type_id),
226
- CutOrientation::Vertical);
227
- } case 0 : {
216
+ case 0 : {
228
217
return node.x1_curr + instance ().parameters ().cut_thickness ;
229
218
} case 1 : {
230
219
return node.x1_prev ;
231
220
} case 2 : {
232
221
return node.x3_curr + instance ().parameters ().cut_thickness ;
233
222
} default : {
234
- assert (false );
235
- return -1 ;
223
+ BinPos bin_pos = node.number_of_bins + std::ceil (std::fabs (df) / 2 ) - 1 ;
224
+ BinTypeId bin_type_id = instance ().bin_type_id (bin_pos);
225
+ if (std::abs (df) % 2 == 0 ) {
226
+ return instance ().left_trim (
227
+ instance ().bin_type (bin_type_id),
228
+ CutOrientation::Horizontal);
229
+ } else {
230
+ return instance ().left_trim (
231
+ instance ().bin_type (bin_type_id),
232
+ CutOrientation::Vertical);
233
+ }
236
234
}
237
235
}
238
236
}
239
237
240
238
Length BranchingScheme::y2_prev (const Node& node, Depth df) const
241
239
{
242
240
switch (df) {
243
- case -2 : {
244
- BinTypeId bin_type_id = instance ().bin_type_id (node.number_of_bins );
245
- return instance ().bottom_trim (
246
- instance ().bin_type (bin_type_id),
247
- CutOrientation::Horizontal);
248
- } case -1 : {
249
- BinTypeId bin_type_id = instance ().bin_type_id (node.number_of_bins );
250
- return instance ().bottom_trim (
251
- instance ().bin_type (bin_type_id),
252
- CutOrientation::Vertical);
253
- } case 0 : {
241
+ case 0 : {
254
242
BinTypeId bin_type_id = instance ().bin_type_id (node.number_of_bins - 1 );
255
243
return instance ().bottom_trim (
256
244
instance ().bin_type (bin_type_id),
@@ -260,31 +248,40 @@ Length BranchingScheme::y2_prev(const Node& node, Depth df) const
260
248
} case 2 : {
261
249
return node.y2_prev ;
262
250
} default : {
263
- assert (false );
264
- return -1 ;
251
+ BinPos bin_pos = node.number_of_bins + std::ceil (std::fabs (df) / 2 ) - 1 ;
252
+ BinTypeId bin_type_id = instance ().bin_type_id (bin_pos);
253
+ if (std::abs (df) % 2 == 0 ) {
254
+ return instance ().bottom_trim (
255
+ instance ().bin_type (bin_type_id),
256
+ CutOrientation::Horizontal);
257
+ } else {
258
+ return instance ().bottom_trim (
259
+ instance ().bin_type (bin_type_id),
260
+ CutOrientation::Vertical);
261
+ }
265
262
}
266
263
}
267
264
}
268
265
269
266
BinPos BranchingScheme::last_bin (const Node& node, Depth df) const
270
267
{
271
- if (df <= -1 ) {
272
- return node.number_of_bins ;
273
- } else {
268
+ if (df >= 0 ) {
274
269
return node.number_of_bins - 1 ;
270
+ } else {
271
+ return node.number_of_bins + std::ceil (std::fabs (df) / 2 ) - 1 ;
275
272
}
276
273
}
277
274
278
275
CutOrientation BranchingScheme::last_bin_orientation (const Node& node, Depth df) const
279
276
{
280
- switch (df) {
281
- case -1 : {
282
- return CutOrientation::Vertical;
283
- } case -2 : {
284
- return CutOrientation::Horizontal;
285
- } default : {
277
+ if (df >= 0 ) {
286
278
return node.first_stage_orientation ;
287
- }
279
+ } else {
280
+ if (std::abs (df) % 2 == 0 ) {
281
+ return CutOrientation::Horizontal;
282
+ } else {
283
+ return CutOrientation::Vertical;
284
+ }
288
285
}
289
286
}
290
287
@@ -293,11 +290,7 @@ BranchingScheme::Front BranchingScheme::front(
293
290
const Insertion& insertion) const
294
291
{
295
292
switch (insertion.df ) {
296
- case -1 : case -2 : {
297
- return {last_bin (node, insertion.df ), last_bin_orientation (node, insertion.df ),
298
- 0 , insertion.x3 , insertion.x1 ,
299
- 0 , insertion.y2 };
300
- } case 0 : {
293
+ case 0 : {
301
294
return {last_bin (node, insertion.df ), last_bin_orientation (node, insertion.df ),
302
295
node.x1_curr , insertion.x3 , insertion.x1 ,
303
296
0 , insertion.y2 };
@@ -310,10 +303,9 @@ BranchingScheme::Front BranchingScheme::front(
310
303
node.x1_prev , insertion.x3 , insertion.x1 ,
311
304
node.y2_prev , insertion.y2 };
312
305
} default : {
313
- assert (false );
314
- return {-1 , CutOrientation::Vertical,
315
- -1 , -1 , -1 ,
316
- -1 , -1 };
306
+ return {last_bin (node, insertion.df ), last_bin_orientation (node, insertion.df ),
307
+ 0 , insertion.x3 , insertion.x1 ,
308
+ 0 , insertion.y2 };
317
309
}
318
310
}
319
311
}
@@ -366,11 +358,7 @@ BranchingScheme::Node BranchingScheme::child_tmp(
366
358
367
359
// Compute x1_prev and y2_prev.
368
360
switch (insertion.df ) {
369
- case -1 : case -2 : {
370
- node.x1_prev = instance ().left_trim (bin_type, o);
371
- node.y2_prev = instance ().bottom_trim (bin_type, o);
372
- break ;
373
- } case 0 : {
361
+ case 0 : {
374
362
node.x1_prev = parent.x1_curr + instance ().parameters ().cut_thickness ;
375
363
node.y2_prev = instance ().bottom_trim (bin_type, o);
376
364
break ;
@@ -383,8 +371,8 @@ BranchingScheme::Node BranchingScheme::child_tmp(
383
371
node.y2_prev = parent.y2_prev ;
384
372
break ;
385
373
} default : {
386
- assert ( false );
387
- break ;
374
+ node. x1_prev = instance (). left_trim (bin_type, o );
375
+ node. y2_prev = instance (). bottom_trim (bin_type, o) ;
388
376
}
389
377
}
390
378
@@ -465,7 +453,25 @@ BranchingScheme::Node BranchingScheme::child_tmp(
465
453
+ (node.x3_curr - node.x1_prev ) * (node.y2_curr - node.y2_prev );
466
454
}
467
455
node.waste = node.current_area - node.item_area ;
468
- assert (node.waste >= 0 );
456
+ if (node.waste < 0 ) {
457
+ throw std::logic_error (
458
+ " rectangleguillotine::BranchingScheme::child_tmp"
459
+ " ; node.waste: " + std::to_string (node.waste )
460
+ + " ; node.current_area: " + std::to_string (node.current_area )
461
+ + " ; node.item_area: " + std::to_string (node.item_area )
462
+ + " ; parent.number_of_bins: " + std::to_string (parent.number_of_bins )
463
+ + " ; node.number_of_bins: " + std::to_string (node.number_of_bins )
464
+ + " ; node.df: " + std::to_string (node.df )
465
+ + " ; node.number_of_items: " + std::to_string (node.number_of_items )
466
+ + " ; node.item_type_id_1: " + std::to_string (node.item_type_id_1 )
467
+ + " ; node.item_type_id_2: " + std::to_string (node.item_type_id_2 )
468
+ + " ; w: " + std::to_string (w)
469
+ + " ; h: " + std::to_string (h)
470
+ + " ; node.x1_curr: " + std::to_string (node.x1_curr )
471
+ + " ; node.y2_curr: " + std::to_string (node.y2_curr )
472
+ + " ; node.x3_curr: " + std::to_string (node.x3_curr )
473
+ + " ." );
474
+ }
469
475
return node;
470
476
}
471
477
@@ -641,15 +647,20 @@ const std::vector<BranchingScheme::Insertion>& BranchingScheme::insertions(
641
647
}
642
648
643
649
// Try inserting a defect.
644
- BinTypeId bin_type_id = instance ().bin_type_id (i);
645
- const BinType& bin_type = instance ().bin_type (bin_type_id);
646
- for (DefectId defect_id = 0 ;
647
- defect_id < (DefectId)bin_type.defects .size ();
648
- ++defect_id) {
649
- const Defect& defect = bin_type.defects [defect_id];
650
- if (instance ().right (defect, o) >= x
651
- && instance ().top (defect, o) >= y) {
652
- insertion_defect (parent, defect_id, df);
650
+ if (parent.parent == nullptr
651
+ || parent.item_type_id_1 != -1
652
+ || parent.item_type_id_2 != -1
653
+ || parent.df < df) {
654
+ BinTypeId bin_type_id = instance ().bin_type_id (i);
655
+ const BinType& bin_type = instance ().bin_type (bin_type_id);
656
+ for (DefectId defect_id = 0 ;
657
+ defect_id < (DefectId)bin_type.defects .size ();
658
+ ++defect_id) {
659
+ const Defect& defect = bin_type.defects [defect_id];
660
+ if (instance ().right (defect, o) >= x
661
+ && instance ().top (defect, o) >= y) {
662
+ insertion_defect (parent, defect_id, df);
663
+ }
653
664
}
654
665
}
655
666
}
@@ -687,15 +698,7 @@ Area BranchingScheme::waste(
687
698
Length BranchingScheme::x1_max (const Node& node, Depth df) const
688
699
{
689
700
switch (df) {
690
- case -1 : case -2 : {
691
- BinTypeId bin_type_id = instance ().bin_type_id (i);
692
- const BinType& bin_type = instance ().bin_type (bin_type_id);
693
- Length x = instance ().width (bin_type, o) - instance ().right_trim (bin_type, o);
694
- if (instance ().parameters ().maximum_distance_1_cuts != -1 )
695
- if (x > x1_prev (node, df) + instance ().parameters ().maximum_distance_1_cuts )
696
- x = x1_prev (node, df) + instance ().parameters ().maximum_distance_1_cuts ;
697
- return x;
698
- } case 0 : {
701
+ case 0 : {
699
702
BinTypeId bin_type_id = instance ().bin_type_id (i);
700
703
const BinType& bin_type = instance ().bin_type (bin_type_id);
701
704
Length x = instance ().width (bin_type, o) - instance ().right_trim (bin_type, o);
@@ -718,7 +721,13 @@ Length BranchingScheme::x1_max(const Node& node, Depth df) const
718
721
} case 2 : {
719
722
return node.x1_max ;
720
723
} default : {
721
- return -1 ;
724
+ BinTypeId bin_type_id = instance ().bin_type_id (i);
725
+ const BinType& bin_type = instance ().bin_type (bin_type_id);
726
+ Length x = instance ().width (bin_type, o) - instance ().right_trim (bin_type, o);
727
+ if (instance ().parameters ().maximum_distance_1_cuts != -1 )
728
+ if (x > x1_prev (node, df) + instance ().parameters ().maximum_distance_1_cuts )
729
+ x = x1_prev (node, df) + instance ().parameters ().maximum_distance_1_cuts ;
730
+ return x;
722
731
}
723
732
}
724
733
}
@@ -763,7 +772,7 @@ void BranchingScheme::insertion_1_item(
763
772
bool rotate,
764
773
Depth df) const
765
774
{
766
- assert (- 2 <= df); assert ( df <= 3 );
775
+ assert (df <= 3 );
767
776
768
777
// Check defect intersection
769
778
const rectangleguillotine::ItemType& item_type = instance ().item_type (item_type_id);
@@ -832,7 +841,7 @@ void BranchingScheme::insertion_2_items(
832
841
bool rotate2,
833
842
Depth df) const
834
843
{
835
- assert (- 2 <= df); assert ( df <= 3 );
844
+ assert (df <= 3 );
836
845
837
846
// Check defect intersection
838
847
const ItemType& item_type_1 = instance ().item_type (item_type_id_1);
@@ -880,7 +889,6 @@ void BranchingScheme::insertion_defect(
880
889
DefectId defect_id,
881
890
Depth df) const
882
891
{
883
- assert (-2 <= df);
884
892
assert (df <= 3 );
885
893
886
894
// Check defect intersection
@@ -1538,7 +1546,8 @@ Solution BranchingScheme::to_solution(
1538
1546
cut_position = subplate2_curr_y2 - (
1539
1547
(w_tmp == instance ().item_type (current_node->item_type_id_2 ).rect .w )?
1540
1548
instance ().item_type (current_node->item_type_id_2 ).rect .h :
1541
- instance ().item_type (current_node->item_type_id_2 ).rect .w );
1549
+ instance ().item_type (current_node->item_type_id_2 ).rect .w )
1550
+ - instance ().parameters ().cut_thickness ;
1542
1551
}
1543
1552
solution_builder.add_node (d, cut_position);
1544
1553
if (current_node->item_type_id_1 != -1 )
@@ -1636,8 +1645,12 @@ std::ostream& packingsolver::rectangleguillotine::operator<<(
1636
1645
std::ostream& os,
1637
1646
const BranchingScheme::Node& node)
1638
1647
{
1639
- os << " number_of_items " << node.number_of_items
1648
+ os << " id " << node.id
1649
+ << " parent " << ((node.parent != nullptr )? node.parent ->id : -1 )
1650
+ << " number_of_items " << node.number_of_items
1640
1651
<< " number_of_bins " << node.number_of_bins
1652
+ << " item_type_id_1 " << node.item_type_id_1
1653
+ << " item_type_id_2 " << node.item_type_id_2
1641
1654
<< std::endl;
1642
1655
os << " item_area " << node.item_area
1643
1656
<< " current_area " << node.current_area
@@ -1655,7 +1668,7 @@ std::ostream& packingsolver::rectangleguillotine::operator<<(
1655
1668
os << " pos_stack" << std::flush;
1656
1669
for (ItemPos j_pos: node.pos_stack )
1657
1670
os << " " << j_pos;
1658
- os << std::endl;
1671
+ // os << std::endl;
1659
1672
1660
1673
return os;
1661
1674
}
0 commit comments