IpTNLPAdapter.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. // Copyright (C) 2004, 2008 International Business Machines and others.
  2. // All Rights Reserved.
  3. // This code is published under the Eclipse Public License.
  4. //
  5. // $Id: IpTNLPAdapter.hpp 1861 2010-12-21 21:34:47Z andreasw $
  6. //
  7. // Authors: Carl Laird, Andreas Waechter IBM 2004-08-13
  8. #ifndef __IPTNLPADAPTER_HPP__
  9. #define __IPTNLPADAPTER_HPP__
  10. #include "IpNLP.hpp"
  11. #include "IpTNLP.hpp"
  12. #include "IpOrigIpoptNLP.hpp"
  13. #include <list>
  14. namespace Ipopt
  15. {
  16. // forward declarations
  17. class ExpansionMatrix;
  18. class ExpansionMatrixSpace;
  19. class IteratesVector;
  20. class TDependencyDetector;
  21. /** This class Adapts the TNLP interface so it looks like an NLP interface.
  22. * This is an Adapter class (Design Patterns) that converts a TNLP to an
  23. * NLP. This allows users to write to the "more convenient" TNLP interface.
  24. */
  25. class TNLPAdapter : public NLP
  26. {
  27. public:
  28. /**@name Constructors/Destructors */
  29. //@{
  30. /** Default constructor */
  31. TNLPAdapter(const SmartPtr<TNLP> tnlp,
  32. const SmartPtr<const Journalist> jnlst = NULL);
  33. /** Default destructor */
  34. virtual ~TNLPAdapter();
  35. //@}
  36. /**@name Exceptions */
  37. //@{
  38. DECLARE_STD_EXCEPTION(INVALID_TNLP);
  39. DECLARE_STD_EXCEPTION(ERROR_IN_TNLP_DERIVATIVE_TEST);
  40. //@}
  41. /** @name TNLPAdapter Initialization. */
  42. //@{
  43. virtual bool ProcessOptions(const OptionsList& options,
  44. const std::string& prefix);
  45. /** Method for creating the derived vector / matrix types
  46. * (Do not delete these, the ). */
  47. virtual bool GetSpaces(SmartPtr<const VectorSpace>& x_space,
  48. SmartPtr<const VectorSpace>& c_space,
  49. SmartPtr<const VectorSpace>& d_space,
  50. SmartPtr<const VectorSpace>& x_l_space,
  51. SmartPtr<const MatrixSpace>& px_l_space,
  52. SmartPtr<const VectorSpace>& x_u_space,
  53. SmartPtr<const MatrixSpace>& px_u_space,
  54. SmartPtr<const VectorSpace>& d_l_space,
  55. SmartPtr<const MatrixSpace>& pd_l_space,
  56. SmartPtr<const VectorSpace>& d_u_space,
  57. SmartPtr<const MatrixSpace>& pd_u_space,
  58. SmartPtr<const MatrixSpace>& Jac_c_space,
  59. SmartPtr<const MatrixSpace>& Jac_d_space,
  60. SmartPtr<const SymMatrixSpace>& Hess_lagrangian_space);
  61. /** Method for obtaining the bounds information */
  62. virtual bool GetBoundsInformation(const Matrix& Px_L,
  63. Vector& x_L,
  64. const Matrix& Px_U,
  65. Vector& x_U,
  66. const Matrix& Pd_L,
  67. Vector& d_L,
  68. const Matrix& Pd_U,
  69. Vector& d_U);
  70. /** Method for obtaining the starting point
  71. * for all the iterates. */
  72. virtual bool GetStartingPoint(
  73. SmartPtr<Vector> x,
  74. bool need_x,
  75. SmartPtr<Vector> y_c,
  76. bool need_y_c,
  77. SmartPtr<Vector> y_d,
  78. bool need_y_d,
  79. SmartPtr<Vector> z_L,
  80. bool need_z_L,
  81. SmartPtr<Vector> z_U,
  82. bool need_z_U
  83. );
  84. /** Method for obtaining an entire iterate as a warmstart point.
  85. * The incoming IteratesVector has to be filled. */
  86. virtual bool GetWarmStartIterate(IteratesVector& warm_start_iterate);
  87. //@}
  88. /** @name TNLPAdapter evaluation routines. */
  89. //@{
  90. virtual bool Eval_f(const Vector& x, Number& f);
  91. virtual bool Eval_grad_f(const Vector& x, Vector& g_f);
  92. virtual bool Eval_c(const Vector& x, Vector& c);
  93. virtual bool Eval_jac_c(const Vector& x, Matrix& jac_c);
  94. virtual bool Eval_d(const Vector& x, Vector& d);
  95. virtual bool Eval_jac_d(const Vector& x, Matrix& jac_d);
  96. virtual bool Eval_h(const Vector& x,
  97. Number obj_factor,
  98. const Vector& yc,
  99. const Vector& yd,
  100. SymMatrix& h);
  101. virtual void GetScalingParameters(
  102. const SmartPtr<const VectorSpace> x_space,
  103. const SmartPtr<const VectorSpace> c_space,
  104. const SmartPtr<const VectorSpace> d_space,
  105. Number& obj_scaling,
  106. SmartPtr<Vector>& x_scaling,
  107. SmartPtr<Vector>& c_scaling,
  108. SmartPtr<Vector>& d_scaling) const;
  109. //@}
  110. /** @name Solution Reporting Methods */
  111. //@{
  112. virtual void FinalizeSolution(SolverReturn status,
  113. const Vector& x,
  114. const Vector& z_L, const Vector& z_U,
  115. const Vector& c, const Vector& d,
  116. const Vector& y_c, const Vector& y_d,
  117. Number obj_value,
  118. const IpoptData* ip_data,
  119. IpoptCalculatedQuantities* ip_cq);
  120. virtual bool IntermediateCallBack(AlgorithmMode mode,
  121. Index iter, Number obj_value,
  122. Number inf_pr, Number inf_du,
  123. Number mu, Number d_norm,
  124. Number regularization_size,
  125. Number alpha_du, Number alpha_pr,
  126. Index ls_trials,
  127. const IpoptData* ip_data,
  128. IpoptCalculatedQuantities* ip_cq);
  129. //@}
  130. /** Method returning information on quasi-Newton approximation. */
  131. virtual void
  132. GetQuasiNewtonApproximationSpaces(SmartPtr<VectorSpace>& approx_space,
  133. SmartPtr<Matrix>& P_approx);
  134. /** Enum for treatment of fixed variables option */
  135. enum FixedVariableTreatmentEnum
  136. {
  137. MAKE_PARAMETER=0,
  138. MAKE_CONSTRAINT,
  139. RELAX_BOUNDS
  140. };
  141. /** Enum for specifying which derivative test is to be performed. */
  142. enum DerivativeTestEnum
  143. {
  144. NO_TEST=0,
  145. FIRST_ORDER_TEST,
  146. SECOND_ORDER_TEST,
  147. ONLY_SECOND_ORDER_TEST
  148. };
  149. /** Enum for specifying technique for computing Jacobian */
  150. enum JacobianApproxEnum
  151. {
  152. JAC_EXACT=0,
  153. JAC_FINDIFF_VALUES
  154. };
  155. /** Method for performing the derivative test */
  156. bool CheckDerivatives(DerivativeTestEnum deriv_test,
  157. Index deriv_test_start_index);
  158. /** @name Methods for IpoptType */
  159. //@{
  160. static void RegisterOptions(SmartPtr<RegisteredOptions> roptions);
  161. //@}
  162. /** Accessor method for the underlying TNLP. */
  163. SmartPtr<TNLP> tnlp() const
  164. {
  165. return tnlp_;
  166. }
  167. /** @name Methods for translating data for IpoptNLP into the TNLP
  168. * data. These methods are used to obtain the current (or
  169. * final) data for the TNLP formulation from the IpoptNLP
  170. * structure. */
  171. //@{
  172. /** Sort the primal variables, and add the fixed values in x */
  173. void ResortX(const Vector& x, Number* x_orig);
  174. void ResortG(const Vector& c, const Vector& d, Number *g_orig);
  175. void ResortBnds(const Vector& x_L, Number* x_L_orig,
  176. const Vector& x_U, Number* x_U_orig);
  177. //@}
  178. private:
  179. /**@name Default Compiler Generated Methods
  180. * (Hidden to avoid implicit creation/calling).
  181. * These methods are not implemented and
  182. * we do not want the compiler to implement
  183. * them for us, so we declare them private
  184. * and do not define them. This ensures that
  185. * they will not be implicitly created/called. */
  186. //@{
  187. /** Copy Constructor */
  188. TNLPAdapter(const TNLPAdapter&);
  189. /** Overloaded Equals Operator */
  190. void operator=(const TNLPAdapter&);
  191. //@}
  192. /** @name Method implementing the detection of linearly dependent
  193. equality constraints */
  194. bool DetermineDependentConstraints(Index n_x_var,
  195. const Index* x_not_fixed_map,
  196. const Number* x_l, const Number* x_u,
  197. const Number* g_l, const Number* g_u,
  198. Index n_c, const Index* c_map,
  199. std::list<Index>& c_deps);
  200. /** Pointer to the TNLP class (class specific to Number* vectors and
  201. * harwell triplet matrices) */
  202. SmartPtr<TNLP> tnlp_;
  203. /** Journalist */
  204. SmartPtr<const Journalist> jnlst_;
  205. /** Object that can be used to detect linearly dependent rows in
  206. * the equality constraint Jacobian */
  207. SmartPtr<TDependencyDetector> dependency_detector_;
  208. /**@name Algorithmic parameters */
  209. //@{
  210. /** Value for a lower bound that denotes -infinity */
  211. Number nlp_lower_bound_inf_;
  212. /** Value for a upper bound that denotes infinity */
  213. Number nlp_upper_bound_inf_;
  214. /** Flag indicating how fixed variables should be handled */
  215. FixedVariableTreatmentEnum fixed_variable_treatment_;
  216. /* Determines relaxation of fixing bound for RELAX_BOUNDS. */
  217. Number bound_relax_factor_;
  218. /* Maximal slack for one-sidedly bounded variables. If a
  219. * variable has only one bound, say a lower bound xL, then an
  220. * upper bound xL + max_onesided_bound_slack_. If this value is
  221. * zero, no upper bound is added. */
  222. /* Took this out: Number max_onesided_bound_slack_; */
  223. /** Enum indicating whether and which derivative test should be
  224. * performed at starting point. */
  225. DerivativeTestEnum derivative_test_;
  226. /** Size of the perturbation for the derivative test */
  227. Number derivative_test_perturbation_;
  228. /** Relative threshold for marking deviation from finite
  229. * difference test */
  230. Number derivative_test_tol_;
  231. /** Flag indicating if all test values should be printed, or only
  232. * those violating the threshold. */
  233. bool derivative_test_print_all_;
  234. /** Index of first quantity to be checked. */
  235. Index derivative_test_first_index_;
  236. /** Flag indicating whether the TNLP with identical structure has
  237. * already been solved before. */
  238. bool warm_start_same_structure_;
  239. /** Flag indicating what Hessian information is to be used. */
  240. HessianApproximationType hessian_approximation_;
  241. /** Number of linear variables. */
  242. Index num_linear_variables_;
  243. /** Flag indicating how Jacobian is computed. */
  244. JacobianApproxEnum jacobian_approximation_;
  245. /** Size of the perturbation for the derivative approximation */
  246. Number findiff_perturbation_;
  247. /** Maximal perturbation of the initial point */
  248. Number point_perturbation_radius_;
  249. /** Flag indicating if rhs should be considered during dependency
  250. * detection */
  251. bool dependency_detection_with_rhs_;
  252. /** Overall convergence tolerance */
  253. Number tol_;
  254. //@}
  255. /**@name Problem Size Data */
  256. //@{
  257. /** full dimension of x (fixed + non-fixed) */
  258. Index n_full_x_;
  259. /** full dimension of g (c + d) */
  260. Index n_full_g_;
  261. /** non-zeros of the jacobian of c */
  262. Index nz_jac_c_;
  263. /** non-zeros of the jacobian of c without added constraints for
  264. * fixed variables. */
  265. Index nz_jac_c_no_extra_;
  266. /** non-zeros of the jacobian of d */
  267. Index nz_jac_d_;
  268. /** number of non-zeros in full-size Jacobian of g */
  269. Index nz_full_jac_g_;
  270. /** number of non-zeros in full-size Hessian */
  271. Index nz_full_h_;
  272. /** number of non-zeros in the non-fixed-size Hessian */
  273. Index nz_h_;
  274. /** Number of fixed variables */
  275. Index n_x_fixed_;
  276. //@}
  277. /** Numbering style of variables and constraints */
  278. TNLP::IndexStyleEnum index_style_;
  279. /** @name Local copy of spaces (for warm start) */
  280. //@{
  281. SmartPtr<const VectorSpace> x_space_;
  282. SmartPtr<const VectorSpace> c_space_;
  283. SmartPtr<const VectorSpace> d_space_;
  284. SmartPtr<const VectorSpace> x_l_space_;
  285. SmartPtr<const MatrixSpace> px_l_space_;
  286. SmartPtr<const VectorSpace> x_u_space_;
  287. SmartPtr<const MatrixSpace> px_u_space_;
  288. SmartPtr<const VectorSpace> d_l_space_;
  289. SmartPtr<const MatrixSpace> pd_l_space_;
  290. SmartPtr<const VectorSpace> d_u_space_;
  291. SmartPtr<const MatrixSpace> pd_u_space_;
  292. SmartPtr<const MatrixSpace> Jac_c_space_;
  293. SmartPtr<const MatrixSpace> Jac_d_space_;
  294. SmartPtr<const SymMatrixSpace> Hess_lagrangian_space_;
  295. //@}
  296. /**@name Local Copy of the Data */
  297. //@{
  298. Number* full_x_; /** copy of the full x vector (fixed & non-fixed) */
  299. Number* full_lambda_; /** copy of lambda (yc & yd) */
  300. Number* full_g_; /** copy of g (c & d) */
  301. Number* jac_g_; /** the values for the full jacobian of g */
  302. Number* c_rhs_; /** the rhs values of c */
  303. //@}
  304. /**@name Tags for deciding when to update internal copies of vectors */
  305. //@{
  306. TaggedObject::Tag x_tag_for_iterates_;
  307. TaggedObject::Tag y_c_tag_for_iterates_;
  308. TaggedObject::Tag y_d_tag_for_iterates_;
  309. TaggedObject::Tag x_tag_for_g_;
  310. TaggedObject::Tag x_tag_for_jac_g_;
  311. //@}
  312. /**@name Methods to update the values in the local copies of vectors */
  313. //@{
  314. bool update_local_x(const Vector& x);
  315. bool update_local_lambda(const Vector& y_c, const Vector& y_d);
  316. //@}
  317. /**@name Internal routines for evaluating g and jac_g (values stored since
  318. * they are used in both c and d routines */
  319. //@{
  320. bool internal_eval_g(bool new_x);
  321. bool internal_eval_jac_g(bool new_x);
  322. //@}
  323. /** @name Internal methods for dealing with finite difference
  324. approxation */
  325. //@{
  326. /** Initialize sparsity structure for finite difference Jacobian */
  327. void initialize_findiff_jac(const Index* iRow, const Index* jCol);
  328. //@}
  329. /**@name Internal Permutation Spaces and matrices
  330. */
  331. //@{
  332. /** Expansion from fixed x (ipopt) to full x */
  333. SmartPtr<ExpansionMatrix> P_x_full_x_;
  334. SmartPtr<ExpansionMatrixSpace> P_x_full_x_space_;
  335. /** Expansion from fixed x_L (ipopt) to full x */
  336. SmartPtr<ExpansionMatrix> P_x_x_L_;
  337. SmartPtr<ExpansionMatrixSpace> P_x_x_L_space_;
  338. /** Expansion from fixed x_U (ipopt) to full x */
  339. SmartPtr<ExpansionMatrix> P_x_x_U_;
  340. SmartPtr<ExpansionMatrixSpace> P_x_x_U_space_;
  341. /** Expansion from c only (ipopt) to full ampl c */
  342. SmartPtr<ExpansionMatrixSpace> P_c_g_space_;
  343. SmartPtr<ExpansionMatrix> P_c_g_;
  344. /** Expansion from d only (ipopt) to full ampl d */
  345. SmartPtr<ExpansionMatrixSpace> P_d_g_space_;
  346. SmartPtr<ExpansionMatrix> P_d_g_;
  347. Index* jac_idx_map_;
  348. Index* h_idx_map_;
  349. /** Position of fixed variables. This is required for a warm start */
  350. Index* x_fixed_map_;
  351. //@}
  352. /** @name Data for finite difference approximations of derivatives */
  353. //@{
  354. /** Number of unique nonzeros in constraint Jacobian */
  355. Index findiff_jac_nnz_;
  356. /** Start position for nonzero indices in ja for each column of
  357. Jacobian */
  358. Index* findiff_jac_ia_;
  359. /** Ordered by columns, for each column the row indices in
  360. Jacobian */
  361. Index* findiff_jac_ja_;
  362. /** Position of entry in original triplet matrix */
  363. Index* findiff_jac_postriplet_;
  364. /** Copy of the lower bounds */
  365. Number* findiff_x_l_;
  366. /** Copy of the upper bounds */
  367. Number* findiff_x_u_;
  368. //@}
  369. };
  370. } // namespace Ipopt
  371. #endif