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:   if (armP->x) 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:   PetscReal               partgdx;
122:   MPI_Comm                comm;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

285:    Level: developer

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

293:   PetscFunctionBegin;
295:   PetscCall(PetscNew(&armP));

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