Actual source code: taosolver_bounds.c
petsc-3.12.5 2020-03-29
1: #include <petsc/private/taoimpl.h>
3: /*@
4: TaoSetVariableBounds - Sets the upper and lower bounds
6: Logically collective on Tao
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: TaoSetObjectiveRoutine(), TaoSetHessianRoutine() TaoSetObjectiveAndGradientRoutine()
16: @*/
18: PetscErrorCode TaoSetVariableBounds(Tao tao, Vec XL, Vec XU)
19: {
24: if (XL) {
26: PetscObjectReference((PetscObject)XL);
27: }
28: if (XU) {
30: PetscObjectReference((PetscObject)XU);
31: }
32: VecDestroy(&tao->XL);
33: VecDestroy(&tao->XU);
34: tao->XL = XL;
35: tao->XU = XU;
36: tao->bounded = PETSC_TRUE;
37: return(0);
38: }
40: /*@C
41: TaoSetVariableBoundsRoutine - Sets a function to be used to compute variable bounds
43: Logically collective on Tao
45: Input Parameters:
46: + tao - the Tao context
47: . func - the bounds computation routine
48: - ctx - [optional] user-defined context for private data for the bounds computation (may be NULL)
50: Calling sequence of func:
51: $ func (Tao tao, Vec xl, Vec xu);
53: + tao - the Tao
54: . xl - vector of lower bounds
55: . xu - vector of upper bounds
56: - ctx - the (optional) user-defined function context
58: Level: beginner
60: .seealso: TaoSetObjectiveRoutine(), TaoSetHessianRoutine() TaoSetObjectiveAndGradientRoutine(), TaoSetVariableBounds()
62: Note: The func passed in to TaoSetVariableBoundsRoutine() takes
63: precedence over any values set in TaoSetVariableBounds().
65: @*/
66: PetscErrorCode TaoSetVariableBoundsRoutine(Tao tao, PetscErrorCode (*func)(Tao, Vec, Vec, void*), void *ctx)
67: {
70: tao->user_boundsP = ctx;
71: tao->ops->computebounds = func;
72: tao->bounded = PETSC_TRUE;
73: return(0);
74: }
76: PetscErrorCode TaoGetVariableBounds(Tao tao, Vec *XL, Vec *XU)
77: {
80: if (XL) {
81: *XL=tao->XL;
82: }
83: if (XU) {
84: *XU=tao->XU;
85: }
86: return(0);
87: }
89: /*@C
90: TaoComputeVariableBounds - Compute the variable bounds using the
91: routine set by TaoSetVariableBoundsRoutine().
93: Collective on Tao
95: Input Parameters:
96: . tao - the Tao context
98: Level: developer
100: .seealso: TaoSetVariableBoundsRoutine(), TaoSetVariableBounds()
101: @*/
103: PetscErrorCode TaoComputeVariableBounds(Tao tao)
104: {
109: PetscStackPush("Tao compute variable bounds");
110: if (!tao->XL || !tao->XU) {
111: if (!tao->solution) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"TaoSetInitialVector must be called before TaoComputeVariableBounds");
112: VecDuplicate(tao->solution, &tao->XL);
113: VecSet(tao->XL, PETSC_NINFINITY);
114: VecDuplicate(tao->solution, &tao->XU);
115: VecSet(tao->XU, PETSC_INFINITY);
116: }
117: if (tao->ops->computebounds) {
118: (*tao->ops->computebounds)(tao,tao->XL,tao->XU,tao->user_boundsP);
119: }
120: PetscStackPop;
121: return(0);
122: }
124: /*@
125: TaoSetInequalityBounds - Sets the upper and lower bounds
127: Logically collective on Tao
129: Input Parameters:
130: + tao - the Tao context
131: . IL - vector of lower bounds
132: - IU - vector of upper bounds
134: Level: beginner
136: .seealso: TaoSetObjectiveRoutine(), TaoSetHessianRoutine() TaoSetObjectiveAndGradientRoutine()
137: @*/
139: PetscErrorCode TaoSetInequalityBounds(Tao tao, Vec IL, Vec IU)
140: {
145: if (IL) {
147: PetscObjectReference((PetscObject)IL);
148: }
149: if (IU) {
151: PetscObjectReference((PetscObject)IU);
152: }
153: VecDestroy(&tao->IL);
154: VecDestroy(&tao->IU);
155: tao->IL = IL;
156: tao->IU = IU;
157: return(0);
158: }
161: PetscErrorCode TaoGetInequalityBounds(Tao tao, Vec *IL, Vec *IU)
162: {
165: if (IL) {
166: *IL=tao->IL;
167: }
168: if (IU) {
169: *IU=tao->IU;
170: }
171: return(0);
172: }
174: /*@C
175: TaoComputeConstraints - Compute the variable bounds using the
176: routine set by TaoSetConstraintsRoutine().
178: Collective on Tao
180: Input Parameters:
181: . tao - the Tao context
183: Level: developer
185: .seealso: TaoSetConstraintsRoutine(), TaoComputeJacobian()
186: @*/
188: PetscErrorCode TaoComputeConstraints(Tao tao, Vec X, Vec C)
189: {
199: if (!tao->ops->computeconstraints) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"TaoSetConstraintsRoutine() has not been called");
200: if (!tao->solution) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"TaoSetInitialVector must be called before TaoComputeConstraints");
201: PetscLogEventBegin(TAO_ConstraintsEval,tao,X,C,NULL);
202: PetscStackPush("Tao constraints evaluation routine");
203: (*tao->ops->computeconstraints)(tao,X,C,tao->user_conP);
204: PetscStackPop;
205: PetscLogEventEnd(TAO_ConstraintsEval,tao,X,C,NULL);
206: tao->nconstraints++;
207: return(0);
208: }
210: /*@C
211: TaoSetConstraintsRoutine - Sets a function to be used to compute constraints. TAO only handles constraints under certain conditions, see manual for details
213: Logically collective on Tao
215: Input Parameters:
216: + tao - the Tao context
217: . c - A vector that will be used to store constraint evaluation
218: . func - the bounds computation routine
219: - ctx - [optional] user-defined context for private data for the constraints computation (may be NULL)
221: Calling sequence of func:
222: $ func (Tao tao, Vec x, Vec c, void *ctx);
224: + tao - the Tao
225: . x - point to evaluate constraints
226: . c - vector constraints evaluated at x
227: - ctx - the (optional) user-defined function context
229: Level: intermediate
231: .seealso: TaoSetObjectiveRoutine(), TaoSetHessianRoutine() TaoSetObjectiveAndGradientRoutine(), TaoSetVariablevBounds()
233: @*/
234: PetscErrorCode TaoSetConstraintsRoutine(Tao tao, Vec c, PetscErrorCode (*func)(Tao, Vec, Vec, void*), void *ctx)
235: {
238: tao->constraints = c;
239: tao->user_conP = ctx;
240: tao->ops->computeconstraints = func;
241: return(0);
242: }
244: /*@
245: TaoComputeDualVariables - Computes the dual vectors corresponding to the bounds
246: of the variables
248: Collective on Tao
250: Input Parameters:
251: . tao - the Tao context
253: Output Parameter:
254: + DL - dual variable vector for the lower bounds
255: - DU - dual variable vector for the upper bounds
257: Level: advanced
259: Note:
260: DL and DU should be created before calling this routine. If calling
261: this routine after using an unconstrained solver, DL and DU are set to all
262: zeros.
264: Level: advanced
266: .seealso: TaoComputeObjective(), TaoSetVariableBounds()
267: @*/
268: PetscErrorCode TaoComputeDualVariables(Tao tao, Vec DL, Vec DU)
269: {
277: if (tao->ops->computedual) {
278: (*tao->ops->computedual)(tao,DL,DU);
279: } else {
280: VecSet(DL,0.0);
281: VecSet(DU,0.0);
282: }
283: return(0);
284: }
286: /*@
287: TaoGetDualVariables - Gets pointers to the dual vectors
289: Collective on Tao
291: Input Parameters:
292: . tao - the Tao context
294: Output Parameter:
295: + DE - dual variable vector for the lower bounds
296: - DI - dual variable vector for the upper bounds
298: Level: advanced
300: .seealso: TaoComputeDualVariables()
301: @*/
302: PetscErrorCode TaoGetDualVariables(Tao tao, Vec *DE, Vec *DI)
303: {
306: if (DE) {
307: *DE = tao->DE;
308: }
309: if (DI) {
310: *DI = tao->DI;
311: }
312: return(0);
313: }
315: /*@C
316: TaoSetEqualityConstraintsRoutine - Sets a function to be used to compute constraints. TAO only handles constraints under certain conditions, see manual for details
318: Logically collective on Tao
320: Input Parameters:
321: + tao - the Tao context
322: . ce - A vector that will be used to store equality constraint evaluation
323: . func - the bounds computation routine
324: - ctx - [optional] user-defined context for private data for the equality constraints computation (may be NULL)
326: Calling sequence of func:
327: $ func (Tao tao, Vec x, Vec ce, void *ctx);
329: + tao - the Tao
330: . x - point to evaluate equality constraints
331: . ce - vector of equality constraints evaluated at x
332: - ctx - the (optional) user-defined function context
334: Level: intermediate
336: .seealso: TaoSetObjectiveRoutine(), TaoSetHessianRoutine() TaoSetObjectiveAndGradientRoutine(), TaoSetVariableBounds()
338: @*/
339: PetscErrorCode TaoSetEqualityConstraintsRoutine(Tao tao, Vec ce, PetscErrorCode (*func)(Tao, Vec, Vec, void*), void *ctx)
340: {
345: if (ce) {
347: PetscObjectReference((PetscObject)ce);
348: }
349: VecDestroy(&tao->constraints_equality);
351: tao->constraints_equality = ce;
352: tao->user_con_equalityP = ctx;
353: tao->ops->computeequalityconstraints = func;
354: return(0);
355: }
358: /*@C
359: TaoSetInequalityConstraintsRoutine - Sets a function to be used to compute constraints. TAO only handles constraints under certain conditions, see manual for details
361: Logically collective on Tao
363: Input Parameters:
364: + tao - the Tao context
365: . ci - A vector that will be used to store inequality constraint evaluation
366: . func - the bounds computation routine
367: - ctx - [optional] user-defined context for private data for the inequality constraints computation (may be NULL)
369: Calling sequence of func:
370: $ func (Tao tao, Vec x, Vec ci, void *ctx);
372: + tao - the Tao
373: . x - point to evaluate inequality constraints
374: . ci - vector of inequality constraints evaluated at x
375: - ctx - the (optional) user-defined function context
377: Level: intermediate
379: .seealso: TaoSetObjectiveRoutine(), TaoSetHessianRoutine() TaoSetObjectiveAndGradientRoutine(), TaoSetVariableBounds()
381: @*/
382: PetscErrorCode TaoSetInequalityConstraintsRoutine(Tao tao, Vec ci, PetscErrorCode (*func)(Tao, Vec, Vec, void*), void *ctx)
383: {
388: if (ci) {
390: PetscObjectReference((PetscObject)ci);
391: }
392: VecDestroy(&tao->constraints_inequality);
393: tao->constraints_inequality = ci;
395: tao->user_con_inequalityP = ctx;
396: tao->ops->computeinequalityconstraints = func;
397: return(0);
398: }
401: /*@C
402: TaoComputeEqualityConstraints - Compute the variable bounds using the
403: routine set by TaoSetEqualityConstraintsRoutine().
405: Collective on Tao
407: Input Parameters:
408: . tao - the Tao context
410: Level: developer
412: .seealso: TaoSetEqualityConstraintsRoutine(), TaoComputeJacobianEquality()
413: @*/
415: PetscErrorCode TaoComputeEqualityConstraints(Tao tao, Vec X, Vec CE)
416: {
426: if (!tao->ops->computeequalityconstraints) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"TaoSetEqualityConstraintsRoutine() has not been called");
427: if (!tao->solution) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"TaoSetInitialVector must be called before TaoComputeEqualityConstraints");
428: PetscLogEventBegin(TAO_ConstraintsEval,tao,X,CE,NULL);
429: PetscStackPush("Tao equality constraints evaluation routine");
430: (*tao->ops->computeequalityconstraints)(tao,X,CE,tao->user_con_equalityP);
431: PetscStackPop;
432: PetscLogEventEnd(TAO_ConstraintsEval,tao,X,CE,NULL);
433: tao->nconstraints++;
434: return(0);
435: }
438: /*@C
439: TaoComputeInequalityConstraints - Compute the variable bounds using the
440: routine set by TaoSetInequalityConstraintsRoutine().
442: Collective on Tao
444: Input Parameters:
445: . tao - the Tao context
447: Level: developer
449: .seealso: TaoSetInequalityConstraintsRoutine(), TaoComputeJacobianInequality()
450: @*/
452: PetscErrorCode TaoComputeInequalityConstraints(Tao tao, Vec X, Vec CI)
453: {
463: if (!tao->ops->computeinequalityconstraints) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"TaoSetInequalityConstraintsRoutine() has not been called");
464: if (!tao->solution) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"TaoSetInitialVector must be called before TaoComputeInequalityConstraints");
465: PetscLogEventBegin(TAO_ConstraintsEval,tao,X,CI,NULL);
466: PetscStackPush("Tao inequality constraints evaluation routine");
467: (*tao->ops->computeinequalityconstraints)(tao,X,CI,tao->user_con_inequalityP);
468: PetscStackPop;
469: PetscLogEventEnd(TAO_ConstraintsEval,tao,X,CI,NULL);
470: tao->nconstraints++;
471: return(0);
472: }