4 * Copyright: (c) Universitaet Karlsruhe
5 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
14 * These must comply to the enum cst_t in lpp.h
16 static const char *mps_cst_encoding[4] = {"N", "E", "L", "G"};
19 * Diffferent line styles which can be used in a mps file
21 typedef enum _mps_line_t {l_raw,
22 l_ind_name, l_ind_objs, l_ind_rows, l_ind_cols, l_ind_rhs, l_ind_end,
23 l_data_row, l_data_col1, l_data_col2, l_data_mst, l_marker} mps_line_t;
25 static void mps_write_line(FILE *out, style_t style, mps_line_t line_type, ...) {
29 assert(style == s_mps_fixed || style == s_mps_free);
30 va_start(args, line_type);
32 if (style == s_mps_fixed) {
33 /* white spaces are important! */
35 case l_raw: fmt = "%s\n"; break;
36 case l_ind_name: fmt = "NAME %s\n"; break;
37 case l_ind_objs: fmt = "OBJSENSE\n"; break;
38 case l_ind_rows: fmt = "ROWS\n"; break;
39 case l_ind_cols: fmt = "COLUMNS\n"; break;
40 case l_ind_rhs: fmt = "RHS\n"; break;
41 case l_ind_end: fmt = "ENDATA\n"; break;
42 case l_data_row: fmt = " %-2s %-8s\n"; break; /* Field 1-2 */
43 case l_data_col1: fmt = " %-8s %-8s %12g\n"; break; /* Field 2-4 */
44 case l_data_col2: fmt = " %-8s %-8s %12g %-8s %12g\n"; break; /* Field 2-6 */
45 case l_data_mst: fmt = " %-8s %12g\n"; break; /* Field 3-4 */
46 case l_marker: fmt = " M%-7d 'MARKER' '%s'\n"; break; /* Field 2,3,5 */
51 case l_raw: fmt = "%s\n"; break;
52 case l_ind_name: fmt = "NAME %s\n"; break;
53 case l_ind_objs: fmt = "OBJSENSE\n"; break;
54 case l_ind_rows: fmt = "ROWS\n"; break;
55 case l_ind_cols: fmt = "COLUMNS\n"; break;
56 case l_ind_rhs: fmt = "RHS\n"; break;
57 case l_ind_end: fmt = "ENDATA\n"; break;
58 case l_data_row: fmt = " %s\t%s\n"; break;
59 case l_data_col1: fmt = " %s\t%s\t%g\n"; break;
60 case l_data_col2: fmt = " %s\t%s\t%g\t%s\t%g\n"; break;
61 case l_data_mst: fmt = " %s\t%g\n"; break;
62 case l_marker: fmt = " M%d\t'MARKER'\t'%s'\n"; break;
67 vfprintf(out, fmt, args);
71 static int mps_insert_markers(FILE *out, style_t style, lpp_var_t curr, lpp_var_t last, int marker_nr) {
72 assert(style == s_mps_fixed || style == s_mps_free);
74 /* print end-marker for last */
75 if (last == lpp_binary)
76 mps_write_line(out, style, l_marker, marker_nr++, "INTEND");
78 /* print begin-marker for curr */
79 if (curr == lpp_binary)
80 mps_write_line(out, style, l_marker, marker_nr++, "INTORG");
85 void mps_write_mps(lpp_t *lpp, style_t style, FILE *out) {
86 int i, count, marker_nr = 0;
87 const lpp_name_t *curr;
88 const matrix_elem_t *elem, *before = NULL;
90 assert(style == s_mps_fixed || style == s_mps_free);
93 mps_write_line(out, style, l_ind_name, lpp->name);
96 if (lpp->opt_type == lpp_maximize) {
97 mps_write_line(out, style, l_ind_objs);
98 mps_write_line(out, style, l_raw, " MAX");
102 mps_write_line(out, style, l_ind_rows);
103 for(i=0; i<lpp->cst_next; ++i) {
105 mps_write_line(out, style, l_data_row, mps_cst_encoding[curr->type.cst_type], curr->name);
109 mps_write_line(out, style, l_ind_cols);
110 last_type = lpp_invalid;
111 for(i=1; i<lpp->var_next; ++i) { /* column 0 is rhs */
115 marker_nr = mps_insert_markers(out, style, curr->type.var_type, last_type, marker_nr);
116 last_type = curr->type.var_type;
118 /* participation in constraints */
120 matrix_foreach_in_col(lpp->m, curr->nr, elem) {
125 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);
130 mps_write_line(out, style, l_data_col1, curr->name, lpp->csts[before->row]->name, (double)before->val);
132 mps_insert_markers(out, style, lpp_invalid, last_type, marker_nr); /* potential end-marker */
135 mps_write_line(out, style, l_ind_rhs);
137 matrix_foreach_in_col(lpp->m, 0, elem) {
142 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);
147 mps_write_line(out, style, l_data_col1, "rhs", lpp->csts[before->row]->name, (double)before->val);
150 mps_write_line(out, style, l_ind_end);
153 void mps_write_mst(lpp_t *lpp, style_t style, FILE *out) {
155 mps_write_line(out, style, l_ind_name, "");
156 for (i=0; i<lpp->var_next; ++i) {
157 const lpp_name_t *var = lpp->vars[i];
158 if (var->value_kind == lpp_value_start)
159 mps_write_line(out, style, l_data_mst, var->name, (double)var->value);
161 mps_write_line(out, style, l_ind_end);