forked from kerrickstaley/lp_solve
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lp_MPS.c
1854 lines (1645 loc) · 58 KB
/
lp_MPS.c
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <string.h>
#include <stdarg.h>
#include <ctype.h>
#include "commonlib.h"
#include "lp_lib.h"
#include "lp_scale.h"
#include "lp_report.h"
#include "lp_MPS.h"
#ifdef FORTIFY
# include "lp_fortify.h"
#endif
/* Define buffer-size controled function mapping */
# if defined _MSC_VER
# define vsnprintf _vsnprintf
# endif
/* MPS file input and output routines for lp_solve */
/* ------------------------------------------------------------------------- */
/*
A: MPS format was named after an early IBM LP product and has emerged
as a de facto standard ASCII medium among most of the commercial LP
codes. Essentially all commercial LP codes accept this format, but if
you are using public domain software and have MPS files, you may need
to write your own reader routine for this. It's not too hard. The
main things to know about MPS format are that it is column oriented (as
opposed to entering the model as equations), and everything (variables,
rows, etc.) gets a name. MPS format is described in more detail in
Murtagh's book, referenced in another section. Also,
ftp://softlib.cs.rice.edu/pub/miplib/mps_format
is a nice short introduction. exports
MPS is an old format, so it is set up as though you were using punch
cards, and is not free format. Fields start in column 1, 5, 15, 25, 40
and 50. Sections of an MPS file are marked by so-called header cards,
which are distinguished by their starting in column 1. Although it is
typical to use upper-case throughout the file (like I said, MPS has
long historical roots), many MPS-readers will accept mixed-case for
anything except the header cards, and some allow mixed-case anywhere.
The names that you choose for the individual entities (constraints or
variables) are not important to the solver; you should pick names that
are meaningful to you, or will be easy for a post-processing code to
read.
Here is a little sample model written in MPS format (explained in more
detail below):
NAME TESTPROB
ROWS
N COST
L LIM1
G LIM2
E MYEQN
COLUMNS
XONE COST 1 LIM1 1
XONE LIM2 1
YTWO COST 4 LIM1 1
YTWO MYEQN -1
ZTHREE COST 9 LIM2 1
ZTHREE MYEQN 1
RHS
RHS1 LIM1 5 LIM2 10
RHS1 MYEQN 7
BOUNDS
UP BND1 XONE 4
LO BND1 YTWO -1
UP BND1 YTWO 1
ENDATA
means:
Optimize
COST: XONE + 4 YTWO + 9 ZTHREE
Subject To
LIM1: XONE + YTWO <= 5
LIM2: XONE + ZTHREE >= 10
MYEQN: - YTWO + ZTHREE = 7
Bounds
0 <= XONE <= 4
-1 <= YTWO <= 1
End
*/
/* copy a MPS name, only trailing spaces are removed. In MPS, names can have
embedded spaces! */
STATIC void namecpy(char *into, char *from)
{
int i;
/* copy at most 8 characters of from, stop at end of string or newline */
for(i = 0; (from[i] != '\0') && (from[i] != '\n') && (from[i] != '\r') && (i < 8); i++)
into[i] = from[i];
/* end with end of string */
into[i] = '\0';
/* remove trailing spaces, if any */
for(i--; (i >= 0) && (into[i] == ' '); i--)
into[i] = '\0';
}
/* scan an MPS line, and pick up the information in the fields that are
present */
/* scan_line for fixed MPS format */
STATIC int scan_lineFIXED(lprec *lp, int section, char* line, char *field1, char *field2, char *field3,
double *field4, char *field5, double *field6)
{
int items = 0, line_len;
char buf[16], *ptr1, *ptr2;
line_len = (int) strlen(line);
while ((line_len) && ((line[line_len-1] == '\n') || (line[line_len-1] == '\r') || (line[line_len-1] == ' ')))
line_len--;
if(line_len >= 1) { /* spaces or N/L/G/E or UP/LO */
strncpy(buf, line, 4);
buf[4] = '\0';
sscanf(buf, "%s", field1);
items++;
}
else
field1[0] = '\0';
line += 4;
if(line_len >= 5) { /* name */
if (line[-1] != ' ') {
report(lp, IMPORTANT, "MPS_readfile: invalid data card; column 4 must be blank\n");
return(-1);
}
namecpy(field2, line);
items++;
}
else
field2[0] = '\0';
line += 10;
if(line_len >= 14) { /* name */
if (line[-1] != ' ' || line[-2] != ' ') {
report(lp, IMPORTANT, "MPS_readfile: invalid data card; columns 13-14 must be blank\n");
return(-1);
}
namecpy(field3, line);
items++;
}
else
field3[0] = '\0';
line += 10;
if(line_len >= 25) { /* number */
if (line[-1] != ' ' || line[-2] != ' ') {
report(lp, IMPORTANT, "MPS_readfile: invalid data card; columns 23-24 must be blank\n");
return(-1);
}
strncpy(buf, line, 15);
buf[15] = '\0';
for(ptr1 = ptr2 = buf; ; ptr1++)
if(!isspace((unsigned char) *ptr1))
if((*(ptr2++) = *ptr1) == 0)
break;
/* *field4 = atof(buf); */
*field4 = strtod(buf, &ptr1);
if(*ptr1) {
report(lp, IMPORTANT, "MPS_readfile: invalid number in columns 25-36 \n");
return(-1);
}
items++;
}
else
*field4 = 0;
line += 15;
if(line_len >= 40) { /* name */
if (line[-1] != ' ' || line[-2] != ' ' || line[-3] != ' ') {
report(lp, IMPORTANT, "MPS_readfile: invalid data card; columns 37-39 must be blank\n");
return(-1);
}
namecpy(field5, line);
items++;
}
else
field5[0] = '\0';
line += 10;
if(line_len >= 50) { /* number */
if (line[-1] != ' ' || line[-2] != ' ') {
report(lp, IMPORTANT, "MPS_readfile: invalid data card; columns 48-49 must be blank\n");
return(-1);
}
strncpy(buf, line, 15);
buf[15] = '\0';
for(ptr1 = ptr2 = buf; ; ptr1++)
if(!isspace((unsigned char) *ptr1))
if((*(ptr2++) = *ptr1) == 0)
break;
/* *field6 = atof(buf); */
*field6 = strtod(buf, &ptr1);
if(*ptr1) {
report(lp, IMPORTANT, "MPS_readfile: invalid number in columns 50-61 \n");
return(-1);
}
items++;
}
else
*field6 = 0;
return(items);
}
STATIC int spaces(char *line, int line_len)
{
int l;
char *line1 = line;
while (*line1 == ' ')
line1++;
l = (int) (line1 - line);
if (line_len < l)
l = line_len;
return(l);
}
STATIC int lenfield(char *line, int line_len)
{
int l;
char *line1 = line;
while ((*line1) && (*line1 != ' '))
line1++;
l = (int) (line1 - line);
if (line_len < l)
l = line_len;
return(l);
}
/* scan_line for fixed MPS format */
STATIC int scan_lineFREE(lprec *lp, int section, char* line, char *field1, char *field2, char *field3,
double *field4, char *field5, double *field6)
{
int items = 0, line_len, len;
char buf[256], *ptr1 = NULL, *ptr2;
line_len = (int) strlen(line);
while ((line_len) && ((line[line_len-1] == '\n') || (line[line_len-1] == '\r') || (line[line_len-1] == ' ')))
line_len--;
len = spaces(line, line_len);
line += len;
line_len -= len;
if ((section == MPSCOLUMNS) || (section == MPSRHS) || (section == MPSRANGES)) {
field1[0] = '\0';
items++;
}
else {
len = lenfield(line, line_len);
if(line_len >= 1) { /* spaces or N/L/G/E or UP/LO */
strncpy(buf, line, len);
buf[len] = '\0';
sscanf(buf, "%s", field1);
if(section == MPSBOUNDS) {
for(ptr1 = field1; *ptr1; ptr1++)
*ptr1=(char)toupper(*ptr1);
}
items++;
}
else
field1[0] = '\0';
line += len;
line_len -= len;
len = spaces(line, line_len);
line += len;
line_len -= len;
}
len = lenfield(line, line_len);
if(line_len >= 1) { /* name */
strncpy(field2, line, len);
field2[len] = '\0';
items++;
}
else
field2[0] = '\0';
line += len;
line_len -= len;
len = spaces(line, line_len);
line += len;
line_len -= len;
len = lenfield(line, line_len);
if(line_len >= 1) { /* name */
strncpy(field3, line, len);
field3[len] = '\0';
items++;
}
else
field3[0] = '\0';
line += len;
line_len -= len;
len = spaces(line, line_len);
line += len;
line_len -= len;
if (*field3) {
if((section == MPSCOLUMNS) && (strcmp(field3, "'MARKER'") == 0)) {
*field4 = 0;
items++;
ptr1 = field3;
}
else if((section == MPSBOUNDS) &&
((strcmp(field1, "FR") == 0) || (strcmp(field1, "MI") == 0) || (strcmp(field1, "PL") == 0) || (strcmp(field1, "BV") == 0)))
/* field3 *is* the variable name */;
else {
/* Some free MPS formats allow that field 2 is not provided after the first time.
The fieldname is then the same as the the defined field the line before.
In that case field2 shifts to field3, field1 shifts to field 2.
This situation is tested by checking if field3 is numerical AND there are an even number of fields after.
*/
char *line1 = line;
int line_len1 = line_len;
int items1 = 0;
while (line_len1 > 0) {
len = lenfield(line1, line_len1);
if (len > 0) {
line1 += len;
line_len1 -= len;
items1++;
}
len = spaces(line1, line_len1);
line1 += len;
line_len1 -= len;
}
if ((items1 % 2) == 0) {
*field4 = strtod(field3, &ptr1);
if(*ptr1 == 0) {
strcpy(field3, field2);
if ((section == MPSROWS) || (section == MPSBOUNDS) /* || (section == MPSSOS) */)
*field2 = 0;
else {
strcpy(field2, field1);
*field1 = 0;
}
items++;
}
else
ptr1 = NULL;
}
else
ptr1 = NULL;
}
}
else {
ptr1 = NULL;
if((section == MPSBOUNDS) &&
((strcmp(field1, "FR") == 0) || (strcmp(field1, "MI") == 0) || (strcmp(field1, "PL") == 0) || (strcmp(field1, "BV") == 0))) {
strcpy(field3, field2);
*field2 = 0;
items++;
}
}
if(ptr1 == NULL) {
len = lenfield(line, line_len);
if(line_len >= 1) { /* number */
strncpy(buf, line, len);
buf[len] = '\0';
for(ptr1 = ptr2 = buf; ; ptr1++)
if(!isspace((unsigned char) *ptr1))
if((*(ptr2++) = *ptr1) == 0)
break;
/* *field4 = atof(buf); */
*field4 = strtod(buf, &ptr1);
if(*ptr1)
return(-1);
items++;
}
else
*field4 = 0;
line += len;
line_len -= len;
len = spaces(line, line_len);
line += len;
line_len -= len;
}
len = lenfield(line, line_len);
if(line_len >= 1) { /* name */
strncpy(field5, line, len);
field5[len] = '\0';
items++;
}
else
field5[0] = '\0';
line += len;
line_len -= len;
len = spaces(line, line_len);
line += len;
line_len -= len;
len = lenfield(line, line_len);
if(line_len >= 1) { /* number */
strncpy(buf, line, len);
buf[len] = '\0';
for(ptr1 = ptr2 = buf; ; ptr1++)
if(!isspace((unsigned char) *ptr1))
if((*(ptr2++) = *ptr1) == 0)
break;
/* *field6 = atof(buf); */
*field6 = strtod(buf, &ptr1);
if(*ptr1)
return(-1);
items++;
}
else
*field6 = 0;
if((section == MPSSOS) && (items == 2)) {
strcpy(field3, field2);
strcpy(field2, field1);
*field1 = 0;
}
if((section != MPSOBJNAME) && (section != MPSBOUNDS)) {
for(ptr1 = field1; *ptr1; ptr1++)
*ptr1=(char)toupper(*ptr1);
}
return(items);
}
STATIC int addmpscolumn(lprec *lp, MYBOOL Int_section, int typeMPS, MYBOOL *Column_ready,
int *count, REAL *Last_column, int *Last_columnno, char *Last_col_name)
{
int ok = TRUE;
if (*Column_ready) {
ok = add_columnex(lp, *count, Last_column, Last_columnno);
if (ok) {
ok = set_col_name(lp, lp->columns, Last_col_name);
}
if (ok) {
set_int(lp, lp->columns, Int_section);
if ((Int_section) && (typeMPS & MPSIBM))
set_bounds(lp, lp->columns, 10.0 / DEF_INFINITE, DEF_INFINITE / 10.0);
}
}
*Column_ready = FALSE;
*count = 0;
return(ok);
}
#if 0
STATIC MYBOOL appendmpsitem(int *count, int rowIndex[], REAL rowValue[])
{
int i = *count;
if(rowValue[i] == 0)
return( FALSE );
while((i > 0) && (rowIndex[i] < rowIndex[i-1])) {
swapINT (rowIndex+i, rowIndex+i-1);
swapREAL(rowValue+i, rowValue+i-1);
i--;
}
(*count)++;
return( TRUE );
}
#endif
STATIC MYBOOL appendmpsitem(int *count, int rowIndex[], REAL rowValue[])
{
int i = *count;
/* Check for non-negativity of the index */
if(rowIndex[i] < 0)
return( FALSE );
/* Move the element so that the index list is sorted ascending */
while((i > 0) && (rowIndex[i] < rowIndex[i-1])) {
swapINT (rowIndex+i, rowIndex+i-1);
swapREAL(rowValue+i, rowValue+i-1);
i--;
}
/* Add same-indexed items (which is rarely encountered), and shorten the list */
if((i < *count) && (rowIndex[i] == rowIndex[i+1])) {
int ii = i + 1;
rowValue[i] += rowValue[ii];
(*count)--;
while(ii < *count) {
rowIndex[ii] = rowIndex[ii+1];
rowValue[ii] = rowValue[ii+1];
ii++;
}
}
/* Update the count and return */
(*count)++;
return( TRUE );
}
MYBOOL MPS_readfile(lprec **newlp, char *filename, int typeMPS, int verbose)
{
MYBOOL status = FALSE;
FILE *fpin;
fpin = fopen(filename, "r");
if(fpin != NULL) {
status = MPS_readhandle(newlp, fpin, typeMPS, verbose);
fclose(fpin);
}
return( status );
}
static int __WINAPI MPS_input(void *fpin, char *buf, int max_size)
{
return(fgets(buf, max_size, (FILE *) fpin) != NULL);
}
MYBOOL __WINAPI MPS_readhandle(lprec **newlp, FILE *filehandle, int typeMPS, int verbose)
{
return(MPS_readex(newlp, (void *) filehandle, MPS_input, typeMPS, verbose));
}
MYBOOL __WINAPI MPS_readex(lprec **newlp, void *userhandle, read_modeldata_func read_modeldata, int typeMPS, int verbose)
{
char field1[BUFSIZ], field2[BUFSIZ], field3[BUFSIZ], field5[BUFSIZ], line[BUFSIZ], tmp[BUFSIZ],
Last_col_name[BUFSIZ], probname[BUFSIZ], OBJNAME[BUFSIZ], *ptr;
int items, row, Lineno, var,
section = MPSUNDEF, variant = 0, NZ = 0, SOS = 0;
MYBOOL Int_section, Column_ready, Column_ready1,
Unconstrained_rows_found = FALSE, OF_found = FALSE, CompleteStatus = FALSE;
double field4, field6;
REAL *Last_column = NULL;
int count = 0, *Last_columnno = NULL;
int OBJSENSE = ROWTYPE_EMPTY;
lprec *lp;
int (*scan_line)(lprec *lp, int section, char* line, char *field1, char *field2, char *field3,
double *field4, char *field5, double *field6);
if(newlp == NULL)
return( CompleteStatus );
else if(*newlp == NULL)
lp = make_lp(0, 0);
else
lp = *newlp;
if((typeMPS & MPSFIXED) == MPSFIXED)
scan_line = scan_lineFIXED;
else if((typeMPS & MPSFREE) == MPSFREE)
scan_line = scan_lineFREE;
else {
report(lp, IMPORTANT, "MPS_readfile: Unrecognized MPS line type.\n");
if (*newlp == NULL)
delete_lp(lp);
return( CompleteStatus );
}
if (lp != NULL) {
lp->source_is_file = TRUE;
lp->verbose = verbose;
strcpy(Last_col_name, "");
strcpy(OBJNAME, "");
Int_section = FALSE;
Column_ready = FALSE;
Lineno = 0;
/* let's initialize line to all zero's */
MEMCLEAR(line, BUFSIZ);
while(read_modeldata(userhandle, line, BUFSIZ - 1)) {
Lineno++;
for(ptr = line; (*ptr) && (isspace((unsigned char) *ptr)); ptr++);
/* skip lines which start with "*", they are comment */
if((line[0] == '*') || (*ptr == 0) || (*ptr == '\n') || (*ptr == '\r')) {
report(lp, FULL, "Comment on line %d: %s", Lineno, line);
continue;
}
report(lp, FULL, "Line %6d: %s", Lineno, line);
/* first check for "special" lines: NAME, ROWS, BOUNDS .... */
/* this must start in the first position of line */
if(line[0] != ' ') {
sscanf(line, "%s", tmp);
if(strcmp(tmp, "NAME") == 0) {
section = MPSNAME;
*probname = 0;
sscanf(line, "NAME %s", probname);
if (!set_lp_name(lp, probname))
break;
}
else if(((typeMPS & MPSFREE) == MPSFREE) && (strcmp(tmp, "OBJSENSE") == 0)) {
section = MPSOBJSENSE;
report(lp, FULL, "Switching to OBJSENSE section\n");
}
else if(((typeMPS & MPSFREE) == MPSFREE) && (strcmp(tmp, "OBJNAME") == 0)) {
section = MPSOBJNAME;
report(lp, FULL, "Switching to OBJNAME section\n");
}
else if(strcmp(tmp, "ROWS") == 0) {
section = MPSROWS;
report(lp, FULL, "Switching to ROWS section\n");
}
else if(strcmp(tmp, "COLUMNS") == 0) {
allocREAL(lp, &Last_column, lp->rows + 1, TRUE);
allocINT(lp, &Last_columnno, lp->rows + 1, TRUE);
count = 0;
if ((Last_column == NULL) || (Last_columnno == NULL))
break;
section = MPSCOLUMNS;
report(lp, FULL, "Switching to COLUMNS section\n");
}
else if(strcmp(tmp, "RHS") == 0) {
if (!addmpscolumn(lp, Int_section, typeMPS, &Column_ready, &count, Last_column, Last_columnno, Last_col_name))
break;
section = MPSRHS;
report(lp, FULL, "Switching to RHS section\n");
}
else if(strcmp(tmp, "BOUNDS") == 0) {
section = MPSBOUNDS;
report(lp, FULL, "Switching to BOUNDS section\n");
}
else if(strcmp(tmp, "RANGES") == 0) {
section = MPSRANGES;
report(lp, FULL, "Switching to RANGES section\n");
}
else if((strcmp(tmp, "SOS") == 0) || (strcmp(tmp, "SETS") == 0)) {
section = MPSSOS;
if(strcmp(tmp, "SOS") == 0)
variant = 0;
else
variant = 1;
report(lp, FULL, "Switching to %s section\n", tmp);
}
else if(strcmp(tmp, "ENDATA") == 0) {
report(lp, FULL, "Finished reading MPS file\n");
CompleteStatus = TRUE;
break;
}
else { /* line does not start with space and does not match above */
report(lp, IMPORTANT, "Unrecognized MPS line %d: %s\n", Lineno, line);
break;
}
}
else { /* normal line, process */
items = scan_line(lp, section, line, field1, field2, field3, &field4, field5, &field6);
if(items < 0){
report(lp, IMPORTANT, "Syntax error on line %d: %s\n", Lineno, line);
break;
}
switch(section) {
case MPSNAME:
report(lp, IMPORTANT, "Error, extra line under NAME line\n");
break;
case MPSOBJSENSE:
if(OBJSENSE != ROWTYPE_EMPTY) {
report(lp, IMPORTANT, "Error, extra line under OBJSENSE line\n");
break;
}
if((strcmp(field1, "MAXIMIZE") == 0) || (strcmp(field1, "MAX") == 0)) {
OBJSENSE = ROWTYPE_OFMAX;
set_maxim(lp);
}
else if((strcmp(field1, "MINIMIZE") == 0) || (strcmp(field1, "MIN") == 0)) {
OBJSENSE = ROWTYPE_OFMIN;
set_minim(lp);
}
else {
report(lp, SEVERE, "Unknown OBJSENSE direction '%s' on line %d\n", field1, Lineno);
break;
}
continue;
case MPSOBJNAME:
if(*OBJNAME) {
report(lp, IMPORTANT, "Error, extra line under OBJNAME line\n");
break;
}
strcpy(OBJNAME, field1);
continue;
/* Process entries in the ROWS section */
case MPSROWS:
/* field1: rel. operator; field2: name of constraint */
report(lp, FULL, "Row %5d: %s %s\n", lp->rows + 1, field1, field2);
if(strcmp(field1, "N") == 0) {
if((*OBJNAME) && (strcmp(field2, OBJNAME)))
/* Ignore this objective name since it is not equal to the OBJNAME name */;
else if(!OF_found) { /* take the first N row as OF, ignore others */
if (!set_row_name(lp, 0, field2))
break;
OF_found = TRUE;
}
else if(!Unconstrained_rows_found) {
report(lp, IMPORTANT, "Unconstrained row %s ignored\n", field2);
report(lp, IMPORTANT, "Further messages of this kind will be suppressed\n");
Unconstrained_rows_found = TRUE;
}
}
else if(strcmp(field1, "L") == 0) {
if ((!str_add_constraint(lp, "" ,LE ,0)) || (!set_row_name(lp, lp->rows, field2)))
break;
}
else if(strcmp(field1, "G") == 0) {
if ((!str_add_constraint(lp, "" ,GE ,0)) || (!set_row_name(lp, lp->rows, field2)))
break;
}
else if(strcmp(field1, "E") == 0) {
if ((!str_add_constraint(lp, "",EQ ,0)) || (!set_row_name(lp, lp->rows, field2)))
break;
}
else {
report(lp, SEVERE, "Unknown relation code '%s' on line %d\n", field1, Lineno);
break;
}
continue;
/* Process entries in the COLUMNS section */
case MPSCOLUMNS:
/* field2: variable; field3: constraint; field4: coef */
/* optional: field5: constraint; field6: coef */
report(lp, FULL, "Column %4d: %s %s %g %s %g\n",
lp->columns + 1, field2, field3, field4, field5, field6);
if((items == 4) || (items == 5) || (items == 6)) {
if (NZ == 0)
strcpy(Last_col_name, field2);
else if(*field2) {
Column_ready1 = (MYBOOL) (strcmp(field2, Last_col_name) != 0);
if(Column_ready1) {
if (find_var(lp, field2, FALSE) >= 0) {
report(lp, SEVERE, "Variable name (%s) is already used!\n", field2);
break;
}
if(Column_ready) { /* Added ability to handle non-standard "same as above" column name */
if (addmpscolumn(lp, Int_section, typeMPS, &Column_ready, &count, Last_column, Last_columnno, Last_col_name)) {
strcpy(Last_col_name, field2);
NZ = 0;
}
else
break;
}
}
}
if(items == 5) { /* there might be an INTEND or INTORG marker */
/* look for " <name> 'MARKER' 'INTORG'"
or " <name> 'MARKER' 'INTEND'" */
if(strcmp(field3, "'MARKER'") != 0)
break;
if(strcmp(field5, "'INTORG'") == 0) {
Int_section = TRUE;
report(lp, FULL, "Switching to integer section\n");
}
else if(strcmp(field5, "'INTEND'") == 0) {
Int_section = FALSE;
report(lp, FULL, "Switching to non-integer section\n");
}
else
report(lp, IMPORTANT, "Unknown marker (ignored) at line %d: %s\n",
Lineno, field5);
}
else if((row = find_row(lp, field3, Unconstrained_rows_found)) >= 0) {
if(row > lp->rows)
report(lp, CRITICAL, "Invalid row %s encountered in the MPS file\n", field3);
Last_columnno[count] = row;
Last_column[count] = (REAL)field4;
if(appendmpsitem(&count, Last_columnno, Last_column)) {
NZ++;
Column_ready = TRUE;
}
}
}
if(items == 6) {
if((row = find_row(lp, field5, Unconstrained_rows_found)) >= 0) {
if(row > lp->rows)
report(lp, CRITICAL, "Invalid row %s encountered in the MPS file\n", field5);
Last_columnno[count] = row;
Last_column[count] = (REAL)field6;
if(appendmpsitem(&count, Last_columnno, Last_column)) {
NZ++;
Column_ready = TRUE;
}
}
}
if((items < 4) || (items > 6)) { /* Wrong! */
report(lp, CRITICAL, "Wrong number of items (%d) in COLUMNS section (line %d)\n",
items, Lineno);
break;
}
continue;
/* Process entries in the RHS section */
/* field2: uninteresting name; field3: constraint name */
/* field4: value */
/* optional: field5: constraint name; field6: value */
case MPSRHS:
report(lp, FULL, "RHS line: %s %s %g %s %g\n",
field2, field3, field4, field5, field6);
if((items != 4) && (items != 6)) {
report(lp, CRITICAL, "Wrong number of items (%d) in RHS section line %d\n",
items, Lineno);
break;
}
if((row = find_row(lp, field3, Unconstrained_rows_found)) >= 0) {
if ((row == 0) && ((typeMPS & MPSNEGOBJCONST) == MPSNEGOBJCONST))
field4 = -field4;
set_rh(lp, row, (REAL)field4);
}
if(items == 6) {
if((row = find_row(lp, field5, Unconstrained_rows_found)) >= 0) {
if ((row == 0) && ((typeMPS & MPSNEGOBJCONST) == MPSNEGOBJCONST))
field6 = -field6;
set_rh(lp, row, (REAL)field6);
}
}
continue;
/* Process entries in the BOUNDS section */
/* field1: bound type; field2: uninteresting name; */
/* field3: variable name; field4: value */
case MPSBOUNDS:
report(lp, FULL, "BOUNDS line: %s %s %s %g\n",
field1, field2, field3, field4);
var = find_var(lp, field3, FALSE);
if(var < 0){ /* bound on undefined var in COLUMNS section ... */
Column_ready = TRUE;
if (!addmpscolumn(lp, FALSE, typeMPS, &Column_ready, &count, Last_column, Last_columnno, field3))
break;
Column_ready = TRUE;
var = find_var(lp, field3, TRUE);
}
if(var < 0) /* undefined var and could add ... */;
else if(strcmp(field1, "UP") == 0) {
/* upper bound */
/* if(!set_bounds(lp, var, get_lowbo(lp, var), field4)) */
if(!set_upbo(lp, var, field4))
break;
}
else if(strcmp(field1, "SC") == 0) {
/* upper bound */
if(field4 == 0)
field4 = lp->infinite;
/* if(!set_bounds(lp, var, get_lowbo(lp, var), field4)) */
if(!set_upbo(lp, var, field4))
break;
set_semicont(lp, var, TRUE);
}
else if(strcmp(field1, "SI") == 0) {
/* upper bound */
if(field4 == 0)
field4 = lp->infinite;
/* if(!set_bounds(lp, var, get_lowbo(lp, var), field4)) */
if(!set_upbo(lp, var, field4))
break;
set_int(lp, var, TRUE);
set_semicont(lp, var, TRUE);
}
else if(strcmp(field1, "LO") == 0) {
/* lower bound */
/* if(!set_bounds(lp, var, field4, get_upbo(lp, var))) */
if(!set_lowbo(lp, var, field4))
break;
}
else if(strcmp(field1, "PL") == 0) { /* plus-ranged variable */
/* if(!set_bounds(lp, var, get_lowbo(lp, var), lp->infinite)) */
if(!set_upbo(lp, var, lp->infinite))
break;
}
else if(strcmp(field1, "MI") == 0) { /* minus-ranged variable */
/* if(!set_bounds(lp, var, -lp->infinite, get_upbo(lp, var))) */
if(!set_lowbo(lp, var, -lp->infinite))
break;
}
else if(strcmp(field1, "FR") == 0) { /* free variable */
set_unbounded(lp, var);
}
else if(strcmp(field1, "FX") == 0) {
/* fixed, upper _and_ lower */
if(!set_bounds(lp, var, field4, field4))
break;
}
else if(strcmp(field1, "BV") == 0) { /* binary variable */
set_binary(lp, var, TRUE);
}
/* AMPL bounds type UI and LI added by E.Imamura (CRIEPI) */
else if(strcmp(field1, "UI") == 0) { /* upper bound for integer variable */
/* if(!set_bounds(lp, var, get_lowbo(lp, var), field4)) */
if(!set_upbo(lp, var, field4))
break;
set_int(lp, var, TRUE);
}
else if(strcmp(field1, "LI") == 0) { /* lower bound for integer variable - corrected by KE */
/* if(!set_bounds(lp, var, field4, get_upbo(lp, var))) */
if(!set_lowbo(lp, var, field4))
break;
set_int(lp, var, TRUE);
}
else {
report(lp, CRITICAL, "BOUND type %s on line %d is not supported",
field1, Lineno);
break;
}
continue;
/* Process entries in the BOUNDS section */
/* We have to implement the following semantics:
D. The RANGES section is for constraints of the form: h <=
constraint <= u . The range of the constraint is r = u - h . The
value of r is specified in the RANGES section, and the value of u or
h is specified in the RHS section. If b is the value entered in the
RHS section, and r is the value entered in the RANGES section, then
u and h are thus defined:
row type sign of r h u
----------------------------------------------
G + or - b b + |r|
L + or - b - |r| b
E + b b + |r|
E - b - |r| b */
/* field2: uninteresting name; field3: constraint name */
/* field4: value */
/* optional: field5: constraint name; field6: value */
case MPSRANGES:
report(lp, FULL, "RANGES line: %s %s %g %s %g",
field2, field3, field4, field5, field6);
if((items != 4) && (items != 6)) {
report(lp, CRITICAL, "Wrong number of items (%d) in RANGES section line %d",
items, Lineno);
break;
}
if((row = find_row(lp, field3, Unconstrained_rows_found)) >= 0) {
/* Determine constraint type */
if(fabs(field4) >= lp->infinite) {
report(lp, IMPORTANT,
"Warning, Range for row %s >= infinity (value %g) on line %d, ignored",
field3, field4, Lineno);
}
else if(field4 == 0) {
/* Change of a GE or LE to EQ */
if(lp->orig_upbo[row] != 0)
set_constr_type(lp, row, EQ);
}
else if(is_chsign(lp, row)) {
/* GE */
lp->orig_upbo[row] = fabs(field4);
}
else if((lp->orig_upbo[row] == 0) && (field4 >= 0)) {
/* EQ with positive sign of r value */
set_constr_type(lp, row, GE);
lp->orig_upbo[row] = field4;
}