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