e88659404dd3a1f9144880dffdffec2987431904
[libfirm] / ir / lpp / mps.c
1 /**
2  * Author:      Daniel Grund
3  * Date:        02.06.2005
4  * Copyright:   (c) Universitaet Karlsruhe
5  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
6  */
7 #include "config.h"
8 #include <stdarg.h>
9 #include <assert.h>
10 #include "mps.h"
11
12 /**
13  * These must comply to the enum cst_t in lpp.h
14  */
15 static const char *mps_cst_encoding[4] = {"N", "E", "L", "G"};
16
17 /**
18  * Diffferent line styles which can be used in a mps file
19  */
20 typedef enum _mps_line_t {l_raw,
21                                                   l_ind_name, l_ind_objs, l_ind_rows, l_ind_cols, l_ind_rhs, l_ind_end,
22                                                   l_data_row, l_data_col1, l_data_col2, l_data_mst, l_marker} mps_line_t;
23
24 static void mps_write_line(FILE *out, style_t style, mps_line_t line_type, ...) {
25         va_list args;
26         const char *fmt = "";
27
28         assert(style == s_mps_fixed || style == s_mps_free);
29         va_start(args, line_type);
30
31         if (style == s_mps_fixed) {
32                 /* white spaces are important! */
33                 switch (line_type) {
34                         case l_raw:       fmt = "%s\n"; break;
35                         case l_ind_name:  fmt = "NAME          %s\n"; break;
36                         case l_ind_objs:  fmt = "OBJSENSE\n"; break;
37                         case l_ind_rows:  fmt = "ROWS\n"; break;
38                         case l_ind_cols:  fmt = "COLUMNS\n"; break;
39                         case l_ind_rhs:   fmt = "RHS\n"; break;
40                         case l_ind_end:   fmt = "ENDATA\n"; break;
41                         case l_data_row:  fmt = " %-2s %-8s\n"; break; /* Field 1-2 */
42                         case l_data_col1: fmt = "    %-8s  %-8s  %12g\n"; break; /* Field 2-4 */
43                         case l_data_col2: fmt = "    %-8s  %-8s  %12g   %-8s  %12g\n"; break; /* Field 2-6 */
44                         case l_data_mst:  fmt = "    %-8s            %12g\n"; break; /* Field 3-4 */
45                         case l_marker:    fmt = "    M%-7d  'MARKER'                 '%s'\n"; break; /* Field 2,3,5 */
46                         default: assert(0);
47                 }
48         } else {
49                 switch (line_type) {
50                         case l_raw:       fmt = "%s\n"; break;
51                         case l_ind_name:  fmt = "NAME %s\n"; break;
52                         case l_ind_objs:  fmt = "OBJSENSE\n"; break;
53                         case l_ind_rows:  fmt = "ROWS\n"; break;
54                         case l_ind_cols:  fmt = "COLUMNS\n"; break;
55                         case l_ind_rhs:   fmt = "RHS\n"; break;
56                         case l_ind_end:   fmt = "ENDATA\n"; break;
57                         case l_data_row:  fmt = " %s\t%s\n"; break;
58                         case l_data_col1: fmt = " %s\t%s\t%g\n"; break;
59                         case l_data_col2: fmt = " %s\t%s\t%g\t%s\t%g\n"; break;
60                         case l_data_mst:  fmt = " %s\t%g\n"; break;
61                         case l_marker:    fmt = " M%d\t'MARKER'\t'%s'\n"; break;
62                         default: assert(0);
63                 }
64         }
65
66         vfprintf(out, fmt, args);
67         va_end(args);
68 }
69
70 static int mps_insert_markers(FILE *out, style_t style, lpp_var_t curr, lpp_var_t last, int marker_nr)
71 {
72         assert(style == s_mps_fixed || style == s_mps_free);
73         if (last != curr) {
74                 /* print end-marker for last */
75                 if (last == lpp_binary)
76                         mps_write_line(out, style, l_marker, marker_nr++, "INTEND");
77
78                 /* print begin-marker for curr */
79                 if (curr == lpp_binary)
80                         mps_write_line(out, style, l_marker, marker_nr++, "INTORG");
81         }
82         return marker_nr;
83 }
84
85 void mps_write_mps(lpp_t *lpp, style_t style, FILE *out)
86 {
87         int i, count, marker_nr = 0;
88         const lpp_name_t *curr;
89         const matrix_elem_t *elem, *before = NULL;
90         lpp_var_t last_type;
91         assert(style == s_mps_fixed || style == s_mps_free);
92
93         /* NAME */
94         mps_write_line(out, style, l_ind_name, lpp->name);
95
96         /* OBJSENSE */
97         if (lpp->opt_type == lpp_maximize) {
98                 mps_write_line(out, style, l_ind_objs);
99                 mps_write_line(out, style, l_raw, " MAX");
100         }
101
102         /* ROWS */
103         mps_write_line(out, style, l_ind_rows);
104         for(i=0; i<lpp->cst_next; ++i) {
105                 curr = lpp->csts[i];
106                 mps_write_line(out, style, l_data_row, mps_cst_encoding[curr->type.cst_type], curr->name);
107         }
108
109         /* COLUMNS */
110         mps_write_line(out, style, l_ind_cols);
111         last_type = lpp_invalid;
112         for(i=1; i<lpp->var_next; ++i) { /* column 0 is rhs */
113                 curr = lpp->vars[i];
114
115                 /* markers */
116                 marker_nr = mps_insert_markers(out, style, curr->type.var_type, last_type, marker_nr);
117                 last_type = curr->type.var_type;
118
119                 /* participation in constraints */
120                 count = 0;
121                 matrix_foreach_in_col(lpp->m, curr->nr, elem) {
122                         if (count == 0) {
123                                 before = elem;
124                                 count = 1;
125                         } else {
126                                 mps_write_line(out, style, l_data_col2, curr->name, lpp->csts[before->row]->name, (double)before->val, lpp->csts[elem->row]->name, (double)elem->val);
127                                 count = 0;
128                         }
129                 }
130                 if (count == 1)
131                         mps_write_line(out, style, l_data_col1, curr->name, lpp->csts[before->row]->name, (double)before->val);
132         }
133         mps_insert_markers(out, style, lpp_invalid, last_type, marker_nr); /* potential end-marker */
134
135         /* RHS */
136         mps_write_line(out, style, l_ind_rhs);
137         count = 0;
138         matrix_foreach_in_col(lpp->m, 0, elem) {
139                 if (count == 0) {
140                         before = elem;
141                         count = 1;
142                 } else {
143                         mps_write_line(out, style, l_data_col2, "rhs", lpp->csts[before->row]->name, (double)before->val, lpp->csts[elem->row]->name, (double)elem->val);
144                         count = 0;
145                 }
146         }
147         if (count == 1)
148                 mps_write_line(out, style, l_data_col1, "rhs", lpp->csts[before->row]->name, (double)before->val);
149
150         /* ENDATA */
151         mps_write_line(out, style, l_ind_end);
152 }
153
154 void mps_write_mst(lpp_t *lpp, style_t style, FILE *out)
155 {
156         int i;
157         mps_write_line(out, style, l_ind_name, "");
158         for (i=0; i<lpp->var_next; ++i) {
159                 const lpp_name_t *var = lpp->vars[i];
160                 if (var->value_kind == lpp_value_start)
161                         mps_write_line(out, style, l_data_mst, var->name, (double)var->value);
162         }
163         mps_write_line(out, style, l_ind_end);
164 }