1 #include <petsc/private/taoimpl.h> /*I "petsctao.h" I*/ 2 3 /*@ 4 TaoSetSolution - Sets the vector holding the initial guess for the solve 5 6 Logically collective on tao 7 8 Input Parameters: 9 + tao - the Tao context 10 - x0 - the initial guess 11 12 Level: beginner 13 .seealso: `Tao`, `TaoCreate()`, `TaoSolve()`, `TaoGetSolution()` 14 @*/ 15 PetscErrorCode TaoSetSolution(Tao tao, Vec x0) 16 { 17 PetscFunctionBegin; 18 PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 19 if (x0) PetscValidHeaderSpecific(x0, VEC_CLASSID, 2); 20 PetscCall(PetscObjectReference((PetscObject)x0)); 21 PetscCall(VecDestroy(&tao->solution)); 22 tao->solution = x0; 23 PetscFunctionReturn(0); 24 } 25 26 PetscErrorCode TaoTestGradient(Tao tao, Vec x, Vec g1) 27 { 28 Vec g2, g3; 29 PetscBool complete_print = PETSC_FALSE, test = PETSC_FALSE; 30 PetscReal hcnorm, fdnorm, hcmax, fdmax, diffmax, diffnorm; 31 PetscScalar dot; 32 MPI_Comm comm; 33 PetscViewer viewer, mviewer; 34 PetscViewerFormat format; 35 PetscInt tabs; 36 static PetscBool directionsprinted = PETSC_FALSE; 37 38 PetscFunctionBegin; 39 PetscObjectOptionsBegin((PetscObject)tao); 40 PetscCall(PetscOptionsName("-tao_test_gradient", "Compare hand-coded and finite difference Gradients", "None", &test)); 41 PetscCall(PetscOptionsViewer("-tao_test_gradient_view", "View difference between hand-coded and finite difference Gradients element entries", "None", &mviewer, &format, &complete_print)); 42 PetscOptionsEnd(); 43 if (!test) { 44 if (complete_print) PetscCall(PetscViewerDestroy(&mviewer)); 45 PetscFunctionReturn(0); 46 } 47 48 PetscCall(PetscObjectGetComm((PetscObject)tao, &comm)); 49 PetscCall(PetscViewerASCIIGetStdout(comm, &viewer)); 50 PetscCall(PetscViewerASCIIGetTab(viewer, &tabs)); 51 PetscCall(PetscViewerASCIISetTab(viewer, ((PetscObject)tao)->tablevel)); 52 PetscCall(PetscViewerASCIIPrintf(viewer, " ---------- Testing Gradient -------------\n")); 53 if (!complete_print && !directionsprinted) { 54 PetscCall(PetscViewerASCIIPrintf(viewer, " Run with -tao_test_gradient_view and optionally -tao_test_gradient <threshold> to show difference\n")); 55 PetscCall(PetscViewerASCIIPrintf(viewer, " of hand-coded and finite difference gradient entries greater than <threshold>.\n")); 56 } 57 if (!directionsprinted) { 58 PetscCall(PetscViewerASCIIPrintf(viewer, " Testing hand-coded Gradient, if (for double precision runs) ||G - Gfd||/||G|| is\n")); 59 PetscCall(PetscViewerASCIIPrintf(viewer, " O(1.e-8), the hand-coded Gradient is probably correct.\n")); 60 directionsprinted = PETSC_TRUE; 61 } 62 if (complete_print) PetscCall(PetscViewerPushFormat(mviewer, format)); 63 64 PetscCall(VecDuplicate(x, &g2)); 65 PetscCall(VecDuplicate(x, &g3)); 66 67 /* Compute finite difference gradient, assume the gradient is already computed by TaoComputeGradient() and put into g1 */ 68 PetscCall(TaoDefaultComputeGradient(tao, x, g2, NULL)); 69 70 PetscCall(VecNorm(g2, NORM_2, &fdnorm)); 71 PetscCall(VecNorm(g1, NORM_2, &hcnorm)); 72 PetscCall(VecNorm(g2, NORM_INFINITY, &fdmax)); 73 PetscCall(VecNorm(g1, NORM_INFINITY, &hcmax)); 74 PetscCall(VecDot(g1, g2, &dot)); 75 PetscCall(VecCopy(g1, g3)); 76 PetscCall(VecAXPY(g3, -1.0, g2)); 77 PetscCall(VecNorm(g3, NORM_2, &diffnorm)); 78 PetscCall(VecNorm(g3, NORM_INFINITY, &diffmax)); 79 PetscCall(PetscViewerASCIIPrintf(viewer, " ||Gfd|| %g, ||G|| = %g, angle cosine = (Gfd'G)/||Gfd||||G|| = %g\n", (double)fdnorm, (double)hcnorm, (double)(PetscRealPart(dot) / (fdnorm * hcnorm)))); 80 PetscCall(PetscViewerASCIIPrintf(viewer, " 2-norm ||G - Gfd||/||G|| = %g, ||G - Gfd|| = %g\n", (double)(diffnorm / PetscMax(hcnorm, fdnorm)), (double)diffnorm)); 81 PetscCall(PetscViewerASCIIPrintf(viewer, " max-norm ||G - Gfd||/||G|| = %g, ||G - Gfd|| = %g\n", (double)(diffmax / PetscMax(hcmax, fdmax)), (double)diffmax)); 82 83 if (complete_print) { 84 PetscCall(PetscViewerASCIIPrintf(viewer, " Hand-coded gradient ----------\n")); 85 PetscCall(VecView(g1, mviewer)); 86 PetscCall(PetscViewerASCIIPrintf(viewer, " Finite difference gradient ----------\n")); 87 PetscCall(VecView(g2, mviewer)); 88 PetscCall(PetscViewerASCIIPrintf(viewer, " Hand-coded minus finite-difference gradient ----------\n")); 89 PetscCall(VecView(g3, mviewer)); 90 } 91 PetscCall(VecDestroy(&g2)); 92 PetscCall(VecDestroy(&g3)); 93 94 if (complete_print) { 95 PetscCall(PetscViewerPopFormat(mviewer)); 96 PetscCall(PetscViewerDestroy(&mviewer)); 97 } 98 PetscCall(PetscViewerASCIISetTab(viewer, tabs)); 99 PetscFunctionReturn(0); 100 } 101 102 /*@ 103 TaoComputeGradient - Computes the gradient of the objective function 104 105 Collective on tao 106 107 Input Parameters: 108 + tao - the Tao context 109 - X - input vector 110 111 Output Parameter: 112 . G - gradient vector 113 114 Options Database Keys: 115 + -tao_test_gradient - compare the user provided gradient with one compute via finite differences to check for errors 116 - -tao_test_gradient_view - display the user provided gradient, the finite difference gradient and the difference between them to help users detect the location of errors in the user provided gradient 117 118 Note: 119 `TaoComputeGradient()` is typically used within the implementation of the optimization method, 120 so most users would not generally call this routine themselves. 121 122 Level: developer 123 124 .seealso: `TaoComputeObjective()`, `TaoComputeObjectiveAndGradient()`, `TaoSetGradient()` 125 @*/ 126 PetscErrorCode TaoComputeGradient(Tao tao, Vec X, Vec G) 127 { 128 PetscReal dummy; 129 130 PetscFunctionBegin; 131 PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 132 PetscValidHeaderSpecific(X, VEC_CLASSID, 2); 133 PetscValidHeaderSpecific(G, VEC_CLASSID, 3); 134 PetscCheckSameComm(tao, 1, X, 2); 135 PetscCheckSameComm(tao, 1, G, 3); 136 PetscCall(VecLockReadPush(X)); 137 if (tao->ops->computegradient) { 138 PetscCall(PetscLogEventBegin(TAO_GradientEval, tao, X, G, NULL)); 139 PetscCallBack("Tao callback gradient", (*tao->ops->computegradient)(tao, X, G, tao->user_gradP)); 140 PetscCall(PetscLogEventEnd(TAO_GradientEval, tao, X, G, NULL)); 141 tao->ngrads++; 142 } else if (tao->ops->computeobjectiveandgradient) { 143 PetscCall(PetscLogEventBegin(TAO_ObjGradEval, tao, X, G, NULL)); 144 PetscCallBack("Tao callback objective/gradient", (*tao->ops->computeobjectiveandgradient)(tao, X, &dummy, G, tao->user_objgradP)); 145 PetscCall(PetscLogEventEnd(TAO_ObjGradEval, tao, X, G, NULL)); 146 tao->nfuncgrads++; 147 } else SETERRQ(PetscObjectComm((PetscObject)tao), PETSC_ERR_ARG_WRONGSTATE, "TaoSetGradient() has not been called"); 148 PetscCall(VecLockReadPop(X)); 149 150 PetscCall(TaoTestGradient(tao, X, G)); 151 PetscFunctionReturn(0); 152 } 153 154 /*@ 155 TaoComputeObjective - Computes the objective function value at a given point 156 157 Collective on tao 158 159 Input Parameters: 160 + tao - the Tao context 161 - X - input vector 162 163 Output Parameter: 164 . f - Objective value at X 165 166 Note: 167 `TaoComputeObjective()` is typically used within the implementation of the optimization algorithm 168 so most users would not generally call this routine themselves. 169 170 Level: developer 171 172 .seealso: `Tao`, `TaoComputeGradient()`, `TaoComputeObjectiveAndGradient()`, `TaoSetObjective()` 173 @*/ 174 PetscErrorCode TaoComputeObjective(Tao tao, Vec X, PetscReal *f) 175 { 176 Vec temp; 177 178 PetscFunctionBegin; 179 PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 180 PetscValidHeaderSpecific(X, VEC_CLASSID, 2); 181 PetscCheckSameComm(tao, 1, X, 2); 182 PetscCall(VecLockReadPush(X)); 183 if (tao->ops->computeobjective) { 184 PetscCall(PetscLogEventBegin(TAO_ObjectiveEval, tao, X, NULL, NULL)); 185 PetscCallBack("Tao callback objective", (*tao->ops->computeobjective)(tao, X, f, tao->user_objP)); 186 PetscCall(PetscLogEventEnd(TAO_ObjectiveEval, tao, X, NULL, NULL)); 187 tao->nfuncs++; 188 } else if (tao->ops->computeobjectiveandgradient) { 189 PetscCall(PetscInfo(tao, "Duplicating variable vector in order to call func/grad routine\n")); 190 PetscCall(VecDuplicate(X, &temp)); 191 PetscCall(PetscLogEventBegin(TAO_ObjGradEval, tao, X, NULL, NULL)); 192 PetscCallBack("Tao callback objective/gradient", (*tao->ops->computeobjectiveandgradient)(tao, X, f, temp, tao->user_objgradP)); 193 PetscCall(PetscLogEventEnd(TAO_ObjGradEval, tao, X, NULL, NULL)); 194 PetscCall(VecDestroy(&temp)); 195 tao->nfuncgrads++; 196 } else SETERRQ(PetscObjectComm((PetscObject)tao), PETSC_ERR_ARG_WRONGSTATE, "TaoSetObjective() has not been called"); 197 PetscCall(PetscInfo(tao, "TAO Function evaluation: %20.19e\n", (double)(*f))); 198 PetscCall(VecLockReadPop(X)); 199 PetscFunctionReturn(0); 200 } 201 202 /*@ 203 TaoComputeObjectiveAndGradient - Computes the objective function value at a given point 204 205 Collective on tao 206 207 Input Parameters: 208 + tao - the Tao context 209 - X - input vector 210 211 Output Parameters: 212 + f - Objective value at X 213 - g - Gradient vector at X 214 215 Note: 216 `TaoComputeObjectiveAndGradient()` is typically used within the implementation of the optimization algorithm, 217 so most users would not generally call this routine themselves. 218 219 Level: developer 220 221 .seealso: `TaoComputeGradient()`, `TaoComputeObjectiveAndGradient()`, `TaoSetObjective()` 222 @*/ 223 PetscErrorCode TaoComputeObjectiveAndGradient(Tao tao, Vec X, PetscReal *f, Vec G) 224 { 225 PetscFunctionBegin; 226 PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 227 PetscValidHeaderSpecific(X, VEC_CLASSID, 2); 228 PetscValidHeaderSpecific(G, VEC_CLASSID, 4); 229 PetscCheckSameComm(tao, 1, X, 2); 230 PetscCheckSameComm(tao, 1, G, 4); 231 PetscCall(VecLockReadPush(X)); 232 if (tao->ops->computeobjectiveandgradient) { 233 PetscCall(PetscLogEventBegin(TAO_ObjGradEval, tao, X, G, NULL)); 234 if (tao->ops->computegradient == TaoDefaultComputeGradient) { 235 PetscCall(TaoComputeObjective(tao, X, f)); 236 PetscCall(TaoDefaultComputeGradient(tao, X, G, NULL)); 237 } else { 238 PetscCallBack("Tao callback objective/gradient", (*tao->ops->computeobjectiveandgradient)(tao, X, f, G, tao->user_objgradP)); 239 } 240 PetscCall(PetscLogEventEnd(TAO_ObjGradEval, tao, X, G, NULL)); 241 tao->nfuncgrads++; 242 } else if (tao->ops->computeobjective && tao->ops->computegradient) { 243 PetscCall(PetscLogEventBegin(TAO_ObjectiveEval, tao, X, NULL, NULL)); 244 PetscCallBack("Tao callback objective", (*tao->ops->computeobjective)(tao, X, f, tao->user_objP)); 245 PetscCall(PetscLogEventEnd(TAO_ObjectiveEval, tao, X, NULL, NULL)); 246 tao->nfuncs++; 247 PetscCall(PetscLogEventBegin(TAO_GradientEval, tao, X, G, NULL)); 248 PetscCallBack("Tao callback gradient", (*tao->ops->computegradient)(tao, X, G, tao->user_gradP)); 249 PetscCall(PetscLogEventEnd(TAO_GradientEval, tao, X, G, NULL)); 250 tao->ngrads++; 251 } else SETERRQ(PetscObjectComm((PetscObject)tao), PETSC_ERR_ARG_WRONGSTATE, "TaoSetObjective() or TaoSetGradient() not set"); 252 PetscCall(PetscInfo(tao, "TAO Function evaluation: %20.19e\n", (double)(*f))); 253 PetscCall(VecLockReadPop(X)); 254 255 PetscCall(TaoTestGradient(tao, X, G)); 256 PetscFunctionReturn(0); 257 } 258 259 /*@C 260 TaoSetObjective - Sets the function evaluation routine for minimization 261 262 Logically collective on tao 263 264 Input Parameters: 265 + tao - the Tao context 266 . func - the objective function 267 - ctx - [optional] user-defined context for private data for the function evaluation 268 routine (may be NULL) 269 270 Calling sequence of func: 271 $ func (Tao tao, Vec x, PetscReal *f, void *ctx); 272 273 + x - input vector 274 . f - function value 275 - ctx - [optional] user-defined function context 276 277 Level: beginner 278 279 .seealso: `TaoSetGradient()`, `TaoSetHessian()`, `TaoSetObjectiveAndGradient()`, `TaoGetObjective()` 280 @*/ 281 PetscErrorCode TaoSetObjective(Tao tao, PetscErrorCode (*func)(Tao, Vec, PetscReal *, void *), void *ctx) 282 { 283 PetscFunctionBegin; 284 PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 285 if (ctx) tao->user_objP = ctx; 286 if (func) tao->ops->computeobjective = func; 287 PetscFunctionReturn(0); 288 } 289 290 /*@C 291 TaoGetObjective - Gets the function evaluation routine for the function to be minimized 292 293 Not collective 294 295 Input Parameter: 296 . tao - the Tao context 297 298 Output Parameters 299 + func - the objective function 300 - ctx - the user-defined context for private data for the function evaluation 301 302 Calling sequence of func: 303 $ func (Tao tao, Vec x, PetscReal *f, void *ctx); 304 305 + x - input vector 306 . f - function value 307 - ctx - [optional] user-defined function context 308 309 Level: beginner 310 311 .seealso: `Tao`, `TaoSetGradient()`, `TaoSetHessian()`, `TaoSetObjective()` 312 @*/ 313 PetscErrorCode TaoGetObjective(Tao tao, PetscErrorCode (**func)(Tao, Vec, PetscReal *, void *), void **ctx) 314 { 315 PetscFunctionBegin; 316 PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 317 if (func) *func = tao->ops->computeobjective; 318 if (ctx) *ctx = tao->user_objP; 319 PetscFunctionReturn(0); 320 } 321 322 /*@C 323 TaoSetResidualRoutine - Sets the residual evaluation routine for least-square applications 324 325 Logically collective on tao 326 327 Input Parameters: 328 + tao - the Tao context 329 . func - the residual evaluation routine 330 - ctx - [optional] user-defined context for private data for the function evaluation 331 routine (may be NULL) 332 333 Calling sequence of func: 334 $ func (Tao tao, Vec x, Vec f, void *ctx); 335 336 + x - input vector 337 . f - function value vector 338 - ctx - [optional] user-defined function context 339 340 Level: beginner 341 342 .seealso: `Tao`, `TaoSetObjective()`, `TaoSetJacobianRoutine()` 343 @*/ 344 PetscErrorCode TaoSetResidualRoutine(Tao tao, Vec res, PetscErrorCode (*func)(Tao, Vec, Vec, void *), void *ctx) 345 { 346 PetscFunctionBegin; 347 PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 348 PetscValidHeaderSpecific(res, VEC_CLASSID, 2); 349 PetscCall(PetscObjectReference((PetscObject)res)); 350 if (tao->ls_res) PetscCall(VecDestroy(&tao->ls_res)); 351 tao->ls_res = res; 352 tao->user_lsresP = ctx; 353 tao->ops->computeresidual = func; 354 355 PetscFunctionReturn(0); 356 } 357 358 /*@ 359 TaoSetResidualWeights - Give weights for the residual values. A vector can be used if only diagonal terms are used, otherwise a matrix can be give. 360 If this function is not provided, or if sigma_v and sigma_w are both NULL, then the identity matrix will be used for weights. 361 362 Collective on tao 363 364 Input Parameters: 365 + tao - the Tao context 366 . sigma_v - vector of weights (diagonal terms only) 367 . n - the number of weights (if using off-diagonal) 368 . rows - index list of rows for sigma_w 369 . cols - index list of columns for sigma_w 370 - vals - array of weights 371 372 Note: Either sigma_v or sigma_w (or both) should be NULL 373 374 Level: intermediate 375 376 .seealso: `Tao`, `TaoSetResidualRoutine()` 377 @*/ 378 PetscErrorCode TaoSetResidualWeights(Tao tao, Vec sigma_v, PetscInt n, PetscInt *rows, PetscInt *cols, PetscReal *vals) 379 { 380 PetscInt i; 381 382 PetscFunctionBegin; 383 PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 384 if (sigma_v) PetscValidHeaderSpecific(sigma_v, VEC_CLASSID, 2); 385 PetscCall(PetscObjectReference((PetscObject)sigma_v)); 386 PetscCall(VecDestroy(&tao->res_weights_v)); 387 tao->res_weights_v = sigma_v; 388 if (vals) { 389 PetscCall(PetscFree(tao->res_weights_rows)); 390 PetscCall(PetscFree(tao->res_weights_cols)); 391 PetscCall(PetscFree(tao->res_weights_w)); 392 PetscCall(PetscMalloc1(n, &tao->res_weights_rows)); 393 PetscCall(PetscMalloc1(n, &tao->res_weights_cols)); 394 PetscCall(PetscMalloc1(n, &tao->res_weights_w)); 395 tao->res_weights_n = n; 396 for (i = 0; i < n; i++) { 397 tao->res_weights_rows[i] = rows[i]; 398 tao->res_weights_cols[i] = cols[i]; 399 tao->res_weights_w[i] = vals[i]; 400 } 401 } else { 402 tao->res_weights_n = 0; 403 tao->res_weights_rows = NULL; 404 tao->res_weights_cols = NULL; 405 } 406 PetscFunctionReturn(0); 407 } 408 409 /*@ 410 TaoComputeResidual - Computes a least-squares residual vector at a given point 411 412 Collective on tao 413 414 Input Parameters: 415 + tao - the Tao context 416 - X - input vector 417 418 Output Parameter: 419 . f - Objective vector at X 420 421 Notes: 422 `TaoComputeResidual()` is typically used within the implementation of the optimization algorithm, 423 so most users would not generally call this routine themselves. 424 425 Level: advanced 426 427 .seealso: `Tao`, `TaoSetResidualRoutine()` 428 @*/ 429 PetscErrorCode TaoComputeResidual(Tao tao, Vec X, Vec F) 430 { 431 PetscFunctionBegin; 432 PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 433 PetscValidHeaderSpecific(X, VEC_CLASSID, 2); 434 PetscValidHeaderSpecific(F, VEC_CLASSID, 3); 435 PetscCheckSameComm(tao, 1, X, 2); 436 PetscCheckSameComm(tao, 1, F, 3); 437 if (tao->ops->computeresidual) { 438 PetscCall(PetscLogEventBegin(TAO_ObjectiveEval, tao, X, NULL, NULL)); 439 PetscCallBack("Tao callback least-squares residual", (*tao->ops->computeresidual)(tao, X, F, tao->user_lsresP)); 440 PetscCall(PetscLogEventEnd(TAO_ObjectiveEval, tao, X, NULL, NULL)); 441 tao->nfuncs++; 442 } else SETERRQ(PetscObjectComm((PetscObject)tao), PETSC_ERR_ARG_WRONGSTATE, "TaoSetResidualRoutine() has not been called"); 443 PetscCall(PetscInfo(tao, "TAO least-squares residual evaluation.\n")); 444 PetscFunctionReturn(0); 445 } 446 447 /*@C 448 TaoSetGradient - Sets the gradient evaluation routine for the function to be optimized 449 450 Logically collective on tao 451 452 Input Parameters: 453 + tao - the Tao context 454 . g - [optional] the vector to internally hold the gradient computation 455 . func - the gradient function 456 - ctx - [optional] user-defined context for private data for the gradient evaluation 457 routine (may be NULL) 458 459 Calling sequence of func: 460 $ func (Tao tao, Vec x, Vec g, void *ctx); 461 462 + x - input vector 463 . g - gradient value (output) 464 - ctx - [optional] user-defined function context 465 466 Level: beginner 467 468 .seealso: `Tao`, `TaoSolve()`, `TaoSetObjective()`, `TaoSetHessian()`, `TaoSetObjectiveAndGradient()`, `TaoGetGradient()` 469 @*/ 470 PetscErrorCode TaoSetGradient(Tao tao, Vec g, PetscErrorCode (*func)(Tao, Vec, Vec, void *), void *ctx) 471 { 472 PetscFunctionBegin; 473 PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 474 if (g) { 475 PetscValidHeaderSpecific(g, VEC_CLASSID, 2); 476 PetscCheckSameComm(tao, 1, g, 2); 477 PetscCall(PetscObjectReference((PetscObject)g)); 478 PetscCall(VecDestroy(&tao->gradient)); 479 tao->gradient = g; 480 } 481 if (func) tao->ops->computegradient = func; 482 if (ctx) tao->user_gradP = ctx; 483 PetscFunctionReturn(0); 484 } 485 486 /*@C 487 TaoGetGradient - Gets the gradient evaluation routine for the function being optimized 488 489 Not collective 490 491 Input Parameter: 492 . tao - the Tao context 493 494 Output Parameters: 495 + g - the vector to internally hold the gradient computation 496 . func - the gradient function 497 - ctx - user-defined context for private data for the gradient evaluation routine 498 499 Calling sequence of func: 500 $ func (Tao tao, Vec x, Vec g, void *ctx); 501 502 + x - input vector 503 . g - gradient value (output) 504 - ctx - [optional] user-defined function context 505 506 Level: beginner 507 508 .seealso: `Tao`, `TaoSetObjective()`, `TaoSetHessian()`, `TaoSetObjectiveAndGradient()`, `TaoSetGradient()` 509 @*/ 510 PetscErrorCode TaoGetGradient(Tao tao, Vec *g, PetscErrorCode (**func)(Tao, Vec, Vec, void *), void **ctx) 511 { 512 PetscFunctionBegin; 513 PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 514 if (g) *g = tao->gradient; 515 if (func) *func = tao->ops->computegradient; 516 if (ctx) *ctx = tao->user_gradP; 517 PetscFunctionReturn(0); 518 } 519 520 /*@C 521 TaoSetObjectiveAndGradient - Sets a combined objective function and gradient evaluation routine for the function to be optimized 522 523 Logically collective on tao 524 525 Input Parameters: 526 + tao - the Tao context 527 . g - [optional] the vector to internally hold the gradient computation 528 . func - the gradient function 529 - ctx - [optional] user-defined context for private data for the gradient evaluation 530 routine (may be NULL) 531 532 Calling sequence of func: 533 $ func (Tao tao, Vec x, PetscReal *f, Vec g, void *ctx); 534 535 + x - input vector 536 . f - objective value (output) 537 . g - gradient value (output) 538 - ctx - [optional] user-defined function context 539 540 Level: beginner 541 542 Note: 543 For some optimization methods using a combined function can be more eifficient. 544 545 .seealso: `Tao`, `TaoSolve()`, `TaoSetObjective()`, `TaoSetHessian()`, `TaoSetGradient()`, `TaoGetObjectiveAndGradient()` 546 @*/ 547 PetscErrorCode TaoSetObjectiveAndGradient(Tao tao, Vec g, PetscErrorCode (*func)(Tao, Vec, PetscReal *, Vec, void *), void *ctx) 548 { 549 PetscFunctionBegin; 550 PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 551 if (g) { 552 PetscValidHeaderSpecific(g, VEC_CLASSID, 2); 553 PetscCheckSameComm(tao, 1, g, 2); 554 PetscCall(PetscObjectReference((PetscObject)g)); 555 PetscCall(VecDestroy(&tao->gradient)); 556 tao->gradient = g; 557 } 558 if (ctx) tao->user_objgradP = ctx; 559 if (func) tao->ops->computeobjectiveandgradient = func; 560 PetscFunctionReturn(0); 561 } 562 563 /*@C 564 TaoGetObjectiveAndGradient - Gets the combined objective function and gradient evaluation routine for the function to be optimized 565 566 Not collective 567 568 Input Parameter: 569 . tao - the Tao context 570 571 Output Parameters: 572 + g - the vector to internally hold the gradient computation 573 . func - the gradient function 574 - ctx - user-defined context for private data for the gradient evaluation routine 575 576 Calling sequence of func: 577 $ func (Tao tao, Vec x, PetscReal *f, Vec g, void *ctx); 578 579 + x - input vector 580 . f - objective value (output) 581 . g - gradient value (output) 582 - ctx - [optional] user-defined function context 583 584 Level: beginner 585 586 .seealso: `Tao`, `TaoSolve()`, `TaoSetObjective()`, `TaoSetGradient()`, `TaoSetHessian()`, `TaoSetObjectiveAndGradient()` 587 @*/ 588 PetscErrorCode TaoGetObjectiveAndGradient(Tao tao, Vec *g, PetscErrorCode (**func)(Tao, Vec, PetscReal *, Vec, void *), void **ctx) 589 { 590 PetscFunctionBegin; 591 PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 592 if (g) *g = tao->gradient; 593 if (func) *func = tao->ops->computeobjectiveandgradient; 594 if (ctx) *ctx = tao->user_objgradP; 595 PetscFunctionReturn(0); 596 } 597 598 /*@ 599 TaoIsObjectiveDefined - Checks to see if the user has 600 declared an objective-only routine. Useful for determining when 601 it is appropriate to call `TaoComputeObjective()` or 602 `TaoComputeObjectiveAndGradient()` 603 604 Not collective 605 606 Input Parameter: 607 . tao - the Tao context 608 609 Output Parameter: 610 . flg - `PETSC_TRUE` if function routine is set by user, `PETSC_FALSE` otherwise 611 612 Level: developer 613 614 .seealso: `Tao`, `TaoSetObjective()`, `TaoIsGradientDefined()`, `TaoIsObjectiveAndGradientDefined()` 615 @*/ 616 PetscErrorCode TaoIsObjectiveDefined(Tao tao, PetscBool *flg) 617 { 618 PetscFunctionBegin; 619 PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 620 if (tao->ops->computeobjective == NULL) *flg = PETSC_FALSE; 621 else *flg = PETSC_TRUE; 622 PetscFunctionReturn(0); 623 } 624 625 /*@ 626 TaoIsGradientDefined - Checks to see if the user has 627 declared an objective-only routine. Useful for determining when 628 it is appropriate to call `TaoComputeGradient()` or 629 `TaoComputeGradientAndGradient()` 630 631 Not Collective 632 633 Input Parameter: 634 . tao - the Tao context 635 636 Output Parameter: 637 . flg - `PETSC_TRUE` if function routine is set by user, `PETSC_FALSE` otherwise 638 639 Level: developer 640 641 .seealso: `TaoSetGradient()`, `TaoIsObjectiveDefined()`, `TaoIsObjectiveAndGradientDefined()` 642 @*/ 643 PetscErrorCode TaoIsGradientDefined(Tao tao, PetscBool *flg) 644 { 645 PetscFunctionBegin; 646 PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 647 if (tao->ops->computegradient == NULL) *flg = PETSC_FALSE; 648 else *flg = PETSC_TRUE; 649 PetscFunctionReturn(0); 650 } 651 652 /*@ 653 TaoIsObjectiveAndGradientDefined - Checks to see if the user has 654 declared a joint objective/gradient routine. Useful for determining when 655 it is appropriate to call `TaoComputeObjective()` or 656 `TaoComputeObjectiveAndGradient()` 657 658 Not Collective 659 660 Input Parameter: 661 . tao - the Tao context 662 663 Output Parameter: 664 . flg - `PETSC_TRUE` if function routine is set by user, `PETSC_FALSE` otherwise 665 666 Level: developer 667 668 .seealso: `TaoSetObjectiveAndGradient()`, `TaoIsObjectiveDefined()`, `TaoIsGradientDefined()` 669 @*/ 670 PetscErrorCode TaoIsObjectiveAndGradientDefined(Tao tao, PetscBool *flg) 671 { 672 PetscFunctionBegin; 673 PetscValidHeaderSpecific(tao, TAO_CLASSID, 1); 674 if (tao->ops->computeobjectiveandgradient == NULL) *flg = PETSC_FALSE; 675 else *flg = PETSC_TRUE; 676 PetscFunctionReturn(0); 677 } 678