xref: /petsc/src/tao/linesearch/impls/armijo/armijo.c (revision 47a470077471b0d3349434564c5c37e6b0b316e3)
1 #include "petscvec.h"
2 #include "taosolver.h"
3 #include "tao-private/taolinesearch_impl.h"
4 #include "armijo.h"
5 
6 #define REPLACE_FIFO 1
7 #define REPLACE_MRU  2
8 
9 #define REFERENCE_MAX  1
10 #define REFERENCE_AVE  2
11 #define REFERENCE_MEAN 3
12 
13 #undef __FUNCT__
14 #define __FUNCT__ "TaoLineSearchDestroy_Armijo"
15 static PetscErrorCode TaoLineSearchDestroy_Armijo(TaoLineSearch ls)
16 {
17   TAOLINESEARCH_ARMIJO_CTX *armP = (TAOLINESEARCH_ARMIJO_CTX *)ls->data;
18   PetscErrorCode           ierr;
19 
20   PetscFunctionBegin;
21   if (armP->memory != PETSC_NULL) {
22     ierr = PetscFree(armP->memory);CHKERRQ(ierr);
23     armP->memory = PETSC_NULL;
24   }
25   if (armP->x) {
26     ierr = PetscObjectDereference((PetscObject)armP->x);CHKERRQ(ierr);
27   }
28   ierr = VecDestroy(&armP->work);CHKERRQ(ierr);
29   ierr = PetscFree(ls->data);CHKERRQ(ierr);
30   ls->data = PETSC_NULL;
31   PetscFunctionReturn(0);
32 }
33 
34 #undef __FUNCT__
35 #define __FUNCT__ "TaoLineSearchReset_Armijo"
36 static PetscErrorCode TaoLineSearchReset_Armijo(TaoLineSearch ls)
37 {
38   TAOLINESEARCH_ARMIJO_CTX *armP = (TAOLINESEARCH_ARMIJO_CTX *)ls->data;
39   PetscErrorCode           ierr;
40 
41   PetscFunctionBegin;
42   if (armP->memory != PETSC_NULL) {
43     ierr = PetscFree(armP->memory);CHKERRQ(ierr);
44     armP->memory = PETSC_NULL;
45   }
46   armP->memorySetup = PETSC_FALSE;
47   PetscFunctionReturn(0);
48 }
49 
50 #undef __FUNCT__
51 #define __FUNCT__ "TaoLineSearchSetFromOptions_Armijo"
52 static PetscErrorCode TaoLineSearchSetFromOptions_Armijo(TaoLineSearch ls)
53 {
54   TAOLINESEARCH_ARMIJO_CTX *armP = (TAOLINESEARCH_ARMIJO_CTX *)ls->data;
55   PetscErrorCode           ierr;
56 
57   PetscFunctionBegin;
58   ierr = PetscOptionsHead("Armijo linesearch options");CHKERRQ(ierr);
59   ierr = PetscOptionsReal("-tao_ls_armijo_alpha", "initial reference constant", "", armP->alpha, &armP->alpha, 0);CHKERRQ(ierr);
60   ierr = PetscOptionsReal("-tao_ls_armijo_beta_inf", "decrease constant one", "", armP->beta_inf, &armP->beta_inf, 0);CHKERRQ(ierr);
61   ierr = PetscOptionsReal("-tao_ls_armijo_beta", "decrease constant", "", armP->beta, &armP->beta, 0);CHKERRQ(ierr);
62   ierr = PetscOptionsReal("-tao_ls_armijo_sigma", "acceptance constant", "", armP->sigma, &armP->sigma, 0);CHKERRQ(ierr);
63   ierr = PetscOptionsInt("-tao_ls_armijo_memory_size", "number of historical elements", "", armP->memorySize, &armP->memorySize, 0);CHKERRQ(ierr);
64   ierr = PetscOptionsInt("-tao_ls_armijo_reference_policy", "policy for updating reference value", "", armP->referencePolicy, &armP->referencePolicy, 0);CHKERRQ(ierr);
65   ierr = PetscOptionsInt("-tao_ls_armijo_replacement_policy", "policy for updating memory", "", armP->replacementPolicy, &armP->replacementPolicy, 0);CHKERRQ(ierr);
66   ierr = PetscOptionsBool("-tao_ls_armijo_nondescending","Use nondescending armijo algorithm","",armP->nondescending,&armP->nondescending, 0);CHKERRQ(ierr);
67   ierr = PetscOptionsTail();CHKERRQ(ierr);
68   PetscFunctionReturn(0);
69 }
70 
71 #undef __FUNCT__
72 #define __FUNCT__ "TaoLineSearchView_Armijo"
73 static PetscErrorCode TaoLineSearchView_Armijo(TaoLineSearch ls, PetscViewer pv)
74 {
75   TAOLINESEARCH_ARMIJO_CTX *armP = (TAOLINESEARCH_ARMIJO_CTX *)ls->data;
76   PetscBool                isascii;
77   PetscErrorCode           ierr;
78 
79   PetscFunctionBegin;
80   ierr = PetscObjectTypeCompare((PetscObject)pv, PETSCVIEWERASCII, &isascii);CHKERRQ(ierr);
81   if (isascii) {
82     ierr = PetscViewerASCIIPrintf(pv,"  maxf=%D, ftol=%g, gtol=%g\n",ls->max_funcs, (double)ls->rtol, (double)ls->ftol);CHKERRQ(ierr);
83     ierr=PetscViewerASCIIPrintf(pv,"  Armijo linesearch",armP->alpha);CHKERRQ(ierr);
84     if (armP->nondescending) {
85       ierr = PetscViewerASCIIPrintf(pv, " (nondescending)");CHKERRQ(ierr);
86     }
87     if (ls->bounded) {
88       ierr = PetscViewerASCIIPrintf(pv," (projected)");CHKERRQ(ierr);
89     }
90     ierr=PetscViewerASCIIPrintf(pv,": alpha=%g beta=%g ",(double)armP->alpha,(double)armP->beta);CHKERRQ(ierr);
91     ierr=PetscViewerASCIIPrintf(pv,"sigma=%g ",(double)armP->sigma);CHKERRQ(ierr);
92     ierr=PetscViewerASCIIPrintf(pv,"memsize=%D\n",armP->memorySize);CHKERRQ(ierr);
93   }
94   PetscFunctionReturn(0);
95 }
96 
97 #undef __FUNCT__
98 #define __FUNCT__ "TaoLineSearchApply_Armijo"
99 /* @ TaoApply_Armijo - This routine performs a linesearch. It
100    backtracks until the (nonmonotone) Armijo conditions are satisfied.
101 
102    Input Parameters:
103 +  tao - TaoSolver context
104 .  X - current iterate (on output X contains new iterate, X + step*S)
105 .  S - search direction
106 .  f - merit function evaluated at X
107 .  G - gradient of merit function evaluated at X
108 .  W - work vector
109 -  step - initial estimate of step length
110 
111    Output parameters:
112 +  f - merit function evaluated at new iterate, X + step*S
113 .  G - gradient of merit function evaluated at new iterate, X + step*S
114 .  X - new iterate
115 -  step - final step length
116 
117    Info is set to one of:
118 .   0 - the line search succeeds; the sufficient decrease
119    condition and the directional derivative condition hold
120 
121    negative number if an input parameter is invalid
122 -   -1 -  step < 0
123 
124    positive number > 1 if the line search otherwise terminates
125 +    1 -  Step is at the lower bound, stepmin.
126 @ */
127 static PetscErrorCode TaoLineSearchApply_Armijo(TaoLineSearch ls, Vec x, PetscReal *f, Vec g, Vec s)
128 {
129   TAOLINESEARCH_ARMIJO_CTX *armP = (TAOLINESEARCH_ARMIJO_CTX *)ls->data;
130   PetscErrorCode           ierr;
131   PetscInt                 i;
132   PetscReal                fact, ref, gdx;
133   PetscInt                 idx;
134   PetscBool                g_computed=PETSC_FALSE; /* to prevent extra gradient computation */
135 
136   PetscFunctionBegin;
137 
138   ls->reason = TAOLINESEARCH_CONTINUE_ITERATING;
139   if (!armP->work) {
140     ierr = VecDuplicate(x,&armP->work);CHKERRQ(ierr);
141     armP->x = x;
142     ierr = PetscObjectReference((PetscObject)armP->x);CHKERRQ(ierr);
143   } else if (x != armP->x) {
144     /* If x has changed, then recreate work */
145     ierr = VecDestroy(&armP->work);CHKERRQ(ierr);
146     ierr = VecDuplicate(x,&armP->work);CHKERRQ(ierr);
147     ierr = PetscObjectDereference((PetscObject)armP->x);CHKERRQ(ierr);
148     armP->x = x;
149     ierr = PetscObjectReference((PetscObject)armP->x);CHKERRQ(ierr);
150   }
151 
152   /* Check linesearch parameters */
153   if (armP->alpha < 1) {
154     ierr = PetscInfo1(ls,"Armijo line search error: alpha (%g) < 1\n", (double)armP->alpha);CHKERRQ(ierr);
155     ls->reason=TAOLINESEARCH_FAILED_BADPARAMETER;
156   } else if ((armP->beta <= 0) || (armP->beta >= 1)) {
157     ierr = PetscInfo1(ls,"Armijo line search error: beta (%g) invalid\n", (double)armP->beta);CHKERRQ(ierr);
158     ls->reason=TAOLINESEARCH_FAILED_BADPARAMETER;
159   } else if ((armP->beta_inf <= 0) || (armP->beta_inf >= 1)) {
160     ierr = PetscInfo1(ls,"Armijo line search error: beta_inf (%g) invalid\n", (double)armP->beta_inf);CHKERRQ(ierr);
161     ls->reason=TAOLINESEARCH_FAILED_BADPARAMETER;
162   } else if ((armP->sigma <= 0) || (armP->sigma >= 0.5)) {
163     ierr = PetscInfo1(ls,"Armijo line search error: sigma (%g) invalid\n", (double)armP->sigma);CHKERRQ(ierr);
164     ls->reason=TAOLINESEARCH_FAILED_BADPARAMETER;
165   } else if (armP->memorySize < 1) {
166     ierr = PetscInfo1(ls,"Armijo line search error: memory_size (%D) < 1\n", armP->memorySize);CHKERRQ(ierr);
167     ls->reason=TAOLINESEARCH_FAILED_BADPARAMETER;
168   } else if ((armP->referencePolicy != REFERENCE_MAX) && (armP->referencePolicy != REFERENCE_AVE) && (armP->referencePolicy != REFERENCE_MEAN)) {
169     ierr = PetscInfo(ls,"Armijo line search error: reference_policy invalid\n");CHKERRQ(ierr);
170     ls->reason=TAOLINESEARCH_FAILED_BADPARAMETER;
171   } else if ((armP->replacementPolicy != REPLACE_FIFO) && (armP->replacementPolicy != REPLACE_MRU)) {
172     ierr = PetscInfo(ls,"Armijo line search error: replacement_policy invalid\n");CHKERRQ(ierr);
173     ls->reason=TAOLINESEARCH_FAILED_BADPARAMETER;
174   } else if (PetscIsInfOrNanReal(*f)) {
175     ierr = PetscInfo(ls,"Armijo line search error: initial function inf or nan\n");CHKERRQ(ierr);
176     ls->reason=TAOLINESEARCH_FAILED_BADPARAMETER;
177   }
178 
179   if (ls->reason != TAOLINESEARCH_CONTINUE_ITERATING) {
180     PetscFunctionReturn(0);
181   }
182 
183   /* Check to see of the memory has been allocated.  If not, allocate
184      the historical array and populate it with the initial function
185      values. */
186   if (armP->memory == PETSC_NULL) {
187     ierr = PetscMalloc(sizeof(PetscReal)*armP->memorySize, &armP->memory );CHKERRQ(ierr);
188   }
189 
190   if (!armP->memorySetup) {
191     for (i = 0; i < armP->memorySize; i++) {
192       armP->memory[i] = armP->alpha*(*f);
193     }
194 
195     armP->current = 0;
196     armP->lastReference = armP->memory[0];
197     armP->memorySetup=PETSC_TRUE;
198   }
199 
200   /* Calculate reference value (MAX) */
201   ref = armP->memory[0];
202   idx = 0;
203 
204   for (i = 1; i < armP->memorySize; i++) {
205     if (armP->memory[i] > ref) {
206       ref = armP->memory[i];
207       idx = i;
208     }
209   }
210 
211   if (armP->referencePolicy == REFERENCE_AVE) {
212     ref = 0;
213     for (i = 0; i < armP->memorySize; i++) {
214       ref += armP->memory[i];
215     }
216     ref = ref / armP->memorySize;
217     ref = PetscMax(ref, armP->memory[armP->current]);
218   } else if (armP->referencePolicy == REFERENCE_MEAN) {
219     ref = PetscMin(ref, 0.5*(armP->lastReference + armP->memory[armP->current]));
220   }
221   ierr = VecDot(g,s,&gdx);CHKERRQ(ierr);
222 
223   if (PetscIsInfOrNanReal(gdx)) {
224     ierr = PetscInfo1(ls,"Initial Line Search step * g is Inf or Nan (%g)\n",(double)gdx);CHKERRQ(ierr);
225     ls->reason=TAOLINESEARCH_FAILED_INFORNAN;
226     PetscFunctionReturn(0);
227   }
228   if (gdx >= 0.0) {
229     ierr = PetscInfo1(ls,"Initial Line Search step is not descent direction (g's=%g)\n",(double)gdx);CHKERRQ(ierr);
230     ls->reason = TAOLINESEARCH_FAILED_ASCENT;
231     PetscFunctionReturn(0);
232   }
233 
234   if (armP->nondescending) {
235     fact = armP->sigma;
236   } else {
237     fact = armP->sigma * gdx;
238   }
239   ls->step = ls->initstep;
240   while (ls->step >= ls->stepmin && (ls->nfeval+ls->nfgeval) < ls->max_funcs) {
241     /* Calculate iterate */
242     ierr = VecCopy(x,armP->work);CHKERRQ(ierr);
243     ierr = VecAXPY(armP->work,ls->step,s);CHKERRQ(ierr);
244     if (ls->bounded) {
245       ierr = VecMedian(ls->lower,armP->work,ls->upper,armP->work);CHKERRQ(ierr);
246     }
247 
248     /* Calculate function at new iterate */
249     if (ls->hasobjective) {
250       ierr = TaoLineSearchComputeObjective(ls,armP->work,f);CHKERRQ(ierr);
251       g_computed=PETSC_FALSE;
252     } else if (ls->usegts) {
253       ierr = TaoLineSearchComputeObjectiveAndGTS(ls,armP->work,f,&gdx);CHKERRQ(ierr);
254       g_computed=PETSC_FALSE;
255     } else {
256       ierr = TaoLineSearchComputeObjectiveAndGradient(ls,armP->work,f,g);CHKERRQ(ierr);
257       g_computed=PETSC_TRUE;
258     }
259     if (ls->step == ls->initstep) {
260       ls->f_fullstep = *f;
261     }
262 
263     if (PetscIsInfOrNanReal(*f)) {
264       ls->step *= armP->beta_inf;
265     } else {
266       /* Check descent condition */
267       if (armP->nondescending && *f <= ref - ls->step*fact*ref)
268 	break;
269       if (!armP->nondescending && *f <= ref + ls->step*fact) {
270         break;
271       }
272 
273       ls->step *= armP->beta;
274     }
275   }
276 
277   /* Check termination */
278   if (PetscIsInfOrNanReal(*f)) {
279     ierr = PetscInfo(ls, "Function is inf or nan.\n");CHKERRQ(ierr);
280     ls->reason = TAOLINESEARCH_FAILED_INFORNAN;
281   } else if (ls->step < ls->stepmin) {
282     ierr = PetscInfo(ls, "Step length is below tolerance.\n");CHKERRQ(ierr);
283     ls->reason = TAOLINESEARCH_HALTED_RTOL;
284   } else if ((ls->nfeval+ls->nfgeval) >= ls->max_funcs) {
285     ierr = PetscInfo2(ls, "Number of line search function evals (%D) > maximum allowed (%D)\n",ls->nfeval+ls->nfgeval, ls->max_funcs);CHKERRQ(ierr);
286     ls->reason = TAOLINESEARCH_HALTED_MAXFCN;
287   }
288   if (ls->reason) {
289     PetscFunctionReturn(0);
290   }
291 
292   /* Successful termination, update memory */
293   armP->lastReference = ref;
294   if (armP->replacementPolicy == REPLACE_FIFO) {
295     armP->memory[armP->current++] = *f;
296     if (armP->current >= armP->memorySize) {
297       armP->current = 0;
298     }
299   } else {
300     armP->current = idx;
301     armP->memory[idx] = *f;
302   }
303 
304   /* Update iterate and compute gradient */
305   ierr = VecCopy(armP->work,x);CHKERRQ(ierr);
306   if (!g_computed) {
307     ierr = TaoLineSearchComputeGradient(ls, x, g);CHKERRQ(ierr);
308   }
309   ierr = PetscInfo2(ls, "%D function evals in line search, step = %g\n",ls->nfeval, (double)ls->step);CHKERRQ(ierr);
310   PetscFunctionReturn(0);
311 }
312 
313 EXTERN_C_BEGIN
314 #undef __FUNCT__
315 #define __FUNCT__ "TaoLineSearchCreate_Armijo"
316 PetscErrorCode TaoLineSearchCreate_Armijo(TaoLineSearch ls)
317 {
318   TAOLINESEARCH_ARMIJO_CTX *armP;
319   PetscErrorCode           ierr;
320 
321   PetscFunctionBegin;
322   PetscValidHeaderSpecific(ls,TAOLINESEARCH_CLASSID,1);
323   ierr = PetscNewLog(ls,&armP);CHKERRQ(ierr);
324 
325   armP->memory = PETSC_NULL;
326   armP->alpha = 1.0;
327   armP->beta = 0.5;
328   armP->beta_inf = 0.5;
329   armP->sigma = 1e-4;
330   armP->memorySize = 1;
331   armP->referencePolicy = REFERENCE_MAX;
332   armP->replacementPolicy = REPLACE_MRU;
333   armP->nondescending=PETSC_FALSE;
334   ls->data = (void*)armP;
335   ls->initstep=1.0;
336   ls->ops->setup=0;
337   ls->ops->apply=TaoLineSearchApply_Armijo;
338   ls->ops->view = TaoLineSearchView_Armijo;
339   ls->ops->destroy = TaoLineSearchDestroy_Armijo;
340   ls->ops->reset = TaoLineSearchReset_Armijo;
341   ls->ops->setfromoptions = TaoLineSearchSetFromOptions_Armijo;
342   PetscFunctionReturn(0);
343 }
344 EXTERN_C_END
345