-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobelisk-quadratic-solve.hpp
280 lines (205 loc) · 10.3 KB
/
obelisk-quadratic-solve.hpp
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
#ifndef _OBELISK_QUADRATIC_SOLVE_
#define _OBELISK_QUADRATIC_SOLVE_
#include <map>
#include <cfloat>
#include <tgmath.h>
#include <assert.h>
#include <coin/ClpSimplex.hpp>
#include <x86_64-linux-gnu/cblas.h>
#include "obelisk-compressed-rrmat.hpp"
#include "obelisk-quadratic-solve.hpp"
using namespace std;
int swap_pointers(double** x, double** y);
int get_rr_inv_pair(double** input_mat, double** inv, compressed_rrmat* lhs, int dim);
int print_nz(int size, double* array);
int column_collapse(compressed_rrmat* lhs, map <int, double*>::iterator lhs_col, double** inv, int inv_cols);
int active_set_alg(compressed_rrmat* lhs, int num_rows, double** inv, double* solution, double* kkt_rhs, int variables, int constraints);
int update_inv(compressed_rrmat* lhs, int* num_rows, double** inv, int inv_cols, int new_active);
class quadratic_programming_problem {
public:
pair<int, double>** problem_matrix;
int* constraint_cols;
double* rhs;
double* quadratic_coefficients;
double* linear_coefficients;
int variables;
int constraints;
int solve(double* this_solution) {
// Generate a linear programming problem to find an interior point for the quadratic
// problem solve. It will have a single additional variable t and constraints requiring
// all other problem and slack variables to be greater than t. Maximize t.
/* If uncommented, print the tech matrix for debugging instead.
double** print_matrix = new double*[this->constraints];
for (int i = 0; i < this->constraints; i++) {
print_matrix[i] = new double[this->variables];
for (int j = 0; j < this->variables; j++) {
print_matrix[i][j] = 0;
}
}
for (int i = 0; i < this->constraints; i++) {
for (int j = 0; j < this->constraint_cols[i]; j++) {
print_matrix[i][this->problem_matrix[i][j].first] = this->problem_matrix[i][j].second;
}
}
for (int i = 0; i < this->constraints; i++) {
for (int j = 0; j < this->variables; j++) {
cout << "\t" << print_matrix[i][j] << "\t";
}
cout << "\n";
}
return 0;
*/
double* init_solution_rhs_l = new double[this->constraints + this->variables];
memset(init_solution_rhs_l, 0, sizeof(double) * (this->constraints + this->variables) );
double* init_solution_rhs_u = new double[this->constraints+this->variables];
memset(init_solution_rhs_u, 0, sizeof(double) * (this->constraints + this->variables) );
double* init_solution_objs = new double[this->variables+1];
memset(init_solution_objs, 0, sizeof(double) * (this->variables + 1) );
CoinPackedMatrix init_solution_mat;
init_solution_mat.setDimensions(this->constraints+this->variables, this->variables+1);
for (int i = 0; i < this->constraints; i++) {
for (int j = 0; j < this->constraint_cols[i]; j++) {
init_solution_mat.modifyCoefficient(i,this->problem_matrix[i][j].first,this->problem_matrix[i][j].second);
}
}
for (int i = 0; i < this->variables; i++) {
init_solution_mat.modifyCoefficient(this->constraints+i,i,1);
init_solution_mat.modifyCoefficient(this->constraints+i,this->variables,-1);
}
for (int i = 0; i < this->constraints; i++) {
init_solution_rhs_l[i] = this->rhs[i];
init_solution_rhs_u[i] = this->rhs[i];
}
for (int i = this->constraints; i < this->constraints+this->variables; i++) {
init_solution_rhs_l[i] = 0;
init_solution_rhs_u[i] = INFINITY;
}
init_solution_objs[this->variables] = 1;
ClpSimplex init_solution_model;
init_solution_model.setOptimizationDirection(-1);
init_solution_model.loadProblem(init_solution_mat,NULL,
NULL,init_solution_objs,init_solution_rhs_l,init_solution_rhs_u);
init_solution_model.primal();
double* init_solution = init_solution_model.primalColumnSolution();
if (init_solution_model.isProvenPrimalInfeasible()) {
double* this_infeasibles = init_solution_model.infeasibilityRay(0);
double* zero_rhs = new double[this->variables+this->constraints];
memset(zero_rhs, 0, sizeof(double) * (this->constraints + this->variables) );
/*init_solution_mat.times(init_solution,zero_rhs);
cout << "\nInitial solution find reports your problem is infeasible. Now reporting infeasibilities variable.\n";
for (int j = 0; j < 1+variables; j++) {
if (this_infeasibles[j] > 1.0e-300 || this_infeasibles[j] < -1.0e-300) {
cout << j << ": " << this_infeasibles[j] << "\n";
cout << "Corresponding RHS: " << init_solution_rhs_l[j] << "\n";
}
}
cout << "\nRHS of zero solution.\n";
for (int j = 0; j < constraints+variables; j++) {
if (1) {
cout << j << ": " << zero_rhs[j] << "\n";
cout << "Corresponding RHS: " << init_solution_rhs_u[j] << "\n";
cout << "Status: " << init_solution_model.getRowStatus(j) << "\n";
}
}
*/
return 1;
}
if (init_solution_model.isProvenDualInfeasible()) {
cout << "\nInitial solution find reports your problem is unbounded. Let's find out how.\n";
double* feasible_solution_rhs_l = new double[this->constraints];
memset(feasible_solution_rhs_l, 0, sizeof(double) * (this->constraints) );
double* feasible_solution_rhs_u = new double[this->constraints];
memset(feasible_solution_rhs_u, 0, sizeof(double) * (this->constraints) );
double* feasible_solution_objs = new double[this->variables];
for (int i = 0; i < this->variables; i++) {
feasible_solution_objs[i] = 1;
}
CoinPackedMatrix feasible_solution_mat;
feasible_solution_mat.setDimensions(this->constraints, this->variables);
for (int i = 0; i < this->constraints; i++) {
for (int j = 0; j < this->constraint_cols[i]; j++) {
feasible_solution_mat.modifyCoefficient(i,this->problem_matrix[i][j].first,this->problem_matrix[i][j].second);
}
}
for (int i = 0; i < constraints; i++) {
feasible_solution_rhs_l[i] = this->rhs[i];
feasible_solution_rhs_u[i] = this->rhs[i];
}
ClpSimplex feasible_solution_model;
feasible_solution_model.setOptimizationDirection(-1);
feasible_solution_model.loadProblem(feasible_solution_mat,NULL,
NULL,feasible_solution_objs,feasible_solution_rhs_l,feasible_solution_rhs_u);
feasible_solution_model.primal();
if (!feasible_solution_model.isProvenDualInfeasible()) {
cout << "\nIt looks bounded or infeasible now. This is a glitch.\n";
return 1;
}
double* this_infeasibles = feasible_solution_model.unboundedRay();
cout << "\nNow reporting unbounded ray by variable.\n";
for (int j = 0; j < constraints+variables; j++) {
if (this_infeasibles[j] > 1.0e-300 || this_infeasibles[j] < -1.0e-300) {
cout << j << ": " << this_infeasibles[j] << "\n";
//cout << "Corresponding RHS: " << feasible_solution_rhs_l[j] << "\n";
}
}
return 1;
}
// for (int i = 0; i < this->variables+this->constraints; i++) cout << " " << rhs[i];
// Create the initial matrix pair, namely the KKT matrix and a diagonal matrix called inv.
// Allocating space enough for every possible future pointer.
double** inv = new double*[this->constraints+2*this->variables];
double** kkt = new double*[this->constraints+this->variables];
for (int i = 0; i < this->constraints+this->variables; i++) {
kkt[i] = new double[this->constraints+this->variables];
inv[i] = new double[this->constraints+this->variables];
memset(inv[i], 0, sizeof(double) * (this->constraints+this->variables) );
memset(kkt[i], 0, sizeof(double) * (this->constraints+this->variables) );
inv[i][i] = 1.0;
}
// Fill initial KKT matrix.
for (int i = 0; i < variables; i++) {
if (quadratic_coefficients[i] != 0) {
kkt[i][i] = -2*quadratic_coefficients[i];
}
}
for (int i = 0; i < this->constraints; i++) {
for (int j = 0; j < this->constraint_cols[i]; j++) {
kkt[this->problem_matrix[i][j].first][i + this->variables] = -this->problem_matrix[i][j].second;
kkt[i + this->variables][this->problem_matrix[i][j].first] = this->problem_matrix[i][j].second;
}
}
/* Now we find the "inverse" of the KKT matrix. This allows us to find a solution to the
initial KKT system *and* to find the solution to the next (one constraint larger) KKT system
with only linear additional row operations. The whole algorithm should require only O(n^3)
scalar operations as a result.
I put "inverse" in quotes because kkt may be singular, in which a case it will
only be row reduced echlon at the end of elimination rather than the identity. This still
allows us to get a solution with matrix-vector multiplication, as long as a solution exists.
*/
compressed_rrmat* kkt_rrmat = new compressed_rrmat;
get_rr_inv_pair(kkt, inv, kkt_rrmat, this->constraints+this->variables);
free(kkt);
// Create the right hand side of the KKT system.
double* constant_kkt_rhs = new double[this->constraints+this->variables];
memset(constant_kkt_rhs, 0, sizeof(double) * (this->constraints + this->variables) );
for (int i = 0; i < this->variables; i++) {
if (quadratic_coefficients[i] != 0) {
constant_kkt_rhs[i] = linear_coefficients[i];
} else {
constant_kkt_rhs[i] = 0;
}
}
for (int i = this->variables; i < this->variables+this->constraints; i++) {
constant_kkt_rhs[i] = rhs[i-this->variables];
}
// Perform the active set algorithm.
for (int i = 0; i < variables; i++) {
this_solution[i] = init_solution[i];
}
active_set_alg(kkt_rrmat, this->constraints+this->variables, inv, this_solution,
constant_kkt_rhs, this->variables, this->constraints);
free(inv);
return 0;
};
};
#endif