-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbatch_scheduling_total_completion_time.hpp
331 lines (279 loc) · 9.3 KB
/
batch_scheduling_total_completion_time.hpp
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
/**
* Single machine batch scheduling problem, total completion time
*
* Input:
* - n jobs; for each job j = 1..n
* - a processing time pⱼ
* - a size sⱼ
* - a batch capacity Q
* Problem:
* - partition the jobs into batches and sequence the batches such that:
* - each job must be in exactly one of the batches
* - the processing time of a batch is equal to the longest processing time
* among all jobs it contains
* - the total size of the jobs in a batch does not exceed its capacity
* Objective:
* - minimize the total completion time of the schedule
*
*/
#pragma once
#include "optimizationtools/containers/indexed_set.hpp"
#include <stdexcept>
#include <fstream>
#include <iostream>
#include <iomanip>
namespace orproblems
{
namespace batch_scheduling_total_completion_time
{
using JobId = int64_t;
using JobPos = int64_t;
using Time = int64_t;
using Size = int64_t;
using Area = int64_t;
/**
* Structure for a job.
*/
struct Job
{
/** Processing-time of the job. */
Time processing_time;
/** Size of the job. */
Size size;
};
/**
* Instance class for a 'batch_scheduling_total_completion_time' problem.
*/
class Instance
{
public:
/*
* Getters
*/
/** Get the number of jobs. */
inline JobId number_of_jobs() const { return jobs_.size(); }
/** Get a job. */
inline const Job& job(JobId job_id) const { return jobs_[job_id]; }
/** Get the capacity of the batches. */
inline Size capacity() const { return capacity_; }
/*
* Outputs
*/
/** Print the instance. */
void format(
std::ostream& os,
int verbosity_level = 1) const
{
if (verbosity_level >= 1) {
os << "Number of jobs: " << number_of_jobs() << std::endl;
os << "Batch capacity: " << capacity() << std::endl;
}
if (verbosity_level >= 2) {
os << std::endl
<< std::setw(12) << "Job"
<< std::setw(12) << "Proc. time"
<< std::setw(12) << "Size"
<< std::endl
<< std::setw(12) << "---"
<< std::setw(12) << "----------"
<< 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.size
<< 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 + "\".");
}
JobPos current_batch_size = -1;
optimizationtools::IndexedSet jobs(number_of_jobs());
JobPos number_of_batches = 0;
JobPos number_of_duplicates = 0;
JobPos number_of_overloaded_batches = 0;
Time current_batch_end = 0;
Time total_completion_time = 0;
if (verbosity_level >= 2) {
os << std::endl << std::right
<< std::setw(12) << "Job"
<< std::setw(12) << "Proc. time"
<< std::setw(12) << "Size"
<< std::setw(12) << "Bat. start"
<< std::setw(12) << "Batch size"
<< std::setw(12) << "Batch end"
<< std::setw(12) << "TCT"
<< std::endl
<< std::setw(12) << "---"
<< std::setw(12) << "----------"
<< std::setw(12) << "----"
<< std::setw(12) << "----------"
<< std::setw(12) << "----------"
<< std::setw(12) << "---------"
<< std::setw(12) << "---"
<< std::endl;
}
while (file >> current_batch_size) {
JobId job_id = -1;
number_of_batches++;
Time current_batch_start = current_batch_end;
Time current_batch_time = 0;
Size current_batch_size = 0;
for (JobPos job_pos = 0; job_pos < current_batch_size; ++job_pos) {
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 << std::endl << "Job " << job_id
<< " has already benn scheduled." << std::endl;
}
}
jobs.add(job_id);
current_batch_size += job.size;
if (current_batch_time < job.processing_time)
current_batch_time = job.processing_time;
current_batch_end = current_batch_start + current_batch_time;
if (verbosity_level >= 2) {
os
<< std::setw(12) << job_id
<< std::setw(12) << job.processing_time
<< std::setw(12) << job.size
<< std::setw(12) << current_batch_start
<< std::setw(12) << current_batch_size
<< std::setw(12) << current_batch_end
<< std::endl;
}
}
total_completion_time += current_batch_size * current_batch_end;
if (verbosity_level >= 2) {
os << "Batch " << number_of_batches - 1
<< "; number of jobs: " << current_batch_size
<< "; total completion time: " << total_completion_time
<< std::endl;
}
// Check batch capacity.
if (current_batch_size > capacity()) {
number_of_overloaded_batches++;
if (verbosity_level == 2)
os << "Batch " << number_of_batches - 1 << " is overloaded." << std::endl;
}
current_batch_start = current_batch_end;
}
bool feasible
= (jobs.size() == number_of_jobs())
&& (number_of_duplicates == 0)
&& (number_of_overloaded_batches == 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
<< "Number of overloaded batches: " << number_of_overloaded_batches << std::endl
<< "Feasible: " << feasible << std::endl
<< "Number of batches: " << number_of_batches << std::endl
<< "Total completion time: " << total_completion_time << std::endl
;
}
return {feasible, total_completion_time};
}
private:
/*
* Private methods
*/
/** Constructor to build an instance manually. */
Instance() { }
/*
* Private attributes
*/
/** Jobs. */
std::vector<Job> jobs_;
/** Capacity of the batches. */
Size capacity_ = 0;
friend class InstanceBuilder;
};
class InstanceBuilder
{
public:
/** Constructor. */
InstanceBuilder() { }
/** Add a job. */
void add_job(
Time processing_time,
Size size)
{
Job job;
job.processing_time = processing_time;
job.size = size;
instance_.jobs_.push_back(job);
}
/** Set the capacity of the batches. */
void set_capacity(Size capacity) { instance_.capacity_ = capacity; }
/** 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 == "alfieri2021") {
read_alfieri2021(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 'alfieri2021' format. */
void read_alfieri2021(std::ifstream& file)
{
JobId number_of_jobs = -1;
Size capacity = -1;
file >> number_of_jobs >> capacity;
set_capacity(capacity);
Time processing_time;
Size size;
for (JobId job_id = 0; job_id < number_of_jobs; ++job_id) {
file >> processing_time >> size;
add_job(processing_time, size);
}
}
/*
* Private attributes
*/
/** Instance. */
Instance instance_;
};
}
}