Repository navigation
Expand file tree
/
Copy pathdistributed_pfss_makespan.hpp
More file actions
328 lines (277 loc) · 9.31 KB
/
Copy pathdistributed_pfss_makespan.hpp
File metadata and controls
328 lines (277 loc) · 9.31 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
/**
* Distributed permutation flow shop scheduling problem, makespan
*
* Input:
* - F factories
* - m machines
* - n jobs composed of m operations for each machine with pᵢⱼ the processing
* time of the operation of job j on machine i, j = 1..n, i = 1..m
* Problem:
* - Find a schedule of jobs for each factory such that:
* - all operations of all jobs are scheduled exactly once
* - for all job j = 1..n, i = 1..m - 1, operations (j, i + 1) starts after
* the end of operation (j, i)
* - the job sequence is the same on all machines of a factory
* Objective:
* - Minimize the makespan of the schedule
*
*/
#pragma once
#include "optimizationtools/containers/indexed_set.hpp"
#include <iostream>
#include <fstream>
#include <iomanip>
namespace orproblems
{
namespace distributed_pfss_makespan
{
using JobId = int64_t;
using JobPos = int64_t;
using MachineId = int64_t;
using FactoryId = int64_t;
using Time = int64_t;
/**
* Instance class for a 'distributed_pfss_makespan' problem.
*/
class Instance
{
public:
/*
* Getters
*/
/** Get the number of factories. */
inline FactoryId number_of_factories() const { return number_of_factories_; }
/** Get the number of machines. */
inline MachineId number_of_machines() const { return number_of_machines_; }
/** Get the number of jobs. */
inline JobId number_of_jobs() const { return processing_times_.size(); }
/** Get the processing-time of a job on a machine. */
inline Time processing_time(
JobId job_id,
MachineId machine_id) const
{
return processing_times_[job_id][machine_id];
}
/*
* Outputs
*/
/** Print the instance. */
void format(
std::ostream& os,
int verbosity_level = 1) const
{
if (verbosity_level >= 1) {
os
<< "Number of factories: " << number_of_factories() << std::endl
<< "Number of machines: " << number_of_machines() << std::endl
<< "Number of jobs: " << number_of_jobs() << std::endl
;
}
if (verbosity_level >= 2) {
os << std::endl
<< std::setw(12) << "Job"
<< std::setw(12) << "Machine"
<< std::setw(12) << "Proc. time"
<< std::endl
<< std::setw(12) << "---"
<< std::setw(12) << "-------"
<< std::setw(12) << "----------"
<< std::endl;
for (JobId job_id = 0; job_id < number_of_jobs(); ++job_id) {
for (MachineId machine_id = 0;
machine_id < number_of_machines();
++machine_id) {
os
<< std::setw(12) << job_id
<< std::setw(12) << machine_id
<< std::setw(12) << processing_time(job_id, machine_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::right
<< std::setw(12) << "Factory"
<< std::setw(12) << "Job"
<< std::setw(12) << "Time"
<< std::endl
<< std::setw(12) << "-------"
<< std::setw(12) << "----"
<< std::endl;
}
optimizationtools::IndexedSet jobs(number_of_jobs());
JobPos number_of_duplicates = 0;
JobPos factory_number_of_jobs = -1;
Time makespan = 0;
for (FactoryId factory_id = 0;
factory_id < number_of_factories();
++factory_id) {
file >> factory_number_of_jobs;
std::vector<Time> times(number_of_machines(), 0);
JobId job_id = -1;
for (JobPos pos = 0; pos < factory_number_of_jobs; ++pos) {
file >> 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);
times[0] = times[0] + processing_time(job_id, 0);
for (MachineId machine_id = 1;
machine_id < number_of_machines();
++machine_id) {
if (times[machine_id - 1] > times[machine_id]) {
times[machine_id] = times[machine_id - 1]
+ processing_time(job_id, machine_id);
} else {
times[machine_id] = times[machine_id]
+ processing_time(job_id, machine_id);
}
}
if (verbosity_level >= 2) {
os
<< std::setw(12) << factory_id
<< std::setw(12) << job_id
<< std::setw(12) << times[number_of_machines() - 1]
<< std::endl;
}
}
makespan = std::max(makespan, times[number_of_machines() - 1]);
}
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;
os << "Number of duplicates: " << number_of_duplicates << std::endl;
os << "Feasible: " << feasible << std::endl;
os << "Makespan: " << makespan << std::endl;
}
return {feasible, makespan};
}
private:
/*
* Private methods
*/
/** Constructor to build an instance manually. */
Instance() { }
/*
* Private attributes
*/
/** Number of factories. */
FactoryId number_of_factories_ = 1;
/** Number of machines. */
MachineId number_of_machines_ = 1;
/** Processing-times. */
std::vector<std::vector<Time>> processing_times_;
friend class InstanceBuilder;
};
class InstanceBuilder
{
public:
/** Constructor. */
InstanceBuilder() { }
/**
* Set the number of machines.
*
* This method resets all the jobs.
*/
void set_number_of_machines(MachineId number_of_machines)
{
instance_.processing_times_.clear();
instance_.number_of_machines_ = number_of_machines;
}
/** Set the number of factories. */
void set_number_of_factories(FactoryId number_of_factories) { instance_.number_of_factories_ = number_of_factories; }
/** Add jobs. */
void add_jobs(JobId number_of_jobs)
{
instance_.processing_times_.insert(
instance_.processing_times_.end(),
number_of_jobs,
std::vector<Time>(instance_.number_of_machines(), 0));
}
/** Set the processing-time of a job on a machine. */
void set_processing_time(
JobId job_id,
MachineId machine_id,
Time processing_time)
{
instance_.processing_times_[job_id][machine_id] = processing_time;
}
/** 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" || format == "naderi2010") {
read_naderi2010(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 'naderi2010' format. */
void read_naderi2010(std::ifstream& file)
{
JobId number_of_jobs;
MachineId number_of_machines;
file >> number_of_jobs;
file >> number_of_machines;
file >> instance_.number_of_factories_;
set_number_of_machines(number_of_machines);
add_jobs(number_of_jobs);
Time processing_time = -1;
MachineId machine_id_tmp = -1;
for (JobId job_id = 0; job_id < number_of_jobs; job_id++) {
for (MachineId machine_id = 0;
machine_id < number_of_machines;
++machine_id) {
file >> machine_id_tmp >> processing_time;
set_processing_time(job_id, machine_id, processing_time);
}
}
}
/*
* Private attributes
*/
/** Instance. */
Instance instance_;
};
}
}