Skip to content

Commit 37a3072

Browse files
committed
Add eligibility in rectangle
1 parent 9901586 commit 37a3072

File tree

5 files changed

+108
-6
lines changed

5 files changed

+108
-6
lines changed

include/packingsolver/rectangle/instance.hpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,16 @@ struct ItemType
120120
/** Weight of the item type. */
121121
Weight weight = 0;
122122

123+
/**
124+
* Eligibility.
125+
*
126+
* - 'eligibility_id == -1' means that the item type can be packed in any
127+
* bin type.
128+
* - 'eligibility_id >= 0' means that the item type can only be packed in
129+
* bin type supporting eligibility id 'eligibility_id'.
130+
*/
131+
EligibilityId eligibility_id = -1;
132+
123133
/*
124134
* Computed attributes
125135
*/
@@ -198,13 +208,19 @@ struct BinType
198208
/** Semi-trailer truck data. */
199209
SemiTrailerTruckData semi_trailer_truck_data;
200210

211+
/** Eligibility ids. */
212+
std::vector<EligibilityId> eligibility_ids;
213+
201214
/** Defects of the bin type. */
202215
std::vector<Defect> defects;
203216

204217
/*
205218
* Computed attributes
206219
*/
207220

221+
/** Item type ids. */
222+
std::vector<ItemTypeId> item_type_ids;
223+
208224
/** Get the area of the bin type. */
209225
inline Area area() const { return rect.x * rect.y; }
210226

include/packingsolver/rectangle/instance_builder.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ class InstanceBuilder
7070
BinTypeId bin_type_id,
7171
const SemiTrailerTruckData& semi_trailer_truck_data);
7272

73+
/** Add an eligibility id to a bin type. */
74+
void add_bin_type_eligibility(
75+
BinTypeId bin_type_id,
76+
EligibilityId eligibility_id);
77+
7378
/** Add a defect. */
7479
DefectId add_defect(
7580
BinTypeId bin_type_id,
@@ -137,6 +142,11 @@ class InstanceBuilder
137142
ItemTypeId item_type_id,
138143
Weight weight);
139144

145+
/** Set the eligibility id of an item type. */
146+
void set_item_type_eligibility(
147+
ItemTypeId item_type_id,
148+
EligibilityId eligibility_id);
149+
140150
/**
141151
* Add an item type from another item type.
142152
*

src/rectangle/branching_scheme.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -519,9 +519,7 @@ const std::vector<BranchingScheme::Insertion>& BranchingScheme::insertions(
519519
const BinType& bin_type = instance.bin_type(bin_type_id);
520520

521521
// Items.
522-
for (ItemTypeId item_type_id = 0;
523-
item_type_id < instance.number_of_item_types();
524-
++item_type_id) {
522+
for (ItemTypeId item_type_id: bin_type.item_type_ids) {
525523

526524
if (!ok[item_type_id])
527525
continue;
@@ -673,9 +671,7 @@ const std::vector<BranchingScheme::Insertion>& BranchingScheme::insertions(
673671
const BinType& bin_type = instance.bin_type(bin_type_id);
674672

675673
// Items.
676-
for (ItemTypeId item_type_id = 0;
677-
item_type_id < instance.number_of_item_types();
678-
++item_type_id) {
674+
for (ItemTypeId item_type_id: bin_type.item_type_ids) {
679675

680676
if (!ok[item_type_id])
681677
continue;

src/rectangle/instance.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,26 @@ std::ostream& Instance::format(
236236
<< std::endl;
237237
}
238238

239+
os
240+
<< std::endl
241+
<< std::setw(12) << "Bin type"
242+
<< std::setw(12) << "Eligibility"
243+
<< std::endl
244+
<< std::setw(12) << "--------"
245+
<< std::setw(12) << "-----------"
246+
<< std::endl;
247+
for (BinTypeId bin_type_id = 0;
248+
bin_type_id < number_of_bin_types();
249+
++bin_type_id) {
250+
const BinType& bin_type = this->bin_type(bin_type_id);
251+
for (EligibilityId eligibility_id: bin_type.eligibility_ids) {
252+
os
253+
<< std::setw(12) << bin_type_id
254+
<< std::setw(12) << eligibility_id
255+
<< std::endl;
256+
}
257+
}
258+
239259
if (number_of_defects() > 0) {
240260
os
241261
<< std::endl
@@ -283,6 +303,7 @@ std::ostream& Instance::format(
283303
<< std::setw(12) << "Oriented"
284304
<< std::setw(12) << "Group id"
285305
<< std::setw(12) << "Weight"
306+
<< std::setw(12) << "Eligibility"
286307
<< std::endl
287308
<< std::setw(12) << "---------"
288309
<< std::setw(12) << "-----"
@@ -292,6 +313,7 @@ std::ostream& Instance::format(
292313
<< std::setw(12) << "--------"
293314
<< std::setw(12) << "--------"
294315
<< std::setw(12) << "------"
316+
<< std::setw(12) << "-----------"
295317
<< std::endl;
296318
for (ItemTypeId item_type_id = 0;
297319
item_type_id < number_of_item_types();
@@ -306,6 +328,7 @@ std::ostream& Instance::format(
306328
<< std::setw(12) << item_type.oriented
307329
<< std::setw(12) << item_type.group_id
308330
<< std::setw(12) << item_type.weight
331+
<< std::setw(12) << item_type.eligibility_id
309332
<< std::endl;
310333
}
311334
}

src/rectangle/instance_builder.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,21 @@ void InstanceBuilder::set_bin_type_semi_trailer_truck_parameters(
109109
instance_.bin_types_[bin_type_id].semi_trailer_truck_data = semi_trailer_truck_data;
110110
}
111111

112+
void InstanceBuilder::add_bin_type_eligibility(
113+
BinTypeId bin_type_id,
114+
EligibilityId eligibility_id)
115+
{
116+
if (bin_type_id < 0 || bin_type_id >= instance_.bin_types_.size()) {
117+
throw std::invalid_argument(
118+
"packingsolver::onedimensional::InstanceBuilder::add_bin_type_eligibility: "
119+
"invalid 'bin_type_id'; "
120+
"bin_type_id: " + std::to_string(bin_type_id) + "; "
121+
"instance_.bin_types_.size(): " + std::to_string(instance_.bin_types_.size()) + ".");
122+
}
123+
124+
instance_.bin_types_[bin_type_id].eligibility_ids.push_back(eligibility_id);
125+
}
126+
112127
DefectId InstanceBuilder::add_defect(
113128
BinTypeId bin_type_id,
114129
Length pos_x,
@@ -153,6 +168,11 @@ void InstanceBuilder::add_bin_type(
153168
set_bin_type_semi_trailer_truck_parameters(
154169
bin_type_id,
155170
bin_type.semi_trailer_truck_data);
171+
for (EligibilityId eligibility_id: bin_type.eligibility_ids) {
172+
add_bin_type_eligibility(
173+
bin_type_id,
174+
eligibility_id);
175+
}
156176
for (const Defect& defect: bin_type.defects) {
157177
add_defect(
158178
bin_type_id,
@@ -276,6 +296,21 @@ void InstanceBuilder::set_item_type_weight(
276296
instance_.item_types_[item_type_id].weight = weight;
277297
}
278298

299+
void InstanceBuilder::set_item_type_eligibility(
300+
ItemTypeId item_type_id,
301+
EligibilityId eligibility_id)
302+
{
303+
if (item_type_id < 0 || item_type_id >= instance_.item_types_.size()) {
304+
throw std::invalid_argument(
305+
"packingsolver::onedimensional::InstanceBuilder::set_item_type_eligibility: "
306+
"invalid 'item_type_id'; "
307+
"item_type_id: " + std::to_string(item_type_id) + "; "
308+
"instance_.item_types_.size(): " + std::to_string(instance_.item_types_.size()) + ".");
309+
}
310+
311+
instance_.item_types_[item_type_id].eligibility_id = eligibility_id;
312+
}
313+
279314
void InstanceBuilder::add_item_type(
280315
const ItemType& item_type,
281316
Profit profit,
@@ -291,6 +326,9 @@ void InstanceBuilder::add_item_type(
291326
set_item_type_weight(
292327
item_type_id,
293328
item_type.weight);
329+
set_item_type_eligibility(
330+
item_type_id,
331+
item_type.eligibility_id);
294332
}
295333

296334
void InstanceBuilder::set_item_types_unweighted()
@@ -657,6 +695,25 @@ Instance InstanceBuilder::build()
657695
instance_.number_of_defects_ += bin_type.defects.size();
658696
}
659697

698+
// Compute bin_type.item_type_ids_.
699+
for (BinTypeId bin_type_id = 0; bin_type_id < instance_.number_of_bin_types(); ++bin_type_id) {
700+
BinType& bin_type = instance_.bin_types_[bin_type_id];
701+
for (ItemTypeId item_type_id = 0;
702+
item_type_id < instance_.number_of_item_types();
703+
++item_type_id) {
704+
const ItemType& item_type = instance_.item_type(item_type_id);
705+
if (item_type.eligibility_id != -1
706+
&& std::find(
707+
bin_type.eligibility_ids.begin(),
708+
bin_type.eligibility_ids.end(),
709+
item_type.eligibility_id)
710+
== bin_type.eligibility_ids.end()) {
711+
continue;
712+
}
713+
bin_type.item_type_ids.push_back(item_type_id);
714+
}
715+
}
716+
660717
// Compute number_of_groups_.
661718
for (ItemTypeId item_type_id = 0;
662719
item_type_id < instance_.number_of_item_types();

0 commit comments

Comments
 (0)