LuksanVlcek6.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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: LuksanVlcek6.cpp 2005 2011-06-06 12:55:16Z stefan $
  6. //
  7. // Authors: Andreas Waechter IBM 2005-10-127
  8. #include "LuksanVlcek6.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. LuksanVlcek6::LuksanVlcek6(Number g_l, Number g_u)
  43. {
  44. g_l_ = g_l;
  45. g_u_ = g_u;
  46. }
  47. bool LuksanVlcek6::InitializeProblem(Index N)
  48. {
  49. N_=N;
  50. if (2*(N_/2)!=N_ || N<=1) {
  51. printf("N needs to be even and at least 2.\n");
  52. return false;
  53. }
  54. return true;
  55. }
  56. // returns the size of the problem
  57. bool LuksanVlcek6::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 LuksanVlcek6.hpp has 4 variables, x[0] through x[3]
  61. n = N_+1;
  62. m = N_/2;
  63. nnz_jac_g = 3 * m;
  64. nnz_h_lag = n + n-1 + n-2 + n-3 + n-4 + n-5 + n-6;
  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 LuksanVlcek6::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=0; i<n; i++) {
  75. x_l[i] = -1e20;
  76. x_u[i] = 1e20;
  77. }
  78. // Set the bounds for the constraints
  79. for (Index i=0; i<m; i++) {
  80. g_l[i] = g_l_;
  81. g_u[i] = g_u_;
  82. }
  83. return true;
  84. }
  85. // returns the initial point for the problem
  86. bool LuksanVlcek6::get_starting_point(Index n, bool init_x, Number* x,
  87. bool init_z, Number* z_L, Number* z_U,
  88. Index m, bool init_lambda,
  89. Number* lambda)
  90. {
  91. if (!init_x || init_z || init_lambda) {
  92. return false;
  93. }
  94. // set the starting point
  95. for (Index i=0; i<n; i++) {
  96. x[i] = 3.;
  97. }
  98. /*
  99. // DELETEME
  100. for (Index i=0; i<n; i++) {
  101. x[i] += 0.1*((Number) i);
  102. }
  103. */
  104. return true;
  105. }
  106. // returns the value of the objective function
  107. bool LuksanVlcek6::eval_f(Index n, const Number* x, bool new_x, Number& obj_value)
  108. {
  109. Number p = 7./3.;
  110. obj_value = 0.;
  111. for (Index i=0; i<N_; i++) {
  112. Number b = (2.+5.*x[i]*x[i])*x[i] + 1.;
  113. for (Index j=Max(0,i-5); j<=Min(N_-1,i+1); j++) {
  114. b += x[j]*(1.+x[j]);
  115. }
  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 LuksanVlcek6::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. for (Index i=0; i<N_; i++) {
  126. Number b = (2.+5.*x[i]*x[i])*x[i] + 1.;
  127. for (Index j=Max(0,i-5); j<=Min(N_-1,i+1); j++) {
  128. b += x[j]*(1.+x[j]);
  129. }
  130. Number pb1 = pow(fabs(b),p-1.);
  131. Number apb1 = p*Sgn(b)*pb1;
  132. for (Index j=Max(0,i-5); j<i; j++) {
  133. grad_f[j] += (1.+2.*x[j])*apb1;
  134. }
  135. grad_f[i] += (3. + 2.*x[i] + 15.*x[i]*x[i])*apb1;
  136. if (i<N_-1) {
  137. grad_f[i+1] = (1.+2.*x[i+1])*apb1;
  138. }
  139. }
  140. grad_f[n-1] = 0.;
  141. return true;
  142. }
  143. // return the value of the constraints: g(x)
  144. bool LuksanVlcek6::eval_g(Index n, const Number* x, bool new_x, Index m, Number* g)
  145. {
  146. for (Index i=0; i<N_/2; i++) {
  147. Number e = exp(x[2*i] - x[2*i+1] - x[2*i+2]);
  148. g[i] = 4.*x[2*i+1] - (x[2*i] - x[2*i+2])*e - 3;
  149. }
  150. return true;
  151. }
  152. // return the structure or values of the jacobian
  153. bool LuksanVlcek6::eval_jac_g(Index n, const Number* x, bool new_x,
  154. Index m, Index nele_jac, Index* iRow, Index *jCol,
  155. Number* values)
  156. {
  157. if (values == NULL) {
  158. // return the structure of the jacobian
  159. Index ijac = 0;
  160. for (Index i=0; i<N_/2; i++) {
  161. iRow[ijac] = i;
  162. jCol[ijac] = 2*i;
  163. ijac++;
  164. iRow[ijac] = i;
  165. jCol[ijac] = 2*i+1;
  166. ijac++;
  167. iRow[ijac] = i;
  168. jCol[ijac] = 2*i+2;
  169. ijac++;
  170. }
  171. DBG_ASSERT(ijac == nele_jac);
  172. }
  173. else {
  174. // return the values of the jacobian of the constraints
  175. Index ijac = 0;
  176. for (Index i=0; i<N_/2; i++) {
  177. Number e = exp(x[2*i] - x[2*i+1] - x[2*i+2]);
  178. Number a1 = (1. + x[2*i] - x[2*i+2])*e;
  179. values[ijac] = -a1;
  180. ijac++;
  181. values[ijac] = 4. + (x[2*i] - x[2*i+2])*e;
  182. ijac++;
  183. values[ijac] = a1;
  184. ijac++;
  185. }
  186. }
  187. return true;
  188. }
  189. //return the structure or values of the hessian
  190. bool LuksanVlcek6::eval_h(Index n, const Number* x, bool new_x,
  191. Number obj_factor, Index m, const Number* lambda,
  192. bool new_lambda, Index nele_hess, Index* iRow,
  193. Index* jCol, Number* values)
  194. {
  195. if (values == NULL) {
  196. Index ihes=0;
  197. // First the diagonal
  198. for (Index i=0; i<n; i++) {
  199. iRow[ihes] = i;
  200. jCol[ihes] = i;
  201. ihes++;
  202. }
  203. // 1st off-diagonal
  204. for (Index i=0; i<n-1; i++) {
  205. iRow[ihes] = i;
  206. jCol[ihes] = i+1;
  207. ihes++;
  208. }
  209. // 2nd off-diagonal
  210. for (Index i=0; i<n-2; i++) {
  211. iRow[ihes] = i;
  212. jCol[ihes] = i+2;
  213. ihes++;
  214. }
  215. // 3rd off-diagonal
  216. for (Index i=0; i<n-3; i++) {
  217. iRow[ihes] = i;
  218. jCol[ihes] = i+3;
  219. ihes++;
  220. }
  221. // 4th off-diagonal
  222. for (Index i=0; i<n-4; i++) {
  223. iRow[ihes] = i;
  224. jCol[ihes] = i+4;
  225. ihes++;
  226. }
  227. // 5th off-diagonal
  228. for (Index i=0; i<n-5; i++) {
  229. iRow[ihes] = i;
  230. jCol[ihes] = i+5;
  231. ihes++;
  232. }
  233. // 6th off-diagonal
  234. for (Index i=0; i<n-6; i++) {
  235. iRow[ihes] = i;
  236. jCol[ihes] = i+6;
  237. ihes++;
  238. }
  239. DBG_ASSERT(ihes == nele_hess);
  240. }
  241. else {
  242. Number p = 7./3.;
  243. // First the objective
  244. values[0] = 0.;
  245. for (Index i=0; i<N_; i++) {
  246. Number b = (2.+5.*x[i]*x[i])*x[i] + 1.;
  247. for (Index j=Max(0,i-5); j<=Min(N_-1,i+1); j++) {
  248. b += x[j]*(1.+x[j]);
  249. }
  250. Number pb1 = pow(fabs(b),p-1.);
  251. Number pb2 = pow(fabs(b),p-2.);
  252. Number apb1 = p*Sgn(b)*pb1;
  253. Number apb2 = p*(p-1.)*pb2;
  254. Number a1 = 3. + 2.*x[i] + 15.*x[i]*x[i];
  255. Number a1apb2 = apb2*a1;
  256. // x[i] x[i]
  257. values[i] += obj_factor*(a1apb2*a1 + apb1*(2. + 30.*x[i]));
  258. // x[i] x[j] j<i
  259. Index ih = n;
  260. Index ip = n-1;
  261. for (Index j=i-1; j>=Max(0,i-5); j--) {
  262. values[ih+j] += obj_factor*a1apb2*(1.+2.*x[j]);
  263. ih += ip;
  264. ip--;
  265. }
  266. for (Index j=Max(0,i-5); j<i; j++) {
  267. Number axj = (1.+2.*x[j]);
  268. // x[j] x[j] j<i
  269. values[j] += obj_factor*(apb2*axj*axj + 2.*apb1);
  270. ih = n;
  271. ip = n-1;
  272. // x[j] x[k] j<i, k<j
  273. for (Index k=j-1; k>=Max(0,i-5); k--) {
  274. values[ih+k] += obj_factor*apb2*(1.+2.*x[k])*axj;
  275. ih += ip;
  276. ip--;
  277. }
  278. }
  279. if (i<N_-1) {
  280. Number axj = (1.+2.*x[i+1]);
  281. // x[i] x[i+1]
  282. values[n+i] = obj_factor*a1apb2*axj;
  283. // x[j=i+1] x[k] k<i
  284. ih = n+n-1;
  285. ip = n-2;
  286. for (Index k=i-1; k>=Max(0,i-5); k--) {
  287. values[ih+k] = obj_factor*apb2*(1.+2.*x[k])*axj;
  288. ih += ip;
  289. ip--;
  290. }
  291. // x[j=i+1] x[k=i+1]
  292. values[i+1] = obj_factor*(apb2*axj*axj+ 2.*apb1);
  293. }
  294. else {
  295. // We could just not set those non-zero elements in the
  296. // structure, but I'm too lazy to do that now
  297. values[n+i] = 0.;
  298. ih = n+n-1;
  299. ip = n-2;
  300. for (Index k=i-1; k>=Max(0,i-5); k--) {
  301. values[ih+k] = 0.;
  302. ih += ip;
  303. ip--;
  304. }
  305. // x[j=i+1] x[k=i+1]
  306. values[i+1] = 0.;
  307. }
  308. }
  309. // Now the constraints
  310. for (Index i=0; i<N_/2; i++) {
  311. Number e = exp(x[2*i] - x[2*i+1] - x[2*i+2]);
  312. Number a1 = 1. + x[2*i] - x[2*i+2];
  313. Number a2 = 1. + a1;
  314. // x[2*i] x[2*i]
  315. values[2*i] -= lambda[i]*a2*e;
  316. // x[2*i] x[2*i+1]
  317. values[n+2*i] += lambda[i]*a1*e;
  318. // x[2*i] x[2*i+2]
  319. values[n+(n-1)+2*i] += lambda[i]*a2*e;
  320. // x[2*i+1] x[2*i+1]
  321. values[2*i+1] -= lambda[i]*(x[2*i]-x[2*i+2])*e;
  322. // x[2*i+1] x[2*i+2]
  323. values[n+2*i+1] -= lambda[i]*a1*e;
  324. // x[2*i+2] x[2*i+2]
  325. values[2*i+2] -= lambda[i]*a2*e;
  326. }
  327. }
  328. return true;
  329. }
  330. void LuksanVlcek6::finalize_solution(SolverReturn status,
  331. Index n, const Number* x, const Number* z_L, const Number* z_U,
  332. Index m, const Number* g, const Number* lambda,
  333. Number obj_value,
  334. const IpoptData* ip_data,
  335. IpoptCalculatedQuantities* ip_cq)
  336. {}