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