Actual source code: owarmijo.c

  1: #include <petsc/private/taolinesearchimpl.h>
  2: #include <../src/tao/linesearch/impls/owarmijo/owarmijo.h>

  4: #define REPLACE_FIFO 1
  5: #define REPLACE_MRU  2

  7: #define REFERENCE_MAX  1
  8: #define REFERENCE_AVE  2
  9: #define REFERENCE_MEAN 3

 11: static PetscErrorCode ProjWork_OWLQN(Vec w, Vec x, Vec gv, PetscReal *gdx)
 12: {
 13:   const PetscReal *xptr, *gptr;
 14:   PetscReal       *wptr;
 15:   PetscInt         low, high, low1, high1, low2, high2, i;

 17:   PetscFunctionBegin;
 18:   PetscCall(VecGetOwnershipRange(w, &low, &high));
 19:   PetscCall(VecGetOwnershipRange(x, &low1, &high1));
 20:   PetscCall(VecGetOwnershipRange(gv, &low2, &high2));

 22:   *gdx = 0.0;
 23:   PetscCall(VecGetArray(w, &wptr));
 24:   PetscCall(VecGetArrayRead(x, &xptr));
 25:   PetscCall(VecGetArrayRead(gv, &gptr));

 27:   for (i = 0; i < high - low; i++) {
 28:     if (xptr[i] * wptr[i] < 0.0) wptr[i] = 0.0;
 29:     *gdx = *gdx + gptr[i] * (wptr[i] - xptr[i]);
 30:   }
 31:   PetscCall(VecRestoreArray(w, &wptr));
 32:   PetscCall(VecRestoreArrayRead(x, &xptr));
 33:   PetscCall(VecRestoreArrayRead(gv, &gptr));
 34:   PetscFunctionReturn(PETSC_SUCCESS);
 35: }

 37: static PetscErrorCode TaoLineSearchDestroy_OWArmijo(TaoLineSearch ls)
 38: {
 39:   TaoLineSearch_OWARMIJO *armP = (TaoLineSearch_OWARMIJO *)ls->data;

 41:   PetscFunctionBegin;
 42:   PetscCall(PetscFree(armP->memory));
 43:   PetscCall(PetscObjectDereference((PetscObject)armP->x));
 44:   PetscCall(VecDestroy(&armP->work));
 45:   PetscCall(PetscFree(ls->data));
 46:   PetscFunctionReturn(PETSC_SUCCESS);
 47: }

 49: static PetscErrorCode TaoLineSearchSetFromOptions_OWArmijo(TaoLineSearch ls, PetscOptionItems PetscOptionsObject)
 50: {
 51:   TaoLineSearch_OWARMIJO *armP = (TaoLineSearch_OWARMIJO *)ls->data;

 53:   PetscFunctionBegin;
 54:   PetscOptionsHeadBegin(PetscOptionsObject, "OWArmijo linesearch options");
 55:   PetscCall(PetscOptionsReal("-tao_ls_OWArmijo_alpha", "initial reference constant", "", armP->alpha, &armP->alpha, NULL));
 56:   PetscCall(PetscOptionsReal("-tao_ls_OWArmijo_beta_inf", "decrease constant one", "", armP->beta_inf, &armP->beta_inf, NULL));
 57:   PetscCall(PetscOptionsReal("-tao_ls_OWArmijo_beta", "decrease constant", "", armP->beta, &armP->beta, NULL));
 58:   PetscCall(PetscOptionsReal("-tao_ls_OWArmijo_sigma", "acceptance constant", "", armP->sigma, &armP->sigma, NULL));
 59:   PetscCall(PetscOptionsInt("-tao_ls_OWArmijo_memory_size", "number of historical elements", "", armP->memorySize, &armP->memorySize, NULL));
 60:   PetscCall(PetscOptionsInt("-tao_ls_OWArmijo_reference_policy", "policy for updating reference value", "", armP->referencePolicy, &armP->referencePolicy, NULL));
 61:   PetscCall(PetscOptionsInt("-tao_ls_OWArmijo_replacement_policy", "policy for updating memory", "", armP->replacementPolicy, &armP->replacementPolicy, NULL));
 62:   PetscCall(PetscOptionsBool("-tao_ls_OWArmijo_nondescending", "Use nondescending OWArmijo algorithm", "", armP->nondescending, &armP->nondescending, NULL));
 63:   PetscOptionsHeadEnd();
 64:   PetscFunctionReturn(PETSC_SUCCESS);
 65: }

 67: static PetscErrorCode TaoLineSearchView_OWArmijo(TaoLineSearch ls, PetscViewer pv)
 68: {
 69:   TaoLineSearch_OWARMIJO *armP = (TaoLineSearch_OWARMIJO *)ls->data;
 70:   PetscBool               isascii;

 72:   PetscFunctionBegin;
 73:   PetscCall(PetscObjectTypeCompare((PetscObject)pv, PETSCVIEWERASCII, &isascii));
 74:   if (isascii) {
 75:     PetscCall(PetscViewerASCIIPrintf(pv, "  OWArmijo linesearch"));
 76:     if (armP->nondescending) PetscCall(PetscViewerASCIIPrintf(pv, " (nondescending)"));
 77:     PetscCall(PetscViewerASCIIPrintf(pv, ": alpha=%g beta=%g ", (double)armP->alpha, (double)armP->beta));
 78:     PetscCall(PetscViewerASCIIPrintf(pv, "sigma=%g ", (double)armP->sigma));
 79:     PetscCall(PetscViewerASCIIPrintf(pv, "memsize=%" PetscInt_FMT "\n", armP->memorySize));
 80:   }
 81:   PetscFunctionReturn(PETSC_SUCCESS);
 82: }

 84: /* @ TaoApply_OWArmijo - This routine performs a linesearch. It
 85:    backtracks until the (nonmonotone) OWArmijo conditions are satisfied.

 87:    Input Parameters:
 88: +  tao - TAO_SOLVER context
 89: .  X - current iterate (on output X contains new iterate, X + step*S)
 90: .  S - search direction
 91: .  f - merit function evaluated at X
 92: .  G - gradient of merit function evaluated at X
 93: .  W - work vector
 94: -  step - initial estimate of step length

 96:    Output parameters:
 97: +  f - merit function evaluated at new iterate, X + step*S
 98: .  G - gradient of merit function evaluated at new iterate, X + step*S
 99: .  X - new iterate
100: -  step - final step length

102:    Info is set to one of:
103: .   0 - the line search succeeds; the sufficient decrease
104:    condition and the directional derivative condition hold

106:    negative number if an input parameter is invalid
107: -   -1 -  step < 0

109:    positive number > 1 if the line search otherwise terminates
110: +    1 -  Step is at the lower bound, stepmin.
111: @ */
112: static PetscErrorCode TaoLineSearchApply_OWArmijo(TaoLineSearch ls, Vec x, PetscReal *f, Vec g, Vec s)
113: {
114:   TaoLineSearch_OWARMIJO *armP = (TaoLineSearch_OWARMIJO *)ls->data;
115:   PetscInt                i, its = 0;
116:   PetscReal               fact, ref, gdx;
117:   PetscInt                idx;
118:   PetscBool               g_computed = PETSC_FALSE; /* to prevent extra gradient computation */
119:   Vec                     g_old;
120:   PetscReal               owlqn_minstep = 0.005;
121:   MPI_Comm                comm;

123:   PetscFunctionBegin;
124:   PetscCall(PetscObjectGetComm((PetscObject)ls, &comm));
125:   fact       = 0.0;
126:   ls->nfeval = 0;
127:   ls->reason = TAOLINESEARCH_CONTINUE_ITERATING;
128:   if (!armP->work) {
129:     PetscCall(VecDuplicate(x, &armP->work));
130:     armP->x = x;
131:     PetscCall(PetscObjectReference((PetscObject)armP->x));
132:   } else if (x != armP->x) {
133:     PetscCall(VecDestroy(&armP->work));
134:     PetscCall(VecDuplicate(x, &armP->work));
135:     PetscCall(PetscObjectDereference((PetscObject)armP->x));
136:     armP->x = x;
137:     PetscCall(PetscObjectReference((PetscObject)armP->x));
138:   }

140:   PetscCall(TaoLineSearchMonitor(ls, 0, *f, 0.0));

142:   /* Check linesearch parameters */
143:   if (armP->alpha < 1) {
144:     PetscCall(PetscInfo(ls, "OWArmijo line search error: alpha (%g) < 1\n", (double)armP->alpha));
145:     ls->reason = TAOLINESEARCH_FAILED_BADPARAMETER;
146:   } else if ((armP->beta <= 0) || (armP->beta >= 1)) {
147:     PetscCall(PetscInfo(ls, "OWArmijo line search error: beta (%g) invalid\n", (double)armP->beta));
148:     ls->reason = TAOLINESEARCH_FAILED_BADPARAMETER;
149:   } else if ((armP->beta_inf <= 0) || (armP->beta_inf >= 1)) {
150:     PetscCall(PetscInfo(ls, "OWArmijo line search error: beta_inf (%g) invalid\n", (double)armP->beta_inf));
151:     ls->reason = TAOLINESEARCH_FAILED_BADPARAMETER;
152:   } else if ((armP->sigma <= 0) || (armP->sigma >= 0.5)) {
153:     PetscCall(PetscInfo(ls, "OWArmijo line search error: sigma (%g) invalid\n", (double)armP->sigma));
154:     ls->reason = TAOLINESEARCH_FAILED_BADPARAMETER;
155:   } else if (armP->memorySize < 1) {
156:     PetscCall(PetscInfo(ls, "OWArmijo line search error: memory_size (%" PetscInt_FMT ") < 1\n", armP->memorySize));
157:     ls->reason = TAOLINESEARCH_FAILED_BADPARAMETER;
158:   } else if ((armP->referencePolicy != REFERENCE_MAX) && (armP->referencePolicy != REFERENCE_AVE) && (armP->referencePolicy != REFERENCE_MEAN)) {
159:     PetscCall(PetscInfo(ls, "OWArmijo line search error: reference_policy invalid\n"));
160:     ls->reason = TAOLINESEARCH_FAILED_BADPARAMETER;
161:   } else if ((armP->replacementPolicy != REPLACE_FIFO) && (armP->replacementPolicy != REPLACE_MRU)) {
162:     PetscCall(PetscInfo(ls, "OWArmijo line search error: replacement_policy invalid\n"));
163:     ls->reason = TAOLINESEARCH_FAILED_BADPARAMETER;
164:   } else if (PetscIsInfOrNanReal(*f)) {
165:     PetscCall(PetscInfo(ls, "OWArmijo line search error: initial function inf or nan\n"));
166:     ls->reason = TAOLINESEARCH_FAILED_BADPARAMETER;
167:   }

169:   if (ls->reason != TAOLINESEARCH_CONTINUE_ITERATING) PetscFunctionReturn(PETSC_SUCCESS);

171:   /* Check to see of the memory has been allocated.  If not, allocate
172:      the historical array and populate it with the initial function
173:      values. */
174:   if (!armP->memory) PetscCall(PetscMalloc1(armP->memorySize, &armP->memory));

176:   if (!armP->memorySetup) {
177:     for (i = 0; i < armP->memorySize; i++) armP->memory[i] = armP->alpha * (*f);
178:     armP->current       = 0;
179:     armP->lastReference = armP->memory[0];
180:     armP->memorySetup   = PETSC_TRUE;
181:   }

183:   /* Calculate reference value (MAX) */
184:   ref = armP->memory[0];
185:   idx = 0;

187:   for (i = 1; i < armP->memorySize; i++) {
188:     if (armP->memory[i] > ref) {
189:       ref = armP->memory[i];
190:       idx = i;
191:     }
192:   }

194:   if (armP->referencePolicy == REFERENCE_AVE) {
195:     ref = 0;
196:     for (i = 0; i < armP->memorySize; i++) ref += armP->memory[i];
197:     ref = ref / armP->memorySize;
198:     ref = PetscMax(ref, armP->memory[armP->current]);
199:   } else if (armP->referencePolicy == REFERENCE_MEAN) {
200:     ref = PetscMin(ref, 0.5 * (armP->lastReference + armP->memory[armP->current]));
201:   }

203:   if (armP->nondescending) fact = armP->sigma;

205:   PetscCall(VecDuplicate(g, &g_old));
206:   PetscCall(VecCopy(g, g_old));

208:   ls->step = ls->initstep;
209:   while (ls->step >= owlqn_minstep && ls->nfeval < ls->max_funcs) {
210:     /* Calculate iterate */
211:     ++its;
212:     PetscCall(VecWAXPY(armP->work, ls->step, s, x));

214:     gdx = 0.0;
215:     PetscCall(ProjWork_OWLQN(armP->work, x, g_old, &gdx));
216:     PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, &gdx, 1, MPIU_REAL, MPIU_SUM, comm));

218:     /* Check the condition of gdx */
219:     if (PetscIsInfOrNanReal(gdx)) {
220:       PetscCall(PetscInfo(ls, "Initial Line Search step * g is infinity or NaN (%g)\n", (double)gdx));
221:       ls->reason = TAOLINESEARCH_FAILED_INFORNAN;
222:       PetscFunctionReturn(PETSC_SUCCESS);
223:     }
224:     if (gdx >= 0.0) {
225:       PetscCall(PetscInfo(ls, "Initial Line Search step is not descent direction (g's=%g)\n", (double)gdx));
226:       ls->reason = TAOLINESEARCH_FAILED_ASCENT;
227:       PetscFunctionReturn(PETSC_SUCCESS);
228:     }

230:     /* Calculate function at new iterate */
231:     PetscCall(VecLockReadPush(x));
232:     PetscCall(TaoLineSearchComputeObjectiveAndGradient(ls, armP->work, f, g));
233:     PetscCall(VecLockReadPop(x));
234:     g_computed = PETSC_TRUE;

236:     PetscCall(TaoLineSearchMonitor(ls, its, *f, ls->step));

238:     if (ls->step == ls->initstep) ls->f_fullstep = *f;

240:     if (PetscIsInfOrNanReal(*f)) {
241:       ls->step *= armP->beta_inf;
242:     } else {
243:       /* Check descent condition */
244:       if (armP->nondescending && *f <= ref - ls->step * fact * ref) break;
245:       if (!armP->nondescending && *f <= ref + armP->sigma * gdx) break;
246:       ls->step *= armP->beta;
247:     }
248:   }
249:   PetscCall(VecDestroy(&g_old));

251:   /* Check termination */
252:   if (PetscIsInfOrNanReal(*f)) {
253:     PetscCall(PetscInfo(ls, "Function is inf or nan.\n"));
254:     ls->reason = TAOLINESEARCH_FAILED_BADPARAMETER;
255:   } else if (ls->step < owlqn_minstep) {
256:     PetscCall(PetscInfo(ls, "Step length is below tolerance.\n"));
257:     ls->reason = TAOLINESEARCH_HALTED_RTOL;
258:   } else if (ls->nfeval >= ls->max_funcs) {
259:     PetscCall(PetscInfo(ls, "Number of line search function evals (%" PetscInt_FMT ") > maximum allowed (%" PetscInt_FMT ")\n", ls->nfeval, ls->max_funcs));
260:     ls->reason = TAOLINESEARCH_HALTED_MAXFCN;
261:   }
262:   if (ls->reason) PetscFunctionReturn(PETSC_SUCCESS);

264:   /* Successful termination, update memory */
265:   ls->reason          = TAOLINESEARCH_SUCCESS;
266:   armP->lastReference = ref;
267:   if (armP->replacementPolicy == REPLACE_FIFO) {
268:     armP->memory[armP->current++] = *f;
269:     if (armP->current >= armP->memorySize) armP->current = 0;
270:   } else {
271:     armP->current     = idx;
272:     armP->memory[idx] = *f;
273:   }

275:   /* Update iterate and compute gradient */
276:   PetscCall(VecCopy(armP->work, x));
277:   if (!g_computed) PetscCall(TaoLineSearchComputeGradient(ls, x, g));
278:   PetscCall(PetscInfo(ls, "%" PetscInt_FMT " function evals in line search, step = %10.4f\n", ls->nfeval, (double)ls->step));
279:   PetscFunctionReturn(PETSC_SUCCESS);
280: }

282: /*MC
283:    TAOLINESEARCHOWARMIJO - Special line-search type for the Orthant-Wise Limited Quasi-Newton (`TAOOWLQN`) algorithm.
284:    Should not be used with any other algorithm.

286:    Level: developer

288: .seealso: `TaoLineSearch`, `TAOOWLQN`, `Tao`
289: M*/
290: PETSC_EXTERN PetscErrorCode TaoLineSearchCreate_OWArmijo(TaoLineSearch ls)
291: {
292:   TaoLineSearch_OWARMIJO *armP;

294:   PetscFunctionBegin;
296:   PetscCall(PetscNew(&armP));

298:   armP->memory            = NULL;
299:   armP->alpha             = 1.0;
300:   armP->beta              = 0.25;
301:   armP->beta_inf          = 0.25;
302:   armP->sigma             = 1e-4;
303:   armP->memorySize        = 1;
304:   armP->referencePolicy   = REFERENCE_MAX;
305:   armP->replacementPolicy = REPLACE_MRU;
306:   armP->nondescending     = PETSC_FALSE;
307:   ls->data                = (void *)armP;
308:   ls->initstep            = 0.1;
309:   ls->ops->monitor        = NULL;
310:   ls->ops->setup          = NULL;
311:   ls->ops->reset          = NULL;
312:   ls->ops->apply          = TaoLineSearchApply_OWArmijo;
313:   ls->ops->view           = TaoLineSearchView_OWArmijo;
314:   ls->ops->destroy        = TaoLineSearchDestroy_OWArmijo;
315:   ls->ops->setfromoptions = TaoLineSearchSetFromOptions_OWArmijo;
316:   PetscFunctionReturn(PETSC_SUCCESS);
317: }