LuksanVlcek2.cpp 7.0 KB

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