Repository navigation
Expand file tree
/
Copy pathparallel_scheduling_with_family_setup_times_twct.hpp
More file actions
377 lines (320 loc) · 9.89 KB
/
Copy pathparallel_scheduling_with_family_setup_times_twct.hpp
File metadata and controls
377 lines (320 loc) · 9.89 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
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
/**
* Identical parallel machine scheduling problem with family setup times, total
* weighted completion time
*
* Input:
* - m machines
* - o families; for each family k = 1..o, a setup time sₖ
* - n jobs; for each job j = 1..n
* - a processing time pⱼ
* - a weight wⱼ
* - a family fⱼ
* Problem:
* - find a schedule for each machine such that:
* - each job is scheduled exactly once
* - if a job j2 is processed on a machine immediately after a job j1 and
* f(j2) != f(j1), then a setup of s(f(j2)) units of time must be performed
* before starting processing j2.
* Objective:
* - minimize the total weighted completion time of the schedule.
*
*/
#pragma once
#include "optimizationtools/containers/indexed_set.hpp"
#include <fstream>
#include <iostream>
#include <iomanip>
namespace orproblems
{
namespace parallel_scheduling_with_family_setup_times_twct
{
using JobId = int64_t;
using MachineId = int64_t;
using FamilyId = int64_t;
using Time = int64_t;
using Weight = int64_t;
/**
* Structure for a family.
*/
struct Family
{
/** Setup time of the family. */
Time setup_time;
/** Jobs of the family. */
std::vector<JobId> job_ids;
};
/**
* Structure for a job.
*/
struct Job
{
/** Processing-time of the job. */
Time processing_time;
/** Weight of the job. */
Weight weight;
/** Family of the job. */
FamilyId family_id;
};
/**
* Instance class for a 'parallel_scheduling_with_family_setup_times_twct' problem.
*/
class Instance
{
public:
/*
* Getters
*/
/** Get the number of machines. */
MachineId number_of_machines() const { return number_of_machines_; }
/** Get the number of families. */
FamilyId number_of_families() const { return families_.size(); }
/** Get a family. */
const Family& family(FamilyId family_id) const { return families_[family_id]; }
/** Get the number of jobs. */
JobId number_of_jobs() const { return jobs_.size(); }
/** Get a job. */
const Job& job(JobId job_id) const { return jobs_[job_id]; }
/*
* Outputs
*/
/** Print the instance. */
void format(
std::ostream& os,
int verbosity_level = 1) const
{
if (verbosity_level >= 1) {
os << "Number of machines: " << number_of_machines() << std::endl;
os << "Number of families: " << number_of_families() << std::endl;
os << "Number of jobs: " << number_of_jobs() << std::endl;
}
if (verbosity_level >= 2) {
os << std::endl
<< std::setw(12) << "Family"
<< std::setw(12) << "Setup"
<< std::setw(12) << "# jobs"
<< std::endl
<< std::setw(12) << "------"
<< std::setw(12) << "-----"
<< std::setw(12) << "------"
<< std::endl;
for (FamilyId family_id = 0;
family_id < number_of_families();
++family_id) {
const Family& family = this->family(family_id);
os
<< std::setw(12) << family_id
<< std::setw(12) << family.setup_time
<< std::setw(12) << family.job_ids.size()
<< std::endl;
}
}
if (verbosity_level >= 2) {
os << std::endl
<< std::setw(12) << "Job"
<< std::setw(12) << "Proc. time"
<< std::setw(12) << "Weight"
<< std::setw(12) << "Family"
<< std::endl
<< std::setw(12) << "---"
<< std::setw(12) << "----------"
<< std::setw(12) << "------"
<< std::setw(12) << "------"
<< std::endl;
for (JobId job_id = 0;
job_id < number_of_jobs();
++job_id) {
const Job& job = this->job(job_id);
os
<< std::setw(12) << job_id
<< std::setw(12) << job.processing_time
<< std::setw(12) << job.weight
<< std::setw(12) << job.family_id
<< std::endl;
}
}
}
/** Check a certificate. */
std::pair<bool, Time> check(
const std::string& certificate_path,
std::ostream& os,
int verbosity_level = 1) const
{
std::ifstream file(certificate_path);
if (!file.good()) {
throw std::runtime_error(
"Unable to open file \"" + certificate_path + "\".");
}
if (verbosity_level >= 2) {
os << std::endl
<< std::setw(12) << "Job"
<< std::setw(12) << "Time"
<< std::setw(12) << "TWCT"
<< std::endl
<< std::setw(12) << "---"
<< std::setw(12) << "----"
<< std::setw(12) << "----"
<< std::endl;
}
JobId job_id_pred = 0;
JobId job_id = -1;
optimizationtools::IndexedSet jobs(number_of_jobs());
JobId number_of_duplicates = 0;
Time time = 0;
Time total_weighted_completion_time = 0;
while (file >> job_id) {
const Job& job = this->job(job_id);
// Check duplicates.
if (jobs.contains(job_id)) {
number_of_duplicates++;
if (verbosity_level >= 2) {
os << "Job " << job_id
<< " has already been scheduled." << std::endl;
}
}
jobs.add(job_id);
if (job.family_id != this->job(job_id_pred).family_id)
time += this->family(job.family_id).setup_time;
time += job.processing_time;
total_weighted_completion_time += job.weight * time;
if (verbosity_level >= 2) {
os
<< std::setw(12) << job_id
<< std::setw(12) << time
<< std::setw(12) << total_weighted_completion_time
<< std::endl;
}
job_id_pred = job_id;
}
bool feasible
= (jobs.size() == number_of_jobs())
&& (number_of_duplicates == 0);
if (verbosity_level >= 2)
os << std::endl;
if (verbosity_level >= 1) {
os
<< "Number of jobs: " << jobs.size() << " / " << number_of_jobs() << std::endl
<< "Number of duplicates: " << number_of_duplicates << std::endl
<< "Feasible: " << feasible << std::endl
<< "Total weighted completion time: " << total_weighted_completion_time << std::endl
;
}
return {feasible, total_weighted_completion_time};
}
private:
/*
* Private methods
*/
/** Constructor to build an instance manually. */
Instance() { }
/*
* Private attributes
*/
/** Number of machines. */
MachineId number_of_machines_;
/** Jobs. */
std::vector<Job> jobs_;
/** Families. */
std::vector<Family> families_;
friend class InstanceBuilder;
};
class InstanceBuilder
{
public:
/** Constructor. */
InstanceBuilder() { }
/** Set the number of machines. */
void set_number_of_machines(
MachineId number_of_machines)
{
instance_.number_of_machines_ = number_of_machines;
}
/** Add a family. */
void add_family(
Time setup_time)
{
Family family;
family.setup_time = setup_time;
instance_.families_.push_back(family);
}
/** Add a job. */
void add_job(
Time processing_time,
Weight weight,
FamilyId family_id)
{
JobId id = instance_.jobs_.size();
Job job;
job.processing_time = processing_time;
job.weight = weight;
job.family_id = family_id;
instance_.jobs_.push_back(job);
instance_.families_[family_id].job_ids.push_back(id);
}
/** Build an instance from a file. */
void read(
const std::string& instance_path,
const std::string& format = "")
{
std::ifstream file(instance_path);
if (!file.good()) {
throw std::runtime_error(
"Unable to open file \"" + instance_path + "\".");
}
if (format == "" || format == "default") {
read_default(file);
} else {
throw std::invalid_argument(
"Unknown instance format \"" + format + "\".");
}
file.close();
}
/*
* Build
*/
/** Build the instance. */
Instance build()
{
return std::move(instance_);
}
private:
/*
* Private methods
*/
/** Read an instance from a file in 'default' format. */
void read_default(std::ifstream& file)
{
MachineId number_of_machines = -1;
file >> number_of_machines;
set_number_of_machines(number_of_machines);
FamilyId number_of_families = -1;
file >> number_of_families;
Time setup_time;
for (FamilyId family_id = 0;
family_id < number_of_families;
++family_id) {
file >> setup_time;
add_family(setup_time);
}
JobId number_of_jobs = -1;
file >> number_of_jobs;
Time processing_time = -1;
Weight weight = -1;
FamilyId family_id = -1;
for (JobId job_id = 0; job_id < number_of_jobs; ++job_id) {
file
>> processing_time
>> weight
>> family_id;
add_job(processing_time,
weight,
family_id);
}
}
/*
* Private attributes
*/
/** Instance. */
Instance instance_;
};
}
}