Actual source code: taosolver_bounds.c
1: #include "include/private/taosolver_impl.h" /*I "taosolver.h" I*/
5: /*@
6: TaoSetVariableBounds - Sets the upper and lower bounds
8: Logically collective on TaoSolver
10: Input Parameters:
11: + tao - the TaoSolver context
12: . XL - vector of lower bounds
13: - XU - vector of upper bounds
15: Level: beginner
17: .seealso: TaoSetObjectiveRoutine(), TaoSetHessianRoutine() TaoSetObjectiveAndGradientRoutine()
18: @*/
20: PetscErrorCode TaoSetVariableBounds(TaoSolver tao, Vec XL, Vec XU)
21: {
25: if (XL) {
27: PetscObjectReference((PetscObject)XL);
28: }
29: if (XU) {
31: PetscObjectReference((PetscObject)XU);
32: }
33: if (tao->XL) {
34: VecDestroy(&tao->XL);
35: }
36: if (tao->XU) {
37: VecDestroy(&tao->XU);
38: }
40: tao->XL = XL;
41: tao->XU = XU;
42:
43: return(0);
44: }
47: /*@C
48: TaoSetVariableBoundsRoutine - Sets a function to be used to compute variable bounds
50: Logically collective on TaoSolver
52: Input Parameters:
53: + tao - the TaoSolver context
54: . func - the bounds computation routine
55: - ctx - [optional] user-defined context for private data for the bounds computation (may be PETSC_NULL)
56:
57: Calling sequence of func:
58: $ func (TaoSolver tao, Vec xl, Vec xu);
60: + tao - the TaoSolver
61: . xl - vector of lower bounds
62: . xu - vector of upper bounds
63: - ctx - the (optional) user-defined function context
65: Level: beginner
67: .seealso: TaoSetObjectiveRoutine(), TaoSetHessianRoutine() TaoSetObjectiveAndGradientRoutine(), TaoSetVariableBounds()
69: Note: The func passed in to TaoSetVariableBoundsRoutine() takes
70: precedence over any values set in TaoSetVariableBounds().
72: @*/
73: PetscErrorCode TaoSetVariableBoundsRoutine(TaoSolver tao, PetscErrorCode (*func)(TaoSolver, Vec, Vec, void*), void *ctx)
74: {
77: tao->user_boundsP = ctx;
78: tao->ops->computebounds = func;
79: return(0);
80: }
84: PetscErrorCode TaoGetVariableBounds(TaoSolver tao, Vec *XL, Vec *XU)
85: {
88: if (XL) {
89: *XL=tao->XL;
90: }
91: if (XU) {
92: *XU=tao->XU;
93: }
94: return(0);
95: }
99: /*@C
100: TaoComputeVariableBounds - Compute the variable bounds using the
101: routine set by TaoSetVariableBoundsRoutine().
103: Collective on TaoSolver
105: Input Parameters:
106: . tao - the TaoSolver context
108: Level: developer
110: .seealso: TaoSetVariableBoundsRoutine(), TaoSetVariableBounds()
111: @*/
113: PetscErrorCode TaoComputeVariableBounds(TaoSolver tao)
114: {
119: if (tao->ops->computebounds == PETSC_NULL) {
120: return(0);
121: }
122: if (tao->XL == PETSC_NULL || tao->XU == PETSC_NULL) {
123: if (tao->solution == PETSC_NULL) {
124: SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"TaoSetInitialVector must be called before TaoComputeVariableBounds");
125: }
126: VecDuplicate(tao->solution, &tao->XL);
127: VecSet(tao->XL, TAO_NINFINITY);
128: VecDuplicate(tao->solution, &tao->XU);
129: VecSet(tao->XU, TAO_INFINITY);
130: }
131: CHKMEMQ;
132: (*tao->ops->computebounds)(tao,tao->XL,tao->XU,tao->user_boundsP);
133:
134: CHKMEMQ;
136: return(0);
137: }
142: /*@C
143: TaoComputeConstraints - Compute the variable bounds using the
144: routine set by TaoSetConstraintsRoutine().
146: Collective on TaoSolver
148: Input Parameters:
149: . tao - the TaoSolver context
151: Level: developer
153: .seealso: TaoSetConstraintsRoutine(), TaoComputeJacobian()
154: @*/
156: PetscErrorCode TaoComputeConstraints(TaoSolver tao, Vec X, Vec C)
157: {
167: if (tao->ops->computeconstraints == PETSC_NULL) {
168: SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"TaoSetConstraintsRoutine() has not been called");
169: }
170: if (tao->solution == PETSC_NULL) {
171: SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"TaoSetInitialVector must be called before TaoComputeConstraints");
172: }
173: PetscLogEventBegin(TaoSolver_ConstraintsEval,tao,X,C,PETSC_NULL);
174: PetscStackPush("TaoSolver constraints evaluation routine");
175: CHKMEMQ;
176: (*tao->ops->computeconstraints)(tao,X,C,tao->user_conP);
177:
178: CHKMEMQ;
179: PetscStackPop;
180: PetscLogEventEnd(TaoSolver_ConstraintsEval,tao,X,C,PETSC_NULL);
181: tao->nconstraints++;
182: return(0);
183: }
188: /*@C
189: TaoSetConstraintsRoutine - Sets a function to be used to compute constraints. TAO only handles constraints under certain conditions, see manual for details
191: Logically collective on TaoSolver
193: Input Parameters:
194: + tao - the TaoSolver context
195: . c - A vector that will be used to store constraint evaluation
196: . func - the bounds computation routine
197: - ctx - [optional] user-defined context for private data for the constraints computation (may be PETSC_NULL)
198:
199: Calling sequence of func:
200: $ func (TaoSolver tao, Vec x, Vec c, void *ctx);
202: + tao - the TaoSolver
203: . x - point to evaluate constraints
204: . c - vector constraints evaluated at x
205: - ctx - the (optional) user-defined function context
207: Level: intermediate
209: .seealso: TaoSetObjectiveRoutine(), TaoSetHessianRoutine() TaoSetObjectiveAndGradientRoutine(), TaoSetVariableBounds()
211: @*/
212: PetscErrorCode TaoSetConstraintsRoutine(TaoSolver tao, Vec c, PetscErrorCode (*func)(TaoSolver, Vec, Vec, void*), void *ctx)
213: {
216: tao->constraints = c;
217: tao->user_conP = ctx;
218: tao->ops->computeconstraints = func;
219: return(0);
220: }
224: /*@
225: TaoComputeDualVariables - Computes the dual vectors corresponding to the bounds
226: of the variables
228: Collective on TaoSolver
230: Input Parameters:
231: . tao - the TaoSolver context
233: Output Parameter:
234: + DL - dual variable vector for the lower bounds
235: - DU - dual variable vector for the upper bounds
237: Level: advanced
239: Note:
240: DL and DU should be created before calling this routine. If calling
241: this routine after using an unconstrained solver, DL and DU are set to all
242: zeros.
244: Level: advanced
246: .seealso: TaoComputeObjective(), TaoSetVariableBounds()
247: @*/
248: PetscErrorCode TaoComputeDualVariables(TaoSolver tao, Vec DL, Vec DU)
249: {
257: if (tao->ops->computedual) {
258: (*tao->ops->computedual)(tao,DL,DU);
259: } else {
260: VecSet(DL,0.0);
261: VecSet(DU,0.0);
262: }
263: return(0);
264: }