MittelmannDistCntrlDiri.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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: MittelmannDistCntrlDiri.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 __MITTELMANNDISTRCNTRLDIRI_HPP__
  10. #define __MITTELMANNDISTRCNTRLDIRI_HPP__
  11. #include "IpTNLP.hpp"
  12. #include "RegisteredTNLP.hpp"
  13. #ifdef HAVE_CONFIG_H
  14. #include "config.h"
  15. #else
  16. #include "configall_system.h"
  17. #endif
  18. #ifdef HAVE_CMATH
  19. # include <cmath>
  20. #else
  21. # ifdef HAVE_MATH_H
  22. # include <math.h>
  23. # else
  24. # error "don't have header file for math"
  25. # endif
  26. #endif
  27. #ifdef HAVE_CSTDIO
  28. # include <cstdio>
  29. #else
  30. # ifdef HAVE_STDIO_H
  31. # include <stdio.h>
  32. # else
  33. # error "don't have header file for stdio"
  34. # endif
  35. #endif
  36. using namespace Ipopt;
  37. /** Base class for distributed control problems with Dirichlet
  38. * boundary conditions, as formulated by Hans Mittelmann as Examples
  39. * 1-3 in "Optimization Techniques for Solving Elliptic Control
  40. * Problems with Control and State Constraints. Part 2: Distributed
  41. * Control"
  42. */
  43. class MittelmannDistCntrlDiriBase : public RegisteredTNLP
  44. {
  45. public:
  46. /** Constructor. N is the number of mesh points in one dimension
  47. * (excluding boundary). */
  48. MittelmannDistCntrlDiriBase();
  49. /** Default destructor */
  50. virtual ~MittelmannDistCntrlDiriBase();
  51. /**@name Overloaded from TNLP */
  52. //@{
  53. /** Method to return some info about the nlp */
  54. virtual bool get_nlp_info(Index& n, Index& m, Index& nnz_jac_g,
  55. Index& nnz_h_lag, IndexStyleEnum& index_style);
  56. /** Method to return the bounds for my problem */
  57. virtual bool get_bounds_info(Index n, Number* x_l, Number* x_u,
  58. Index m, Number* g_l, Number* g_u);
  59. /** Method to return the starting point for the algorithm */
  60. virtual bool get_starting_point(Index n, bool init_x, Number* x,
  61. bool init_z, Number* z_L, Number* z_U,
  62. Index m, bool init_lambda,
  63. Number* lambda);
  64. /** Method to return the objective value */
  65. virtual bool eval_f(Index n, const Number* x, bool new_x, Number& obj_value);
  66. /** Method to return the gradient of the objective */
  67. virtual bool eval_grad_f(Index n, const Number* x, bool new_x, Number* grad_f);
  68. /** Method to return the constraint residuals */
  69. virtual bool eval_g(Index n, const Number* x, bool new_x, Index m, Number* g);
  70. /** Method to return:
  71. * 1) The structure of the jacobian (if "values" is NULL)
  72. * 2) The values of the jacobian (if "values" is not NULL)
  73. */
  74. virtual bool eval_jac_g(Index n, const Number* x, bool new_x,
  75. Index m, Index nele_jac, Index* iRow, Index *jCol,
  76. Number* values);
  77. /** Method to return:
  78. * 1) The structure of the hessian of the lagrangian (if "values" is NULL)
  79. * 2) The values of the hessian of the lagrangian (if "values" is not NULL)
  80. */
  81. virtual bool eval_h(Index n, const Number* x, bool new_x,
  82. Number obj_factor, Index m, const Number* lambda,
  83. bool new_lambda, Index nele_hess, Index* iRow,
  84. Index* jCol, Number* values);
  85. //@}
  86. /** Method for returning scaling parameters */
  87. virtual bool get_scaling_parameters(Number& obj_scaling,
  88. bool& use_x_scaling, Index n,
  89. Number* x_scaling,
  90. bool& use_g_scaling, Index m,
  91. Number* g_scaling);
  92. /** @name Solution Methods */
  93. //@{
  94. /** This method is called after the optimization, and could write an
  95. * output file with the optimal profiles */
  96. virtual void finalize_solution(SolverReturn status,
  97. Index n, const Number* x, const Number* z_L, const Number* z_U,
  98. Index m, const Number* g, const Number* lambda,
  99. Number obj_value,
  100. const IpoptData* ip_data,
  101. IpoptCalculatedQuantities* ip_cq);
  102. //@}
  103. protected:
  104. /** Method for setting the internal parameters that define the
  105. * problem. It must be called by the child class in its
  106. * implementation of InitializeParameters. */
  107. void SetBaseParameters(Index N, Number alpha, Number lb_y,
  108. Number ub_y, Number lb_u, Number ub_u,
  109. Number u_init);
  110. /**@name Functions that defines a particular instance. */
  111. //@{
  112. /** Target profile function for y */
  113. virtual Number y_d_cont(Number x1, Number x2) const =0;
  114. /** Forcing function for the elliptic equation */
  115. virtual Number d_cont(Number x1, Number x2, Number y, Number u) const =0;
  116. /** First partial derivative of forcing function w.r.t. y */
  117. virtual Number d_cont_dy(Number x1, Number x2, Number y, Number u) const =0;
  118. /** First partial derivative of forcing function w.r.t. u */
  119. virtual Number d_cont_du(Number x1, Number x2, Number y, Number u) const =0;
  120. /** Second partial derivative of forcing function w.r.t. y,y */
  121. virtual Number d_cont_dydy(Number x1, Number x2, Number y, Number u) const =0;
  122. //@}
  123. private:
  124. /**@name Methods to block default compiler methods.
  125. * The compiler automatically generates the following three methods.
  126. * Since the default compiler implementation is generally not what
  127. * you want (for all but the most simple classes), we usually
  128. * put the declarations of these methods in the private section
  129. * and never implement them. This prevents the compiler from
  130. * implementing an incorrect "default" behavior without us
  131. * knowing. (See Scott Meyers book, "Effective C++")
  132. *
  133. */
  134. //@{
  135. MittelmannDistCntrlDiriBase(const MittelmannDistCntrlDiriBase&);
  136. MittelmannDistCntrlDiriBase& operator=(const MittelmannDistCntrlDiriBase&);
  137. //@}
  138. /**@name Problem specification */
  139. //@{
  140. /** Number of mesh points in one dimension (excluding boundary) */
  141. Index N_;
  142. /** Step size */
  143. Number h_;
  144. /** h_ squaredd */
  145. Number hh_;
  146. /** overall lower bound on y */
  147. Number lb_y_;
  148. /** overall upper bound on y */
  149. Number ub_y_;
  150. /** overall lower bound on u */
  151. Number lb_u_;
  152. /** overall upper bound on u */
  153. Number ub_u_;
  154. /** Initial value for the constrols u */
  155. Number u_init_;
  156. /** Weighting parameter for the control target deviation functional
  157. * in the objective */
  158. Number alpha_;
  159. /** Array for the target profile for y */
  160. Number* y_d_;
  161. //@}
  162. /**@name Auxilliary methods */
  163. //@{
  164. /** Translation of mesh point indices to NLP variable indices for
  165. * y(x_ij) */
  166. inline Index y_index(Index i, Index j) const
  167. {
  168. return j + (N_+2)*i;
  169. }
  170. /** Translation of mesh point indices to NLP variable indices for
  171. * u(x_ij) */
  172. inline Index u_index(Index i, Index j) const
  173. {
  174. return (N_+2)*(N_+2) + (j-1) + (N_)*(i-1);
  175. }
  176. /** Translation of interior mesh point indices to the corresponding
  177. * PDE constraint number */
  178. inline Index pde_index(Index i, Index j) const
  179. {
  180. return (j-1) + N_*(i-1);
  181. }
  182. /** Compute the grid coordinate for given index in x1 direction */
  183. inline Number x1_grid(Index i) const
  184. {
  185. return h_*(Number)i;
  186. }
  187. /** Compute the grid coordinate for given index in x2 direction */
  188. inline Number x2_grid(Index i) const
  189. {
  190. return h_*(Number)i;
  191. }
  192. //@}
  193. };
  194. /** Class implementating Example 1 */
  195. class MittelmannDistCntrlDiri1 : public MittelmannDistCntrlDiriBase
  196. {
  197. public:
  198. MittelmannDistCntrlDiri1()
  199. {}
  200. virtual ~MittelmannDistCntrlDiri1()
  201. {}
  202. virtual bool InitializeProblem(Index N)
  203. {
  204. if (N<1) {
  205. printf("N has to be at least 1.");
  206. return false;
  207. }
  208. Number alpha = 0.001;
  209. Number lb_y = -1e20;
  210. Number ub_y = 0.185;
  211. Number lb_u = 1.5;
  212. Number ub_u = 4.5;
  213. Number u_init = (ub_u+lb_u)/2.;
  214. SetBaseParameters(N, alpha, lb_y, ub_y, lb_u, ub_u, u_init);
  215. return true;
  216. }
  217. protected:
  218. /** Target profile function for y */
  219. virtual Number y_d_cont(Number x1, Number x2) const
  220. {
  221. return 1. + 2.*(x1*(x1-1.)+x2*(x2-1.));
  222. }
  223. /** Forcing function for the elliptic equation */
  224. virtual Number d_cont(Number x1, Number x2, Number y, Number u) const
  225. {
  226. return pow(y,3) - y - u;
  227. }
  228. /** First partial derivative of forcing function w.r.t. y */
  229. virtual Number d_cont_dy(Number x1, Number x2, Number y, Number u) const
  230. {
  231. return 3.*y*y - 1.;
  232. }
  233. /** First partial derivative of forcing function w.r.t. u */
  234. virtual Number d_cont_du(Number x1, Number x2, Number y, Number u) const
  235. {
  236. return -1.;
  237. }
  238. /** Second partial derivative of forcing function w.r.t y,y */
  239. virtual Number d_cont_dydy(Number x1, Number x2, Number y, Number u) const
  240. {
  241. return 6.*y;
  242. }
  243. private:
  244. /**@name hide implicitly defined contructors copy operators */
  245. //@{
  246. MittelmannDistCntrlDiri1(const MittelmannDistCntrlDiri1&);
  247. MittelmannDistCntrlDiri1& operator=(const MittelmannDistCntrlDiri1&);
  248. //@}
  249. };
  250. /** Class implementating Example 2 */
  251. class MittelmannDistCntrlDiri2 : public MittelmannDistCntrlDiriBase
  252. {
  253. public:
  254. MittelmannDistCntrlDiri2()
  255. {}
  256. virtual ~MittelmannDistCntrlDiri2()
  257. {}
  258. virtual bool InitializeProblem(Index N)
  259. {
  260. if (N<1) {
  261. printf("N has to be at least 1.");
  262. return false;
  263. }
  264. Number alpha = 0.;
  265. Number lb_y = -1e20;
  266. Number ub_y = 0.185;
  267. Number lb_u = 1.5;
  268. Number ub_u = 4.5;
  269. Number u_init = (ub_u+lb_u)/2.;
  270. SetBaseParameters(N, alpha, lb_y, ub_y, lb_u, ub_u, u_init);
  271. return true;
  272. }
  273. protected:
  274. /** Target profile function for y */
  275. virtual Number y_d_cont(Number x1, Number x2) const
  276. {
  277. return 1. + 2.*(x1*(x1-1.)+x2*(x2-1.));
  278. }
  279. /** Forcing function for the elliptic equation */
  280. virtual Number d_cont(Number x1, Number x2, Number y, Number u) const
  281. {
  282. return pow(y,3) - y - u;
  283. }
  284. /** First partial derivative of forcing function w.r.t. y */
  285. virtual Number d_cont_dy(Number x1, Number x2, Number y, Number u) const
  286. {
  287. return 3.*y*y - 1.;
  288. }
  289. /** First partial derivative of forcing function w.r.t. u */
  290. virtual Number d_cont_du(Number x1, Number x2, Number y, Number u) const
  291. {
  292. return -1.;
  293. }
  294. /** Second partial derivative of forcing function w.r.t y,y */
  295. virtual Number d_cont_dydy(Number x1, Number x2, Number y, Number u) const
  296. {
  297. return 6.*y;
  298. }
  299. private:
  300. /**@name hide implicitly defined contructors copy operators */
  301. //@{
  302. MittelmannDistCntrlDiri2(const MittelmannDistCntrlDiri2&);
  303. MittelmannDistCntrlDiri2& operator=(const MittelmannDistCntrlDiri2&);
  304. //@}
  305. };
  306. /** Class implementating Example 3 */
  307. class MittelmannDistCntrlDiri3 : public MittelmannDistCntrlDiriBase
  308. {
  309. public:
  310. MittelmannDistCntrlDiri3()
  311. :
  312. pi_(4.*atan(1.))
  313. {}
  314. virtual ~MittelmannDistCntrlDiri3()
  315. {}
  316. virtual bool InitializeProblem(Index N)
  317. {
  318. if (N<1) {
  319. printf("N has to be at least 1.");
  320. return false;
  321. }
  322. Number alpha = 0.001;
  323. Number lb_y = -1e20;
  324. Number ub_y = 0.11;
  325. Number lb_u = -5;
  326. Number ub_u = 5.;
  327. Number u_init = (ub_u+lb_u)/2.;
  328. SetBaseParameters(N, alpha, lb_y, ub_y, lb_u, ub_u, u_init);
  329. return true;
  330. }
  331. protected:
  332. /** Target profile function for y */
  333. virtual Number y_d_cont(Number x1, Number x2) const
  334. {
  335. return sin(2.*pi_*x1)*sin(2.*pi_*x2);
  336. }
  337. /** Forcing function for the elliptic equation */
  338. virtual Number d_cont(Number x1, Number x2, Number y, Number u) const
  339. {
  340. return -exp(y) - u;
  341. }
  342. /** First partial derivative of forcing function w.r.t. y */
  343. virtual Number d_cont_dy(Number x1, Number x2, Number y, Number u) const
  344. {
  345. return -exp(y);
  346. }
  347. /** First partial derivative of forcing function w.r.t. u */
  348. virtual Number d_cont_du(Number x1, Number x2, Number y, Number u) const
  349. {
  350. return -1.;
  351. }
  352. /** Second partial derivative of forcing function w.r.t y,y */
  353. virtual Number d_cont_dydy(Number x1, Number x2, Number y, Number u) const
  354. {
  355. return -exp(y);
  356. }
  357. private:
  358. /**@name hide implicitly defined contructors copy operators */
  359. //@{
  360. MittelmannDistCntrlDiri3(const MittelmannDistCntrlDiri3&);
  361. MittelmannDistCntrlDiri3& operator=(const MittelmannDistCntrlDiri3&);
  362. //@}
  363. /** Value of pi (made available for convenience) */
  364. const Number pi_;
  365. };
  366. class MittelmannDistCntrlDiri3a : public MittelmannDistCntrlDiriBase
  367. {
  368. public:
  369. MittelmannDistCntrlDiri3a()
  370. :
  371. pi_(4.*atan(1.))
  372. {}
  373. virtual ~MittelmannDistCntrlDiri3a()
  374. {}
  375. virtual bool InitializeProblem(Index N)
  376. {
  377. if (N<1) {
  378. printf("N has to be at least 1.");
  379. return false;
  380. }
  381. Number alpha = 0.;
  382. Number lb_y = -1e20;
  383. Number ub_y = 0.11;
  384. Number lb_u = -5;
  385. Number ub_u = 5.;
  386. Number u_init = (ub_u+lb_u)/2.;
  387. SetBaseParameters(N, alpha, lb_y, ub_y, lb_u, ub_u, u_init);
  388. return true;
  389. }
  390. protected:
  391. /** Target profile function for y */
  392. virtual Number y_d_cont(Number x1, Number x2) const
  393. {
  394. return sin(2.*pi_*x1)*sin(2.*pi_*x2);
  395. }
  396. /** Forcing function for the elliptic equation */
  397. virtual Number d_cont(Number x1, Number x2, Number y, Number u) const
  398. {
  399. return -exp(y) - u;
  400. }
  401. /** First partial derivative of forcing function w.r.t. y */
  402. virtual Number d_cont_dy(Number x1, Number x2, Number y, Number u) const
  403. {
  404. return -exp(y);
  405. }
  406. /** First partial derivative of forcing function w.r.t. u */
  407. virtual Number d_cont_du(Number x1, Number x2, Number y, Number u) const
  408. {
  409. return -1.;
  410. }
  411. /** Second partial derivative of forcing function w.r.t y,y */
  412. virtual Number d_cont_dydy(Number x1, Number x2, Number y, Number u) const
  413. {
  414. return -exp(y);
  415. }
  416. private:
  417. /**@name hide implicitly defined contructors copy operators */
  418. //@{
  419. MittelmannDistCntrlDiri3a(const MittelmannDistCntrlDiri3a&);
  420. MittelmannDistCntrlDiri3a& operator=(const MittelmannDistCntrlDiri3a&);
  421. //@}
  422. /** Value of pi (made available for convenience) */
  423. const Number pi_;
  424. };
  425. #endif