-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJobshopSA.cpp
208 lines (185 loc) · 5.56 KB
/
JobshopSA.cpp
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
#include <cstdio>
#include <cctype>
#include <list>
#include "Graph.h"
#include "Schedule.h"
#define PB push_back
#define INST_BEASLEY 0
#define INST_TAILLARD 1
using namespace std;
int main(int argc, char* argv[])
{
double modulation = 0.85;
double initial_temperature = 50;
double alpha_warming = 2;
double alpha_cooling = 0.85;
int cooling_age_length = 300;
double warming_threshold = 0.95;
int max_moves_without_improvement = 2000;
int instance_format = INST_BEASLEY;
int limit = 0;
double totaltime;
// INTERPRETACJA ARGUMENTOW WIERSZA POLECEN
/*
WYBOR FORMATU INSTANCJI
jobshop inst_file - Beasley (default)
jobshop inst_file {T|B} - Taillard/Beasley
jobshop inst_file {T|B} n - Beasley lub Taillard z ograniczeniem
do n pierwszych zadan
*/
if (argc == 1)
{
printf("usage: jobshop inst_file [ {T|B} {0..infinity} ]\n");
return 0;
}
else
{
if (argc >= 3)
{
if (tolower(argv[2][0]) == 't') instance_format = INST_TAILLARD;
else if (tolower(argv[2][0]) == 'b') instance_format = INST_BEASLEY;
else printf("Niewlasciwy parametr formatu instancji - domyslnie Beasley\n");
if (argc >= 4)
{
if (atoi(argv[3]) != 0) limit = atoi(argv[3]);
}
}
}
// OTWIERANIE PLIKU PODANEGO W 1 ARGUMENCIE
FILE *source;
if((source = fopen(argv[1], "r")) == NULL)
{
printf("Blad otwarcia pliku!\n");
return 1;
}
//pamietamy argumenty z maina (potrzebne do testow)
// LICZBA ZADAN I MASZYN - odczytywanie 1-ego wiersza
int noJobs, noMachines;
long int smieci;
int lower_bound = 0, upper_bound = 0;
if (instance_format == INST_TAILLARD)
fscanf(source, "%d %d %ld %ld %d %d", &noJobs, &noMachines, &smieci, &smieci, &lower_bound, &upper_bound);
else
fscanf(source, "%d %d", &noJobs, &noMachines);
//s - uszeregowanie
Schedule s(noMachines);
s.lower_bound = lower_bound;
s.upper_bound = upper_bound;
//wektory m - machines, t - times --> dla danego Joba
vector<int> m, t;
// Beasley
if (instance_format == INST_BEASLEY)
{
// Jesli istnieje ograniczenie liczby szeregowanych zadan:
if (limit > 0 && limit <= noJobs)
noJobs = limit;
int i, j;
for(int i = 0; i < noJobs; i++)
{
m.clear();
t.clear();
//poniewaz czytamy ba biezaco czas i maszyne dla danej operacji,
//to mozemy 2 wektory pomocnicze wykorzystac dla kazdego zadania
//(wystarcza 2 pomocnicze)
for(int j = 0; j < noMachines; j++)
{
int machine_tmp, time_tmp;
fscanf(source, "%d %d ", &machine_tmp, &time_tmp);
m.push_back(machine_tmp);
t.push_back(time_tmp);
}
s.add_job(t, m);
}
}
// Taillard
else if (instance_format == INST_TAILLARD)
{
// ile zadan jest do pominiecia
int redundant_tasks =
(limit > 0 && limit <= noJobs)
? noJobs - limit
: 0;
noJobs -= redundant_tasks;
//tablica 2d (wektor wektorow) do przechowywania czasow
//potrzebna tylko do instancji taillarda
//(musimy na raz zapamietac wszystkie czasy, w przeciwienstwie do beasley'a)
vector<vector<int> > tailard_times;
//resize: liczba wierszy to liczba jobow,
//liczba kolumn nie okreslona (bo robimy push_back)
tailard_times.resize(noJobs);
for(int i = 0; i < noJobs; i++)
{
tailard_times[i].resize(0);
}
for(int i = 0; i < noJobs; i++)
{
tailard_times[i].clear();
}
// odczytalismy juz noJobs i noMachines, teraz pomijamy smieci z pierwszego wiersza
char skip[256];
void *result = fgets(skip, 256, source);
// pomijamy smieci ("Times")
result = fgets(skip, 100, source);
for(int i = 0; i < noJobs; i++)
{
for(int j = 0; j < noMachines; j++)
{
int time_tmp;
fscanf(source, "%d ", &time_tmp);
tailard_times[i].push_back(time_tmp);
}
}
// pomijamy reszte zadan
for (int i = 0; i < redundant_tasks; i++)
{
result = fgets(skip, 256, source);
}
// pomijamy smieci ("Machines")
result = fgets(skip, 100, source);
for(int i = 0; i < noJobs; i++)
{
m.clear();
//tutaj vector m tez jest wykorzystywany wiele razy
for(int j = 0; j < noMachines; j++)
{
int machine_tmp;
fscanf(source, "%d ", &machine_tmp);
m.push_back(machine_tmp-1);
// -1 bo w instancjach tai maszyny sa numerowane od 1
}
s.add_job(tailard_times[i], m);
}
// pomijamy reszte zadan
for (int i = 0; i < redundant_tasks; i++)
{
result = fgets(skip, 256, source);
}
}
if(fclose(source) != 0)
printf("Blad zamkniecia pliku %s!\n", argv[1]);
//jesli jest tylko 1 zadane, to rozwiazanie jest gotowe
if(noJobs == 1)
{
s.create_graph();
printf("%d\n", s.get_cmax());
s.print_start_times();
printf("\n");
}
//rozwiazanie metoda simulated annealing
else
totaltime = s.solve_using_SA(modulation, initial_temperature, alpha_warming, alpha_cooling, cooling_age_length, warming_threshold, max_moves_without_improvement);
//zapisywanie wynikow jakosciowych do pliku
char buffer[] = "wyniki.txt";
FILE *times;
times = fopen(buffer, "a");
//fprintf(stderr, "\nOtworzono plik: %s\n", buffer);
fprintf(times, "%s\t%d\t%d\t%f\t%d", argv[1], noJobs, noMachines, totaltime, s.get_cmax());
if(instance_format == INST_TAILLARD)
fprintf(times, "\t%d\t%d\n", lower_bound, upper_bound);
else
fprintf(times, "\n");
if(fclose(times) != 0)
printf("Blad zamkniecia pliku %s!\n", buffer);
//fprintf(stderr, "przetworzono %s\n", argv[1]);
return 0;
}