1 #include <petsc/private/taolinesearchimpl.h> 2 #include <../src/tao/linesearch/impls/owarmijo/owarmijo.h> 3 4 #define REPLACE_FIFO 1 5 #define REPLACE_MRU 2 6 7 #define REFERENCE_MAX 1 8 #define REFERENCE_AVE 2 9 #define REFERENCE_MEAN 3 10 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; 16 17 PetscFunctionBegin; 18 PetscCall(VecGetOwnershipRange(w, &low, &high)); 19 PetscCall(VecGetOwnershipRange(x, &low1, &high1)); 20 PetscCall(VecGetOwnershipRange(gv, &low2, &high2)); 21 22 *gdx = 0.0; 23 PetscCall(VecGetArray(w, &wptr)); 24 PetscCall(VecGetArrayRead(x, &xptr)); 25 PetscCall(VecGetArrayRead(gv, &gptr)); 26 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 } 36 37 static PetscErrorCode TaoLineSearchDestroy_OWArmijo(TaoLineSearch ls) 38 { 39 TaoLineSearch_OWARMIJO *armP = (TaoLineSearch_OWARMIJO *)ls->data; 40 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 } 48 49 static PetscErrorCode TaoLineSearchSetFromOptions_OWArmijo(TaoLineSearch ls, PetscOptionItems PetscOptionsObject) 50 { 51 TaoLineSearch_OWARMIJO *armP = (TaoLineSearch_OWARMIJO *)ls->data; 52 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 } 66 67 static PetscErrorCode TaoLineSearchView_OWArmijo(TaoLineSearch ls, PetscViewer pv) 68 { 69 TaoLineSearch_OWARMIJO *armP = (TaoLineSearch_OWARMIJO *)ls->data; 70 PetscBool isascii; 71 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 } 83 84 /* @ TaoApply_OWArmijo - This routine performs a linesearch. It 85 backtracks until the (nonmonotone) OWArmijo conditions are satisfied. 86 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 95 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 101 102 Info is set to one of: 103 . 0 - the line search succeeds; the sufficient decrease 104 condition and the directional derivative condition hold 105 106 negative number if an input parameter is invalid 107 - -1 - step < 0 108 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; 123 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 } 140 141 PetscCall(TaoLineSearchMonitor(ls, 0, *f, 0.0)); 142 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 } 169 170 if (ls->reason != TAOLINESEARCH_CONTINUE_ITERATING) PetscFunctionReturn(PETSC_SUCCESS); 171 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)); 176 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 } 183 184 /* Calculate reference value (MAX) */ 185 ref = armP->memory[0]; 186 idx = 0; 187 188 for (i = 1; i < armP->memorySize; i++) { 189 if (armP->memory[i] > ref) { 190 ref = armP->memory[i]; 191 idx = i; 192 } 193 } 194 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 } 203 204 if (armP->nondescending) fact = armP->sigma; 205 206 PetscCall(VecDuplicate(g, &g_old)); 207 PetscCall(VecCopy(g, g_old)); 208 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)); 214 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)); 218 219 /* Check the condition of gdx */ 220 if (PetscIsInfOrNanReal(gdx)) { 221 PetscCall(PetscInfo(ls, "Initial Line Search step * g is infinity 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 } 230 231 /* Calculate function at new iterate */ 232 PetscCall(VecLockReadPush(x)); 233 PetscCall(TaoLineSearchComputeObjectiveAndGradient(ls, armP->work, f, g)); 234 PetscCall(VecLockReadPop(x)); 235 g_computed = PETSC_TRUE; 236 237 PetscCall(TaoLineSearchMonitor(ls, its, *f, ls->step)); 238 239 if (ls->step == ls->initstep) ls->f_fullstep = *f; 240 241 if (PetscIsInfOrNanReal(*f)) { 242 ls->step *= armP->beta_inf; 243 } else { 244 /* Check descent condition */ 245 if (armP->nondescending && *f <= ref - ls->step * fact * ref) break; 246 if (!armP->nondescending && *f <= ref + armP->sigma * gdx) break; 247 ls->step *= armP->beta; 248 } 249 } 250 PetscCall(VecDestroy(&g_old)); 251 252 /* Check termination */ 253 if (PetscIsInfOrNanReal(*f)) { 254 PetscCall(PetscInfo(ls, "Function is inf or nan.\n")); 255 ls->reason = TAOLINESEARCH_FAILED_BADPARAMETER; 256 } else if (ls->step < owlqn_minstep) { 257 PetscCall(PetscInfo(ls, "Step length is below tolerance.\n")); 258 ls->reason = TAOLINESEARCH_HALTED_RTOL; 259 } else if (ls->nfeval >= ls->max_funcs) { 260 PetscCall(PetscInfo(ls, "Number of line search function evals (%" PetscInt_FMT ") > maximum allowed (%" PetscInt_FMT ")\n", ls->nfeval, ls->max_funcs)); 261 ls->reason = TAOLINESEARCH_HALTED_MAXFCN; 262 } 263 if (ls->reason) PetscFunctionReturn(PETSC_SUCCESS); 264 265 /* Successful termination, update memory */ 266 ls->reason = TAOLINESEARCH_SUCCESS; 267 armP->lastReference = ref; 268 if (armP->replacementPolicy == REPLACE_FIFO) { 269 armP->memory[armP->current++] = *f; 270 if (armP->current >= armP->memorySize) armP->current = 0; 271 } else { 272 armP->current = idx; 273 armP->memory[idx] = *f; 274 } 275 276 /* Update iterate and compute gradient */ 277 PetscCall(VecCopy(armP->work, x)); 278 if (!g_computed) PetscCall(TaoLineSearchComputeGradient(ls, x, g)); 279 PetscCall(PetscInfo(ls, "%" PetscInt_FMT " function evals in line search, step = %10.4f\n", ls->nfeval, (double)ls->step)); 280 PetscFunctionReturn(PETSC_SUCCESS); 281 } 282 283 /*MC 284 TAOLINESEARCHOWARMIJO - Special line-search type for the Orthant-Wise Limited Quasi-Newton (`TAOOWLQN`) algorithm. 285 Should not be used with any other algorithm. 286 287 Level: developer 288 289 .seealso: `TaoLineSearch`, `TAOOWLQN`, `Tao` 290 M*/ 291 PETSC_EXTERN PetscErrorCode TaoLineSearchCreate_OWArmijo(TaoLineSearch ls) 292 { 293 TaoLineSearch_OWARMIJO *armP; 294 295 PetscFunctionBegin; 296 PetscValidHeaderSpecific(ls, TAOLINESEARCH_CLASSID, 1); 297 PetscCall(PetscNew(&armP)); 298 299 armP->memory = NULL; 300 armP->alpha = 1.0; 301 armP->beta = 0.25; 302 armP->beta_inf = 0.25; 303 armP->sigma = 1e-4; 304 armP->memorySize = 1; 305 armP->referencePolicy = REFERENCE_MAX; 306 armP->replacementPolicy = REPLACE_MRU; 307 armP->nondescending = PETSC_FALSE; 308 ls->data = (void *)armP; 309 ls->initstep = 0.1; 310 ls->ops->monitor = NULL; 311 ls->ops->setup = NULL; 312 ls->ops->reset = NULL; 313 ls->ops->apply = TaoLineSearchApply_OWArmijo; 314 ls->ops->view = TaoLineSearchView_OWArmijo; 315 ls->ops->destroy = TaoLineSearchDestroy_OWArmijo; 316 ls->ops->setfromoptions = TaoLineSearchSetFromOptions_OWArmijo; 317 PetscFunctionReturn(PETSC_SUCCESS); 318 } 319