IpNLPScaling.hpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. // Copyright (C) 2004, 2007 International Business Machines and others.
  2. // All Rights Reserved.
  3. // This code is published under the Eclipse Public License.
  4. //
  5. // $Id: IpNLPScaling.hpp 2036 2011-07-02 17:21:08Z stefan $
  6. //
  7. // Authors: Carl Laird, Andreas Waechter IBM 2004-08-13
  8. #ifndef __IPNLPSCALING_HPP__
  9. #define __IPNLPSCALING_HPP__
  10. #include "IpOptionsList.hpp"
  11. #include "IpRegOptions.hpp"
  12. namespace Ipopt
  13. {
  14. // forward declarations
  15. class Vector;
  16. class VectorSpace;
  17. class Matrix;
  18. class MatrixSpace;
  19. class SymMatrix;
  20. class SymMatrixSpace;
  21. class ScaledMatrixSpace;
  22. class SymScaledMatrixSpace;
  23. /** This is the abstract base class for problem scaling.
  24. * It is repsonsible for determining the scaling factors
  25. * and mapping quantities in and out of scaled and unscaled
  26. * versions
  27. */
  28. class NLPScalingObject : public ReferencedObject
  29. {
  30. public:
  31. /**@name Constructors/Destructors */
  32. //@{
  33. NLPScalingObject();
  34. /** Default destructor */
  35. virtual ~NLPScalingObject();
  36. //@}
  37. /** Method to initialize the options */
  38. bool Initialize(const Journalist& jnlst,
  39. const OptionsList& options,
  40. const std::string& prefix)
  41. {
  42. jnlst_ = &jnlst;
  43. return InitializeImpl(options, prefix);
  44. }
  45. /** Methods to map scaled and unscaled matrices */
  46. //@{
  47. /** Returns an obj-scaled version of the given scalar */
  48. virtual Number apply_obj_scaling(const Number& f)=0;
  49. /** Returns an obj-unscaled version of the given scalar */
  50. virtual Number unapply_obj_scaling(const Number& f)=0;
  51. /** Returns an x-scaled version of the given vector */
  52. virtual SmartPtr<Vector>
  53. apply_vector_scaling_x_NonConst(const SmartPtr<const Vector>& v)=0;
  54. /** Returns an x-scaled version of the given vector */
  55. virtual SmartPtr<const Vector>
  56. apply_vector_scaling_x(const SmartPtr<const Vector>& v)=0;
  57. /** Returns an x-unscaled version of the given vector */
  58. virtual SmartPtr<Vector>
  59. unapply_vector_scaling_x_NonConst(const SmartPtr<const Vector>& v)=0;
  60. /** Returns an x-unscaled version of the given vector */
  61. virtual SmartPtr<const Vector>
  62. unapply_vector_scaling_x(const SmartPtr<const Vector>& v)=0;
  63. /** Returns an c-scaled version of the given vector */
  64. virtual SmartPtr<const Vector>
  65. apply_vector_scaling_c(const SmartPtr<const Vector>& v)=0;
  66. /** Returns an c-unscaled version of the given vector */
  67. virtual SmartPtr<const Vector>
  68. unapply_vector_scaling_c(const SmartPtr<const Vector>& v)=0;
  69. /** Returns an c-scaled version of the given vector */
  70. virtual SmartPtr<Vector>
  71. apply_vector_scaling_c_NonConst(const SmartPtr<const Vector>& v)=0;
  72. /** Returns an c-unscaled version of the given vector */
  73. virtual SmartPtr<Vector>
  74. unapply_vector_scaling_c_NonConst(const SmartPtr<const Vector>& v)=0;
  75. /** Returns an d-scaled version of the given vector */
  76. virtual SmartPtr<const Vector>
  77. apply_vector_scaling_d(const SmartPtr<const Vector>& v)=0;
  78. /** Returns an d-unscaled version of the given vector */
  79. virtual SmartPtr<const Vector>
  80. unapply_vector_scaling_d(const SmartPtr<const Vector>& v)=0;
  81. /** Returns an d-scaled version of the given vector */
  82. virtual SmartPtr<Vector>
  83. apply_vector_scaling_d_NonConst(const SmartPtr<const Vector>& v)=0;
  84. /** Returns an d-unscaled version of the given vector */
  85. virtual SmartPtr<Vector>
  86. unapply_vector_scaling_d_NonConst(const SmartPtr<const Vector>& v)=0;
  87. /** Returns a scaled version of the jacobian for c. If the
  88. * overloaded method does not make a new matrix, make sure to set
  89. * the matrix ptr passed in to NULL.
  90. */
  91. virtual SmartPtr<const Matrix>
  92. apply_jac_c_scaling(SmartPtr<const Matrix> matrix)=0;
  93. /** Returns a scaled version of the jacobian for d If the
  94. * overloaded method does not create a new matrix, make sure to
  95. * set the matrix ptr passed in to NULL.
  96. */
  97. virtual SmartPtr<const Matrix>
  98. apply_jac_d_scaling(SmartPtr<const Matrix> matrix)=0;
  99. /** Returns a scaled version of the hessian of the lagrangian If
  100. * the overloaded method does not create a new matrix, make sure
  101. * to set the matrix ptr passed in to NULL.
  102. */
  103. virtual SmartPtr<const SymMatrix>
  104. apply_hessian_scaling(SmartPtr<const SymMatrix> matrix)=0;
  105. //@}
  106. /** Methods for scaling bounds - these wrap those above */
  107. //@{
  108. /** Returns an x-scaled vector in the x_L or x_U space */
  109. SmartPtr<Vector> apply_vector_scaling_x_LU_NonConst(
  110. const Matrix& Px_LU,
  111. const SmartPtr<const Vector>& lu,
  112. const VectorSpace& x_space);
  113. /** Returns an x-scaled vector in the x_L or x_U space */
  114. SmartPtr<const Vector> apply_vector_scaling_x_LU(
  115. const Matrix& Px_LU,
  116. const SmartPtr<const Vector>& lu,
  117. const VectorSpace& x_space);
  118. /** Returns an d-scaled vector in the d_L or d_U space */
  119. SmartPtr<Vector> apply_vector_scaling_d_LU_NonConst(
  120. const Matrix& Pd_LU,
  121. const SmartPtr<const Vector>& lu,
  122. const VectorSpace& d_space);
  123. /** Returns an d-scaled vector in the d_L or d_U space */
  124. SmartPtr<const Vector> apply_vector_scaling_d_LU(
  125. const Matrix& Pd_LU,
  126. const SmartPtr<const Vector>& lu,
  127. const VectorSpace& d_space);
  128. /** Returns an d-unscaled vector in the d_L or d_U space */
  129. SmartPtr<Vector> unapply_vector_scaling_d_LU_NonConst(
  130. const Matrix& Pd_LU,
  131. const SmartPtr<const Vector>& lu,
  132. const VectorSpace& d_space);
  133. /** Returns an d-unscaled vector in the d_L or d_U space */
  134. SmartPtr<const Vector> unapply_vector_scaling_d_LU(
  135. const Matrix& Pd_LU,
  136. const SmartPtr<const Vector>& lu,
  137. const VectorSpace& d_space);
  138. //@}
  139. /** Methods for scaling the gradient of the objective - wraps the
  140. * virtual methods above
  141. */
  142. //@{
  143. /** Returns a grad_f scaled version (d_f * D_x^{-1}) of the given vector */
  144. virtual SmartPtr<Vector>
  145. apply_grad_obj_scaling_NonConst(const SmartPtr<const Vector>& v);
  146. /** Returns a grad_f scaled version (d_f * D_x^{-1}) of the given vector */
  147. virtual SmartPtr<const Vector>
  148. apply_grad_obj_scaling(const SmartPtr<const Vector>& v);
  149. /** Returns a grad_f unscaled version (d_f * D_x^{-1}) of the
  150. * given vector */
  151. virtual SmartPtr<Vector>
  152. unapply_grad_obj_scaling_NonConst(const SmartPtr<const Vector>& v);
  153. /** Returns a grad_f unscaled version (d_f * D_x^{-1}) of the
  154. * given vector */
  155. virtual SmartPtr<const Vector>
  156. unapply_grad_obj_scaling(const SmartPtr<const Vector>& v);
  157. //@}
  158. /** @name Methods for determining whether scaling for entities is
  159. * done */
  160. //@{
  161. /** Returns true if the primal x variables are scaled. */
  162. virtual bool have_x_scaling()=0;
  163. /** Returns true if the equality constraints are scaled. */
  164. virtual bool have_c_scaling()=0;
  165. /** Returns true if the inequality constraints are scaled. */
  166. virtual bool have_d_scaling()=0;
  167. //@}
  168. /** This method is called by the IpoptNLP's at a convenient time to
  169. * compute and/or read scaling factors
  170. */
  171. virtual void DetermineScaling(const SmartPtr<const VectorSpace> x_space,
  172. const SmartPtr<const VectorSpace> c_space,
  173. const SmartPtr<const VectorSpace> d_space,
  174. const SmartPtr<const MatrixSpace> jac_c_space,
  175. const SmartPtr<const MatrixSpace> jac_d_space,
  176. const SmartPtr<const SymMatrixSpace> h_space,
  177. SmartPtr<const MatrixSpace>& new_jac_c_space,
  178. SmartPtr<const MatrixSpace>& new_jac_d_space,
  179. SmartPtr<const SymMatrixSpace>& new_h_space,
  180. const Matrix& Px_L, const Vector& x_L,
  181. const Matrix& Px_U, const Vector& x_U)=0;
  182. protected:
  183. /** Implementation of the initialization method that has to be
  184. * overloaded by for each derived class. */
  185. virtual bool InitializeImpl(const OptionsList& options,
  186. const std::string& prefix)=0;
  187. /** Accessor method for the journalist */
  188. const Journalist& Jnlst() const
  189. {
  190. return *jnlst_;
  191. }
  192. private:
  193. /**@name Default Compiler Generated Methods
  194. * (Hidden to avoid implicit creation/calling).
  195. * These methods are not implemented and
  196. * we do not want the compiler to implement
  197. * them for us, so we declare them private
  198. * and do not define them. This ensures that
  199. * they will not be implicitly created/called. */
  200. //@{
  201. /** Copy Constructor */
  202. NLPScalingObject(const NLPScalingObject&);
  203. /** Overloaded Equals Operator */
  204. void operator=(const NLPScalingObject&);
  205. //@}
  206. SmartPtr<const Journalist> jnlst_;
  207. };
  208. /** This is a base class for many standard scaling
  209. * techniques. The overloaded classes only need to
  210. * provide the scaling parameters
  211. */
  212. class StandardScalingBase : public NLPScalingObject
  213. {
  214. public:
  215. /**@name Constructors/Destructors */
  216. //@{
  217. StandardScalingBase();
  218. /** Default destructor */
  219. virtual ~StandardScalingBase();
  220. //@}
  221. /** Methods to map scaled and unscaled matrices */
  222. //@{
  223. /** Returns an obj-scaled version of the given scalar */
  224. virtual Number apply_obj_scaling(const Number& f);
  225. /** Returns an obj-unscaled version of the given scalar */
  226. virtual Number unapply_obj_scaling(const Number& f);
  227. /** Returns an x-scaled version of the given vector */
  228. virtual SmartPtr<Vector>
  229. apply_vector_scaling_x_NonConst(const SmartPtr<const Vector>& v);
  230. /** Returns an x-scaled version of the given vector */
  231. virtual SmartPtr<const Vector>
  232. apply_vector_scaling_x(const SmartPtr<const Vector>& v);
  233. /** Returns an x-unscaled version of the given vector */
  234. virtual SmartPtr<Vector>
  235. unapply_vector_scaling_x_NonConst(const SmartPtr<const Vector>& v);
  236. /** Returns an x-unscaled version of the given vector */
  237. virtual SmartPtr<const Vector>
  238. unapply_vector_scaling_x(const SmartPtr<const Vector>& v);
  239. /** Returns an c-scaled version of the given vector */
  240. virtual SmartPtr<const Vector>
  241. apply_vector_scaling_c(const SmartPtr<const Vector>& v);
  242. /** Returns an c-unscaled version of the given vector */
  243. virtual SmartPtr<const Vector>
  244. unapply_vector_scaling_c(const SmartPtr<const Vector>& v);
  245. /** Returns an c-scaled version of the given vector */
  246. virtual SmartPtr<Vector>
  247. apply_vector_scaling_c_NonConst(const SmartPtr<const Vector>& v);
  248. /** Returns an c-unscaled version of the given vector */
  249. virtual SmartPtr<Vector>
  250. unapply_vector_scaling_c_NonConst(const SmartPtr<const Vector>& v);
  251. /** Returns an d-scaled version of the given vector */
  252. virtual SmartPtr<const Vector>
  253. apply_vector_scaling_d(const SmartPtr<const Vector>& v);
  254. /** Returns an d-unscaled version of the given vector */
  255. virtual SmartPtr<const Vector>
  256. unapply_vector_scaling_d(const SmartPtr<const Vector>& v);
  257. /** Returns an d-scaled version of the given vector */
  258. virtual SmartPtr<Vector>
  259. apply_vector_scaling_d_NonConst(const SmartPtr<const Vector>& v);
  260. /** Returns an d-unscaled version of the given vector */
  261. virtual SmartPtr<Vector>
  262. unapply_vector_scaling_d_NonConst(const SmartPtr<const Vector>& v);
  263. /** Returns a scaled version of the jacobian for c. If the
  264. * overloaded method does not make a new matrix, make sure to set
  265. * the matrix ptr passed in to NULL.
  266. */
  267. virtual SmartPtr<const Matrix>
  268. apply_jac_c_scaling(SmartPtr<const Matrix> matrix);
  269. /** Returns a scaled version of the jacobian for d If the
  270. * overloaded method does not create a new matrix, make sure to
  271. * set the matrix ptr passed in to NULL.
  272. */
  273. virtual SmartPtr<const Matrix>
  274. apply_jac_d_scaling(SmartPtr<const Matrix> matrix);
  275. /** Returns a scaled version of the hessian of the lagrangian If
  276. * the overloaded method does not create a new matrix, make sure
  277. * to set the matrix ptr passed in to NULL.
  278. */
  279. virtual SmartPtr<const SymMatrix>
  280. apply_hessian_scaling(SmartPtr<const SymMatrix> matrix);
  281. //@}
  282. /** @name Methods for determining whether scaling for entities is
  283. * done */
  284. //@{
  285. virtual bool have_x_scaling();
  286. virtual bool have_c_scaling();
  287. virtual bool have_d_scaling();
  288. //@}
  289. /** This method is called by the IpoptNLP's at a convenient time to
  290. * compute and/or read scaling factors
  291. */
  292. virtual void DetermineScaling(const SmartPtr<const VectorSpace> x_space,
  293. const SmartPtr<const VectorSpace> c_space,
  294. const SmartPtr<const VectorSpace> d_space,
  295. const SmartPtr<const MatrixSpace> jac_c_space,
  296. const SmartPtr<const MatrixSpace> jac_d_space,
  297. const SmartPtr<const SymMatrixSpace> h_space,
  298. SmartPtr<const MatrixSpace>& new_jac_c_space,
  299. SmartPtr<const MatrixSpace>& new_jac_d_space,
  300. SmartPtr<const SymMatrixSpace>& new_h_space,
  301. const Matrix& Px_L, const Vector& x_L,
  302. const Matrix& Px_U, const Vector& x_U);
  303. /** Methods for IpoptType */
  304. //@{
  305. static void RegisterOptions(SmartPtr<RegisteredOptions> roptions);
  306. //@}
  307. protected:
  308. /** Overloaded initialization method */
  309. virtual bool InitializeImpl(const OptionsList& options,
  310. const std::string& prefix);
  311. /** This is the method that has to be overloaded by a particular
  312. * scaling method that somehow computes the scaling vectors dx,
  313. * dc, and dd. The pointers to those vectors can be NULL, in
  314. * which case no scaling for that item will be done later. */
  315. virtual void DetermineScalingParametersImpl(
  316. const SmartPtr<const VectorSpace> x_space,
  317. const SmartPtr<const VectorSpace> c_space,
  318. const SmartPtr<const VectorSpace> d_space,
  319. const SmartPtr<const MatrixSpace> jac_c_space,
  320. const SmartPtr<const MatrixSpace> jac_d_space,
  321. const SmartPtr<const SymMatrixSpace> h_space,
  322. const Matrix& Px_L, const Vector& x_L,
  323. const Matrix& Px_U, const Vector& x_U,
  324. Number& df,
  325. SmartPtr<Vector>& dx,
  326. SmartPtr<Vector>& dc,
  327. SmartPtr<Vector>& dd)=0;
  328. private:
  329. /**@name Default Compiler Generated Methods
  330. * (Hidden to avoid implicit creation/calling).
  331. * These methods are not implemented and
  332. * we do not want the compiler to implement
  333. * them for us, so we declare them private
  334. * and do not define them. This ensures that
  335. * they will not be implicitly created/called. */
  336. //@{
  337. /** Copy Constructor */
  338. StandardScalingBase(const StandardScalingBase&);
  339. /** Overloaded Equals Operator */
  340. void operator=(const StandardScalingBase&);
  341. //@}
  342. /** Scaling parameters - we only need to keep copies of
  343. * the objective scaling and the x scaling - the others we can
  344. * get from the scaled matrix spaces.
  345. */
  346. //@{
  347. /** objective scaling parameter */
  348. Number df_;
  349. /** x scaling */
  350. SmartPtr<Vector> dx_;
  351. //@}
  352. /** Scaled Matrix Spaces */
  353. //@{
  354. /** Scaled jacobian of c space */
  355. SmartPtr<ScaledMatrixSpace> scaled_jac_c_space_;
  356. /** Scaled jacobian of d space */
  357. SmartPtr<ScaledMatrixSpace> scaled_jac_d_space_;
  358. /** Scaled hessian of lagrangian spacea */
  359. SmartPtr<SymScaledMatrixSpace> scaled_h_space_;
  360. //@}
  361. /** @name Algorithmic parameters */
  362. //@{
  363. /** Additional scaling value for the objective function */
  364. Number obj_scaling_factor_;
  365. //@}
  366. };
  367. /** Class implementing the scaling object that doesn't to any scaling */
  368. class NoNLPScalingObject : public StandardScalingBase
  369. {
  370. public:
  371. /**@name Constructors/Destructors */
  372. //@{
  373. NoNLPScalingObject()
  374. {}
  375. /** Default destructor */
  376. virtual ~NoNLPScalingObject()
  377. {}
  378. //@}
  379. protected:
  380. /** Overloaded from StandardScalingBase */
  381. virtual void DetermineScalingParametersImpl(
  382. const SmartPtr<const VectorSpace> x_space,
  383. const SmartPtr<const VectorSpace> c_space,
  384. const SmartPtr<const VectorSpace> d_space,
  385. const SmartPtr<const MatrixSpace> jac_c_space,
  386. const SmartPtr<const MatrixSpace> jac_d_space,
  387. const SmartPtr<const SymMatrixSpace> h_space,
  388. const Matrix& Px_L, const Vector& x_L,
  389. const Matrix& Px_U, const Vector& x_U,
  390. Number& df,
  391. SmartPtr<Vector>& dx,
  392. SmartPtr<Vector>& dc,
  393. SmartPtr<Vector>& dd);
  394. private:
  395. /**@name Default Compiler Generated Methods
  396. * (Hidden to avoid implicit creation/calling).
  397. * These methods are not implemented and
  398. * we do not want the compiler to implement
  399. * them for us, so we declare them private
  400. * and do not define them. This ensures that
  401. * they will not be implicitly created/called. */
  402. //@{
  403. /** Copy Constructor */
  404. NoNLPScalingObject(const NoNLPScalingObject&);
  405. /** Overloaded Equals Operator */
  406. void operator=(const NoNLPScalingObject&);
  407. //@}
  408. };
  409. } // namespace Ipopt
  410. #endif