LuksanVlcek5.cpp 7.1 KB

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