Repository navigation
Expand file tree
/
Copy pathMPSRead.cpp
More file actions
392 lines (368 loc) · 10.2 KB
/
Copy pathMPSRead.cpp
File metadata and controls
392 lines (368 loc) · 10.2 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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/******************************************************************************
* HELP (Homogeneous and self-dual Efficient Linear Programming) *
* *
* An experimental implementation of Homogeneous and Self-Dual Algorithm *
* for Linear Programming. *
* *
* Author: Tian Xie (Research Center for Management Science and Information *
* Analytics, Shanghai University of Finance and Economics) *
* *
* Credits: Fundamental implementation idea originated from COPL_LP. *
* (Xiong Zhang and Yinyu Ye) *
* See http://web.stanford.edu/~yyye/Col.html . *
******************************************************************************/
#include "LP.h"
#include "Hash.h"
const int CARD_POS[] = {4, 14, 24, 39, 49};
FILE* Fin;
const int MAX_LINE_LENGTH = 256;
char Buf[MAX_LINE_LENGTH];
unsigned long long Obj_Row;
THashTable Hash_Row, Hash_Col;
int ColItemNo;
void MPS_CopyName(char* Dest, char* Src)
{
int Lenm1 = min((int) strlen(Src), 8) - 1;
while (Lenm1 >= 0 && (Src[Lenm1] == ' ' || Src[Lenm1] == '\r' || Src[Lenm1] == '\n'))
Lenm1 --;
Dest[Lenm1 + 1] = 0;
for (; Lenm1 >= 0; Lenm1 --)
Dest[Lenm1] = Src[Lenm1];
}
unsigned long long MPS_HashCode(char* S)
{
int Lenm1 = min((int) strlen(S), 8) - 1;
unsigned long long Ret = 0;
while (Lenm1 >= 0 && (S[Lenm1] == ' ' || S[Lenm1] == '\r' || S[Lenm1] == '\n'))
Lenm1 --;
for (; Lenm1 >= 0; Lenm1 --)
Ret = (Ret << 8) | ((unsigned char) S[Lenm1]);
return Ret;
}
void MPS_ReadLine()
{
while (1)
{
if (feof(Fin))
CheckError(1, "MPS_ReadLine: EOF before ENDATA!");
if (fgets(Buf, MAX_LINE_LENGTH, Fin) == NULL)
CheckError(1, "MPS_ReadLine: fgets ERROR!");
if (Buf[0] != '*' && Buf[0] != '\r' && Buf[0] != '\n')
break;
}
for (int i = 0; Buf[i]; i ++)
if (Buf[i] == '\r' || Buf[i] == '\n')
{
Buf[i] = 0;
break;
}
}
void MPS_NAME()
{
if (strncmp(Buf, "NAME", 4))
CheckError(1, "MPS_NAME: Expected NAME!");
MPS_CopyName(Problem_Name, Buf + 14);
printf(" NAME SECTION\n");
printf(" NAME = \"%s\"\n", Problem_Name);
MPS_ReadLine(); // For MPS_ROWS
}
void MPS_ROWS()
{
if (strncmp(Buf, "ROWS", 4))
CheckError(1, "MPS_ROWS: Expected ROWS!");
printf(" ROWS SECTION\n");
while (1)
{
MPS_ReadLine();
if (Buf[0] != ' ') // End of ROWS
break;
char Type = toupper(Buf[1]);
if (Type == ' ')
Type = toupper(Buf[2]);
if (Type != 'N' && Type != 'E' && Type != 'G' && Type != 'L')
{
printf(" Warning: Unknown Type: \"%c\", Ignored.\n", Type);
continue;
}
unsigned long long Hash = MPS_HashCode(Buf + CARD_POS[0]);
if (Type == 'N')
{
if (Obj_Row)
{
printf(" Warning: Duplicate Objective Row, Ignored.\n");
continue;
}
Obj_Row = Hash; // Store Objective Row's Hash Value
Hash_Row.Insert(Hash, HASH_OBJECTIVE); // Objective
continue;
}
if (Hash_Row.Insert(Hash, n_Row) == HASH_INSERT_FOUND)
{
printf(" Warning: Duplicate Row Name, HashCode = %llu, Ignored.\n", Hash);
continue;
}
Row_Type[n_Row] = Type;
V_RHS[n_Row] = 0.0;
n_Row ++;
}
printf(" %d Rows Read (Objective Row Excluded)\n", n_Row);
if (! Obj_Row)
CheckError(1, "MPS_ROWS: No Objective Row!");
}
void MPS_COLUMNS()
{
if (strncmp(Buf, "COLUMNS", 7))
CheckError(1, "MPS_COLUMNS: Expected COLUMNS!");
printf(" COLUMNS SECTION\n");
ColItemNo = 0;
while (1)
{
ColItemNo ++;
MPS_ReadLine();
if (Buf[0] != ' ') // End of COLUMNS
break;
unsigned long long ColHash = MPS_HashCode(Buf + CARD_POS[0]);
int ColID = Hash_Col.Find(ColHash);
if (ColID == HASH_NOT_FOUND)
{
Hash_Col.Insert(ColHash, n_Col);
ColID = n_Col;
V_Cost[n_Col] = 0.0;
V_Matrix_Col_Head[n_Col] = -1;
n_Col ++;
}
int CurCardLen = strlen(Buf);
for (int ip = 1; ip <= 3 && CARD_POS[ip] < CurCardLen; ip += 2)
{
unsigned long long RowHash = MPS_HashCode(Buf + CARD_POS[ip]);
int RowID = Hash_Row.Find(RowHash);
if (RowID == HASH_NOT_FOUND)
{
printf(" Warning: Row Name Not Found, HashCode = %llu, Ignored.\n", RowHash);
continue;
}
double RCValue = atof(Buf + CARD_POS[ip + 1]);
if (RowID == HASH_OBJECTIVE) // Objective, Minimization
{
V_Cost[ColID] = RCValue;
continue;
}
if (fabs(RCValue) < Input_Tolerance)
{
printf(" Warning: Zero Element Found, %.10lf, Ignored.\n", RCValue);
continue;
}
// Cannot check duplicate element
V_Matrix_Row[n_Element] = RowID;
V_Matrix_Value[n_Element] = RCValue;
V_Matrix_Col_Next[n_Element] = V_Matrix_Col_Head[ColID];
V_Matrix_Col_Head[ColID] = n_Element;
n_Element ++;
}
}
printf(" %d Columns and %d Elements Read (Objective Row Excluded)\n", n_Col, n_Element);
}
void MPS_RHS()
{
if (strncmp(Buf, "RHS", 3))
{
printf(" NULL RHS Section!\n");
return;
}
printf(" RHS SECTION\n");
unsigned long long Enabled_RHS_Hash = MPS_HashCode(Enabled_RHS);
int n_RHS = 0;
while (1)
{
MPS_ReadLine();
if (Buf[0] != ' ') // End of RHS
break;
if (Enabled_RHS_Hash != 0 && Enabled_RHS_Hash != MPS_HashCode(Buf + CARD_POS[0]))
continue;
int CurCardLen = strlen(Buf);
for (int ip = 1; ip <= 3 && CARD_POS[ip] < CurCardLen; ip += 2)
{
unsigned long long RowHash = MPS_HashCode(Buf + CARD_POS[ip]);
int RowID = Hash_Row.Find(RowHash);
if (RowID == HASH_NOT_FOUND)
{
printf(" Warning: Row Name Not Found, HashCode = %llu, Ignored.\n", RowHash);
continue;
}
double RCValue = atof(Buf + CARD_POS[ip + 1]);
if (RowID == HASH_OBJECTIVE) // Objective, Minimization
{
printf(" Warning: Row Name Cannot Be Cost Row, Ignored.\n");
continue;
}
if (fabs(RCValue) < Input_Tolerance)
{
printf(" Warning: Zero Element Found, %.10lf, Ignored.\n", RCValue);
continue;
}
V_RHS[RowID] = RCValue;
n_RHS ++;
}
}
printf(" %d RHS Read\n", n_RHS);
}
void MPS_RANGES()
{
for (int i = 0; i < n_Row; i ++)
V_RHS_r[i] = V_RHS[i];
if (strncmp(Buf, "RANGES", 6))
{
printf(" NULL RANGES Section!\n");
return;
}
printf(" RANGES SECTION\n");
unsigned long long Enabled_RANGES_Hash = MPS_HashCode(Enabled_RANGES);
int n_RANGES = 0;
while (1)
{
MPS_ReadLine();
if (Buf[0] != ' ') // End of RANGES
break;
if (Enabled_RANGES_Hash != 0 && Enabled_RANGES_Hash != MPS_HashCode(Buf + CARD_POS[0]))
continue;
int CurCardLen = strlen(Buf);
for (int ip = 1; ip <= 3 && CARD_POS[ip] < CurCardLen; ip += 2)
{
unsigned long long RowHash = MPS_HashCode(Buf + CARD_POS[ip]);
int RowID = Hash_Row.Find(RowHash);
if (RowID == HASH_NOT_FOUND)
{
printf(" Warning: Row Name Not Found, HashCode = %llu, Ignored.\n", RowHash);
continue;
}
double Value = atof(Buf + CARD_POS[ip + 1]);
if (RowID == HASH_OBJECTIVE) // Objective, Minimization
{
printf(" Warning: Row Name Cannot Be Cost Row, Ignored.\n");
continue;
}
if (fabs(Value) < Input_Tolerance)
{
printf(" Warning: Zero Element Found, %.10lf, Ignored.\n", Value);
continue;
}
// TODO, Follow lpguide
if (Row_Type[RowID] == 'E')
{
if (Value < 0)
V_RHS_r[RowID] += Value;
else
V_RHS[RowID] += Value;
}
else if (Row_Type[RowID] == 'L')
V_RHS_r[RowID] -= fabs(Value);
else if (Row_Type[RowID] == 'G')
V_RHS[RowID] += fabs(Value);
else
{
printf(" Warning: Duplicate RANGES, HashCode = %llu, Ignored.\n", RowHash);
continue;
}
Row_Type[RowID] = 'R'; // Ranged
}
}
printf(" %d RANGES Read\n", n_RANGES);
}
void MPS_BOUNDS()
{
for (int i = 0; i < n_Col; i ++)
{
V_LB[i] = Var_Lower_Bound;
V_UB[i] = Var_Upper_Bound;
}
if (strncmp(Buf, "BOUNDS", 6))
{
printf(" NULL BOUNDS Section!\n");
return;
}
printf(" BOUNDS SECTION\n");
unsigned long long Enabled_BOUNDS_Hash = MPS_HashCode(Enabled_BOUNDS);
int n_BOUNDS = 0;
while (1)
{
MPS_ReadLine();
if (Buf[0] != ' ') // End of BOUNDS
break;
if (Enabled_BOUNDS_Hash != 0 && Enabled_BOUNDS_Hash != MPS_HashCode(Buf + CARD_POS[0]))
continue;
unsigned long long ColHash = MPS_HashCode(Buf + CARD_POS[1]);
int ColID = Hash_Col.Find(ColHash);
if (ColID == HASH_NOT_FOUND)
{
printf(" Warning: Column Name Not Found, HashCode = %llu, Ignored.\n", ColHash);
continue;
}
double Value = 0.0;
if (strlen(Buf) > 24)
Value = atof(Buf + CARD_POS[2]);
if (Buf[1] == 'L' && Buf[2] == 'O' && Buf[3] == ' ')
V_LB[ColID] = Value;
else if (Buf[1] == 'U' && Buf[2] == 'P' && Buf[3] == ' ')
V_UB[ColID] = Value;
else if (Buf[1] == 'F' && Buf[2] == 'X' && Buf[3] == ' ')
V_LB[ColID] = V_UB[ColID] = Value;
else if (Buf[1] == 'F' && Buf[2] == 'R' && Buf[3] == ' ')
{
V_LB[ColID] = -MaxPositive;
V_UB[ColID] = MaxPositive;
}
else if (Buf[1] == 'M' && Buf[2] == 'I' && Buf[3] == ' ')
{
V_LB[ColID] = -MaxPositive;
V_UB[ColID] = Value;
}
else if (Buf[1] == 'P' && Buf[2] == 'L' && Buf[3] == ' ')
{
V_LB[ColID] = Value;
V_UB[ColID] = MaxPositive;
}
n_BOUNDS ++;
}
printf(" %d BOUNDS Read\n", n_BOUNDS);
}
void MPS_ENDATA()
{
if (strncmp(Buf, "ENDATA", 6))
CheckError(1, "MPS_ENDATA: Expected ENDATA!");
}
int MPS_ReadFile()
{
n_Row = 0;
n_Col = 0;
Hash_Row.Init(MAX_ROWS, 999983);
Hash_Col.Init(MAX_COLS, 999983);
Obj_Row = 0;
n_Element = 0;
V_Cost_Intercept = 0;
printf("ReadMPSFile BEGIN\n");
Fin = fopen(Filename, "r");
MPS_ReadLine();
MPS_NAME();
MPS_ROWS();
MPS_COLUMNS();
MPS_RHS();
MPS_RANGES();
MPS_BOUNDS();
MPS_ENDATA();
fclose(Fin);
Hash_Row.Release();
Hash_Col.Release();
printf("ReadMPSFile END\n");
return 0;
}
void MPS_PrintMatrix()
{
FILE* Mout = fopen("Matrix.txt", "w");
fprintf(Mout, "nrow = %d;\n", n_Row);
fprintf(Mout, "ncol = %d;\n", n_Col);
fprintf(Mout, "A = zeros(nrow, ncol);\n");
for (int j = 0; j < n_Col; j ++)
for (int p = V_Matrix_Col_Head[j]; p != -1; p = V_Matrix_Col_Next[p])
fprintf(Mout, "A(%d, %d) = %.8lf;\n", V_Matrix_Row[p] + 1, j + 1, V_Matrix_Value[p]);
fclose(Mout);
}