-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathConfigInt.cpp
304 lines (233 loc) · 5.63 KB
/
ConfigInt.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
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
#include <ilcplex/ilocplex.h>
#include <iostream>
#include "ConfigInt.hpp"
ILOSTLBEGIN
typedef IloArray< IloArray<IloNumArray> > NumMatrix;
using namespace std;
/**
*TODO
* chemin critique
* creation des intervalles de chaque noeud (plus tot, lus tard)
* Pour tout noeud
* Reduction d'espace de recherche pour la config
* creation de la config
*/
ConfigInt::ConfigInt(){}
ConfigInt::ConfigInt(Parser &p):
_n(p.jobs()),_bigM(1000000), _T(p.getHorizon()),
_r(p.nOfRes()), model(env), S(env, p.jobs()), constraints(env), x(env, p.jobs()),
_b(p.reqJobsMach()), _p(p.durationsVector()), _B(p.resAvail()){
for (int i = 0; i < _n; i++) {
x[i] = IloArray<IloNumVar> (env, _n);
char name1[256];
sprintf(name1, "S %d ", i);
S[i] = IloNumVar(env, 0, p.getHorizon() - p.durationsVector()[i], ILOINT, name1);
//f[i] = IloArray <IloArray <IloNumVar> > (env, _n);
for (int j = 0; j < _n; j++) {
char name[256];
sprintf(name, "X %d %d", i, j);
x[i][j] = IloNumVar(env, 0, 1, ILOBOOL, name);
for (int k = 0; k < p.nOfRes(); k++) {
sprintf(name, "f %d %d %d", i, j, k);
}
}
}
cout<<"INIT DONE\n";
}
IloObjective ConfigInt::objective(){
IloObjective obj(env, S[_n - 1], IloObjective::Minimize, "OBJ");
return obj;
}
int ConfigInt::bigM(){
return _bigM;
}
IloNumArray ConfigInt::v(){
return _v;
}
vector<int> ConfigInt::Slate(){
return _Slate;
}
void ConfigInt::addConstraints(Parser &p){
for(int i = 0; i < _n; i++){
IloExpr e00(env);
e00 = x[i][i];
model.add(e00 == 0);
e00.end();
for (int j = 0; j < p.sucVector()[i].size(); j++) {
IloExpr e0(env);
e0 = x[i][p.sucVector()[i][j]];
model.add(e0 == 1);
e0.end();
}
}
/*Les deux premiere*/
for(int i = 0; i < _n; i++){
IloExpr e2(env);
for(int j =0; j < _n; j++){
e2 = x[i][j] + x[j][i] ;
model.add(e2 <= 1);
e2.end();
IloExpr e3(env);
for(int k = 0; k <_n; k++){
e3 = x[i][j] + x[j][k] - x[i][k] ;
model.add(e3 <= 1);
}
e3.end();
}
}
for(int i = 0; i < _n; i++){
IloExpr e4(env);
for(int j = 0; j < _n; j++){
e4 = S[j] - S[i] - (p.durationsVector()[i] + bigM())*x[i][j];
model.add(e4 >= - bigM());
e4.end();
}
}
}
bool ConfigInt::createConfig(IloNumArray v){
bool boolNewConf = false;
//cout<<"......\n";
vector< vector <int> > tmp;
vector< vector <int> > conf2;
for(int i = 1; i< _n-1; i++){
vector<int> tmpI;
tmpI.push_back(i);
for(int j = 1; j<_n-1; j++){
if(i != j){
if(v[j] <= v[i]){
if ((v[j] + _p[j]) > v[i]){
tmpI.push_back(j);
}
}
}
}
tmp.push_back(tmpI);
tmpI.clear();
}
for(int j = 0; j<tmp.size(); j++){
for(int k =0; k < _r; k++){
int sum = 0;
for(int i = 0; i < tmp[j].size(); i++){
int index = tmp[j][i];
sum += _b[index][k];
}
if(sum > _B[k]){
boolNewConf = true;
conf2.push_back(tmp[j]);
}
}
}
for(int a = 0; a < conf2.size(); a++){
IloExpr e(env);
for(int b = 0; b < conf2[a].size(); b++){
for(int c = 0; c < conf2[a].size(); c++){
if(b != c){
e += x[conf2[a][b]][conf2[a][c]];
}
}
}
//cout << "e = "<<e<<"\n";
model.add(e >= 1);
e.end();
}
if(!conf2.empty()){
for(int k =0; k<conf2.size(); k++){
F.push_back(conf2[k]);
}
}
return boolNewConf;
}
void ConfigInt::solve(Parser& p){
try{
model.add(objective());
//cout << "objectif"<< endl;
addConstraints(p);
//cout << "const"<< endl;
/*premier solve*/
IloCplex cplex(model);
cplex.solve();
cout<<"OK\n";
//addConfig(p);
/*second solve*/
//IloCplex cplex(model);
//cplex.solve();
cout <<"\n\nSOL= " <<cplex.getObjValue()<<"\n\n";
cout << cplex.getObjValue() << endl;
bool finished= false;
while(!finished){
IloNumArray _v(env);
//cout <<"OK\n";
cplex.getValues(_v, S);
//cout<<"OK2\n";
finished = !createConfig(_v);
_v.end();
//cout<<"OK2\n";
cplex.solve();
cout << "\n\nSOL= " << cplex.getObjValue() << "\n\n";
for (int i = 0; i < p.jobs(); i++) {
cout << "S" << i << " = " << cplex.getValue(S[i]) << "\n";
}
cout << cplex.getObjValue() << endl;
}
IloNumArray _v(env);
cplex.getValues(_v, S);
//cout<<"v NULL?? "<<_v<<" and _n = "<<_n<<"\n";
for(int i = 0; i < _n; i++){
_vSol.push_back(_v[i]);
}
}
catch (IloException& e) {
cerr << "ERROR : " << e << "\n";
}
}
//line1 n r
//line2 R1 R2 ...
//line3 B1 B2 ...
//line4 j1 j2 ..
//line5 S1 S2 ...
//line6 p1 p2 ...
//line7 to r b11 b12 ...
void ConfigInt::writeSolution(string fileName){
//string fname = "sol.txt";
ofstream file(fileName.c_str(), ios::out);
//cout<<"file OKKK\n";
if(file){
file << _n << " "<< _r << "\n";
//line2
for(int i = 0; i<_r; i++){
file << i << " ";
}
file << "\n";
//line3
for(int i = 0; i<_r; i++){
file << _B[i] << " ";
}
file << "\n";
//line4
for(int i = 0; i<_n; i++){
file << i << " ";
}
file << "\n";
//line 5
for(int i =0; i< _n; i++){
file << _vSol.front() << " ";
//cout << i<< " : "<<_vSol.front()<<"\n";
_vSol.pop_front();
}
//line6
for(int i = 0; i<_n; i++){
file << _p[i] << " ";
}
file << "\n";
//from line7
for(int i= 0; i<_r; i++){
for(int j = 0; j<_n; j++){
file << _b[i][j] << " ";
}
file << "\n";
}
file.close();
}
else
cerr << "Error while trying to write the file\n";
}