MittelmannBndryCntrlDiri.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. // Copyright (C) 2005, 2006 International Business Machines and others.
  2. // All Rights Reserved.
  3. // This code is published under the Eclipse Public License.
  4. //
  5. // $Id: MittelmannBndryCntrlDiri.hpp 2005 2011-06-06 12:55:16Z stefan $
  6. //
  7. // Authors: Andreas Waechter IBM 2005-10-18
  8. // based on MyNLP.hpp
  9. #ifndef __MITTELMANNBNDRYCNTRLDIRI_HPP__
  10. #define __MITTELMANNBNDRYCNTRLDIRI_HPP__
  11. #include "RegisteredTNLP.hpp"
  12. #ifdef HAVE_CONFIG_H
  13. #include "config.h"
  14. #else
  15. #include "configall_system.h"
  16. #endif
  17. #ifdef HAVE_CMATH
  18. # include <cmath>
  19. #else
  20. # ifdef HAVE_MATH_H
  21. # include <math.h>
  22. # else
  23. # error "don't have header file for math"
  24. # endif
  25. #endif
  26. #ifdef HAVE_CSTDIO
  27. # include <cstdio>
  28. #else
  29. # ifdef HAVE_STDIO_H
  30. # include <stdio.h>
  31. # else
  32. # error "don't have header file for stdio"
  33. # endif
  34. #endif
  35. using namespace Ipopt;
  36. /** Base class for boundary control problems with Dirichlet boundary
  37. * conditions, as formulated by Hans Mittelmann as Examples 1-4 in
  38. * "Optimization Techniques for Solving Elliptic Control Problems
  39. * with Control and State Constraints. Part 2: Boundary Control"
  40. *
  41. * Here, the control variables are identical to the values of y on
  42. * the boundary, and therefore we don't need any explicit
  43. * optimization variables for u.
  44. */
  45. class MittelmannBndryCntrlDiriBase : public RegisteredTNLP
  46. {
  47. public:
  48. /** Constructor. */
  49. MittelmannBndryCntrlDiriBase();
  50. /** Default destructor */
  51. virtual ~MittelmannBndryCntrlDiriBase();
  52. /**@name Overloaded from TNLP */
  53. //@{
  54. /** Method to return some info about the nlp */
  55. virtual bool get_nlp_info(Index& n, Index& m, Index& nnz_jac_g,
  56. Index& nnz_h_lag, IndexStyleEnum& index_style);
  57. /** Method to return the bounds for my problem */
  58. virtual bool get_bounds_info(Index n, Number* x_l, Number* x_u,
  59. Index m, Number* g_l, Number* g_u);
  60. /** Method to return the starting point for the algorithm */
  61. virtual bool get_starting_point(Index n, bool init_x, Number* x,
  62. bool init_z, Number* z_L, Number* z_U,
  63. Index m, bool init_lambda,
  64. Number* lambda);
  65. /** Method to return the objective value */
  66. virtual bool eval_f(Index n, const Number* x, bool new_x, Number& obj_value);
  67. /** Method to return the gradient of the objective */
  68. virtual bool eval_grad_f(Index n, const Number* x, bool new_x, Number* grad_f);
  69. /** Method to return the constraint residuals */
  70. virtual bool eval_g(Index n, const Number* x, bool new_x, Index m, Number* g);
  71. /** Method to return:
  72. * 1) The structure of the jacobian (if "values" is NULL)
  73. * 2) The values of the jacobian (if "values" is not NULL)
  74. */
  75. virtual bool eval_jac_g(Index n, const Number* x, bool new_x,
  76. Index m, Index nele_jac, Index* iRow, Index *jCol,
  77. Number* values);
  78. /** Method to return:
  79. * 1) The structure of the hessian of the lagrangian (if "values" is NULL)
  80. * 2) The values of the hessian of the lagrangian (if "values" is not NULL)
  81. */
  82. virtual bool eval_h(Index n, const Number* x, bool new_x,
  83. Number obj_factor, Index m, const Number* lambda,
  84. bool new_lambda, Index nele_hess, Index* iRow,
  85. Index* jCol, Number* values);
  86. //@}
  87. /** Method for returning scaling parameters */
  88. virtual bool get_scaling_parameters(Number& obj_scaling,
  89. bool& use_x_scaling, Index n,
  90. Number* x_scaling,
  91. bool& use_g_scaling, Index m,
  92. Number* g_scaling);
  93. /** @name Solution Methods */
  94. //@{
  95. /** This method is called after the optimization, and could write an
  96. * output file with the optimal profiles */
  97. virtual void finalize_solution(SolverReturn status,
  98. Index n, const Number* x, const Number* z_L, const Number* z_U,
  99. Index m, const Number* g, const Number* lambda,
  100. Number obj_valu,
  101. const IpoptData* ip_data,
  102. IpoptCalculatedQuantities* ip_cq);
  103. //@}
  104. protected:
  105. /** Method for setting the internal parameters that define the
  106. * problem. It must be called by the child class in its
  107. * implementation of InitializeParameters. */
  108. void SetBaseParameters(Index N, Number alpha, Number lb_y,
  109. Number ub_y, Number lb_u, Number ub_u,
  110. Number d_const);
  111. /**@name Functions that defines a particular instance. */
  112. //@{
  113. /** Target profile function for y */
  114. virtual Number y_d_cont(Number x1, Number x2) const =0;
  115. //@}
  116. private:
  117. /**@name Methods to block default compiler methods.
  118. * The compiler automatically generates the following three methods.
  119. * Since the default compiler implementation is generally not what
  120. * you want (for all but the most simple classes), we usually
  121. * put the declarations of these methods in the private section
  122. * and never implement them. This prevents the compiler from
  123. * implementing an incorrect "default" behavior without us
  124. * knowing. (See Scott Meyers book, "Effective C++")
  125. *
  126. */
  127. //@{
  128. MittelmannBndryCntrlDiriBase(const MittelmannBndryCntrlDiriBase&);
  129. MittelmannBndryCntrlDiriBase& operator=(const MittelmannBndryCntrlDiriBase&);
  130. //@}
  131. /**@name Problem specification */
  132. //@{
  133. /** Number of mesh points in one dimension (excluding boundary) */
  134. Index N_;
  135. /** Step size */
  136. Number h_;
  137. /** h_ squaredd */
  138. Number hh_;
  139. /** overall lower bound on y */
  140. Number lb_y_;
  141. /** overall upper bound on y */
  142. Number ub_y_;
  143. /** overall lower bound on u */
  144. Number lb_u_;
  145. /** overall upper bound on u */
  146. Number ub_u_;
  147. /** Constant value of d appearing in elliptical equation */
  148. Number d_const_;
  149. /** Weighting parameter for the control target deviation functional
  150. * in the objective */
  151. Number alpha_;
  152. /** Array for the target profile for y */
  153. Number* y_d_;
  154. //@}
  155. /**@name Auxilliary methods */
  156. //@{
  157. /** Translation of mesh point indices to NLP variable indices for
  158. * y(x_ij) */
  159. inline Index y_index(Index i, Index j) const
  160. {
  161. return j + (N_+2)*i;
  162. }
  163. /** Translation of interior mesh point indices to the corresponding
  164. * PDE constraint number */
  165. inline Index pde_index(Index i, Index j) const
  166. {
  167. return (j-1) + N_*(i-1);
  168. }
  169. /** Compute the grid coordinate for given index in x1 direction */
  170. inline Number x1_grid(Index i) const
  171. {
  172. return h_*(Number)i;
  173. }
  174. /** Compute the grid coordinate for given index in x2 direction */
  175. inline Number x2_grid(Index i) const
  176. {
  177. return h_*(Number)i;
  178. }
  179. //@}
  180. };
  181. /** Class implementating Example 1 */
  182. class MittelmannBndryCntrlDiri1 : public MittelmannBndryCntrlDiriBase
  183. {
  184. public:
  185. MittelmannBndryCntrlDiri1()
  186. {}
  187. virtual ~MittelmannBndryCntrlDiri1()
  188. {}
  189. virtual bool InitializeProblem(Index N)
  190. {
  191. if (N<1) {
  192. printf("N has to be at least 1.");
  193. return false;
  194. }
  195. Number alpha = 0.01;
  196. Number lb_y = -1e20;
  197. Number ub_y = 3.5;
  198. Number lb_u = 0.;
  199. Number ub_u = 10.;
  200. Number d_const = -20.;
  201. SetBaseParameters(N, alpha, lb_y, ub_y, lb_u, ub_u, d_const);
  202. return true;
  203. }
  204. protected:
  205. /** Target profile function for y */
  206. virtual Number y_d_cont(Number x1, Number x2) const
  207. {
  208. return 3. + 5.*(x1*(x1-1.)*x2*(x2-1.));
  209. }
  210. private:
  211. /**@name hide implicitly defined contructors copy operators */
  212. //@{
  213. MittelmannBndryCntrlDiri1(const MittelmannBndryCntrlDiri1&);
  214. MittelmannBndryCntrlDiri1& operator=(const MittelmannBndryCntrlDiri1&);
  215. //@}
  216. };
  217. /** Class implementating Example 2 */
  218. class MittelmannBndryCntrlDiri2 : public MittelmannBndryCntrlDiriBase
  219. {
  220. public:
  221. MittelmannBndryCntrlDiri2()
  222. {}
  223. virtual ~MittelmannBndryCntrlDiri2()
  224. {}
  225. virtual bool InitializeProblem(Index N)
  226. {
  227. if (N<1) {
  228. printf("N has to be at least 1.");
  229. return false;
  230. }
  231. Number alpha = 0.;
  232. Number lb_y = -1e20;
  233. Number ub_y = 3.5;
  234. Number lb_u = 0.;
  235. Number ub_u = 10.;
  236. Number d_const = -20.;
  237. SetBaseParameters(N, alpha, lb_y, ub_y, lb_u, ub_u, d_const);
  238. return true;
  239. }
  240. protected:
  241. /** Target profile function for y */
  242. virtual Number y_d_cont(Number x1, Number x2) const
  243. {
  244. return 3. + 5.*(x1*(x1-1.)*x2*(x2-1.));
  245. }
  246. private:
  247. /**@name hide implicitly defined contructors copy operators */
  248. //@{
  249. MittelmannBndryCntrlDiri2(const MittelmannBndryCntrlDiri2&);
  250. MittelmannBndryCntrlDiri2& operator=(const MittelmannBndryCntrlDiri2&);
  251. //@}
  252. };
  253. /** Class implementating Example 3 */
  254. class MittelmannBndryCntrlDiri3 : public MittelmannBndryCntrlDiriBase
  255. {
  256. public:
  257. MittelmannBndryCntrlDiri3()
  258. {}
  259. virtual ~MittelmannBndryCntrlDiri3()
  260. {}
  261. virtual bool InitializeProblem(Index N)
  262. {
  263. if (N<1) {
  264. printf("N has to be at least 1.");
  265. return false;
  266. }
  267. Number alpha = 0.01;
  268. Number lb_y = -1e20;
  269. Number ub_y = 3.2;
  270. Number lb_u = 1.6;
  271. Number ub_u = 2.3;
  272. Number d_const = -20.;
  273. SetBaseParameters(N, alpha, lb_y, ub_y, lb_u, ub_u, d_const);
  274. return true;
  275. }
  276. protected:
  277. /** Target profile function for y */
  278. virtual Number y_d_cont(Number x1, Number x2) const
  279. {
  280. return 3. + 5.*(x1*(x1-1.)*x2*(x2-1.));
  281. }
  282. private:
  283. /**@name hide implicitly defined contructors copy operators */
  284. //@{
  285. MittelmannBndryCntrlDiri3(const MittelmannBndryCntrlDiri3&);
  286. MittelmannBndryCntrlDiri3& operator=(const MittelmannBndryCntrlDiri3&);
  287. //@}
  288. };
  289. /** Class implementating Example 4 */
  290. class MittelmannBndryCntrlDiri4 : public MittelmannBndryCntrlDiriBase
  291. {
  292. public:
  293. MittelmannBndryCntrlDiri4()
  294. {}
  295. virtual ~MittelmannBndryCntrlDiri4()
  296. {}
  297. virtual bool InitializeProblem(Index N)
  298. {
  299. if (N<1) {
  300. printf("N has to be at least 1.");
  301. return false;
  302. }
  303. Number alpha = 0.;
  304. Number lb_y = -1e20;
  305. Number ub_y = 3.2;
  306. Number lb_u = 1.6;
  307. Number ub_u = 2.3;
  308. Number d_const = -20.;
  309. SetBaseParameters(N, alpha, lb_y, ub_y, lb_u, ub_u, d_const);
  310. return true;
  311. }
  312. protected:
  313. /** Target profile function for y */
  314. virtual Number y_d_cont(Number x1, Number x2) const
  315. {
  316. return 3. + 5.*(x1*(x1-1.)*x2*(x2-1.));
  317. }
  318. private:
  319. /**@name hide implicitly defined contructors copy operators */
  320. //@{
  321. MittelmannBndryCntrlDiri4(const MittelmannBndryCntrlDiri4&);
  322. MittelmannBndryCntrlDiri4& operator=(const MittelmannBndryCntrlDiri4&);
  323. //@}
  324. };
  325. #endif