LuksanVlcek1.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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: LuksanVlcek1.cpp 2005 2011-06-06 12:55:16Z stefan $
  6. //
  7. // Authors: Andreas Waechter IBM 2005-10-127
  8. #include "LuksanVlcek1.hpp"
  9. #ifdef HAVE_CONFIG_H
  10. #include "config.h"
  11. #else
  12. #include "configall_system.h"
  13. #endif
  14. #ifdef HAVE_CMATH
  15. # include <cmath>
  16. #else
  17. # ifdef HAVE_MATH_H
  18. # include <math.h>
  19. # else
  20. # error "don't have header file for math"
  21. # endif
  22. #endif
  23. #ifdef HAVE_CSTDIO
  24. # include <cstdio>
  25. #else
  26. # ifdef HAVE_STDIO_H
  27. # include <stdio.h>
  28. # else
  29. # error "don't have header file for stdio"
  30. # endif
  31. #endif
  32. using namespace Ipopt;
  33. LuksanVlcek1::LuksanVlcek1(Number g_l, Number g_u)
  34. {
  35. g_l_ = g_l;
  36. g_u_ = g_u;
  37. }
  38. bool LuksanVlcek1::InitializeProblem(Index N)
  39. {
  40. N_=N;
  41. if (N_<=2) {
  42. printf("N needs to be at least 3.\n");
  43. return false;
  44. }
  45. return true;
  46. }
  47. // returns the size of the problem
  48. bool LuksanVlcek1::get_nlp_info(Index& n, Index& m, Index& nnz_jac_g,
  49. Index& nnz_h_lag, IndexStyleEnum& index_style)
  50. {
  51. // The problem described in LuksanVlcek1.hpp has 4 variables, x[0] through x[3]
  52. n = N_;
  53. m = N_-2;
  54. nnz_jac_g = m*3;
  55. nnz_h_lag = n + n-1;
  56. // use the C style numbering of matrix indices (starting at 0)
  57. index_style = TNLP::C_STYLE;
  58. return true;
  59. }
  60. // returns the variable bounds
  61. bool LuksanVlcek1::get_bounds_info(Index n, Number* x_l, Number* x_u,
  62. Index m, Number* g_l, Number* g_u)
  63. {
  64. // none of the variables have bounds
  65. for (Index i=0; i<n; i++) {
  66. x_l[i] = -1e20;
  67. x_u[i] = 1e20;
  68. }
  69. // Set the bounds for the constraints
  70. for (Index i=0; i<m; i++) {
  71. g_l[i] = g_l_;
  72. g_u[i] = g_u_;
  73. }
  74. return true;
  75. }
  76. // returns the initial point for the problem
  77. bool LuksanVlcek1::get_starting_point(Index n, bool init_x, Number* x,
  78. bool init_z, Number* z_L, Number* z_U,
  79. Index m, bool init_lambda,
  80. Number* lambda)
  81. {
  82. if (!init_x || init_z || init_lambda) {
  83. return false;
  84. }
  85. // set the starting point
  86. for (Index i=0; i<n/2; i++) {
  87. x[2*i] = -1.2;
  88. x[2*i+1] = 1.;
  89. }
  90. if (n != 2*(n/2)) {
  91. x[n-1] = -1.2;
  92. }
  93. /*
  94. // DELETEME
  95. for (Index i=0; i<n; i++) {
  96. x[i] += 0.001*((Number) i);
  97. }
  98. */
  99. return true;
  100. }
  101. // returns the value of the objective function
  102. bool LuksanVlcek1::eval_f(Index n, const Number* x, bool new_x, Number& obj_value)
  103. {
  104. obj_value = 0.;
  105. for (Index i=0; i<N_-1; i++) {
  106. Number a1 = x[i]*x[i]-x[i+1];
  107. Number a2 = x[i] - 1.;
  108. obj_value += 100.*a1*a1 + a2*a2;
  109. }
  110. return true;
  111. }
  112. // return the gradient of the objective function grad_{x} f(x)
  113. bool LuksanVlcek1::eval_grad_f(Index n, const Number* x, bool new_x, Number* grad_f)
  114. {
  115. grad_f[0] = 0.;
  116. for (Index i=0; i<N_-1; i++) {
  117. grad_f[i] += 400.*x[i]*(x[i]*x[i]-x[i+1]) + 2.*(x[i]-1.);
  118. grad_f[i+1] = -200.*(x[i]*x[i]-x[i+1]);
  119. }
  120. return true;
  121. }
  122. // return the value of the constraints: g(x)
  123. bool LuksanVlcek1::eval_g(Index n, const Number* x, bool new_x, Index m, Number* g)
  124. {
  125. for (Index i=0; i<N_-2; i++) {
  126. g[i] = 3.*pow(x[i+1],3.) + 2.*x[i+2] - 5.
  127. + sin(x[i+1]-x[i+2])*sin(x[i+1]+x[i+2]) + 4.*x[i+1]
  128. - x[i]*exp(x[i]-x[i+1]) - 3.;
  129. }
  130. return true;
  131. }
  132. // return the structure or values of the jacobian
  133. bool LuksanVlcek1::eval_jac_g(Index n, const Number* x, bool new_x,
  134. Index m, Index nele_jac, Index* iRow, Index *jCol,
  135. Number* values)
  136. {
  137. if (values == NULL) {
  138. // return the structure of the jacobian
  139. Index ijac=0;
  140. for (Index i=0; i<N_-2; i++) {
  141. iRow[ijac] = i;
  142. jCol[ijac] = i;
  143. ijac++;
  144. iRow[ijac] = i;
  145. jCol[ijac] = i+1;
  146. ijac++;
  147. iRow[ijac] = i;
  148. jCol[ijac] = i+2;
  149. ijac++;
  150. }
  151. }
  152. else {
  153. // return the values of the jacobian of the constraints
  154. Index ijac=0;
  155. for (Index i=0; i<N_-2; i++) {
  156. // x[i]
  157. values[ijac] = -(1.+x[i])*exp(x[i]-x[i+1]);
  158. ijac++;
  159. // x[i+1]
  160. values[ijac] = 9.*x[i+1]*x[i+1]
  161. + cos(x[i+1]-x[i+2])*sin(x[i+1]+x[i+2])
  162. + sin(x[i+1]-x[i+2])*cos(x[i+1]+x[i+2])
  163. + 4. + x[i]*exp(x[i]-x[i+1]);
  164. ijac++;
  165. // x[i+2]
  166. values[ijac] = 2.
  167. - cos(x[i+1]-x[i+2])*sin(x[i+1]+x[i+2])
  168. + sin(x[i+1]-x[i+2])*cos(x[i+1]+x[i+2]);
  169. ijac++;
  170. }
  171. }
  172. return true;
  173. }
  174. //return the structure or values of the hessian
  175. bool LuksanVlcek1::eval_h(Index n, const Number* x, bool new_x,
  176. Number obj_factor, Index m, const Number* lambda,
  177. bool new_lambda, Index nele_hess, Index* iRow,
  178. Index* jCol, Number* values)
  179. {
  180. if (values == NULL) {
  181. Index ihes=0;
  182. for (Index i=0; i<N_; i++) {
  183. iRow[ihes] = i;
  184. jCol[ihes] = i;
  185. ihes++;
  186. if (i<N_-1) {
  187. iRow[ihes] = i;
  188. jCol[ihes] = i+1;
  189. ihes++;
  190. }
  191. }
  192. DBG_ASSERT(ihes == nele_hess);
  193. }
  194. else {
  195. Index ihes=0;
  196. for (Index i=0; i<N_; i++) {
  197. // x[i],x[i]
  198. if (i<N_-1) {
  199. values[ihes] = obj_factor*(2.+400.*(3.*x[i]*x[i]-x[i+1]));
  200. if (i<N_-2) {
  201. values[ihes] -= lambda[i]*(2.+x[i])*exp(x[i]-x[i+1]);
  202. }
  203. }
  204. else {
  205. values[ihes] = 0.;
  206. }
  207. if (i>0) {
  208. // x[i+1]x[i+1]
  209. values[ihes] += obj_factor*200.;
  210. if (i<N_-1) {
  211. values[ihes] += lambda[i-1]*(18.*x[i]
  212. - 2.*sin(x[i]-x[i+1])*sin(x[i]+x[i+1])
  213. + 2.*cos(x[i]-x[i+1])*cos(x[i]+x[i+1])
  214. - x[i-1]*exp(x[i-1]-x[i]));
  215. }
  216. }
  217. if (i>1) {
  218. // x[i+2]x[i+2]
  219. values[ihes] +=
  220. lambda[i-2]*(- 2.*sin(x[i-1]-x[i])*sin(x[i-1]+x[i])
  221. - 2.*cos(x[i-1]-x[i])*cos(x[i-1]+x[i]));
  222. }
  223. ihes++;
  224. if (i<N_-1) {
  225. // x[i],x[i+1]
  226. values[ihes] = obj_factor*(-400.*x[i]);
  227. if (i<N_-2) {
  228. values[ihes] += lambda[i]*(1.+x[i])*exp(x[i]-x[i+1]);
  229. }
  230. /*
  231. if (i>0) {
  232. // x[i+1],x[i+2]
  233. values[ihes] +=
  234. lambda[i-1]*( sin(x[i]-x[i+1])*sin(x[i]+x[i+1])
  235. + cos(x[i]-x[i+1])*cos(x[i]+x[i+1])
  236. - cos(x[i]-x[i+1])*cos(x[i]+x[i+1])
  237. - sin(x[i]-x[i+1])*sin(x[i]+x[i+1])
  238. );
  239. }
  240. */
  241. ihes++;
  242. }
  243. }
  244. DBG_ASSERT(ihes == nele_hess);
  245. }
  246. return true;
  247. }
  248. void LuksanVlcek1::finalize_solution(SolverReturn status,
  249. Index n, const Number* x, const Number* z_L, const Number* z_U,
  250. Index m, const Number* g, const Number* lambda,
  251. Number obj_value,
  252. const IpoptData* ip_data,
  253. IpoptCalculatedQuantities* ip_cq)
  254. {}