Actual source code: bqpip.c
petsc-3.10.5 2019-03-28
1: /*
2: This file implements a Mehrotra predictor-corrector method for
3: bound-constrained quadratic programs.
5: */
7: #include <../src/tao/quadratic/impls/bqpip/bqpipimpl.h>
8: #include <petscksp.h>
10: static PetscErrorCode QPIPComputeResidual(TAO_BQPIP *qp,Tao tao)
11: {
13: PetscReal dtmp = 1.0 - qp->psteplength;
16: /* Compute R3 and R5 */
18: VecScale(qp->R3,dtmp);
19: VecScale(qp->R5,dtmp);
20: qp->pinfeas=dtmp*qp->pinfeas;
22: VecCopy(qp->S,tao->gradient);
23: VecAXPY(tao->gradient,-1.0,qp->Z);
25: MatMult(tao->hessian,tao->solution,qp->RHS);
26: VecScale(qp->RHS,-1.0);
27: VecAXPY(qp->RHS,-1.0,qp->C);
28: VecAXPY(tao->gradient,-1.0,qp->RHS);
30: VecNorm(tao->gradient,NORM_1,&qp->dinfeas);
31: qp->rnorm=(qp->dinfeas+qp->pinfeas)/(qp->m+qp->n);
32: return(0);
33: }
35: static PetscErrorCode QPIPSetInitialPoint(TAO_BQPIP *qp, Tao tao)
36: {
38: PetscReal two=2.0,p01=1;
39: PetscReal gap1,gap2,fff,mu;
42: /* Compute function, Gradient R=Hx+b, and Hessian */
43: MatMult(tao->hessian,tao->solution,tao->gradient);
44: VecCopy(qp->C,qp->Work);
45: VecAXPY(qp->Work,0.5,tao->gradient);
46: VecAXPY(tao->gradient,1.0,qp->C);
47: VecDot(tao->solution,qp->Work,&fff);
48: qp->pobj = fff + qp->d;
50: if (PetscIsInfOrNanReal(qp->pobj)) SETERRQ(PETSC_COMM_SELF,1, "User provided data contains Inf or NaN");
52: /* Initialize slack vectors */
53: /* T = XU - X; G = X - XL */
54: VecCopy(qp->XU,qp->T);
55: VecAXPY(qp->T,-1.0,tao->solution);
56: VecCopy(tao->solution,qp->G);
57: VecAXPY(qp->G,-1.0,qp->XL);
59: VecSet(qp->GZwork,p01);
60: VecSet(qp->TSwork,p01);
62: VecPointwiseMax(qp->G,qp->G,qp->GZwork);
63: VecPointwiseMax(qp->T,qp->T,qp->TSwork);
65: /* Initialize Dual Variable Vectors */
66: VecCopy(qp->G,qp->Z);
67: VecReciprocal(qp->Z);
69: VecCopy(qp->T,qp->S);
70: VecReciprocal(qp->S);
72: MatMult(tao->hessian,qp->Work,qp->RHS);
73: VecAbs(qp->RHS);
74: VecSet(qp->Work,p01);
75: VecPointwiseMax(qp->RHS,qp->RHS,qp->Work);
77: VecPointwiseDivide(qp->RHS,tao->gradient,qp->RHS);
78: VecNorm(qp->RHS,NORM_1,&gap1);
79: mu = PetscMin(10.0,(gap1+10.0)/qp->m);
81: VecScale(qp->S,mu);
82: VecScale(qp->Z,mu);
84: VecSet(qp->TSwork,p01);
85: VecSet(qp->GZwork,p01);
86: VecPointwiseMax(qp->S,qp->S,qp->TSwork);
87: VecPointwiseMax(qp->Z,qp->Z,qp->GZwork);
89: qp->mu=0;qp->dinfeas=1.0;qp->pinfeas=1.0;
90: while ((qp->dinfeas+qp->pinfeas)/(qp->m+qp->n) >= qp->mu) {
91: VecScale(qp->G,two);
92: VecScale(qp->Z,two);
93: VecScale(qp->S,two);
94: VecScale(qp->T,two);
96: QPIPComputeResidual(qp,tao);
98: VecCopy(tao->solution,qp->R3);
99: VecAXPY(qp->R3,-1.0,qp->G);
100: VecAXPY(qp->R3,-1.0,qp->XL);
102: VecCopy(tao->solution,qp->R5);
103: VecAXPY(qp->R5,1.0,qp->T);
104: VecAXPY(qp->R5,-1.0,qp->XU);
106: VecNorm(qp->R3,NORM_INFINITY,&gap1);
107: VecNorm(qp->R5,NORM_INFINITY,&gap2);
108: qp->pinfeas=PetscMax(gap1,gap2);
110: /* Compute the duality gap */
111: VecDot(qp->G,qp->Z,&gap1);
112: VecDot(qp->T,qp->S,&gap2);
114: qp->gap = gap1+gap2;
115: qp->dobj = qp->pobj - qp->gap;
116: if (qp->m>0) {
117: qp->mu=qp->gap/(qp->m);
118: } else {
119: qp->mu=0.0;
120: }
121: qp->rgap=qp->gap/(PetscAbsReal(qp->dobj) + PetscAbsReal(qp->pobj) + 1.0);
122: }
123: return(0);
124: }
126: static PetscErrorCode QPIPStepLength(TAO_BQPIP *qp)
127: {
128: PetscReal tstep1,tstep2,tstep3,tstep4,tstep;
132: /* Compute stepsize to the boundary */
133: VecStepMax(qp->G,qp->DG,&tstep1);
134: VecStepMax(qp->T,qp->DT,&tstep2);
135: VecStepMax(qp->S,qp->DS,&tstep3);
136: VecStepMax(qp->Z,qp->DZ,&tstep4);
138: tstep = PetscMin(tstep1,tstep2);
139: qp->psteplength = PetscMin(0.95*tstep,1.0);
141: tstep = PetscMin(tstep3,tstep4);
142: qp->dsteplength = PetscMin(0.95*tstep,1.0);
144: qp->psteplength = PetscMin(qp->psteplength,qp->dsteplength);
145: qp->dsteplength = qp->psteplength;
146: return(0);
147: }
149: static PetscErrorCode QPIPComputeNormFromCentralPath(TAO_BQPIP *qp,PetscReal *norm)
150: {
152: PetscReal gap[2],mu[2],nmu;
155: VecPointwiseMult(qp->GZwork,qp->G,qp->Z);
156: VecPointwiseMult(qp->TSwork,qp->T,qp->S);
157: VecNorm(qp->TSwork,NORM_1,&mu[0]);
158: VecNorm(qp->GZwork,NORM_1,&mu[1]);
160: nmu=-(mu[0]+mu[1])/qp->m;
162: VecShift(qp->GZwork,nmu);
163: VecShift(qp->TSwork,nmu);
165: VecNorm(qp->GZwork,NORM_2,&gap[0]);
166: VecNorm(qp->TSwork,NORM_2,&gap[1]);
167: gap[0]*=gap[0];
168: gap[1]*=gap[1];
170: qp->pathnorm=PetscSqrtScalar(gap[0]+gap[1]);
171: *norm=qp->pathnorm;
172: return(0);
173: }
175: static PetscErrorCode QPIPComputeStepDirection(TAO_BQPIP *qp,Tao tao)
176: {
180: /* Calculate DG */
181: VecCopy(tao->stepdirection,qp->DG);
182: VecAXPY(qp->DG,1.0,qp->R3);
184: /* Calculate DT */
185: VecCopy(tao->stepdirection,qp->DT);
186: VecScale(qp->DT,-1.0);
187: VecAXPY(qp->DT,-1.0,qp->R5);
189: /* Calculate DZ */
190: VecAXPY(qp->DZ,-1.0,qp->Z);
191: VecPointwiseDivide(qp->GZwork,qp->DG,qp->G);
192: VecPointwiseMult(qp->GZwork,qp->GZwork,qp->Z);
193: VecAXPY(qp->DZ,-1.0,qp->GZwork);
195: /* Calculate DS */
196: VecAXPY(qp->DS,-1.0,qp->S);
197: VecPointwiseDivide(qp->TSwork,qp->DT,qp->T);
198: VecPointwiseMult(qp->TSwork,qp->TSwork,qp->S);
199: VecAXPY(qp->DS,-1.0,qp->TSwork);
200: return(0);
201: }
203: static PetscErrorCode TaoSetup_BQPIP(Tao tao)
204: {
205: TAO_BQPIP *qp =(TAO_BQPIP*)tao->data;
209: /* Set pointers to Data */
210: VecGetSize(tao->solution,&qp->n);
212: /* Allocate some arrays */
213: if (!tao->gradient) {
214: VecDuplicate(tao->solution,&tao->gradient);
215: }
216: if (!tao->stepdirection) {
217: VecDuplicate(tao->solution,&tao->stepdirection);
218: }
219: if (!tao->XL) {
220: VecDuplicate(tao->solution,&tao->XL);
221: VecSet(tao->XL,PETSC_NINFINITY);
222: }
223: if (!tao->XU) {
224: VecDuplicate(tao->solution,&tao->XU);
225: VecSet(tao->XU,PETSC_INFINITY);
226: }
228: VecDuplicate(tao->solution,&qp->Work);
229: VecDuplicate(tao->solution,&qp->XU);
230: VecDuplicate(tao->solution,&qp->XL);
231: VecDuplicate(tao->solution,&qp->HDiag);
232: VecDuplicate(tao->solution,&qp->DiagAxpy);
233: VecDuplicate(tao->solution,&qp->RHS);
234: VecDuplicate(tao->solution,&qp->RHS2);
235: VecDuplicate(tao->solution,&qp->C);
237: VecDuplicate(tao->solution,&qp->G);
238: VecDuplicate(tao->solution,&qp->DG);
239: VecDuplicate(tao->solution,&qp->S);
240: VecDuplicate(tao->solution,&qp->Z);
241: VecDuplicate(tao->solution,&qp->DZ);
242: VecDuplicate(tao->solution,&qp->GZwork);
243: VecDuplicate(tao->solution,&qp->R3);
245: VecDuplicate(tao->solution,&qp->T);
246: VecDuplicate(tao->solution,&qp->DT);
247: VecDuplicate(tao->solution,&qp->DS);
248: VecDuplicate(tao->solution,&qp->TSwork);
249: VecDuplicate(tao->solution,&qp->R5);
250: qp->m=2*qp->n;
251: return(0);
252: }
254: static PetscErrorCode TaoSolve_BQPIP(Tao tao)
255: {
256: TAO_BQPIP *qp = (TAO_BQPIP*)tao->data;
257: PetscErrorCode ierr;
258: PetscInt its;
259: PetscReal d1,d2,ksptol,sigmamu;
260: PetscReal gnorm,dstep,pstep,step=0;
261: PetscReal gap[4];
262: PetscBool getdiagop;
265: qp->dobj = 0.0;
266: qp->pobj = 1.0;
267: qp->gap = 10.0;
268: qp->rgap = 1.0;
269: qp->mu = 1.0;
270: qp->dinfeas = 1.0;
271: qp->psteplength = 0.0;
272: qp->dsteplength = 0.0;
274: /* TODO
275: - Remove fixed variables and treat them correctly
276: - Use index sets for the infinite versus finite bounds
277: - Update remaining code for fixed and free variables
278: - Fix inexact solves for predictor and corrector
279: */
281: /* Tighten infinite bounds, things break when we don't do this
282: -- see test_bqpip.c
283: */
284: TaoComputeVariableBounds(tao);
285: VecSet(qp->XU,1.0e20);
286: VecSet(qp->XL,-1.0e20);
287: VecPointwiseMax(qp->XL,qp->XL,tao->XL);
288: VecPointwiseMin(qp->XU,qp->XU,tao->XU);
289: VecMedian(qp->XL,tao->solution,qp->XU,tao->solution);
291: /* Evaluate gradient and Hessian at zero to get the correct values
292: without contaminating them with numerical artifacts.
293: */
294: VecSet(qp->Work,0);
295: TaoComputeObjectiveAndGradient(tao,qp->Work,&qp->d,qp->C);
296: TaoComputeHessian(tao,qp->Work,tao->hessian,tao->hessian_pre);
297: MatHasOperation(tao->hessian,MATOP_GET_DIAGONAL,&getdiagop);
298: if (getdiagop) {
299: MatGetDiagonal(tao->hessian,qp->HDiag);
300: }
302: /* Initialize starting point and residuals */
303: QPIPSetInitialPoint(qp,tao);
304: QPIPComputeResidual(qp,tao);
306: /* Enter main loop */
307: tao->reason = TAO_CONTINUE_ITERATING;
308: while (1) {
310: /* Check Stopping Condition */
311: gnorm = PetscSqrtScalar(qp->gap + qp->dinfeas);
312: TaoLogConvergenceHistory(tao,qp->pobj,gnorm,qp->pinfeas,tao->ksp_its);
313: TaoMonitor(tao,tao->niter,qp->pobj,gnorm,qp->pinfeas,step);
314: (*tao->ops->convergencetest)(tao,tao->cnvP);
315: if (tao->reason != TAO_CONTINUE_ITERATING) break;
316: tao->niter++;
317: tao->ksp_its = 0;
319: /*
320: Dual Infeasibility Direction should already be in the right
321: hand side from computing the residuals
322: */
324: QPIPComputeNormFromCentralPath(qp,&d1);
326: VecSet(qp->DZ,0.0);
327: VecSet(qp->DS,0.0);
329: /*
330: Compute the Primal Infeasiblitiy RHS and the
331: Diagonal Matrix to be added to H and store in Work
332: */
333: VecPointwiseDivide(qp->DiagAxpy,qp->Z,qp->G);
334: VecPointwiseMult(qp->GZwork,qp->DiagAxpy,qp->R3);
335: VecAXPY(qp->RHS,-1.0,qp->GZwork);
337: VecPointwiseDivide(qp->TSwork,qp->S,qp->T);
338: VecAXPY(qp->DiagAxpy,1.0,qp->TSwork);
339: VecPointwiseMult(qp->TSwork,qp->TSwork,qp->R5);
340: VecAXPY(qp->RHS,-1.0,qp->TSwork);
342: /* Determine the solving tolerance */
343: ksptol = qp->mu/10.0;
344: ksptol = PetscMin(ksptol,0.001);
345: KSPSetTolerances(tao->ksp,ksptol,1e-30,1e30,PetscMax(10,qp->n));
347: /* Shift the diagonals of the Hessian matrix */
348: MatDiagonalSet(tao->hessian,qp->DiagAxpy,ADD_VALUES);
349: if (!getdiagop) {
350: VecCopy(qp->DiagAxpy,qp->HDiag);
351: VecScale(qp->HDiag,-1.0);
352: }
353: MatAssemblyBegin(tao->hessian,MAT_FINAL_ASSEMBLY);
354: MatAssemblyEnd(tao->hessian,MAT_FINAL_ASSEMBLY);
356: KSPSetOperators(tao->ksp,tao->hessian,tao->hessian_pre);
357: KSPSolve(tao->ksp,qp->RHS,tao->stepdirection);
358: KSPGetIterationNumber(tao->ksp,&its);
359: tao->ksp_its += its;
360: tao->ksp_tot_its += its;
362: /* Restore the true diagonal of the Hessian matrix */
363: if (getdiagop) {
364: MatDiagonalSet(tao->hessian,qp->HDiag,INSERT_VALUES);
365: } else {
366: MatDiagonalSet(tao->hessian,qp->HDiag,ADD_VALUES);
367: }
368: MatAssemblyBegin(tao->hessian,MAT_FINAL_ASSEMBLY);
369: MatAssemblyEnd(tao->hessian,MAT_FINAL_ASSEMBLY);
370: QPIPComputeStepDirection(qp,tao);
371: QPIPStepLength(qp);
373: /* Calculate New Residual R1 in Work vector */
374: MatMult(tao->hessian,tao->stepdirection,qp->RHS2);
375: VecAXPY(qp->RHS2,1.0,qp->DS);
376: VecAXPY(qp->RHS2,-1.0,qp->DZ);
377: VecAYPX(qp->RHS2,qp->dsteplength,tao->gradient);
379: VecNorm(qp->RHS2,NORM_2,&qp->dinfeas);
380: VecDot(qp->DZ,qp->DG,gap);
381: VecDot(qp->DS,qp->DT,gap+1);
383: qp->rnorm = (qp->dinfeas+qp->psteplength*qp->pinfeas)/(qp->m+qp->n);
384: pstep = qp->psteplength;
385: step = PetscMin(qp->psteplength,qp->dsteplength);
386: sigmamu = (pstep*pstep*(gap[0]+gap[1]) + (1 - pstep)*qp->gap)/qp->m;
388: if (qp->predcorr && step < 0.9) {
389: if (sigmamu < qp->mu) {
390: sigmamu = sigmamu/qp->mu;
391: sigmamu = sigmamu*sigmamu*sigmamu;
392: } else {
393: sigmamu = 1.0;
394: }
395: sigmamu = sigmamu*qp->mu;
397: /* Compute Corrector Step */
398: VecPointwiseMult(qp->DZ,qp->DG,qp->DZ);
399: VecScale(qp->DZ,-1.0);
400: VecShift(qp->DZ,sigmamu);
401: VecPointwiseDivide(qp->DZ,qp->DZ,qp->G);
403: VecPointwiseMult(qp->DS,qp->DS,qp->DT);
404: VecScale(qp->DS,-1.0);
405: VecShift(qp->DS,sigmamu);
406: VecPointwiseDivide(qp->DS,qp->DS,qp->T);
408: VecCopy(qp->DZ,qp->RHS2);
409: VecAXPY(qp->RHS2,-1.0,qp->DS);
410: VecAXPY(qp->RHS2,1.0,qp->RHS);
412: /* Approximately solve the linear system */
413: MatDiagonalSet(tao->hessian,qp->DiagAxpy,ADD_VALUES);
414: if (!getdiagop) {
415: VecCopy(qp->DiagAxpy,qp->HDiag);
416: VecScale(qp->HDiag,-1.0);
417: }
418: MatAssemblyBegin(tao->hessian,MAT_FINAL_ASSEMBLY);
419: MatAssemblyEnd(tao->hessian,MAT_FINAL_ASSEMBLY);
421: /* Solve using the previous tolerances that were set */
422: KSPSolve(tao->ksp,qp->RHS2,tao->stepdirection);
423: KSPGetIterationNumber(tao->ksp,&its);
424: tao->ksp_its += its;
425: tao->ksp_tot_its += its;
427: if (getdiagop) {
428: MatDiagonalSet(tao->hessian,qp->HDiag,INSERT_VALUES);
429: } else {
430: MatDiagonalSet(tao->hessian,qp->HDiag,ADD_VALUES);
431: }
432: MatAssemblyBegin(tao->hessian,MAT_FINAL_ASSEMBLY);
433: MatAssemblyEnd(tao->hessian,MAT_FINAL_ASSEMBLY);
434: QPIPComputeStepDirection(qp,tao);
435: QPIPStepLength(qp);
436: } /* End Corrector step */
439: /* Take the step */
440: dstep = qp->dsteplength;
442: VecAXPY(qp->Z,dstep,qp->DZ);
443: VecAXPY(qp->S,dstep,qp->DS);
444: VecAXPY(tao->solution,dstep,tao->stepdirection);
445: VecAXPY(qp->G,dstep,qp->DG);
446: VecAXPY(qp->T,dstep,qp->DT);
448: /* Compute Residuals */
449: QPIPComputeResidual(qp,tao);
451: /* Evaluate quadratic function */
452: MatMult(tao->hessian,tao->solution,qp->Work);
454: VecDot(tao->solution,qp->Work,&d1);
455: VecDot(tao->solution,qp->C,&d2);
456: VecDot(qp->G,qp->Z,gap);
457: VecDot(qp->T,qp->S,gap+1);
459: /* Compute the duality gap */
460: qp->pobj = d1/2.0 + d2+qp->d;
461: qp->gap = gap[0]+gap[1];
462: qp->dobj = qp->pobj - qp->gap;
463: if (qp->m > 0) {
464: qp->mu = qp->gap/(qp->m);
465: }
466: qp->rgap = qp->gap/(PetscAbsReal(qp->dobj) + PetscAbsReal(qp->pobj) + 1.0);
467: } /* END MAIN LOOP */
468: return(0);
469: }
471: static PetscErrorCode TaoView_BQPIP(Tao tao,PetscViewer viewer)
472: {
474: return(0);
475: }
477: static PetscErrorCode TaoSetFromOptions_BQPIP(PetscOptionItems *PetscOptionsObject,Tao tao)
478: {
479: TAO_BQPIP *qp = (TAO_BQPIP*)tao->data;
483: PetscOptionsHead(PetscOptionsObject,"Interior point method for bound constrained quadratic optimization");
484: PetscOptionsInt("-tao_bqpip_predcorr","Use a predictor-corrector method","",qp->predcorr,&qp->predcorr,0);
485: PetscOptionsTail();
486: KSPSetFromOptions(tao->ksp);
487: return(0);
488: }
490: static PetscErrorCode TaoDestroy_BQPIP(Tao tao)
491: {
492: TAO_BQPIP *qp = (TAO_BQPIP*)tao->data;
496: if (tao->setupcalled) {
497: VecDestroy(&qp->G);
498: VecDestroy(&qp->DG);
499: VecDestroy(&qp->Z);
500: VecDestroy(&qp->DZ);
501: VecDestroy(&qp->GZwork);
502: VecDestroy(&qp->R3);
503: VecDestroy(&qp->S);
504: VecDestroy(&qp->DS);
505: VecDestroy(&qp->T);
507: VecDestroy(&qp->DT);
508: VecDestroy(&qp->TSwork);
509: VecDestroy(&qp->R5);
510: VecDestroy(&qp->HDiag);
511: VecDestroy(&qp->Work);
512: VecDestroy(&qp->XL);
513: VecDestroy(&qp->XU);
514: VecDestroy(&qp->DiagAxpy);
515: VecDestroy(&qp->RHS);
516: VecDestroy(&qp->RHS2);
517: VecDestroy(&qp->C);
518: }
519: PetscFree(tao->data);
520: return(0);
521: }
523: static PetscErrorCode TaoComputeDual_BQPIP(Tao tao,Vec DXL,Vec DXU)
524: {
525: TAO_BQPIP *qp = (TAO_BQPIP*)tao->data;
526: PetscErrorCode ierr;
529: VecCopy(qp->Z,DXL);
530: VecCopy(qp->S,DXU);
531: VecScale(DXU,-1.0);
532: return(0);
533: }
535: /*MC
536: TAOBQPIP - interior-point method for quadratic programs with
537: box constraints.
539: Notes:
540: This algorithms solves quadratic problems only, the Hessian will
541: only be computed once.
543: Options Database Keys:
544: . -tao_bqpip_predcorr - use a predictor/corrector method
546: Level: beginner
547: M*/
549: PETSC_EXTERN PetscErrorCode TaoCreate_BQPIP(Tao tao)
550: {
551: TAO_BQPIP *qp;
555: PetscNewLog(tao,&qp);
557: tao->ops->setup = TaoSetup_BQPIP;
558: tao->ops->solve = TaoSolve_BQPIP;
559: tao->ops->view = TaoView_BQPIP;
560: tao->ops->setfromoptions = TaoSetFromOptions_BQPIP;
561: tao->ops->destroy = TaoDestroy_BQPIP;
562: tao->ops->computedual = TaoComputeDual_BQPIP;
564: /* Override default settings (unless already changed) */
565: if (!tao->max_it_changed) tao->max_it=100;
566: if (!tao->max_funcs_changed) tao->max_funcs = 500;
567: #if defined(PETSC_USE_REAL_SINGLE)
568: if (!tao->catol_changed) tao->catol=1e-6;
569: #else
570: if (!tao->catol_changed) tao->catol=1e-12;
571: #endif
573: /* Initialize pointers and variables */
574: qp->n = 0;
575: qp->m = 0;
577: qp->predcorr = 1;
578: tao->data = (void*)qp;
580: KSPCreate(((PetscObject)tao)->comm,&tao->ksp);
581: PetscObjectIncrementTabLevel((PetscObject)tao->ksp, (PetscObject)tao, 1);
582: KSPSetOptionsPrefix(tao->ksp,tao->hdr.prefix);
583: KSPSetType(tao->ksp,KSPCG);
584: KSPSetTolerances(tao->ksp,1e-14,1e-30,1e30,PetscMax(10,qp->n));
585: return(0);
586: }