Actual source code: taosolver_bounds.c

  1: #include <petsc/private/taoimpl.h>

  3: /*@
  4:   TaoSetVariableBounds - Sets the upper and lower bounds for the optimization problem

  6:   Logically collective

  8:   Input Parameters:
  9: + tao - the Tao context
 10: . XL  - vector of lower bounds
 11: - XU  - vector of upper bounds

 13:   Level: beginner

 15: .seealso: `Tao`, `TaoSetObjective()`, `TaoSetHessian()`, `TaoSetObjectiveAndGradient()`, `TaoGetVariableBounds()`
 16: @*/
 17: PetscErrorCode TaoSetVariableBounds(Tao tao, Vec XL, Vec XU)
 18: {
 22:   PetscObjectReference((PetscObject)XL);
 23:   PetscObjectReference((PetscObject)XU);
 24:   VecDestroy(&tao->XL);
 25:   VecDestroy(&tao->XU);
 26:   tao->XL      = XL;
 27:   tao->XU      = XU;
 28:   tao->bounded = (PetscBool)(XL || XU);
 29:   return 0;
 30: }

 32: /*@C
 33:   TaoSetVariableBoundsRoutine - Sets a function to be used to compute lower and upper variable bounds for the optimization

 35:   Logically collective

 37:   Input Parameters:
 38: + tao - the Tao context
 39: . func - the bounds computation routine
 40: - ctx - [optional] user-defined context for private data for the bounds computation (may be NULL)

 42:   Calling sequence of func:
 43: $      func (Tao tao, Vec xl, Vec xu);

 45: + tao - the Tao
 46: . xl  - vector of lower bounds
 47: . xu  - vector of upper bounds
 48: - ctx - the (optional) user-defined function context

 50:   Level: beginner

 52:   Note:
 53:   The func passed to `TaoSetVariableBoundsRoutine()` takes precedence over any values set in `TaoSetVariableBounds()`.

 55: .seealso: `Tao`, `TaoSetObjective()`, `TaoSetHessian()`, `TaoSetObjectiveAndGradient()`, `TaoSetVariableBounds()`

 57: @*/
 58: PetscErrorCode TaoSetVariableBoundsRoutine(Tao tao, PetscErrorCode (*func)(Tao, Vec, Vec, void *), void *ctx)
 59: {
 61:   tao->user_boundsP       = ctx;
 62:   tao->ops->computebounds = func;
 63:   tao->bounded            = func ? PETSC_TRUE : PETSC_FALSE;
 64:   return 0;
 65: }

 67: /*@
 68:   TaoGetVariableBounds - Gets the upper and lower bounds vectors set with `TaoSetVariableBounds()`

 70:   Not collective

 72:   Input Parameter:
 73: . tao - the Tao context

 75:   Output Parameters:
 76: + XL  - vector of lower bounds
 77: - XU  - vector of upper bounds

 79:   Level: beginner

 81: .seealso: `Tao`, `TaoSetObjective()`, `TaoSetHessian()`, `TaoSetObjectiveAndGradient()`, `TaoSetVariableBounds()`
 82: @*/
 83: PetscErrorCode TaoGetVariableBounds(Tao tao, Vec *XL, Vec *XU)
 84: {
 86:   if (XL) *XL = tao->XL;
 87:   if (XU) *XU = tao->XU;
 88:   return 0;
 89: }

 91: /*@C
 92:    TaoComputeVariableBounds - Compute the variable bounds using the
 93:    routine set by `TaoSetVariableBoundsRoutine()`.

 95:    Collective

 97:    Input Parameter:
 98: .  tao - the Tao context

100:    Level: developer

102: .seealso: `Tao`, `TaoSetVariableBoundsRoutine()`, `TaoSetVariableBounds()`
103: @*/

105: PetscErrorCode TaoComputeVariableBounds(Tao tao)
106: {
108:   if (tao->ops->computebounds) {
109:     if (!tao->XL) {
110:       VecDuplicate(tao->solution, &tao->XL);
111:       VecSet(tao->XL, PETSC_NINFINITY);
112:     }
113:     if (!tao->XU) {
114:       VecDuplicate(tao->solution, &tao->XU);
115:       VecSet(tao->XU, PETSC_INFINITY);
116:     }
117:     PetscCallBack("Tao callback variable bounds", (*tao->ops->computebounds)(tao, tao->XL, tao->XU, tao->user_boundsP));
118:   }
119:   return 0;
120: }

122: /*@
123:   TaoSetInequalityBounds - Sets the upper and lower bounds

125:   Logically collective

127:   Input Parameters:
128: + tao - the Tao context
129: . IL  - vector of lower bounds
130: - IU  - vector of upper bounds

132:   Level: beginner

134: .seealso: `Tao`, `TaoSetObjective()`, `TaoSetHessian()`, `TaoSetObjectiveAndGradient()`, `TaoGetInequalityBounds()`
135: @*/
136: PetscErrorCode TaoSetInequalityBounds(Tao tao, Vec IL, Vec IU)
137: {
141:   PetscObjectReference((PetscObject)IL);
142:   PetscObjectReference((PetscObject)IU);
143:   VecDestroy(&tao->IL);
144:   VecDestroy(&tao->IU);
145:   tao->IL               = IL;
146:   tao->IU               = IU;
147:   tao->ineq_doublesided = (PetscBool)(IL || IU);
148:   return 0;
149: }

151: /*@
152:   TaoGetInequalityBounds - Gets the upper and lower bounds set via `TaoSetInequalityBounds()`

154:   Logically collective

156:   Input Parameter:
157: . tao - the Tao context

159:   Output Parameters:
160: + IL  - vector of lower bounds
161: - IU  - vector of upper bounds

163:   Level: beginner

165: .seealso: `TaoSetObjective()`, `TaoSetHessian()`, `TaoSetObjectiveAndGradient()`, `TaoSetInequalityBounds()`
166: @*/
167: PetscErrorCode TaoGetInequalityBounds(Tao tao, Vec *IL, Vec *IU)
168: {
170:   if (IL) *IL = tao->IL;
171:   if (IU) *IU = tao->IU;
172:   return 0;
173: }

175: /*@C
176:    TaoComputeConstraints - Compute the variable bounds using the
177:    routine set by `TaoSetConstraintsRoutine()`.

179:    Collective

181:    Input Parameters:
182: .  tao - the Tao context

184:    Level: developer

186: .seealso: `Tao`, `TaoSetConstraintsRoutine()`, `TaoComputeJacobian()`
187: @*/

189: PetscErrorCode TaoComputeConstraints(Tao tao, Vec X, Vec C)
190: {
196:   PetscLogEventBegin(TAO_ConstraintsEval, tao, X, C, NULL);
197:   PetscCallBack("Tao callback constraints", (*tao->ops->computeconstraints)(tao, X, C, tao->user_conP));
198:   PetscLogEventEnd(TAO_ConstraintsEval, tao, X, C, NULL);
199:   tao->nconstraints++;
200:   return 0;
201: }

203: /*@C
204:   TaoSetConstraintsRoutine - Sets a function to be used to compute constraints.  Tao only handles constraints under certain conditions, see manual for details

206:   Logically collective

208:   Input Parameters:
209: + tao - the Tao context
210: . c   - A vector that will be used to store constraint evaluation
211: . func - the bounds computation routine
212: - ctx - [optional] user-defined context for private data for the constraints computation (may be NULL)

214:   Calling sequence of func:
215: $      func (Tao tao, Vec x, Vec c, void *ctx);

217: + tao - the Tao
218: . x   - point to evaluate constraints
219: . c   - vector constraints evaluated at x
220: - ctx - the (optional) user-defined function context

222:   Level: intermediate

224: .seealso: `Tao`, `TaoSetObjective()`, `TaoSetHessian()`, `TaoSetObjectiveAndGradient()`, `TaoSetVariablevBounds()`

226: @*/
227: PetscErrorCode TaoSetConstraintsRoutine(Tao tao, Vec c, PetscErrorCode (*func)(Tao, Vec, Vec, void *), void *ctx)
228: {
231:   PetscObjectReference((PetscObject)c);
232:   VecDestroy(&tao->constraints);
233:   tao->constrained             = func ? PETSC_TRUE : PETSC_FALSE;
234:   tao->constraints             = c;
235:   tao->user_conP               = ctx;
236:   tao->ops->computeconstraints = func;
237:   return 0;
238: }

240: /*@
241:   TaoComputeDualVariables - Computes the dual vectors corresponding to the bounds
242:   of the variables

244:   Collective

246:   Input Parameter:
247: . tao - the Tao context

249:   Output Parameters:
250: + DL - dual variable vector for the lower bounds
251: - DU - dual variable vector for the upper bounds

253:   Level: advanced

255:   Note:
256:   DL and DU should be created before calling this routine.  If calling
257:   this routine after using an unconstrained solver, DL and DU are set to all
258:   zeros.

260:   Level: advanced

262:  .seealso: `Tao`, `TaoComputeObjective()`, `TaoSetVariableBounds()`
263: @*/
264: PetscErrorCode TaoComputeDualVariables(Tao tao, Vec DL, Vec DU)
265: {
271:   if (tao->ops->computedual) {
272:     PetscUseTypeMethod(tao, computedual, DL, DU);
273:   } else {
274:     VecSet(DL, 0.0);
275:     VecSet(DU, 0.0);
276:   }
277:   return 0;
278: }

280: /*@
281:   TaoGetDualVariables - Gets the dual vectors

283:   Collective

285:   Input Parameter:
286: . tao - the Tao context

288:   Output Parameters:
289: + DE - dual variable vector for the lower bounds
290: - DI - dual variable vector for the upper bounds

292:   Level: advanced

294: .seealso: `Tao`, `TaoComputeDualVariables()`
295: @*/
296: PetscErrorCode TaoGetDualVariables(Tao tao, Vec *DE, Vec *DI)
297: {
299:   if (DE) *DE = tao->DE;
300:   if (DI) *DI = tao->DI;
301:   return 0;
302: }

304: /*@C
305:   TaoSetEqualityConstraintsRoutine - Sets a function to be used to compute constraints.  Tao only handles constraints under certain conditions, see manual for details

307:   Logically collective

309:   Input Parameters:
310: + tao - the Tao context
311: . ce   - A vector that will be used to store equality constraint evaluation
312: . func - the bounds computation routine
313: - ctx - [optional] user-defined context for private data for the equality constraints computation (may be NULL)

315:   Calling sequence of func:
316: $      func (Tao tao, Vec x, Vec ce, void *ctx);

318: + tao - the Tao
319: . x   - point to evaluate equality constraints
320: . ce   - vector of equality constraints evaluated at x
321: - ctx - the (optional) user-defined function context

323:   Level: intermediate

325: .seealso: `Tao`, `TaoSetObjective()`, `TaoSetHessian()`, `TaoSetObjectiveAndGradient()`, `TaoSetVariableBounds()`

327: @*/
328: PetscErrorCode TaoSetEqualityConstraintsRoutine(Tao tao, Vec ce, PetscErrorCode (*func)(Tao, Vec, Vec, void *), void *ctx)
329: {
332:   PetscObjectReference((PetscObject)ce);
333:   VecDestroy(&tao->constraints_equality);
334:   tao->eq_constrained                  = func ? PETSC_TRUE : PETSC_FALSE;
335:   tao->constraints_equality            = ce;
336:   tao->user_con_equalityP              = ctx;
337:   tao->ops->computeequalityconstraints = func;
338:   return 0;
339: }

341: /*@C
342:   TaoSetInequalityConstraintsRoutine - Sets a function to be used to compute constraints.  Tao only handles constraints under certain conditions, see manual for details

344:   Logically collective

346:   Input Parameters:
347: + tao - the Tao context
348: . ci   - A vector that will be used to store inequality constraint evaluation
349: . func - the bounds computation routine
350: - ctx - [optional] user-defined context for private data for the inequality constraints computation (may be NULL)

352:   Calling sequence of func:
353: $      func (Tao tao, Vec x, Vec ci, void *ctx);

355: + tao - the Tao
356: . x   - point to evaluate inequality constraints
357: . ci   - vector of inequality constraints evaluated at x
358: - ctx - the (optional) user-defined function context

360:   Level: intermediate

362:  .seealso: `Tao, `TaoSetObjective()`, `TaoSetHessian()`, `TaoSetObjectiveAndGradient()`, `TaoSetVariableBounds()`

364: @*/
365: PetscErrorCode TaoSetInequalityConstraintsRoutine(Tao tao, Vec ci, PetscErrorCode (*func)(Tao, Vec, Vec, void *), void *ctx)
366: {
369:   PetscObjectReference((PetscObject)ci);
370:   VecDestroy(&tao->constraints_inequality);
371:   tao->constraints_inequality            = ci;
372:   tao->ineq_constrained                  = func ? PETSC_TRUE : PETSC_FALSE;
373:   tao->user_con_inequalityP              = ctx;
374:   tao->ops->computeinequalityconstraints = func;
375:   return 0;
376: }

378: /*@C
379:    TaoComputeEqualityConstraints - Compute the variable bounds using the
380:    routine set by `TaoSetEqualityConstraintsRoutine()`.

382:    Collective

384:    Input Parameter:
385: .  tao - the Tao context

387:    Output Parameters:
388: +  X - point the equality constraints were evaluated on
389: -  CE   - vector of equality constraints evaluated at X

391:    Level: developer

393: .seealso: `Tao`, `TaoSetEqualityConstraintsRoutine()`, `TaoComputeJacobianEquality()`, `TaoComputeInequalityConstraints()`
394: @*/

396: PetscErrorCode TaoComputeEqualityConstraints(Tao tao, Vec X, Vec CE)
397: {
403:   PetscLogEventBegin(TAO_ConstraintsEval, tao, X, CE, NULL);
404:   PetscCallBack("Tao callback equality constraints", (*tao->ops->computeequalityconstraints)(tao, X, CE, tao->user_con_equalityP));
405:   PetscLogEventEnd(TAO_ConstraintsEval, tao, X, CE, NULL);
406:   tao->nconstraints++;
407:   return 0;
408: }

410: /*@C
411:    TaoComputeInequalityConstraints - Compute the variable bounds using the
412:    routine set by `TaoSetInequalityConstraintsRoutine()`.

414:    Collective

416:    Input Parameter:
417: .  tao - the Tao context

419:    Output Parameters:
420: +  X - point the inequality constraints were evaluated on
421: -  CE   - vector of inequality constraints evaluated at X

423:    Level: developer

425: .seealso: `Tao`, `TaoSetInequalityConstraintsRoutine()`, `TaoComputeJacobianInequality()`, `TaoComputeEqualityConstraints()`
426: @*/

428: PetscErrorCode TaoComputeInequalityConstraints(Tao tao, Vec X, Vec CI)
429: {
435:   PetscLogEventBegin(TAO_ConstraintsEval, tao, X, CI, NULL);
436:   PetscCallBack("Tao callback inequality constraints", (*tao->ops->computeinequalityconstraints)(tao, X, CI, tao->user_con_inequalityP));
437:   PetscLogEventEnd(TAO_ConstraintsEval, tao, X, CI, NULL);
438:   tao->nconstraints++;
439:   return 0;
440: }