| 1 |
/** |
| 2 |
* @brief Convex Quadratic Programming library |
| 3 |
* |
| 4 |
* From: |
| 5 |
* http://ra.uni-trier.de/~huebner/software.html |
| 6 |
* |
| 7 |
* ---------------------------------------------------------------------- |
| 8 |
* This program is free software; you can redistribute it and/or modify |
| 9 |
* it under the terms of the GNU General Public License as published by |
| 10 |
* the Free Software Foundation; either version 2 of the License, or |
| 11 |
* (at your option) any later version. |
| 12 |
* |
| 13 |
* This program is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
* GNU General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU General Public License |
| 19 |
* along with this program; if not, write to the Free Software |
| 20 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 |
* ---------------------------------------------------------------------- |
| 22 |
* |
| 23 |
* @author Ewgenij Hübner |
| 24 |
* |
| 25 |
* $$ |
| 26 |
*/ |
| 27 |
|
| 28 |
#include <gsl/gsl_blas.h> |
| 29 |
#include <gsl/gsl_linalg.h> |
| 30 |
#include <gsl/gsl_math.h> |
| 31 |
#include <gsl/gsl_matrix.h> |
| 32 |
#include <gsl/gsl_permutation.h> |
| 33 |
#include <gsl/gsl_vector.h> |
| 34 |
|
| 35 |
#include "gsl_cqp.h" |
| 36 |
#include "initial_point.h" |
| 37 |
|
| 38 |
/* |
| 39 |
static void |
| 40 |
print_vectors(const gsl_vector*,const gsl_vector*,const gsl_vector*,const |
| 41 |
gsl_vector*); |
| 42 |
*/ |
| 43 |
|
| 44 |
static double compute_gap_infeasible_points(const gsl_cqp_data *cqp, |
| 45 |
const gsl_vector *x, |
| 46 |
const gsl_vector *y, |
| 47 |
const gsl_vector *z); |
| 48 |
|
| 49 |
static int compute_residuals(const gsl_cqp_data *cqp, const gsl_vector *x, |
| 50 |
const gsl_vector *y, const gsl_vector *z, |
| 51 |
const gsl_vector *s, gsl_vector *r); |
| 52 |
|
| 53 |
static int build_kkt_matrix(const gsl_cqp_data *cqp, const gsl_vector *z, |
| 54 |
const gsl_vector *s, gsl_matrix *kkt_matrix); |
| 55 |
|
| 56 |
double step_length(const gsl_vector *s, const gsl_vector *z, |
| 57 |
const gsl_vector *delta_s, const gsl_vector *delta_z); |
| 58 |
|
| 59 |
double gsl_vector_max_norm(const gsl_vector *v); |
| 60 |
|
| 61 |
double gsl_matrix_max_norm(const gsl_matrix *M); |
| 62 |
|
| 63 |
typedef struct { |
| 64 |
size_t n; /* dimension of the problem*/ |
| 65 |
size_t me; /* number of the equality constraints*/ |
| 66 |
size_t mi; /* number of the inequality constraints */ |
| 67 |
gsl_vector *s; /* Slack vector for the constrain: Cx+s = d */ |
| 68 |
|
| 69 |
gsl_matrix *kkt_matrix; |
| 70 |
gsl_vector *r; /* the vector of the right-hand side in the kkt-system: |
| 71 |
r=(r_Q,r_A,r_C) */ |
| 72 |
|
| 73 |
double tau; /* a constant for the Mehrotra's heuristic */ |
| 74 |
|
| 75 |
/* gondzio's parameters */ |
| 76 |
size_t k_max; /*maximal number of corrections */ |
| 77 |
double beta_min, |
| 78 |
beta_max; /* relatibe threhold values for outlier compl. products */ |
| 79 |
double delta_alpha; /* the required increas of stepsize */ |
| 80 |
double gamma; /* the minimum acceptable increase of stepsize */ |
| 81 |
|
| 82 |
double data_norm; /* norm of the problem data */ |
| 83 |
|
| 84 |
} mg_pdip_state; |
| 85 |
|
| 86 |
static int mg_pdip_alloc(void *vstate, size_t n, size_t me, size_t mi) { |
| 87 |
mg_pdip_state *state = (mg_pdip_state *)vstate; |
| 88 |
|
| 89 |
state->s = gsl_vector_alloc(mi); |
| 90 |
if (state->s == 0) { |
| 91 |
GSL_ERROR_VAL("failed to initialize space for the slack vector s", |
| 92 |
GSL_ENOMEM, 0); |
| 93 |
} |
| 94 |
|
| 95 |
state->kkt_matrix = gsl_matrix_calloc(n + me + mi, n + me + mi); |
| 96 |
if (state->kkt_matrix == 0) { |
| 97 |
gsl_vector_free(state->s); |
| 98 |
GSL_ERROR_VAL("failed to initialize space for the KKT-Matrix", |
| 99 |
GSL_ENOMEM, 0); |
| 100 |
} |
| 101 |
|
| 102 |
state->r = gsl_vector_alloc(state->kkt_matrix->size1); |
| 103 |
if (state->r == 0) { |
| 104 |
gsl_matrix_free(state->kkt_matrix); |
| 105 |
gsl_vector_free(state->s); |
| 106 |
GSL_ERROR_VAL( |
| 107 |
"failed to initialize space for right-hand side of the KKT-System", |
| 108 |
GSL_ENOMEM, 0); |
| 109 |
} |
| 110 |
|
| 111 |
return GSL_SUCCESS; |
| 112 |
} |
| 113 |
|
| 114 |
static int mg_pdip_set(void *vstate, const gsl_cqp_data *cqp, gsl_vector *x, |
| 115 |
gsl_vector *y, gsl_vector *z, double *gap, |
| 116 |
double *residuals_norm, double *data_norm, |
| 117 |
double *infeasibility, double *infeasibility_min) { |
| 118 |
int status = GSL_SUCCESS; |
| 119 |
// size_t i, j = 0; //, debug=0; |
| 120 |
|
| 121 |
mg_pdip_state *state = (mg_pdip_state *)vstate; |
| 122 |
|
| 123 |
/* Initial points */ |
| 124 |
if (cqp->A->size1 != 0) |
| 125 |
status = pdip_initial_point_feasible_x(cqp->A, cqp->b, x); |
| 126 |
else |
| 127 |
gsl_vector_set_zero(x); |
| 128 |
|
| 129 |
if (status != GSL_SUCCESS) { |
| 130 |
return status; |
| 131 |
} |
| 132 |
|
| 133 |
status = pdip_initial_point_feasible_s(cqp->C, cqp->d, x, state->s); |
| 134 |
if (status != GSL_SUCCESS) { |
| 135 |
return status; |
| 136 |
} |
| 137 |
|
| 138 |
if (cqp->A->size1 != 0) { |
| 139 |
status = pdip_initial_point_y(cqp->Q, cqp->q, cqp->A, x, y); |
| 140 |
} |
| 141 |
|
| 142 |
status = pdip_initial_point_z(z); |
| 143 |
if (status != GSL_SUCCESS) { |
| 144 |
return status; |
| 145 |
} |
| 146 |
|
| 147 |
status = pdip_initial_point_strict_feasible(z, state->s); |
| 148 |
if (status != GSL_SUCCESS) { |
| 149 |
return status; |
| 150 |
} |
| 151 |
|
| 152 |
/* Dualtity gap */ |
| 153 |
status = gsl_blas_ddot(z, state->s, gap); |
| 154 |
if (status != GSL_SUCCESS) { |
| 155 |
return status; |
| 156 |
} |
| 157 |
|
| 158 |
*gap /= (double)cqp->C->size1; |
| 159 |
|
| 160 |
status = build_kkt_matrix(cqp, z, state->s, state->kkt_matrix); |
| 161 |
if (status != GSL_SUCCESS) { |
| 162 |
return status; |
| 163 |
} |
| 164 |
|
| 165 |
state->tau = 3.0; |
| 166 |
|
| 167 |
/* for the convergence conditions */ |
| 168 |
/* data_norm = ||Q,A,C,q,b,d||_{infinity} */ |
| 169 |
*data_norm = gsl_matrix_max_norm(cqp->Q); |
| 170 |
|
| 171 |
if (cqp->A->size1 != 0) { |
| 172 |
*data_norm = GSL_MAX_DBL(*data_norm, gsl_matrix_max_norm(cqp->A)); |
| 173 |
} |
| 174 |
|
| 175 |
*data_norm = GSL_MAX_DBL(*data_norm, gsl_matrix_max_norm(cqp->C)); |
| 176 |
|
| 177 |
*data_norm = GSL_MAX_DBL(*data_norm, gsl_vector_max_norm(cqp->q)); |
| 178 |
|
| 179 |
if (cqp->A->size1 != 0) { |
| 180 |
*data_norm = GSL_MAX_DBL(*data_norm, gsl_vector_max_norm(cqp->b)); |
| 181 |
} |
| 182 |
|
| 183 |
*data_norm = GSL_MAX_DBL(*data_norm, gsl_vector_max_norm(cqp->d)); |
| 184 |
|
| 185 |
state->data_norm = *data_norm; |
| 186 |
|
| 187 |
/* ||r||_{infinity}=||r_Q, r_A, r_C||_{infinity} */ |
| 188 |
status = compute_residuals(cqp, x, y, z, state->s, state->r); |
| 189 |
*residuals_norm = gsl_vector_max_norm(state->r); |
| 190 |
|
| 191 |
*infeasibility = |
| 192 |
(*residuals_norm + compute_gap_infeasible_points(cqp, x, y, z)) / |
| 193 |
(state->data_norm); |
| 194 |
*infeasibility_min = *infeasibility; |
| 195 |
|
| 196 |
/* Gondzio's paameters */ |
| 197 |
state->k_max = 0; |
| 198 |
state->beta_min = 0.1; |
| 199 |
state->beta_max = 10.0; |
| 200 |
state->delta_alpha = 0.1; |
| 201 |
state->gamma = 0.1; |
| 202 |
|
| 203 |
// if(debug) |
| 204 |
// { |
| 205 |
// printf("\nStart points:\n"); |
| 206 |
// print_vectors(x, y, z, state->s); |
| 207 |
// printf("\nDuality gap: %e\n",*gap); |
| 208 |
// |
| 209 |
// printf("\nKKT-matrix:\n"); |
| 210 |
// for(i=0; i<state->kkt_matrix->size1; i++) |
| 211 |
// { |
| 212 |
// for(j=0; j<state->kkt_matrix->size2; j++) |
| 213 |
// printf("%4.2f |
| 214 |
//",gsl_matrix_get(state->kkt_matrix,i,j)); |
| 215 |
// printf("\n"); |
| 216 |
// } |
| 217 |
// printf("\n"); |
| 218 |
// } |
| 219 |
|
| 220 |
return GSL_SUCCESS; |
| 221 |
} |
| 222 |
|
| 223 |
static int mg_pdip_iterate(void *vstate, const gsl_cqp_data *cqp, gsl_vector *x, |
| 224 |
gsl_vector *y, gsl_vector *z, double *gap, |
| 225 |
double *residuals_norm, double *infeasibility, |
| 226 |
double *infeasibility_min) { |
| 227 |
size_t i, j = 0; //, debug=0; |
| 228 |
int signum; |
| 229 |
double sigma, alpha, alpha_gondzio; |
| 230 |
double tmp_v, tmp_vt, tmp_d; |
| 231 |
|
| 232 |
gsl_vector_view r_block, delta_block; |
| 233 |
|
| 234 |
mg_pdip_state *state = (mg_pdip_state *)vstate; |
| 235 |
|
| 236 |
gsl_permutation *p; |
| 237 |
|
| 238 |
gsl_vector *delta; |
| 239 |
gsl_vector *delta_s; |
| 240 |
|
| 241 |
gsl_vector *r_zs; |
| 242 |
|
| 243 |
gsl_vector *delta_gondzio; |
| 244 |
gsl_vector *delta_s_gondzio; |
| 245 |
|
| 246 |
p = gsl_permutation_alloc(state->kkt_matrix->size1); |
| 247 |
if (p == 0) { |
| 248 |
GSL_ERROR_VAL("failed to initialize space for permutation vector", |
| 249 |
GSL_ENOMEM, 0); |
| 250 |
} |
| 251 |
|
| 252 |
delta = gsl_vector_alloc(state->kkt_matrix->size2); |
| 253 |
if (delta == 0) { |
| 254 |
gsl_permutation_free(p); |
| 255 |
GSL_ERROR_VAL("failed to initialize space for predictor step", |
| 256 |
GSL_ENOMEM, 0); |
| 257 |
} |
| 258 |
delta_gondzio = gsl_vector_alloc(state->kkt_matrix->size2); |
| 259 |
if (delta_gondzio == 0) { |
| 260 |
gsl_vector_free(delta); |
| 261 |
gsl_permutation_free(p); |
| 262 |
GSL_ERROR_VAL("failed to initialize space for predictor step", |
| 263 |
GSL_ENOMEM, 0); |
| 264 |
} |
| 265 |
|
| 266 |
delta_s_gondzio = gsl_vector_alloc(z->size); |
| 267 |
if (delta_s_gondzio == 0) { |
| 268 |
gsl_vector_free(delta_gondzio); |
| 269 |
gsl_vector_free(delta); |
| 270 |
gsl_permutation_free(p); |
| 271 |
GSL_ERROR_VAL("failed to initialize space for LM s", GSL_ENOMEM, 0); |
| 272 |
} |
| 273 |
|
| 274 |
delta_s = gsl_vector_alloc(z->size); |
| 275 |
if (delta_s == 0) { |
| 276 |
gsl_vector_free(delta_s_gondzio); |
| 277 |
gsl_vector_free(delta_gondzio); |
| 278 |
gsl_vector_free(delta); |
| 279 |
gsl_permutation_free(p); |
| 280 |
GSL_ERROR_VAL("failed to initialize space for LM s", GSL_ENOMEM, 0); |
| 281 |
} |
| 282 |
|
| 283 |
r_zs = gsl_vector_alloc(z->size); |
| 284 |
if (r_zs == 0) { |
| 285 |
gsl_vector_free(delta_s); |
| 286 |
gsl_vector_free(delta_s_gondzio); |
| 287 |
gsl_vector_free(delta_gondzio); |
| 288 |
gsl_vector_free(delta); |
| 289 |
gsl_permutation_free(p); |
| 290 |
GSL_ERROR_VAL("failed to initialize space for LM s", GSL_ENOMEM, 0); |
| 291 |
} |
| 292 |
|
| 293 |
/* the right-hand side of the KKT-system: r = -(r_Q, r_A, r_C+Z^{-1}*r_zs) |
| 294 |
*/ |
| 295 |
/* the vecors of variables: delta = (delta_x, -delta_y, -delta_z) */ |
| 296 |
/* delta_s for the slack variable s: delta_s = Z^{-1}(-r_zs - S*delta_z) */ |
| 297 |
|
| 298 |
/********* Predictor Step ******************/ |
| 299 |
/* in the predictor step: r_zs = ZSe */ |
| 300 |
r_block = gsl_vector_subvector(state->r, cqp->Q->size1 + cqp->A->size1, |
| 301 |
cqp->C->size1); |
| 302 |
gsl_blas_daxpy(1.0, state->s, &r_block.vector); |
| 303 |
gsl_blas_dscal(-1.0, state->r); |
| 304 |
|
| 305 |
/* solve the KKT-system: */ |
| 306 |
/* evaluate the LU-decomposition of the KKT-matrix*/ |
| 307 |
gsl_linalg_LU_decomp(state->kkt_matrix, p, &signum); |
| 308 |
/* find the predictor step */ |
| 309 |
gsl_linalg_LU_solve(state->kkt_matrix, p, state->r, delta); |
| 310 |
|
| 311 |
for (i = 0; i < delta_s->size; i++) { |
| 312 |
/* find delta_s for the slack variable s: delta_s = Z^{-1}(-r_zs - |
| 313 |
* S*delta_z) */ |
| 314 |
gsl_vector_set( |
| 315 |
delta_s, i, |
| 316 |
gsl_vector_get(state->s, i) * |
| 317 |
(gsl_vector_get(delta, cqp->Q->size2 + cqp->A->size1 + i) / |
| 318 |
gsl_vector_get(z, i) - |
| 319 |
1.0)); |
| 320 |
} |
| 321 |
|
| 322 |
/* find the stepsize of the predictor step */ |
| 323 |
delta_block = gsl_vector_subvector(delta, cqp->Q->size1 + cqp->A->size1, |
| 324 |
cqp->C->size1); |
| 325 |
alpha = step_length(state->s, z, delta_s, &delta_block.vector); |
| 326 |
|
| 327 |
// if(debug) |
| 328 |
// { |
| 329 |
// printf("\n *** Predictor Step ***\n"); |
| 330 |
// printf("\nthe right-hand side of the KKT-system:\n"); |
| 331 |
// for(i=0; i<state->r->size; i++) |
| 332 |
// printf("r[%lu]=%f ",i,gsl_vector_get(state->r,i)); |
| 333 |
// printf("\n"); |
| 334 |
// printf("\nsolution (delta_x,-delta_y,-delta_z):\n"); |
| 335 |
// for(i=0; i<delta->size; i++) |
| 336 |
// printf("%6.3f ",gsl_vector_get(delta,i)); |
| 337 |
// printf("\n"); |
| 338 |
// printf("\ndelta_s\n"); |
| 339 |
// for(i=0; i<delta_s->size; i++) |
| 340 |
// printf("%6.3f ",gsl_vector_get(delta_s,i)); |
| 341 |
// printf("\n"); |
| 342 |
// printf("the stepsize for the predictor step=%f\n",alpha); |
| 343 |
// } |
| 344 |
|
| 345 |
/************ Evaluation of the centering parameter sigma ***************/ |
| 346 |
/* sigma = (gap_aff/gap)^tau */ |
| 347 |
sigma = 0.0; |
| 348 |
for (i = 0; i < z->size; i++) { |
| 349 |
sigma += |
| 350 |
(gsl_vector_get(z, i) - |
| 351 |
alpha * gsl_vector_get(delta, cqp->Q->size2 + cqp->A->size1 + i)) * |
| 352 |
(gsl_vector_get(state->s, i) + alpha * gsl_vector_get(delta_s, i)); |
| 353 |
} |
| 354 |
sigma /= (cqp->C->size1 * (*gap)); |
| 355 |
sigma = pow(sigma, state->tau); |
| 356 |
|
| 357 |
// if(debug) |
| 358 |
// printf("the centering parameter sigma =%f\n",sigma); |
| 359 |
|
| 360 |
/************ Corrector Step ******************/ |
| 361 |
|
| 362 |
/* modify the right-hand side of the kkt-system in oder to find the |
| 363 |
* Mehrotra's corrector step */ |
| 364 |
|
| 365 |
r_block = gsl_vector_subvector(state->r, cqp->Q->size1 + cqp->A->size1, |
| 366 |
cqp->C->size1); |
| 367 |
for (i = 0; i < cqp->C->size1; i++) { |
| 368 |
tmp_d = -sigma * (*gap) - |
| 369 |
gsl_vector_get(delta, cqp->Q->size2 + cqp->A->size1 + i) * |
| 370 |
gsl_vector_get(delta_s, i); |
| 371 |
gsl_vector_set( |
| 372 |
r_zs, i, |
| 373 |
gsl_vector_get(z, i) * gsl_vector_get(state->s, i) + tmp_d); |
| 374 |
gsl_vector_set( |
| 375 |
&r_block.vector, i, |
| 376 |
gsl_vector_get(&r_block.vector, i) - tmp_d / gsl_vector_get(z, i)); |
| 377 |
} |
| 378 |
|
| 379 |
/* find the corrector step */ |
| 380 |
gsl_linalg_LU_solve(state->kkt_matrix, p, state->r, delta); |
| 381 |
|
| 382 |
for (i = 0; i < delta_s->size; i++) { |
| 383 |
/* delta_s = Z^{-1}*(-r_zs-S*delta_z) */ |
| 384 |
gsl_vector_set( |
| 385 |
delta_s, i, |
| 386 |
(-gsl_vector_get(r_zs, i) + |
| 387 |
gsl_vector_get(delta, cqp->Q->size2 + cqp->A->size1 + i) * |
| 388 |
gsl_vector_get(state->s, i)) / |
| 389 |
gsl_vector_get(z, i)); |
| 390 |
} |
| 391 |
|
| 392 |
/* evaluate the stepsize */ |
| 393 |
delta_block = gsl_vector_subvector(delta, cqp->Q->size1 + cqp->A->size1, |
| 394 |
cqp->C->size1); |
| 395 |
alpha = step_length(state->s, z, delta_s, &delta_block.vector); |
| 396 |
|
| 397 |
// if(debug) |
| 398 |
// { |
| 399 |
// printf("\n *** Corrector Step ***\n"); |
| 400 |
// printf("the right-hand side:\n"); |
| 401 |
// for(i=0; i<state->r->size; i++) |
| 402 |
// printf("r[%lu]=%f ",i,gsl_vector_get(state->r,i)); |
| 403 |
// printf("\n"); |
| 404 |
// for(i=0; i<delta->size; i++) |
| 405 |
// printf("%6.3f ",gsl_vector_get(delta,i)); |
| 406 |
// printf("\n"); |
| 407 |
// printf("\ndelta_s\n"); |
| 408 |
// for(i=0; i<delta_s->size; i++) |
| 409 |
// printf("%6.3f ",gsl_vector_get(delta_s,i)); |
| 410 |
// printf("\n"); |
| 411 |
// printf("the stepsize for the corrector step=%f\n",alpha); |
| 412 |
// } |
| 413 |
|
| 414 |
/* Gondzio's centrality correction steps */ |
| 415 |
|
| 416 |
i = 0; |
| 417 |
while (i < state->k_max) { |
| 418 |
alpha_gondzio = GSL_MIN_DBL(alpha + state->delta_alpha, 1.0); |
| 419 |
r_block = gsl_vector_subvector(state->r, cqp->Q->size1 + cqp->A->size1, |
| 420 |
cqp->C->size1); |
| 421 |
|
| 422 |
for (j = 0; j < z->size; j++) { |
| 423 |
tmp_v = |
| 424 |
(gsl_vector_get(z, j) - |
| 425 |
alpha_gondzio * |
| 426 |
gsl_vector_get(delta, cqp->Q->size1 + cqp->A->size1 + j)) * |
| 427 |
(gsl_vector_get(state->s, j) + |
| 428 |
alpha_gondzio * gsl_vector_get(delta_s, j)); |
| 429 |
tmp_vt = GSL_MIN_DBL( |
| 430 |
GSL_MAX_DBL(tmp_v, state->beta_min * sigma * (*gap)), |
| 431 |
state->beta_max * sigma * (*gap)); |
| 432 |
gsl_vector_set(r_zs, j, gsl_vector_get(r_zs, j) - (tmp_vt - tmp_v)); |
| 433 |
gsl_vector_set(&r_block.vector, j, |
| 434 |
gsl_vector_get(&r_block.vector, j) + |
| 435 |
(tmp_vt - tmp_v) / gsl_vector_get(z, j)); |
| 436 |
} |
| 437 |
|
| 438 |
gsl_linalg_LU_solve(state->kkt_matrix, p, state->r, delta_gondzio); |
| 439 |
for (j = 0; j < delta_s->size; j++) { |
| 440 |
gsl_vector_set(delta_s_gondzio, j, |
| 441 |
(-gsl_vector_get(r_zs, j) + |
| 442 |
gsl_vector_get(delta_gondzio, |
| 443 |
cqp->Q->size2 + cqp->A->size1 + j) * |
| 444 |
gsl_vector_get(state->s, j)) / |
| 445 |
gsl_vector_get(z, j)); |
| 446 |
} |
| 447 |
|
| 448 |
/* evaluate the stepsize */ |
| 449 |
delta_block = gsl_vector_subvector( |
| 450 |
delta_gondzio, cqp->Q->size1 + cqp->A->size1, cqp->C->size1); |
| 451 |
alpha_gondzio = step_length(state->s, z, delta_s, &delta_block.vector); |
| 452 |
|
| 453 |
if (alpha_gondzio >= alpha + state->gamma * state->delta_alpha) { |
| 454 |
i++; |
| 455 |
alpha = alpha_gondzio; |
| 456 |
gsl_blas_dcopy(delta_gondzio, delta); |
| 457 |
gsl_blas_dcopy(delta_s_gondzio, delta_s); |
| 458 |
} else |
| 459 |
break; |
| 460 |
} |
| 461 |
|
| 462 |
/* heuristic for step length */ |
| 463 |
alpha = GSL_MIN_DBL(0.995 * alpha, 1.0); |
| 464 |
|
| 465 |
/* Update */ |
| 466 |
/* x^k = x^k + alpha*delta_x */ |
| 467 |
delta_block = gsl_vector_subvector(delta, 0, cqp->Q->size1); |
| 468 |
gsl_blas_daxpy(alpha, &delta_block.vector, x); |
| 469 |
|
| 470 |
/* y^k = y^k - alpha*(-delta_y) */ |
| 471 |
if (y != 0) { |
| 472 |
delta_block = gsl_vector_subvector(delta, cqp->Q->size1, cqp->A->size1); |
| 473 |
gsl_blas_daxpy(-alpha, &delta_block.vector, y); |
| 474 |
} |
| 475 |
|
| 476 |
/* z^k = z^k - alpha*(-delta_z) */ |
| 477 |
delta_block = gsl_vector_subvector(delta, cqp->Q->size1 + cqp->A->size1, |
| 478 |
cqp->C->size1); |
| 479 |
gsl_blas_daxpy(-alpha, &delta_block.vector, z); |
| 480 |
|
| 481 |
/* s^k = s^k + alpha*(delta_s) */ |
| 482 |
gsl_blas_daxpy(alpha, delta_s, state->s); |
| 483 |
|
| 484 |
/* duality gap */ |
| 485 |
gsl_blas_ddot(z, state->s, gap); |
| 486 |
*gap /= cqp->C->size1; |
| 487 |
|
| 488 |
/* data for the next iteration */ |
| 489 |
compute_residuals(cqp, x, y, z, state->s, state->r); |
| 490 |
*residuals_norm = gsl_vector_max_norm(state->r); |
| 491 |
|
| 492 |
build_kkt_matrix(cqp, z, state->s, state->kkt_matrix); |
| 493 |
|
| 494 |
/* for the infeasibility test */ |
| 495 |
*infeasibility = |
| 496 |
(*residuals_norm + compute_gap_infeasible_points(cqp, x, y, z)) / |
| 497 |
(state->data_norm); |
| 498 |
*infeasibility_min = GSL_MIN_DBL(*infeasibility, *infeasibility_min); |
| 499 |
|
| 500 |
// if(debug) |
| 501 |
// { |
| 502 |
// printf("current iteration points\n"); |
| 503 |
// print_vectors(x, y, z, state->s); |
| 504 |
// |
| 505 |
// printf("\nduality gap: %e\n",*gap); |
| 506 |
// } |
| 507 |
|
| 508 |
gsl_permutation_free(p); |
| 509 |
gsl_vector_free(delta); |
| 510 |
gsl_vector_free(delta_gondzio); |
| 511 |
gsl_vector_free(delta_s_gondzio); |
| 512 |
gsl_vector_free(delta_s); |
| 513 |
gsl_vector_free(r_zs); |
| 514 |
|
| 515 |
return GSL_SUCCESS; |
| 516 |
} |
| 517 |
|
| 518 |
static void mg_pdip_free(void *vstate) { |
| 519 |
mg_pdip_state *state = (mg_pdip_state *)vstate; |
| 520 |
|
| 521 |
gsl_vector_free(state->s); |
| 522 |
gsl_matrix_free(state->kkt_matrix); |
| 523 |
gsl_vector_free(state->r); |
| 524 |
} |
| 525 |
|
| 526 |
/* |
| 527 |
static int |
| 528 |
mg_pdip_restart (void *vstate) |
| 529 |
{ |
| 530 |
mg_pdip_state *state = (mg_pdip_state *) vstate; |
| 531 |
|
| 532 |
return GSL_SUCCESS; |
| 533 |
} |
| 534 |
*/ |
| 535 |
|
| 536 |
static int compute_residuals(const gsl_cqp_data *cqp, const gsl_vector *x, |
| 537 |
const gsl_vector *y, const gsl_vector *z, |
| 538 |
const gsl_vector *s, gsl_vector *r) { |
| 539 |
gsl_vector_view r_block; |
| 540 |
|
| 541 |
/*gsl_cqp_geconstraints * constraints = (gsl_cqp_geconstraints *) |
| 542 |
* cqp->constraints;*/ |
| 543 |
|
| 544 |
/* r_Q=Qx+q-A^ty-C^tz */ |
| 545 |
r_block = gsl_vector_subvector(r, 0, cqp->Q->size1); |
| 546 |
gsl_blas_dcopy(cqp->q, &r_block.vector); |
| 547 |
gsl_blas_dsymv(CblasUpper, 1.0, cqp->Q, x, 1.0, &r_block.vector); |
| 548 |
/*gsl_blas_dgemv(CblasNoTrans, 1.0, cqp->Q, x, 1.0, r_Q);*/ |
| 549 |
if (cqp->A->size1 != 0) { |
| 550 |
gsl_blas_dgemv(CblasTrans, -1.0, cqp->A, y, 1.0, &r_block.vector); |
| 551 |
} |
| 552 |
gsl_blas_dgemv(CblasTrans, -1.0, cqp->C, z, 1.0, &r_block.vector); |
| 553 |
|
| 554 |
/* r_A=Ax-b */ |
| 555 |
if (cqp->A->size1 != 0) { |
| 556 |
r_block = gsl_vector_subvector(r, cqp->Q->size1, cqp->A->size1); |
| 557 |
gsl_blas_dcopy(cqp->b, &r_block.vector); |
| 558 |
gsl_blas_dgemv(CblasNoTrans, 1.0, cqp->A, x, -1.0, &r_block.vector); |
| 559 |
} |
| 560 |
|
| 561 |
/* r_C=Cx-s-d */ |
| 562 |
r_block = |
| 563 |
gsl_vector_subvector(r, cqp->Q->size1 + cqp->A->size1, cqp->C->size1); |
| 564 |
gsl_blas_dcopy(s, &r_block.vector); |
| 565 |
gsl_blas_daxpy(1.0, cqp->d, &r_block.vector); |
| 566 |
gsl_blas_dgemv(CblasNoTrans, 1.0, cqp->C, x, -1.0, &r_block.vector); |
| 567 |
|
| 568 |
return GSL_SUCCESS; |
| 569 |
} |
| 570 |
|
| 571 |
static int build_kkt_matrix(const gsl_cqp_data *cqp, const gsl_vector *z, |
| 572 |
const gsl_vector *s, gsl_matrix *kkt_matrix) { |
| 573 |
size_t i; |
| 574 |
|
| 575 |
gsl_matrix_view kkt_block; |
| 576 |
|
| 577 |
/*KKT - Matrix |
| 578 |
|
| 579 |
|Q A^t C^t | |
| 580 |
kkt_matrix =|A 0 0 | |
| 581 |
|C 0 -Z^{-1}S| |
| 582 |
|
| 583 |
*/ |
| 584 |
/* 1. Block */ |
| 585 |
kkt_block = |
| 586 |
gsl_matrix_submatrix(kkt_matrix, 0, 0, cqp->Q->size1, cqp->Q->size2); |
| 587 |
gsl_matrix_memcpy(&kkt_block.matrix, cqp->Q); |
| 588 |
|
| 589 |
/* 2. Block */ |
| 590 |
if (cqp->A->size1 != 0) { |
| 591 |
kkt_block = gsl_matrix_submatrix(kkt_matrix, 0, cqp->Q->size2, |
| 592 |
cqp->A->size2, cqp->A->size1); |
| 593 |
gsl_matrix_transpose_memcpy(&kkt_block.matrix, cqp->A); |
| 594 |
} |
| 595 |
|
| 596 |
/* 3. Block */ |
| 597 |
kkt_block = |
| 598 |
gsl_matrix_submatrix(kkt_matrix, 0, cqp->Q->size2 + cqp->A->size1, |
| 599 |
cqp->C->size2, cqp->C->size1); |
| 600 |
gsl_matrix_transpose_memcpy(&kkt_block.matrix, cqp->C); |
| 601 |
|
| 602 |
/* 4. Block */ |
| 603 |
if (cqp->A->size1 != 0) { |
| 604 |
kkt_block = gsl_matrix_submatrix(kkt_matrix, cqp->Q->size1, 0, |
| 605 |
cqp->A->size1, cqp->A->size2); |
| 606 |
gsl_matrix_memcpy(&kkt_block.matrix, cqp->A); |
| 607 |
} |
| 608 |
|
| 609 |
/* 5. Block */ |
| 610 |
kkt_block = gsl_matrix_submatrix(kkt_matrix, cqp->Q->size1 + cqp->A->size1, |
| 611 |
0, cqp->C->size1, cqp->C->size2); |
| 612 |
gsl_matrix_memcpy(&kkt_block.matrix, cqp->C); |
| 613 |
|
| 614 |
/* Null Block */ |
| 615 |
kkt_block = gsl_matrix_submatrix(kkt_matrix, cqp->Q->size1, cqp->Q->size2, |
| 616 |
cqp->A->size1 + cqp->C->size1, |
| 617 |
cqp->A->size1 + cqp->C->size1); |
| 618 |
gsl_matrix_set_zero(&kkt_block.matrix); |
| 619 |
|
| 620 |
/* 6. Block */ |
| 621 |
for (i = cqp->Q->size1 + cqp->A->size1; i < kkt_matrix->size1; i++) { |
| 622 |
gsl_matrix_set( |
| 623 |
kkt_matrix, i, i, |
| 624 |
-gsl_vector_get(s, i - (cqp->Q->size1 + cqp->A->size1)) / |
| 625 |
gsl_vector_get(z, i - (cqp->Q->size1 + cqp->A->size1))); |
| 626 |
} |
| 627 |
|
| 628 |
return GSL_SUCCESS; |
| 629 |
} |
| 630 |
|
| 631 |
double step_length(const gsl_vector *s, const gsl_vector *z, |
| 632 |
const gsl_vector *delta_s, const gsl_vector *delta_z) { |
| 633 |
double alpha = 1.0; |
| 634 |
size_t i; |
| 635 |
|
| 636 |
for (i = 0; i < delta_s->size; i++) { |
| 637 |
if (gsl_vector_get(delta_z, i) > 0.0) |
| 638 |
alpha = GSL_MIN_DBL( |
| 639 |
alpha, gsl_vector_get(z, i) / gsl_vector_get(delta_z, i)); |
| 640 |
|
| 641 |
if (gsl_vector_get(delta_s, i) < 0.0) |
| 642 |
alpha = GSL_MIN_DBL( |
| 643 |
alpha, -gsl_vector_get(s, i) / gsl_vector_get(delta_s, i)); |
| 644 |
} |
| 645 |
|
| 646 |
return alpha; |
| 647 |
} |
| 648 |
|
| 649 |
static double compute_gap_infeasible_points(const gsl_cqp_data *cqp, |
| 650 |
const gsl_vector *x, |
| 651 |
const gsl_vector *y, |
| 652 |
const gsl_vector *z) { |
| 653 |
double g, tmp_d; |
| 654 |
|
| 655 |
gsl_vector *tmp_v = gsl_vector_alloc(cqp->q->size); |
| 656 |
|
| 657 |
gsl_blas_dcopy(cqp->q, tmp_v); |
| 658 |
gsl_blas_dsymv(CblasUpper, 1.0, cqp->Q, x, 1.0, tmp_v); |
| 659 |
gsl_blas_ddot(x, tmp_v, &g); |
| 660 |
|
| 661 |
if (cqp->A->size1 != 0) { |
| 662 |
gsl_blas_ddot(cqp->b, y, &tmp_d); |
| 663 |
g -= tmp_d; |
| 664 |
} |
| 665 |
|
| 666 |
gsl_blas_ddot(cqp->d, z, &tmp_d); |
| 667 |
g -= tmp_d; |
| 668 |
|
| 669 |
gsl_vector_free(tmp_v); |
| 670 |
|
| 671 |
return g; |
| 672 |
} |
| 673 |
|
| 674 |
double gsl_matrix_max_norm(const gsl_matrix *M) { |
| 675 |
size_t i, j; |
| 676 |
double max_norm = 0.0; |
| 677 |
|
| 678 |
for (i = 0; i < M->size1; i++) |
| 679 |
for (j = 0; j < M->size2; j++) |
| 680 |
max_norm = GSL_MAX_DBL(max_norm, fabs(gsl_matrix_get(M, i, j))); |
| 681 |
|
| 682 |
return max_norm; |
| 683 |
} |
| 684 |
|
| 685 |
double gsl_vector_max_norm(const gsl_vector *v) { |
| 686 |
size_t i; |
| 687 |
double max_norm = fabs(gsl_vector_get(v, 0)); |
| 688 |
|
| 689 |
for (i = 1; i < v->size; i++) |
| 690 |
max_norm = GSL_MAX_DBL(max_norm, fabs(gsl_vector_get(v, i))); |
| 691 |
|
| 692 |
return max_norm; |
| 693 |
} |
| 694 |
|
| 695 |
/* |
| 696 |
static void |
| 697 |
print_vectors(const gsl_vector * x, const gsl_vector * y, const gsl_vector * z, |
| 698 |
const gsl_vector * s) |
| 699 |
{ |
| 700 |
size_t i; |
| 701 |
|
| 702 |
printf("\nx[1 x %lu]:\n",x->size); |
| 703 |
for(i=0; i<x->size; i++) |
| 704 |
printf("%f ",gsl_vector_get(x,i)); |
| 705 |
printf("\n"); |
| 706 |
|
| 707 |
if( y != NULL ) { |
| 708 |
printf("\ny[1 x %lu]: (LM zu Ax=b)\n",y->size); |
| 709 |
for(i=0; i<y->size; i++) |
| 710 |
printf("%f ",gsl_vector_get(y,i)); |
| 711 |
printf("\n"); |
| 712 |
} |
| 713 |
|
| 714 |
printf("\nz[1 x %lu]: (LM zu Cx>=d)\n",z->size); |
| 715 |
for(i=0; i<z->size; i++) |
| 716 |
printf("%f ",gsl_vector_get(z,i)); |
| 717 |
printf("\n"); |
| 718 |
|
| 719 |
printf("\ns[1 x %lu]: (Slack zu Cx>=d)\n",s->size); |
| 720 |
for(i=0; i<s->size; i++) |
| 721 |
printf("%f ",gsl_vector_get(s,i)); |
| 722 |
printf("\n"); |
| 723 |
|
| 724 |
} |
| 725 |
*/ |
| 726 |
|
| 727 |
static const gsl_cqpminimizer_type mg_pdip_type = { |
| 728 |
"mg_pdip", /* name of the method: Mehrotra-Gondzio primal-dual interior |
| 729 |
point method*/ |
| 730 |
sizeof(mg_pdip_state), &mg_pdip_alloc, &mg_pdip_set, &mg_pdip_iterate, |
| 731 |
/* &mg_pdip_restart, */ |
| 732 |
&mg_pdip_free}; |
| 733 |
|
| 734 |
const gsl_cqpminimizer_type *gsl_cqpminimizer_mg_pdip = &mg_pdip_type; |