Actual source code: lmvm.c
petsc-3.5.4 2015-05-23
1: #include <petsctaolinesearch.h>
2: #include <../src/tao/matrix/lmvmmat.h>
3: #include <../src/tao/unconstrained/impls/lmvm/lmvm.h>
5: #define LMVM_BFGS 0
6: #define LMVM_SCALED_GRADIENT 1
7: #define LMVM_GRADIENT 2
11: static PetscErrorCode TaoSolve_LMVM(Tao tao)
12: {
13: TAO_LMVM *lmP = (TAO_LMVM *)tao->data;
14: PetscReal f, fold, gdx, gnorm;
15: PetscReal step = 1.0;
16: PetscReal delta;
17: PetscErrorCode ierr;
18: PetscInt stepType;
19: PetscInt iter = 0;
20: TaoConvergedReason reason = TAO_CONTINUE_ITERATING;
21: TaoLineSearchConvergedReason ls_status = TAOLINESEARCH_CONTINUE_ITERATING;
25: if (tao->XL || tao->XU || tao->ops->computebounds) {
26: PetscPrintf(((PetscObject)tao)->comm,"WARNING: Variable bounds have been set but will be ignored by lmvm algorithm\n");
27: }
29: /* Check convergence criteria */
30: TaoComputeObjectiveAndGradient(tao, tao->solution, &f, tao->gradient);
31: VecNorm(tao->gradient,NORM_2,&gnorm);
32: if (PetscIsInfOrNanReal(f) || PetscIsInfOrNanReal(gnorm)) SETERRQ(PETSC_COMM_SELF,1, "User provided compute function generated Inf or NaN");
34: TaoMonitor(tao, iter, f, gnorm, 0.0, step, &reason);
35: if (reason != TAO_CONTINUE_ITERATING) return(0);
37: /* Set initial scaling for the function */
38: if (f != 0.0) {
39: delta = 2.0 * PetscAbsScalar(f) / (gnorm*gnorm);
40: } else {
41: delta = 2.0 / (gnorm*gnorm);
42: }
43: MatLMVMSetDelta(lmP->M,delta);
45: /* Set counter for gradient/reset steps */
46: lmP->bfgs = 0;
47: lmP->sgrad = 0;
48: lmP->grad = 0;
50: /* Have not converged; continue with Newton method */
51: while (reason == TAO_CONTINUE_ITERATING) {
52: /* Compute direction */
53: MatLMVMUpdate(lmP->M,tao->solution,tao->gradient);
54: MatLMVMSolve(lmP->M, tao->gradient, lmP->D);
55: ++lmP->bfgs;
57: /* Check for success (descent direction) */
58: VecDot(lmP->D, tao->gradient, &gdx);
59: if ((gdx <= 0.0) || PetscIsInfOrNanReal(gdx)) {
60: /* Step is not descent or direction produced not a number
61: We can assert bfgsUpdates > 1 in this case because
62: the first solve produces the scaled gradient direction,
63: which is guaranteed to be descent
65: Use steepest descent direction (scaled)
66: */
68: ++lmP->grad;
70: if (f != 0.0) {
71: delta = 2.0 * PetscAbsScalar(f) / (gnorm*gnorm);
72: } else {
73: delta = 2.0 / (gnorm*gnorm);
74: }
75: MatLMVMSetDelta(lmP->M, delta);
76: MatLMVMReset(lmP->M);
77: MatLMVMUpdate(lmP->M, tao->solution, tao->gradient);
78: MatLMVMSolve(lmP->M,tao->gradient, lmP->D);
80: /* On a reset, the direction cannot be not a number; it is a
81: scaled gradient step. No need to check for this condition. */
83: lmP->bfgs = 1;
84: ++lmP->sgrad;
85: stepType = LMVM_SCALED_GRADIENT;
86: } else {
87: if (1 == lmP->bfgs) {
88: /* The first BFGS direction is always the scaled gradient */
89: ++lmP->sgrad;
90: stepType = LMVM_SCALED_GRADIENT;
91: } else {
92: ++lmP->bfgs;
93: stepType = LMVM_BFGS;
94: }
95: }
96: VecScale(lmP->D, -1.0);
98: /* Perform the linesearch */
99: fold = f;
100: VecCopy(tao->solution, lmP->Xold);
101: VecCopy(tao->gradient, lmP->Gold);
103: TaoLineSearchApply(tao->linesearch, tao->solution, &f, tao->gradient, lmP->D, &step,&ls_status);
104: TaoAddLineSearchCounts(tao);
106: while (ls_status != TAOLINESEARCH_SUCCESS && ls_status != TAOLINESEARCH_SUCCESS_USER && (stepType != LMVM_GRADIENT)) {
107: /* Linesearch failed */
108: /* Reset factors and use scaled gradient step */
109: f = fold;
110: VecCopy(lmP->Xold, tao->solution);
111: VecCopy(lmP->Gold, tao->gradient);
113: switch(stepType) {
114: case LMVM_BFGS:
115: /* Failed to obtain acceptable iterate with BFGS step */
116: /* Attempt to use the scaled gradient direction */
118: if (f != 0.0) {
119: delta = 2.0 * PetscAbsScalar(f) / (gnorm*gnorm);
120: } else {
121: delta = 2.0 / (gnorm*gnorm);
122: }
123: MatLMVMSetDelta(lmP->M, delta);
124: MatLMVMReset(lmP->M);
125: MatLMVMUpdate(lmP->M, tao->solution, tao->gradient);
126: MatLMVMSolve(lmP->M, tao->gradient, lmP->D);
128: /* On a reset, the direction cannot be not a number; it is a
129: scaled gradient step. No need to check for this condition. */
131: lmP->bfgs = 1;
132: ++lmP->sgrad;
133: stepType = LMVM_SCALED_GRADIENT;
134: break;
136: case LMVM_SCALED_GRADIENT:
137: /* The scaled gradient step did not produce a new iterate;
138: attempt to use the gradient direction.
139: Need to make sure we are not using a different diagonal scaling */
140: MatLMVMSetDelta(lmP->M, 1.0);
141: MatLMVMReset(lmP->M);
142: MatLMVMUpdate(lmP->M, tao->solution, tao->gradient);
143: MatLMVMSolve(lmP->M, tao->gradient, lmP->D);
145: lmP->bfgs = 1;
146: ++lmP->grad;
147: stepType = LMVM_GRADIENT;
148: break;
149: }
150: VecScale(lmP->D, -1.0);
152: /* Perform the linesearch */
153: TaoLineSearchApply(tao->linesearch, tao->solution, &f, tao->gradient, lmP->D, &step, &ls_status);
154: TaoAddLineSearchCounts(tao);
155: }
157: if (ls_status != TAOLINESEARCH_SUCCESS && ls_status != TAOLINESEARCH_SUCCESS_USER) {
158: /* Failed to find an improving point */
159: f = fold;
160: VecCopy(lmP->Xold, tao->solution);
161: VecCopy(lmP->Gold, tao->gradient);
162: step = 0.0;
163: reason = TAO_DIVERGED_LS_FAILURE;
164: tao->reason = TAO_DIVERGED_LS_FAILURE;
165: }
166: /* Check for termination */
167: VecNorm(tao->gradient, NORM_2, &gnorm);
168: iter++;
169: TaoMonitor(tao,iter,f,gnorm,0.0,step,&reason);
170: }
171: return(0);
172: }
176: static PetscErrorCode TaoSetUp_LMVM(Tao tao)
177: {
178: TAO_LMVM *lmP = (TAO_LMVM *)tao->data;
179: PetscInt n,N;
183: /* Existence of tao->solution checked in TaoSetUp() */
184: if (!tao->gradient) {VecDuplicate(tao->solution,&tao->gradient); }
185: if (!tao->stepdirection) {VecDuplicate(tao->solution,&tao->stepdirection); }
186: if (!lmP->D) {VecDuplicate(tao->solution,&lmP->D); }
187: if (!lmP->Xold) {VecDuplicate(tao->solution,&lmP->Xold); }
188: if (!lmP->Gold) {VecDuplicate(tao->solution,&lmP->Gold); }
190: /* Create matrix for the limited memory approximation */
191: VecGetLocalSize(tao->solution,&n);
192: VecGetSize(tao->solution,&N);
193: MatCreateLMVM(((PetscObject)tao)->comm,n,N,&lmP->M);
194: MatLMVMAllocateVectors(lmP->M,tao->solution);
195: return(0);
196: }
198: /* ---------------------------------------------------------- */
201: static PetscErrorCode TaoDestroy_LMVM(Tao tao)
202: {
203: TAO_LMVM *lmP = (TAO_LMVM *)tao->data;
207: if (tao->setupcalled) {
208: VecDestroy(&lmP->Xold);
209: VecDestroy(&lmP->Gold);
210: VecDestroy(&lmP->D);
211: MatDestroy(&lmP->M);
212: }
213: PetscFree(tao->data);
214: return(0);
215: }
217: /*------------------------------------------------------------*/
220: static PetscErrorCode TaoSetFromOptions_LMVM(Tao tao)
221: {
225: PetscOptionsHead("Limited-memory variable-metric method for unconstrained optimization");
226: TaoLineSearchSetFromOptions(tao->linesearch);
227: PetscOptionsTail();
228: return(0);
229: return(0);
230: }
232: /*------------------------------------------------------------*/
235: static PetscErrorCode TaoView_LMVM(Tao tao, PetscViewer viewer)
236: {
237: TAO_LMVM *lm = (TAO_LMVM *)tao->data;
238: PetscBool isascii;
242: PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii);
243: if (isascii) {
244: PetscViewerASCIIPushTab(viewer);
245: PetscViewerASCIIPrintf(viewer, "BFGS steps: %D\n", lm->bfgs);
246: PetscViewerASCIIPrintf(viewer, "Scaled gradient steps: %D\n", lm->sgrad);
247: PetscViewerASCIIPrintf(viewer, "Gradient steps: %D\n", lm->grad);
248: PetscViewerASCIIPopTab(viewer);
249: }
250: return(0);
251: }
253: /* ---------------------------------------------------------- */
255: /*MC
256: TAOLMVM - Limited Memory Variable Metric method is a quasi-Newton
257: optimization solver for unconstrained minimization. It solves
258: the Newton step
259: Hkdk = - gk
261: using an approximation Bk in place of Hk, where Bk is composed using
262: the BFGS update formula. A More-Thuente line search is then used
263: to computed the steplength in the dk direction
264: Options Database Keys:
265: + -tao_lmm_vectors - number of vectors to use for approximation
266: . -tao_lmm_scale_type - "none","scalar","broyden"
267: . -tao_lmm_limit_type - "none","average","relative","absolute"
268: . -tao_lmm_rescale_type - "none","scalar","gl"
269: . -tao_lmm_limit_mu - mu limiting factor
270: . -tao_lmm_limit_nu - nu limiting factor
271: . -tao_lmm_delta_min - minimum delta value
272: . -tao_lmm_delta_max - maximum delta value
273: . -tao_lmm_broyden_phi - phi factor for Broyden scaling
274: . -tao_lmm_scalar_alpha - alpha factor for scalar scaling
275: . -tao_lmm_rescale_alpha - alpha factor for rescaling diagonal
276: . -tao_lmm_rescale_beta - beta factor for rescaling diagonal
277: . -tao_lmm_scalar_history - amount of history for scalar scaling
278: . -tao_lmm_rescale_history - amount of history for rescaling diagonal
279: - -tao_lmm_eps - rejection tolerance
281: Level: beginner
282: M*/
284: EXTERN_C_BEGIN
287: PetscErrorCode TaoCreate_LMVM(Tao tao)
288: {
289: TAO_LMVM *lmP;
290: const char *morethuente_type = TAOLINESEARCHMT;
294: tao->ops->setup = TaoSetUp_LMVM;
295: tao->ops->solve = TaoSolve_LMVM;
296: tao->ops->view = TaoView_LMVM;
297: tao->ops->setfromoptions = TaoSetFromOptions_LMVM;
298: tao->ops->destroy = TaoDestroy_LMVM;
300: PetscNewLog(tao,&lmP);
301: lmP->D = 0;
302: lmP->M = 0;
303: lmP->Xold = 0;
304: lmP->Gold = 0;
306: tao->data = (void*)lmP;
307: tao->max_it = 2000;
308: tao->max_funcs = 4000;
309: tao->fatol = 1e-4;
310: tao->frtol = 1e-4;
312: TaoLineSearchCreate(((PetscObject)tao)->comm,&tao->linesearch);
313: TaoLineSearchSetType(tao->linesearch,morethuente_type);
314: TaoLineSearchUseTaoRoutines(tao->linesearch,tao);
315: return(0);
316: }
317: EXTERN_C_END