-
Notifications
You must be signed in to change notification settings - Fork 1
/
c_linkfirst_1.cpp
executable file
·300 lines (264 loc) · 8.54 KB
/
c_linkfirst_1.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
#include <fstream>
#include <sstream>
#include <string>
#include <ilcplex/ilocplex.h>
ILOSTLBEGIN //a macro that is needed for portability (necessary)
typedef IloArray<IloNumArray> Xjk;
typedef IloArray<Xjk> Xijk;
typedef IloArray<Xijk> Xijkl; //xijkl[l][i][j][k]
int main (int argc, char *argv[])
{
ifstream infile;
FILE *file;
infile.open("Proj3_op.txt");
if(!infile){
cerr << "Unable to open the file\n";
exit(1);
}
cout << "Before Everything!!!" << "\n";
IloEnv env;
IloInt i,j,varCount1,varCount2,varCount3,conCount; //same as “int i;”
IloInt k,w,K,W,E,l,P,N,L;
IloInt tab, newline, val; //from file
char line[2048];
try {
N = 4;
K = 2;
W = (IloInt)atoi(argv[1]);
L = 12;
IloModel model(env); //set up a model object
IloNumVarArray var1(env);// = IloNumVarArray(env,K*W*N*N);
// IloNumVarArray var2(env);
IloNumVarArray var3(env);// = IloNumVarArray(env,W); //declare an array of variable objects, for unknowns
IloNumVar W_max(env, 0, W, ILOINT);
//var1: c_ijk_w
//var2: X_ijk_l
IloRangeArray con(env);// = IloRangeArray(env,N*N + 3*w); //declare an array of constraint objects
IloNumArray2 t = IloNumArray2(env,N); //Traffic Demand
IloNumArray2 e = IloNumArray2(env,N); //edge matrix
//IloObjective obj;
//Define the Xijk matrix
Xijkl xijkl_m(env, L);
for(l=0;l<L;l++){
xijkl_m[l] = Xijk(env, N);
for(i=0;i<N;i++){
xijkl_m[l][i] = Xjk(env, N);
for(j=0;j<N;j++){
xijkl_m[l][i][j] = IloNumArray(env, K);
}
}
}
//reset everything to zero here
for(l=0;l<L;l++)
for(i=0;i<N;i++)
for(j=0;j<N;j++)
for(k=0;k<K;k++)
xijkl_m[l][i][j][k] = 0;
/*L = { 0 1, 0 2, 0 3, 1 0, 1 2, 1 3, 2 0, 2 1, 2 3, 3 0, 3 1, 3 2} */
//l = 0 , for_all i,j,k
xijkl_m[0][0][1][0] = 1; // k = 0
xijkl_m[0][0][2][1] = xijkl_m[0][0][3][1] = xijkl_m[0][2][1][1] = xijkl_m[0][3][1][1] = 1; // k = 1
//l = 1
xijkl_m[1][0][2][0] = 1; // k = 0
xijkl_m[1][0][1][1] = xijkl_m[1][1][2][1] = xijkl_m[1][3][2][1] = 1; //k=1
//l = 2
xijkl_m[2][0][3][0] = 1; //k=0
xijkl_m[2][1][3][1] = xijkl_m[2][2][3][1] = 1; //k=1
//l=3
xijkl_m[3][1][0][0] = 1; //k=0
xijkl_m[3][1][2][1] = xijkl_m[3][1][3][1] = xijkl_m[3][2][0][1] = xijkl_m[3][3][0][1] = 1;//k = 1
//l=4
xijkl_m[4][1][2][0] = 1;//k=0
xijkl_m[4][0][2][1] = xijkl_m[4][1][0][1] = 1;
//l=5
xijkl_m[5][1][3][0] = 1;
xijkl_m[5][0][3][1] = 1;
//l=6
xijkl_m[6][2][0][0] = 1;
xijkl_m[6][1][0][1] = xijkl_m[6][2][1][1] = xijkl_m[6][2][3][1] = 1;
//l=7
xijkl_m[7][2][1][0] = 1;
xijkl_m[7][0][1][1] = xijkl_m[7][2][0][1] = 1;
//l=8
xijkl_m[8][2][3][0] = 1;
//l=9
xijkl_m[9][3][0][0] = 1;
xijkl_m[9][3][1][1] = xijkl_m[9][3][2][1] = 1;
//l=10
xijkl_m[10][3][1][0] = 1;
xijkl_m[10][3][0][1] = 1;
//l = 11
xijkl_m[11][3][2][0] = 1;
/* string linetmp;
IloInt xi,xj,xk;
xi = xj = xk = 0;
int path_no = 0;
while(infile.getline(line, 2048)){
linetmp = line;
//cout << "Current line: " << linetmp << endl;
if(linetmp.find("\n") == linetmp.length()-1){
//cout << "Line: " << linetmp << endl;
path_no = 0;
continue;
}
istringstream iss(linetmp);
string sub_a[3];//max
int s_cnt = 0;
do{
string sub;
getline(iss, sub, '\t');
//cout << "*" << sub;
if(!sub.empty()){
sub_a[s_cnt++] = sub;
}
}while(!iss.eof());
//process a line
cout << "Cnt:" << s_cnt << endl;
int ii;
for(ii=0;ii<s_cnt;ii++){
if(ii+1 == s_cnt)
continue;
cout << "Path: " << path_no << "\t\t";
IloInt xi,xj;
int ti,tj;
istringstream(sub_a[ii]) >> ti;
istringstream(sub_a[ii+1]) >> tj;
cout << ti << " " << tj << "\t";
xi = (IloInt)ti-1;
xj = (IloInt)tj-1;
xijk_matrix[xi][xj][path_no] = 1;
}
cout << "\n";
if(path_no == 0)
path_no = 1;
else
path_no = 0;
}
*/
cout<<"bahre\n";
/*for(i=0;i<N;i++)
e[i] = IloNumArray(env,N);
e[0][1] = e[0][2] = e[0][5] = e[0][6] = IloNum(1);
e[1][0] = e[1][2] = e[1][4] = e[1][7] = IloNum(1);
e[2][0] = e[2][1] = e[2][3] = e[2][8] = IloNum(1);
e[3][2] = e[3][5] = e[3][4] = e[3][8] = IloNum(1);
e[4][1] = e[4][3] = e[4][5] = e[4][7] = IloNum(1);
e[5][0] = e[5][3] = e[5][4] = e[5][6] = IloNum(1);
e[6][0] = e[6][5] = e[6][7] = e[6][8] = IloNum(1);
e[7][1] = e[7][6] = e[7][4] = e[7][8] = IloNum(1);
e[8][2] = e[8][4] = e[8][6] = e[8][7] = IloNum(1);*/
for(i=0;i<N;i++){
t[i] = IloNumArray(env,N);
for(j=0;j<N;j++){
if(i == j)
t[i][j] = IloNum(0);
else if(i != j)
t[i][j] = IloNum(3);
}
}
printf("ikde\n");
//Minimize W_max
IloObjective obj=IloMinimize(env);
obj.setLinearCoef(W_max, 1.0);
cout << "here khali\n";
//Setting var1[] for Demands Constraints
for(i=0;i<N;i++)
for(j=0;j<N;j++)
for(k=0;k<K;k++)
for(w=0;w<W;w++)
var1.add(IloNumVar(env, 0, 1, ILOINT));
//c_ijk_w variables set.
for(i=0;i<N*N*K*W;i++)
var1[i];
//Setting var2[] for Wavelength Constraints_1
//E = 10;
/*for(i=0;i<9;i++)
for(j=0;j<9;j++)
for(k=0;k<K;k++)
// for(w=0;w<E;w++)
var2.add(IloNumVar(env, 0, 1, ILOINT));*/
for(w = 0;w < W;w++)
var3.add(IloNumVar(env, 0, W, ILOINT)); //Variables for u_w
cout<<"variables ready\n";
//IloRangeArray con1 = IloRangeArray(env, 1);
//con1.add(IloRange(env, 0, 3));
//con1[0].setLinearCoef(IloNumVar(env,0,1,ILOINT),1.0);
//cout << "Dumy Set\n";
conCount = 0;
for(i=0;i<N;i++)
for(j=0;j<N;j++){
con.add(IloRange(env, t[i][j], t[i][j]));
//varCount1 = 0;
for(k=0;k<K;k++)
for(w=0;w<W;w++){
con[conCount].setLinearCoef(var1[i*N*W*K+j*W*K+k*W+w],1.0);
//cout << "Before Adding Constraint\n";
//con[1].setLinearCoef(IloNumVar(env, 0, 1, ILOINT), 1.0);
//cout<<"coef set "<<varCount1;
}
conCount++;
}//Adding Demands Constraints to con
cout<<"1st\n";
IloInt z= 0;
for(w=0;w<W;w++){
for(l=0;l<L;l++){
con.add(IloRange(env, -IloInfinity, 1));
for(i=0;i<N;i++){
for(j=0;j<N;j++){
for(k=0;k<K;k++){
con[conCount].setLinearCoef(var1[i*N*W*K+j*W*K+k*W+w],xijkl_m[l][i][j][k]);
}
}
}
conCount++;
}
}
cout<<"2nd\n";
//Adding Wavelength Constraints_1 to con
P = N * (N-1) * K;
for(w=0;w<W;w++){
con.add(IloRange(env, -IloInfinity, 0));
varCount1 = 0;
for(i=0;i<N;i++)
for(j=0;j<N;j++)
for(k=0;k<K;k++){
con[conCount].setLinearCoef(var1[i*N*W*K+j*W*K+k*W+w],1.0);
}
con[conCount].setLinearCoef(var3[w],-P);
conCount++;
}
cout<<"3rd\n";
varCount3 = 0;
con.add(IloRange(env, 0, IloInfinity));
con[conCount].setLinearCoef(W_max, 1.0);
for(w = 0;w < W ;w++){
con[conCount].setLinearCoef(var3[w], -1.0 * w);
}
cout<<"after constraints\n";
//model.add(obj); //add objective function into model
model.add(IloMinimize(env,obj));
model.add(con); //add constraints into model
IloCplex cplex(model); //create a cplex object and extract the //model to this cplex object
// Optimize the problem and obtain solution.
if ( !cplex.solve() ) {
env.error() << "Failed to optimize LP" << endl;
throw(-1);
}
IloNumVar vals(env); //declare an array to store the outputs
//if 2 dimensional: IloNumArray2 vals(env);
env.out() << "Solution status = " << cplex.getStatus() << endl;
//return the status: Feasible/Optimal/Infeasible/Unbounded/Error/…
env.out() << "Solution value = " << cplex.getObjValue() << endl;
//return the optimal value for objective function
//cplex.getValues(vals, W_max); //get the variable outputs
env.out() << "Values = " << vals << endl; //env.out() : output stream
}
catch (IloException& e) {
cerr << "Concert exception caught: " << e << endl;
}
catch (...) {
cerr << "Unknown exception caught" << endl;
}
env.end(); //close the CPLEX environment
return 0;
} // END main