summaryrefslogtreecommitdiffhomepage
path: root/ir/lpp/mps.c
blob: 3a31862d290918d74ef8482ec2c9c28ea285ff06 (plain)
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
/*
 * This file is part of libFirm.
 * Copyright (C) 2012 Universitaet Karlsruhe
 */

/**
 * @file
 * @author Daniel Grund
 */
#include "mps.h"

#include "panic.h"
#include <assert.h>
#include <stdarg.h>

/**
 * These must comply to the enum cst_t in lpp.h
 */
static const char *mps_cst_encoding[4] = {"N", "E", "L", "G"};

/**
 * Diffferent line styles which can be used in a mps file
 */
typedef enum {
	l_raw, l_ind_name, l_ind_objs, l_ind_rows, l_ind_cols, l_ind_rhs, l_ind_end,
	l_data_row, l_data_col1, l_data_col2, l_data_mst, l_marker
} mps_line_t;

static void mps_write_line(FILE *out, lpp_mps_style_t style,
                           mps_line_t line_type, ...)
{
	va_list args;
	const char *fmt = "";

	assert(style == s_mps_fixed || style == s_mps_free);
	va_start(args, line_type);

	if (style == s_mps_fixed) {
		/* white spaces are important! */
		switch (line_type) {
			case l_raw:       fmt = "%s\n"; break;
			case l_ind_name:  fmt = "NAME          %s\n"; break;
			case l_ind_objs:  fmt = "OBJSENSE\n"; break;
			case l_ind_rows:  fmt = "ROWS\n"; break;
			case l_ind_cols:  fmt = "COLUMNS\n"; break;
			case l_ind_rhs:   fmt = "RHS\n"; break;
			case l_ind_end:   fmt = "ENDATA\n"; break;
			case l_data_row:  fmt = " %-2s %-8s\n"; break; /* Field 1-2 */
			case l_data_col1: fmt = "    %-8s  %-8s  %12g\n"; break; /* Field 2-4 */
			case l_data_col2: fmt = "    %-8s  %-8s  %12g   %-8s  %12g\n"; break; /* Field 2-6 */
			case l_data_mst:  fmt = "    %-8s            %12g\n"; break; /* Field 3-4 */
			case l_marker:    fmt = "    M%-7d  'MARKER'                 '%s'\n"; break; /* Field 2,3,5 */
			default: panic("invalid line type");
		}
	} else {
		switch (line_type) {
			case l_raw:       fmt = "%s\n"; break;
			case l_ind_name:  fmt = "NAME %s\n"; break;
			case l_ind_objs:  fmt = "OBJSENSE\n"; break;
			case l_ind_rows:  fmt = "ROWS\n"; break;
			case l_ind_cols:  fmt = "COLUMNS\n"; break;
			case l_ind_rhs:   fmt = "RHS\n"; break;
			case l_ind_end:   fmt = "ENDATA\n"; break;
			case l_data_row:  fmt = " %s\t%s\n"; break;
			case l_data_col1: fmt = " %s\t%s\t%g\n"; break;
			case l_data_col2: fmt = " %s\t%s\t%g\t%s\t%g\n"; break;
			case l_data_mst:  fmt = " %s\t%g\n"; break;
			case l_marker:    fmt = " M%d\t'MARKER'\t'%s'\n"; break;
			default: panic("invalid line type");
		}
	}

	vfprintf(out, fmt, args);
	va_end(args);
}

static int mps_insert_markers(FILE *out, lpp_mps_style_t style, lpp_var_t curr,
                              lpp_var_t last, int marker_nr)
{
	assert(style == s_mps_fixed || style == s_mps_free);
	if (last != curr) {
		/* print end-marker for last */
		if (last == lpp_binary)
			mps_write_line(out, style, l_marker, marker_nr++, "INTEND");

		/* print begin-marker for curr */
		if (curr == lpp_binary)
			mps_write_line(out, style, l_marker, marker_nr++, "INTORG");
	}
	return marker_nr;
}

void mps_write_mps(lpp_t *lpp, lpp_mps_style_t style, FILE *out)
{
	int i, count, marker_nr = 0;
	const lpp_name_t *curr;
	const matrix_elem_t *before = NULL;
	lpp_var_t last_type;
	assert(style == s_mps_fixed || style == s_mps_free);

	/* NAME */
	mps_write_line(out, style, l_ind_name, lpp->name);

	/* OBJSENSE */
	if (lpp->opt_type == lpp_maximize) {
		mps_write_line(out, style, l_ind_objs);
		mps_write_line(out, style, l_raw, " MAX");
	}

	/* ROWS */
	mps_write_line(out, style, l_ind_rows);
	for(i=0; i<lpp->cst_next; ++i) {
		curr = lpp->csts[i];
		mps_write_line(out, style, l_data_row, mps_cst_encoding[curr->type.cst_type], curr->name);
	}

	/* COLUMNS */
	mps_write_line(out, style, l_ind_cols);
	last_type = lpp_invalid;
	for(i=1; i<lpp->var_next; ++i) { /* column 0 is rhs */
		curr = lpp->vars[i];

		/* markers */
		marker_nr = mps_insert_markers(out, style, curr->type.var_type, last_type, marker_nr);
		last_type = curr->type.var_type;

		/* participation in constraints */
		count = 0;
		matrix_foreach_in_col(lpp->m, curr->nr, elem) {
			if (count == 0) {
				before = elem;
				count = 1;
			} else {
				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);
				count = 0;
			}
		}
		if (count == 1)
			mps_write_line(out, style, l_data_col1, curr->name, lpp->csts[before->row]->name, (double)before->val);
	}
	mps_insert_markers(out, style, lpp_invalid, last_type, marker_nr); /* potential end-marker */

	/* RHS */
	mps_write_line(out, style, l_ind_rhs);
	count = 0;
	matrix_foreach_in_col(lpp->m, 0, elem) {
		if (count == 0) {
			before = elem;
			count = 1;
		} else {
			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);
			count = 0;
		}
	}
	if (count == 1)
		mps_write_line(out, style, l_data_col1, "rhs", lpp->csts[before->row]->name, (double)before->val);

	/* ENDATA */
	mps_write_line(out, style, l_ind_end);
}

void mps_write_mst(lpp_t *lpp, lpp_mps_style_t style, FILE *out)
{
	int i;
	mps_write_line(out, style, l_ind_name, "");
	for (i=0; i<lpp->var_next; ++i) {
		const lpp_name_t *var = lpp->vars[i];
		if (var->value_kind == lpp_value_start)
			mps_write_line(out, style, l_data_mst, var->name, (double)var->value);
	}
	mps_write_line(out, style, l_ind_end);
}