IpOrigIpoptNLP.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. // Copyright (C) 2004, 2010 International Business Machines and others.
  2. // All Rights Reserved.
  3. // This code is published under the Eclipse Public License.
  4. //
  5. // $Id: IpOrigIpoptNLP.hpp 2160 2012-12-26 19:14:42Z stefan $
  6. //
  7. // Authors: Carl Laird, Andreas Waechter IBM 2004-08-13
  8. #ifndef __IPORIGIPOPTNLP_HPP__
  9. #define __IPORIGIPOPTNLP_HPP__
  10. #include "IpIpoptNLP.hpp"
  11. #include "IpException.hpp"
  12. #include "IpTimingStatistics.hpp"
  13. namespace Ipopt
  14. {
  15. /** enumeration for the Hessian information type. */
  16. enum HessianApproximationType {
  17. EXACT=0,
  18. LIMITED_MEMORY
  19. };
  20. /** enumeration for the Hessian approximation space. */
  21. enum HessianApproximationSpace {
  22. NONLINEAR_VARS=0,
  23. ALL_VARS
  24. };
  25. /** This class maps the traditional NLP into
  26. * something that is more useful by Ipopt.
  27. * This class takes care of storing the
  28. * calculated model results, handles caching,
  29. * and (some day) takes care of addition of slacks.
  30. */
  31. class OrigIpoptNLP : public IpoptNLP
  32. {
  33. public:
  34. /**@name Constructors/Destructors */
  35. //@{
  36. OrigIpoptNLP(const SmartPtr<const Journalist>& jnlst,
  37. const SmartPtr<NLP>& nlp,
  38. const SmartPtr<NLPScalingObject>& nlp_scaling);
  39. /** Default destructor */
  40. virtual ~OrigIpoptNLP();
  41. //@}
  42. /** Initialize - overloaded from IpoptNLP */
  43. virtual bool Initialize(const Journalist& jnlst,
  44. const OptionsList& options,
  45. const std::string& prefix);
  46. /** Initialize (create) structures for
  47. * the iteration data */
  48. virtual bool InitializeStructures(SmartPtr<Vector>& x,
  49. bool init_x,
  50. SmartPtr<Vector>& y_c,
  51. bool init_y_c,
  52. SmartPtr<Vector>& y_d,
  53. bool init_y_d,
  54. SmartPtr<Vector>& z_L,
  55. bool init_z_L,
  56. SmartPtr<Vector>& z_U,
  57. bool init_z_U,
  58. SmartPtr<Vector>& v_L,
  59. SmartPtr<Vector>& v_U
  60. );
  61. /** Method accessing the GetWarmStartIterate of the NLP */
  62. virtual bool GetWarmStartIterate(IteratesVector& warm_start_iterate)
  63. {
  64. return nlp_->GetWarmStartIterate(warm_start_iterate);
  65. }
  66. /** Accessor methods for model data */
  67. //@{
  68. /** Objective value */
  69. virtual Number f(const Vector& x);
  70. /** Objective value (depending in mu) - incorrect version for
  71. * OrigIpoptNLP */
  72. virtual Number f(const Vector& x, Number mu);
  73. /** Gradient of the objective */
  74. virtual SmartPtr<const Vector> grad_f(const Vector& x);
  75. /** Gradient of the objective (depending in mu) - incorrect
  76. * version for OrigIpoptNLP */
  77. virtual SmartPtr<const Vector> grad_f(const Vector& x, Number mu);
  78. /** Equality constraint residual */
  79. virtual SmartPtr<const Vector> c(const Vector& x);
  80. /** Jacobian Matrix for equality constraints */
  81. virtual SmartPtr<const Matrix> jac_c(const Vector& x);
  82. /** Inequality constraint residual (reformulated
  83. * as equalities with slacks */
  84. virtual SmartPtr<const Vector> d(const Vector& x);
  85. /** Jacobian Matrix for inequality constraints*/
  86. virtual SmartPtr<const Matrix> jac_d(const Vector& x);
  87. /** Hessian of the Lagrangian */
  88. virtual SmartPtr<const SymMatrix> h(const Vector& x,
  89. Number obj_factor,
  90. const Vector& yc,
  91. const Vector& yd
  92. );
  93. /** Hessian of the Lagrangian (depending in mu) - incorrect
  94. * version for OrigIpoptNLP */
  95. virtual SmartPtr<const SymMatrix> h(const Vector& x,
  96. Number obj_factor,
  97. const Vector& yc,
  98. const Vector& yd,
  99. Number mu);
  100. /** Provides a Hessian matrix from the correct matrix space with
  101. * uninitialized values. This can be used in LeastSquareMults to
  102. * obtain a "zero Hessian". */
  103. virtual SmartPtr<const SymMatrix> uninitialized_h();
  104. /** Lower bounds on x */
  105. virtual SmartPtr<const Vector> x_L() const
  106. {
  107. return x_L_;
  108. }
  109. /** Permutation matrix (x_L_ -> x) */
  110. virtual SmartPtr<const Matrix> Px_L() const
  111. {
  112. return Px_L_;
  113. }
  114. /** Upper bounds on x */
  115. virtual SmartPtr<const Vector> x_U() const
  116. {
  117. return x_U_;
  118. }
  119. /** Permutation matrix (x_U_ -> x */
  120. virtual SmartPtr<const Matrix> Px_U() const
  121. {
  122. return Px_U_;
  123. }
  124. /** Lower bounds on d */
  125. virtual SmartPtr<const Vector> d_L() const
  126. {
  127. return d_L_;
  128. }
  129. /** Permutation matrix (d_L_ -> d) */
  130. virtual SmartPtr<const Matrix> Pd_L() const
  131. {
  132. return Pd_L_;
  133. }
  134. /** Upper bounds on d */
  135. virtual SmartPtr<const Vector> d_U() const
  136. {
  137. return d_U_;
  138. }
  139. /** Permutation matrix (d_U_ -> d */
  140. virtual SmartPtr<const Matrix> Pd_U() const
  141. {
  142. return Pd_U_;
  143. }
  144. virtual SmartPtr<const SymMatrixSpace> HessianMatrixSpace() const
  145. {
  146. return h_space_;
  147. }
  148. //@}
  149. /** Accessor method for vector/matrix spaces pointers */
  150. virtual void GetSpaces(SmartPtr<const VectorSpace>& x_space,
  151. SmartPtr<const VectorSpace>& c_space,
  152. SmartPtr<const VectorSpace>& d_space,
  153. SmartPtr<const VectorSpace>& x_l_space,
  154. SmartPtr<const MatrixSpace>& px_l_space,
  155. SmartPtr<const VectorSpace>& x_u_space,
  156. SmartPtr<const MatrixSpace>& px_u_space,
  157. SmartPtr<const VectorSpace>& d_l_space,
  158. SmartPtr<const MatrixSpace>& pd_l_space,
  159. SmartPtr<const VectorSpace>& d_u_space,
  160. SmartPtr<const MatrixSpace>& pd_u_space,
  161. SmartPtr<const MatrixSpace>& Jac_c_space,
  162. SmartPtr<const MatrixSpace>& Jac_d_space,
  163. SmartPtr<const SymMatrixSpace>& Hess_lagrangian_space);
  164. /** Method for adapting the variable bounds. This is called if
  165. * slacks are becoming too small */
  166. virtual void AdjustVariableBounds(const Vector& new_x_L,
  167. const Vector& new_x_U,
  168. const Vector& new_d_L,
  169. const Vector& new_d_U);
  170. /** @name Counters for the number of function evaluations. */
  171. //@{
  172. virtual Index f_evals() const
  173. {
  174. return f_evals_;
  175. }
  176. virtual Index grad_f_evals() const
  177. {
  178. return grad_f_evals_;
  179. }
  180. virtual Index c_evals() const
  181. {
  182. return c_evals_;
  183. }
  184. virtual Index jac_c_evals() const
  185. {
  186. return jac_c_evals_;
  187. }
  188. virtual Index d_evals() const
  189. {
  190. return d_evals_;
  191. }
  192. virtual Index jac_d_evals() const
  193. {
  194. return jac_d_evals_;
  195. }
  196. virtual Index h_evals() const
  197. {
  198. return h_evals_;
  199. }
  200. //@}
  201. /** Solution Routines - overloaded from IpoptNLP*/
  202. //@{
  203. void FinalizeSolution(SolverReturn status,
  204. const Vector& x, const Vector& z_L, const Vector& z_U,
  205. const Vector& c, const Vector& d,
  206. const Vector& y_c, const Vector& y_d,
  207. Number obj_value,
  208. const IpoptData* ip_data,
  209. IpoptCalculatedQuantities* ip_cq);
  210. bool IntermediateCallBack(AlgorithmMode mode,
  211. Index iter, Number obj_value,
  212. Number inf_pr, Number inf_du,
  213. Number mu, Number d_norm,
  214. Number regularization_size,
  215. Number alpha_du, Number alpha_pr,
  216. Index ls_trials,
  217. SmartPtr<const IpoptData> ip_data,
  218. SmartPtr<IpoptCalculatedQuantities> ip_cq);
  219. //@}
  220. /** @name Methods for IpoptType */
  221. //@{
  222. /** Called by IpoptType to register the options */
  223. static void RegisterOptions(SmartPtr<RegisteredOptions> roptions);
  224. //@}
  225. /** Accessor method to the underlying NLP */
  226. SmartPtr<NLP> nlp()
  227. {
  228. return nlp_;
  229. }
  230. /**@name Methods related to function evaluation timing. */
  231. //@{
  232. /** Reset the timing statistics */
  233. void ResetTimes();
  234. void PrintTimingStatistics(Journalist& jnlst,
  235. EJournalLevel level,
  236. EJournalCategory category) const;
  237. const TimedTask& f_eval_time() const
  238. {
  239. return f_eval_time_;
  240. }
  241. const TimedTask& grad_f_eval_time() const
  242. {
  243. return grad_f_eval_time_;
  244. }
  245. const TimedTask& c_eval_time() const
  246. {
  247. return c_eval_time_;
  248. }
  249. const TimedTask& jac_c_eval_time() const
  250. {
  251. return jac_c_eval_time_;
  252. }
  253. const TimedTask& d_eval_time() const
  254. {
  255. return d_eval_time_;
  256. }
  257. const TimedTask& jac_d_eval_time() const
  258. {
  259. return jac_d_eval_time_;
  260. }
  261. const TimedTask& h_eval_time() const
  262. {
  263. return h_eval_time_;
  264. }
  265. Number TotalFunctionEvaluationCpuTime() const;
  266. Number TotalFunctionEvaluationSysTime() const;
  267. Number TotalFunctionEvaluationWallclockTime() const;
  268. //@}
  269. private:
  270. /** journalist */
  271. SmartPtr<const Journalist> jnlst_;
  272. /** Pointer to the NLP */
  273. SmartPtr<NLP> nlp_;
  274. /** Necessary Vector/Matrix spaces */
  275. //@{
  276. SmartPtr<const VectorSpace> x_space_;
  277. SmartPtr<const VectorSpace> c_space_;
  278. SmartPtr<const VectorSpace> d_space_;
  279. SmartPtr<const VectorSpace> x_l_space_;
  280. SmartPtr<const MatrixSpace> px_l_space_;
  281. SmartPtr<const VectorSpace> x_u_space_;
  282. SmartPtr<const MatrixSpace> px_u_space_;
  283. SmartPtr<const VectorSpace> d_l_space_;
  284. SmartPtr<const MatrixSpace> pd_l_space_;
  285. SmartPtr<const VectorSpace> d_u_space_;
  286. SmartPtr<const MatrixSpace> pd_u_space_;
  287. SmartPtr<const MatrixSpace> jac_c_space_;
  288. SmartPtr<const MatrixSpace> jac_d_space_;
  289. SmartPtr<const SymMatrixSpace> h_space_;
  290. SmartPtr<const MatrixSpace> scaled_jac_c_space_;
  291. SmartPtr<const MatrixSpace> scaled_jac_d_space_;
  292. SmartPtr<const SymMatrixSpace> scaled_h_space_;
  293. //@}
  294. /**@name Storage for Model Quantities */
  295. //@{
  296. /** Objective function */
  297. CachedResults<Number> f_cache_;
  298. /** Gradient of the objective function */
  299. CachedResults<SmartPtr<const Vector> > grad_f_cache_;
  300. /** Equality constraint residuals */
  301. CachedResults<SmartPtr<const Vector> > c_cache_;
  302. /** Jacobian Matrix for equality constraints
  303. * (current iteration) */
  304. CachedResults<SmartPtr<const Matrix> > jac_c_cache_;
  305. /** Inequality constraint residual (reformulated
  306. * as equalities with slacks */
  307. CachedResults<SmartPtr<const Vector> > d_cache_;
  308. /** Jacobian Matrix for inequality constraints
  309. * (current iteration) */
  310. CachedResults<SmartPtr<const Matrix> > jac_d_cache_;
  311. /** Hessian of the lagrangian
  312. * (current iteration) */
  313. CachedResults<SmartPtr<const SymMatrix> > h_cache_;
  314. /** Unscaled version of x vector */
  315. CachedResults<SmartPtr<const Vector> > unscaled_x_cache_;
  316. /** Lower bounds on x */
  317. SmartPtr<const Vector> x_L_;
  318. /** Permutation matrix (x_L_ -> x) */
  319. SmartPtr<const Matrix> Px_L_;
  320. /** Upper bounds on x */
  321. SmartPtr<const Vector> x_U_;
  322. /** Permutation matrix (x_U_ -> x */
  323. SmartPtr<const Matrix> Px_U_;
  324. /** Lower bounds on d */
  325. SmartPtr<const Vector> d_L_;
  326. /** Permutation matrix (d_L_ -> d) */
  327. SmartPtr<const Matrix> Pd_L_;
  328. /** Upper bounds on d */
  329. SmartPtr<const Vector> d_U_;
  330. /** Permutation matrix (d_U_ -> d */
  331. SmartPtr<const Matrix> Pd_U_;
  332. /** Original unmodified lower bounds on x */
  333. SmartPtr<const Vector> orig_x_L_;
  334. /** Original unmodified upper bounds on x */
  335. SmartPtr<const Vector> orig_x_U_;
  336. //@}
  337. /**@name Default Compiler Generated Methods
  338. * (Hidden to avoid implicit creation/calling).
  339. * These methods are not implemented and
  340. * we do not want the compiler to implement
  341. * them for us, so we declare them private
  342. * and do not define them. This ensures that
  343. * they will not be implicitly created/called. */
  344. //@{
  345. /** Default Constructor */
  346. OrigIpoptNLP();
  347. /** Copy Constructor */
  348. OrigIpoptNLP(const OrigIpoptNLP&);
  349. /** Overloaded Equals Operator */
  350. void operator=(const OrigIpoptNLP&);
  351. //@}
  352. /** @name auxilliary functions */
  353. //@{
  354. /** relax the bounds by a relative move of relax_bound_factor.
  355. * Here, relax_bound_factor should be negative (or zero) for
  356. * lower bounds, and positive (or zero) for upper bounds.
  357. */
  358. void relax_bounds(Number bound_relax_factor, Vector& bounds);
  359. /** Method for getting the unscaled version of the x vector */
  360. SmartPtr<const Vector> get_unscaled_x(const Vector& x);
  361. //@}
  362. /** @name Algorithmic parameters */
  363. //@{
  364. /** relaxation factor for the bounds */
  365. Number bound_relax_factor_;
  366. /** Flag indicating whether the primal variables should be
  367. * projected back into original bounds are optimization. */
  368. bool honor_original_bounds_;
  369. /** Flag indicating whether the TNLP with identical structure has
  370. * already been solved before. */
  371. bool warm_start_same_structure_;
  372. /** Flag indicating what Hessian information is to be used. */
  373. HessianApproximationType hessian_approximation_;
  374. /** Flag indicating in which space Hessian is to be approximated. */
  375. HessianApproximationSpace hessian_approximation_space_;
  376. /** Flag indicating whether it is desired to check if there are
  377. * Nan or Inf entries in first and second derivative matrices. */
  378. bool check_derivatives_for_naninf_;
  379. /** Flag indicating if we need to ask for equality constraint
  380. * Jacobians only once */
  381. bool jac_c_constant_;
  382. /** Flag indicating if we need to ask for inequality constraint
  383. * Jacobians only once */
  384. bool jac_d_constant_;
  385. /** Flag indicating if we need to ask for Hessian only once */
  386. bool hessian_constant_;
  387. //@}
  388. /** @name Counters for the function evaluations */
  389. //@{
  390. Index f_evals_;
  391. Index grad_f_evals_;
  392. Index c_evals_;
  393. Index jac_c_evals_;
  394. Index d_evals_;
  395. Index jac_d_evals_;
  396. Index h_evals_;
  397. //@}
  398. /** Flag indicating if initialization method has been called */
  399. bool initialized_;
  400. /**@name Timing statistics for the function evaluations. */
  401. //@{
  402. TimedTask f_eval_time_;
  403. TimedTask grad_f_eval_time_;
  404. TimedTask c_eval_time_;
  405. TimedTask jac_c_eval_time_;
  406. TimedTask d_eval_time_;
  407. TimedTask jac_d_eval_time_;
  408. TimedTask h_eval_time_;
  409. //@}
  410. };
  411. } // namespace Ipopt
  412. #endif