aboutsummaryrefslogtreecommitdiffstats
path: root/native/src/linprog/glpk.cpp
blob: 8253579f5d5a65fbee47992f52b1c6caf46ee50c (plain) (blame)
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
#include <assert.h>
#include <glpk.h>

#include <iostream>

#include "cpu_time.h"

#include "linprog/glpk.h"

class GLPKSolution : public Solution
{
private:
	glp_prob *glpk;
	const LinearProgram &linprog;
	const unsigned int num_cols;
	const unsigned int num_rows;
	unsigned int num_coeffs;

	bool solved;

	void solve(double var_lb, double var_ub);
	void set_objective();
	void set_bounds(double col_lb, double col_ub);
	void set_coefficients();
public:

	GLPKSolution(const LinearProgram &lp, unsigned int max_num_vars,
		     double var_lb = 0.0, double var_ub = 1.0);

	~GLPKSolution();

	double get_value(unsigned int var) const
	{
		return glp_get_col_prim(glpk, var + 1);
	}

	bool is_solved() const
	{
		return solved;
	}
};

GLPKSolution::GLPKSolution(const LinearProgram& lp, unsigned int max_num_vars,
			   double var_lb, double var_ub)
	: glpk(glp_create_prob()),
	  linprog(lp),
	  num_cols(max_num_vars),
	  num_rows(lp.get_equalities().size() +
		   lp.get_inequalities().size()),
	  num_coeffs(0),
	  solved(false)
{
	if (num_cols)
		solve(var_lb, var_ub);
	else
		// Trivial case: no variables.
		// This can happen if a task set does not
		// contain any shared resources.
		solved = true;
}

void GLPKSolution::solve(double var_lb, double var_ub)
{
#if DEBUG_LP_OVERHEADS >= 3
	static DEFINE_CPU_CLOCK(init_costs);
	static DEFINE_CPU_CLOCK(model_costs);
	static DEFINE_CPU_CLOCK(solver_costs);

	init_costs.start();
#endif
	glp_term_out(GLP_OFF);
	glp_set_obj_dir(glpk, GLP_MAX);
	glp_add_cols(glpk, num_cols);
	glp_add_rows(glpk, num_rows);

#if DEBUG_LP_OVERHEADS >= 3
	init_costs.stop();
	model_costs.start();
#endif

	set_objective();
	set_bounds(var_lb, var_ub);
	set_coefficients();

#if DEBUG_LP_OVERHEADS >= 3
	model_costs.stop();
	solver_costs.start();
#endif

	glp_smcp glpk_params;

	glp_init_smcp(&glpk_params);

	/* Set solver options. The presolver is essential. The other two
	 * options seem to make the solver slightly faster.
	 *
	 * Tested with GLPK 4.43 on wks-50-12.
	 */
	glpk_params.presolve = GLP_ON;
	glpk_params.pricing  = GLP_PT_STD;
	glpk_params.r_test   = GLP_RT_STD;

	solved = glp_simplex(glpk, &glpk_params) == 0 &&
		glp_get_status(glpk) == GLP_OPT;

#if DEBUG_LP_OVERHEADS >= 3
	solver_costs.stop();

	std::cout << init_costs << std::endl
		  << model_costs << std::endl
		  << solver_costs << std::endl;
#endif
}

GLPKSolution::~GLPKSolution()
{
	glp_delete_prob(glpk);
}

void GLPKSolution::set_objective()
{
	assert(linprog.get_objective()->get_terms().size() <= num_cols);

	foreach (linprog.get_objective()->get_terms(), term)
		glp_set_obj_coef(glpk, term->second + 1, term->first);
}


void GLPKSolution::set_bounds(double col_lb, double col_ub)
{
	unsigned int r = 1;

	foreach(linprog.get_equalities(), equ)
	{
		glp_set_row_bnds(glpk, r++, GLP_FX,
				 equ->second, equ->second);

	        num_coeffs += equ->first->get_terms().size();
	}

	foreach(linprog.get_inequalities(), inequ)
	{
		glp_set_row_bnds(glpk, r++, GLP_UP,
				 0, inequ->second);

		num_coeffs += inequ->first->get_terms().size();
	}

	for (unsigned int c = 1; c <= num_cols; c++)
		glp_set_col_bnds(glpk, c, GLP_DB, col_lb, col_ub);
}

void GLPKSolution::set_coefficients()
{
	int *row_idx, *col_idx;
	double *coeff;

	row_idx = new int[1 + num_coeffs];
	col_idx = new int[1 + num_coeffs];
	coeff   = new double[1 + num_coeffs];

	unsigned int r = 1, k = 1;

	foreach(linprog.get_equalities(), equ)
	{
		foreach(equ->first->get_terms(), term)
		{
			assert(k <= num_coeffs);

			row_idx[k] = r;
			col_idx[k] = 1 + term->second;
			coeff[k]   = term->first;

			k += 1;
		}
		r += 1;
	}


	foreach(linprog.get_inequalities(), inequ)
	{
		foreach(inequ->first->get_terms(), term)
		{
			assert(k <= num_coeffs);

			row_idx[k] = r;
			col_idx[k] = 1 + term->second;
			coeff[k]   = term->first;

			k += 1;
		}
		r += 1;
	}

	glp_load_matrix(glpk, num_coeffs, row_idx, col_idx, coeff);

	delete[] row_idx;
	delete[] col_idx;
	delete[] coeff;
}


Solution *glpk_solve(const LinearProgram& lp, unsigned int max_num_vars)
{
	GLPKSolution *sol =  new GLPKSolution(lp, max_num_vars);
	if (sol->is_solved())
		return sol;
	else
	{
		delete sol;
		return NULL;
	}
}