xref: /petsc/src/tao/interface/taosolver_bounds.c (revision f155c23239e6d3f7c7ec79ff00b4f28519d0ce99)
1 #include <petsc/private/taoimpl.h> /*I "petsctao.h" I*/
2 
3 /*@
4   TaoSetVariableBounds - Sets the upper and lower bounds
5 
6   Logically collective on Tao
7 
8   Input Parameters:
9 + tao - the Tao context
10 . XL  - vector of lower bounds
11 - XU  - vector of upper bounds
12 
13   Level: beginner
14 
15 .seealso: TaoSetObjective(), TaoSetHessian(), TaoSetObjectiveAndGradient(), TaoGetVariableBounds()
16 @*/
17 PetscErrorCode TaoSetVariableBounds(Tao tao, Vec XL, Vec XU)
18 {
19   PetscErrorCode ierr;
20 
21   PetscFunctionBegin;
22   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
23   if (XL) PetscValidHeaderSpecific(XL,VEC_CLASSID,2);
24   if (XU) PetscValidHeaderSpecific(XU,VEC_CLASSID,3);
25   ierr = PetscObjectReference((PetscObject)XL);CHKERRQ(ierr);
26   ierr = PetscObjectReference((PetscObject)XU);CHKERRQ(ierr);
27   ierr = VecDestroy(&tao->XL);CHKERRQ(ierr);
28   ierr = VecDestroy(&tao->XU);CHKERRQ(ierr);
29   tao->XL = XL;
30   tao->XU = XU;
31   tao->bounded = (PetscBool)(XL || XU);
32   PetscFunctionReturn(0);
33 }
34 
35 /*@C
36   TaoSetVariableBoundsRoutine - Sets a function to be used to compute variable bounds
37 
38   Logically collective on Tao
39 
40   Input Parameters:
41 + tao - the Tao context
42 . func - the bounds computation routine
43 - ctx - [optional] user-defined context for private data for the bounds computation (may be NULL)
44 
45   Calling sequence of func:
46 $      func (Tao tao, Vec xl, Vec xu);
47 
48 + tao - the Tao
49 . xl  - vector of lower bounds
50 . xu  - vector of upper bounds
51 - ctx - the (optional) user-defined function context
52 
53   Level: beginner
54 
55 .seealso: TaoSetObjective(), TaoSetHessian(), TaoSetObjectiveAndGradient(), TaoSetVariableBounds()
56 
57 Note: The func passed in to TaoSetVariableBoundsRoutine() takes
58 precedence over any values set in TaoSetVariableBounds().
59 
60 @*/
61 PetscErrorCode TaoSetVariableBoundsRoutine(Tao tao, PetscErrorCode (*func)(Tao, Vec, Vec, void*), void *ctx)
62 {
63   PetscFunctionBegin;
64   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
65   tao->user_boundsP = ctx;
66   tao->ops->computebounds = func;
67   tao->bounded = func ? PETSC_TRUE : PETSC_FALSE;
68   PetscFunctionReturn(0);
69 }
70 
71 /*@
72   TaoGetVariableBounds - Gets the upper and lower bounds vectors set with TaoSetVariableBounds
73 
74   Not collective
75 
76   Input Parameter:
77 . tao - the Tao context
78 
79   Output Parametrs:
80 + XL  - vector of lower bounds
81 - XU  - vector of upper bounds
82 
83   Level: beginner
84 
85 .seealso: TaoSetObjective(), TaoSetHessian(), TaoSetObjectiveAndGradient(), TaoSetVariableBounds()
86 @*/
87 PetscErrorCode TaoGetVariableBounds(Tao tao, Vec *XL, Vec *XU)
88 {
89   PetscFunctionBegin;
90   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
91   if (XL) *XL = tao->XL;
92   if (XU) *XU = tao->XU;
93   PetscFunctionReturn(0);
94 }
95 
96 /*@C
97    TaoComputeVariableBounds - Compute the variable bounds using the
98    routine set by TaoSetVariableBoundsRoutine().
99 
100    Collective on Tao
101 
102    Input Parameter:
103 .  tao - the Tao context
104 
105    Level: developer
106 
107 .seealso: TaoSetVariableBoundsRoutine(), TaoSetVariableBounds()
108 @*/
109 
110 PetscErrorCode TaoComputeVariableBounds(Tao tao)
111 {
112   PetscErrorCode ierr;
113 
114   PetscFunctionBegin;
115   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
116   if (!tao->XL || !tao->XU) {
117     PetscCheck(tao->solution,PetscObjectComm((PetscObject)tao),PETSC_ERR_ORDER,"TaoSetSolution must be called before TaoComputeVariableBounds");
118     ierr = VecDuplicate(tao->solution, &tao->XL);CHKERRQ(ierr);
119     ierr = VecSet(tao->XL, PETSC_NINFINITY);CHKERRQ(ierr);
120     ierr = VecDuplicate(tao->solution, &tao->XU);CHKERRQ(ierr);
121     ierr = VecSet(tao->XU, PETSC_INFINITY);CHKERRQ(ierr);
122   }
123   if (tao->ops->computebounds) {
124     PetscStackPush("Tao compute variable bounds");
125     ierr = (*tao->ops->computebounds)(tao,tao->XL,tao->XU,tao->user_boundsP);CHKERRQ(ierr);
126     PetscStackPop;
127   }
128   PetscFunctionReturn(0);
129 }
130 
131 /*@
132   TaoSetInequalityBounds - Sets the upper and lower bounds
133 
134   Logically collective on Tao
135 
136   Input Parameters:
137 + tao - the Tao context
138 . IL  - vector of lower bounds
139 - IU  - vector of upper bounds
140 
141   Level: beginner
142 
143 .seealso: TaoSetObjective(), TaoSetHessian(), TaoSetObjectiveAndGradient(), TaoGetInequalityBounds()
144 @*/
145 PetscErrorCode TaoSetInequalityBounds(Tao tao, Vec IL, Vec IU)
146 {
147   PetscErrorCode ierr;
148 
149   PetscFunctionBegin;
150   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
151   if (IL) PetscValidHeaderSpecific(IL,VEC_CLASSID,2);
152   if (IU) PetscValidHeaderSpecific(IU,VEC_CLASSID,3);
153   ierr = PetscObjectReference((PetscObject)IL);CHKERRQ(ierr);
154   ierr = PetscObjectReference((PetscObject)IU);CHKERRQ(ierr);
155   ierr = VecDestroy(&tao->IL);CHKERRQ(ierr);
156   ierr = VecDestroy(&tao->IU);CHKERRQ(ierr);
157   tao->IL = IL;
158   tao->IU = IU;
159   tao->ineq_doublesided = (PetscBool)(IL || IU);
160   PetscFunctionReturn(0);
161 }
162 
163 /*@
164   TaoGetInequalityBounds - Gets the upper and lower bounds set via TaoSetInequalityBounds
165 
166   Logically collective on Tao
167 
168   Input Parameter:
169 . tao - the Tao context
170 
171   Output Parameters:
172 + IL  - vector of lower bounds
173 - IU  - vector of upper bounds
174 
175   Level: beginner
176 
177 .seealso: TaoSetObjective(), TaoSetHessian(), TaoSetObjectiveAndGradient(), TaoSetInequalityBounds()
178 @*/
179 PetscErrorCode TaoGetInequalityBounds(Tao tao, Vec *IL, Vec *IU)
180 {
181   PetscFunctionBegin;
182   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
183   if (IL) *IL = tao->IL;
184   if (IU) *IU = tao->IU;
185   PetscFunctionReturn(0);
186 }
187 
188 /*@C
189    TaoComputeConstraints - Compute the variable bounds using the
190    routine set by TaoSetConstraintsRoutine().
191 
192    Collective on Tao
193 
194    Input Parameters:
195 .  tao - the Tao context
196 
197    Level: developer
198 
199 .seealso: TaoSetConstraintsRoutine(), TaoComputeJacobian()
200 @*/
201 
202 PetscErrorCode TaoComputeConstraints(Tao tao, Vec X, Vec C)
203 {
204   PetscErrorCode ierr;
205 
206   PetscFunctionBegin;
207   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
208   PetscValidHeaderSpecific(X,VEC_CLASSID,2);
209   PetscValidHeaderSpecific(C,VEC_CLASSID,3);
210   PetscCheckSameComm(tao,1,X,2);
211   PetscCheckSameComm(tao,1,C,3);
212   PetscCheck(tao->ops->computeconstraints,PetscObjectComm((PetscObject)tao),PETSC_ERR_ARG_WRONGSTATE,"TaoSetConstraintsRoutine() has not been called");
213   ierr = PetscLogEventBegin(TAO_ConstraintsEval,tao,X,C,NULL);CHKERRQ(ierr);
214   PetscStackPush("Tao constraints evaluation routine");
215   ierr = (*tao->ops->computeconstraints)(tao,X,C,tao->user_conP);CHKERRQ(ierr);
216   PetscStackPop;
217   ierr = PetscLogEventEnd(TAO_ConstraintsEval,tao,X,C,NULL);CHKERRQ(ierr);
218   tao->nconstraints++;
219   PetscFunctionReturn(0);
220 }
221 
222 /*@C
223   TaoSetConstraintsRoutine - Sets a function to be used to compute constraints.  TAO only handles constraints under certain conditions, see manual for details
224 
225   Logically collective on Tao
226 
227   Input Parameters:
228 + tao - the Tao context
229 . c   - A vector that will be used to store constraint evaluation
230 . func - the bounds computation routine
231 - ctx - [optional] user-defined context for private data for the constraints computation (may be NULL)
232 
233   Calling sequence of func:
234 $      func (Tao tao, Vec x, Vec c, void *ctx);
235 
236 + tao - the Tao
237 . x   - point to evaluate constraints
238 . c   - vector constraints evaluated at x
239 - ctx - the (optional) user-defined function context
240 
241   Level: intermediate
242 
243 .seealso: TaoSetObjective(), TaoSetHessian(), TaoSetObjectiveAndGradient(), TaoSetVariablevBounds()
244 
245 @*/
246 PetscErrorCode TaoSetConstraintsRoutine(Tao tao, Vec c, PetscErrorCode (*func)(Tao, Vec, Vec, void*), void *ctx)
247 {
248   PetscErrorCode ierr;
249 
250   PetscFunctionBegin;
251   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
252   if (c) PetscValidHeaderSpecific(c,VEC_CLASSID,2);
253   ierr = PetscObjectReference((PetscObject)c);CHKERRQ(ierr);
254   ierr = VecDestroy(&tao->constraints);CHKERRQ(ierr);
255   tao->constrained = func ? PETSC_TRUE : PETSC_FALSE;
256   tao->constraints = c;
257   tao->user_conP = ctx;
258   tao->ops->computeconstraints = func;
259   PetscFunctionReturn(0);
260 }
261 
262 /*@
263   TaoComputeDualVariables - Computes the dual vectors corresponding to the bounds
264   of the variables
265 
266   Collective on Tao
267 
268   Input Parameter:
269 . tao - the Tao context
270 
271   Output Parameters:
272 + DL - dual variable vector for the lower bounds
273 - DU - dual variable vector for the upper bounds
274 
275   Level: advanced
276 
277   Note:
278   DL and DU should be created before calling this routine.  If calling
279   this routine after using an unconstrained solver, DL and DU are set to all
280   zeros.
281 
282   Level: advanced
283 
284 .seealso: TaoComputeObjective(), TaoSetVariableBounds()
285 @*/
286 PetscErrorCode TaoComputeDualVariables(Tao tao, Vec DL, Vec DU)
287 {
288   PetscErrorCode ierr;
289 
290   PetscFunctionBegin;
291   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
292   PetscValidHeaderSpecific(DL,VEC_CLASSID,2);
293   PetscValidHeaderSpecific(DU,VEC_CLASSID,3);
294   PetscCheckSameComm(tao,1,DL,2);
295   PetscCheckSameComm(tao,1,DU,3);
296   if (tao->ops->computedual) {
297     ierr = (*tao->ops->computedual)(tao,DL,DU);CHKERRQ(ierr);
298   } else {
299     ierr = VecSet(DL,0.0);CHKERRQ(ierr);
300     ierr = VecSet(DU,0.0);CHKERRQ(ierr);
301   }
302   PetscFunctionReturn(0);
303 }
304 
305 /*@
306   TaoGetDualVariables - Gets pointers to the dual vectors
307 
308   Collective on Tao
309 
310   Input Parameter:
311 . tao - the Tao context
312 
313   Output Parameters:
314 + DE - dual variable vector for the lower bounds
315 - DI - dual variable vector for the upper bounds
316 
317   Level: advanced
318 
319 .seealso: TaoComputeDualVariables()
320 @*/
321 PetscErrorCode TaoGetDualVariables(Tao tao, Vec *DE, Vec *DI)
322 {
323   PetscFunctionBegin;
324   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
325   if (DE) *DE = tao->DE;
326   if (DI) *DI = tao->DI;
327   PetscFunctionReturn(0);
328 }
329 
330 /*@C
331   TaoSetEqualityConstraintsRoutine - Sets a function to be used to compute constraints.  TAO only handles constraints under certain conditions, see manual for details
332 
333   Logically collective on Tao
334 
335   Input Parameters:
336 + tao - the Tao context
337 . ce   - A vector that will be used to store equality constraint evaluation
338 . func - the bounds computation routine
339 - ctx - [optional] user-defined context for private data for the equality constraints computation (may be NULL)
340 
341   Calling sequence of func:
342 $      func (Tao tao, Vec x, Vec ce, void *ctx);
343 
344 + tao - the Tao
345 . x   - point to evaluate equality constraints
346 . ce   - vector of equality constraints evaluated at x
347 - ctx - the (optional) user-defined function context
348 
349   Level: intermediate
350 
351 .seealso: TaoSetObjective(), TaoSetHessian(), TaoSetObjectiveAndGradient(), TaoSetVariableBounds()
352 
353 @*/
354 PetscErrorCode TaoSetEqualityConstraintsRoutine(Tao tao, Vec ce, PetscErrorCode (*func)(Tao, Vec, Vec, void*), void *ctx)
355 {
356   PetscErrorCode ierr;
357 
358   PetscFunctionBegin;
359   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
360   if (ce) PetscValidHeaderSpecific(ce,VEC_CLASSID,2);
361   ierr = PetscObjectReference((PetscObject)ce);CHKERRQ(ierr);
362   ierr = VecDestroy(&tao->constraints_equality);CHKERRQ(ierr);
363   tao->eq_constrained = func ? PETSC_TRUE : PETSC_FALSE;
364   tao->constraints_equality = ce;
365   tao->user_con_equalityP = ctx;
366   tao->ops->computeequalityconstraints = func;
367   PetscFunctionReturn(0);
368 }
369 
370 /*@C
371   TaoSetInequalityConstraintsRoutine - Sets a function to be used to compute constraints.  TAO only handles constraints under certain conditions, see manual for details
372 
373   Logically collective on Tao
374 
375   Input Parameters:
376 + tao - the Tao context
377 . ci   - A vector that will be used to store inequality constraint evaluation
378 . func - the bounds computation routine
379 - ctx - [optional] user-defined context for private data for the inequality constraints computation (may be NULL)
380 
381   Calling sequence of func:
382 $      func (Tao tao, Vec x, Vec ci, void *ctx);
383 
384 + tao - the Tao
385 . x   - point to evaluate inequality constraints
386 . ci   - vector of inequality constraints evaluated at x
387 - ctx - the (optional) user-defined function context
388 
389   Level: intermediate
390 
391 .seealso: TaoSetObjective(), TaoSetHessian(), TaoSetObjectiveAndGradient(), TaoSetVariableBounds()
392 
393 @*/
394 PetscErrorCode TaoSetInequalityConstraintsRoutine(Tao tao, Vec ci, PetscErrorCode (*func)(Tao, Vec, Vec, void*), void *ctx)
395 {
396   PetscErrorCode ierr;
397 
398   PetscFunctionBegin;
399   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
400   if (ci) PetscValidHeaderSpecific(ci,VEC_CLASSID,2);
401   ierr = PetscObjectReference((PetscObject)ci);CHKERRQ(ierr);
402   ierr = VecDestroy(&tao->constraints_inequality);CHKERRQ(ierr);
403   tao->constraints_inequality = ci;
404   tao->ineq_constrained = func ? PETSC_TRUE : PETSC_FALSE;
405   tao->user_con_inequalityP = ctx;
406   tao->ops->computeinequalityconstraints = func;
407   PetscFunctionReturn(0);
408 }
409 
410 /*@C
411    TaoComputeEqualityConstraints - Compute the variable bounds using the
412    routine set by TaoSetEqualityConstraintsRoutine().
413 
414    Collective on Tao
415 
416    Input Parameters:
417 .  tao - the Tao context
418 
419    Level: developer
420 
421 .seealso: TaoSetEqualityConstraintsRoutine(), TaoComputeJacobianEquality()
422 @*/
423 
424 PetscErrorCode TaoComputeEqualityConstraints(Tao tao, Vec X, Vec CE)
425 {
426   PetscErrorCode ierr;
427 
428   PetscFunctionBegin;
429   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
430   PetscValidHeaderSpecific(X,VEC_CLASSID,2);
431   PetscValidHeaderSpecific(CE,VEC_CLASSID,3);
432   PetscCheckSameComm(tao,1,X,2);
433   PetscCheckSameComm(tao,1,CE,3);
434   PetscCheck(tao->ops->computeequalityconstraints,PetscObjectComm((PetscObject)tao),PETSC_ERR_ARG_WRONGSTATE,"TaoSetEqualityConstraintsRoutine() has not been called");
435   ierr = PetscLogEventBegin(TAO_ConstraintsEval,tao,X,CE,NULL);CHKERRQ(ierr);
436   PetscStackPush("Tao equality constraints evaluation routine");
437   ierr = (*tao->ops->computeequalityconstraints)(tao,X,CE,tao->user_con_equalityP);CHKERRQ(ierr);
438   PetscStackPop;
439   ierr = PetscLogEventEnd(TAO_ConstraintsEval,tao,X,CE,NULL);CHKERRQ(ierr);
440   tao->nconstraints++;
441   PetscFunctionReturn(0);
442 }
443 
444 /*@C
445    TaoComputeInequalityConstraints - Compute the variable bounds using the
446    routine set by TaoSetInequalityConstraintsRoutine().
447 
448    Collective on Tao
449 
450    Input Parameters:
451 .  tao - the Tao context
452 
453    Level: developer
454 
455 .seealso: TaoSetInequalityConstraintsRoutine(), TaoComputeJacobianInequality()
456 @*/
457 
458 PetscErrorCode TaoComputeInequalityConstraints(Tao tao, Vec X, Vec CI)
459 {
460   PetscErrorCode ierr;
461 
462   PetscFunctionBegin;
463   PetscValidHeaderSpecific(tao,TAO_CLASSID,1);
464   PetscValidHeaderSpecific(X,VEC_CLASSID,2);
465   PetscValidHeaderSpecific(CI,VEC_CLASSID,3);
466   PetscCheckSameComm(tao,1,X,2);
467   PetscCheckSameComm(tao,1,CI,3);
468   PetscCheck(tao->ops->computeinequalityconstraints,PetscObjectComm((PetscObject)tao),PETSC_ERR_ARG_WRONGSTATE,"TaoSetInequalityConstraintsRoutine() has not been called");
469   ierr = PetscLogEventBegin(TAO_ConstraintsEval,tao,X,CI,NULL);CHKERRQ(ierr);
470   PetscStackPush("Tao inequality constraints evaluation routine");
471   ierr = (*tao->ops->computeinequalityconstraints)(tao,X,CI,tao->user_con_inequalityP);CHKERRQ(ierr);
472   PetscStackPop;
473   ierr = PetscLogEventEnd(TAO_ConstraintsEval,tao,X,CI,NULL);CHKERRQ(ierr);
474   tao->nconstraints++;
475   PetscFunctionReturn(0);
476 }
477