IpRegOptions.hpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  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: IpRegOptions.hpp 2189 2013-03-31 15:06:11Z stefan $
  6. //
  7. // Authors: Carl Laird, Andreas Waechter IBM 2005-06-18
  8. #ifndef __IPREGOPTIONS_HPP__
  9. #define __IPREGOPTIONS_HPP__
  10. #include "IpUtils.hpp"
  11. #include "IpReferenced.hpp"
  12. #include "IpException.hpp"
  13. #include "IpSmartPtr.hpp"
  14. #include <map>
  15. namespace Ipopt
  16. {
  17. enum RegisteredOptionType
  18. {
  19. OT_Number,
  20. OT_Integer,
  21. OT_String,
  22. OT_Unknown
  23. };
  24. /** Base class for registered options. The derived types are more
  25. * specific to a string option or a Number (real) option, etc.
  26. */
  27. class RegisteredOption : public ReferencedObject
  28. {
  29. public:
  30. /** class to hold the valid string settings for a string option */
  31. class string_entry
  32. {
  33. public:
  34. string_entry(const std::string& value, const std::string& description)
  35. : value_(value), description_(description)
  36. {}
  37. std::string value_;
  38. std::string description_;
  39. };
  40. /** Constructors / Destructors */
  41. //@{
  42. RegisteredOption(Index counter)
  43. :
  44. type_(OT_Unknown),
  45. has_lower_(false),
  46. has_upper_(false),
  47. counter_(counter)
  48. {}
  49. RegisteredOption(const std::string& name,
  50. const std::string& short_description,
  51. const std::string& long_description,
  52. const std::string& registering_category,
  53. Index counter)
  54. :
  55. name_(name),
  56. short_description_(short_description),
  57. long_description_(long_description),
  58. registering_category_(registering_category),
  59. type_(OT_Unknown),
  60. has_lower_(false),
  61. has_upper_(false),
  62. counter_(counter)
  63. {}
  64. RegisteredOption(const RegisteredOption& copy)
  65. :
  66. name_(copy.name_),
  67. short_description_(copy.short_description_),
  68. long_description_(copy.long_description_),
  69. registering_category_(copy.registering_category_),
  70. type_(copy.type_),
  71. has_lower_(copy.has_lower_),
  72. lower_(copy.lower_),
  73. has_upper_(copy.has_upper_),
  74. upper_(copy.upper_),
  75. valid_strings_(copy.valid_strings_),
  76. counter_(copy.counter_)
  77. {}
  78. virtual ~RegisteredOption()
  79. {}
  80. //@}
  81. DECLARE_STD_EXCEPTION(ERROR_CONVERTING_STRING_TO_ENUM);
  82. /** Standard Get / Set Methods */
  83. //@{
  84. /** Get the option's name (tag in the input file) */
  85. virtual const std::string& Name() const
  86. {
  87. return name_;
  88. }
  89. /** Set the option's name (tag in the input file) */
  90. virtual void SetName(const std::string& name)
  91. {
  92. name_ = name;
  93. }
  94. /** Get the short description */
  95. virtual const std::string& ShortDescription() const
  96. {
  97. return short_description_;
  98. }
  99. /** Get the long description */
  100. virtual const std::string& LongDescription() const
  101. {
  102. return long_description_;
  103. }
  104. /** Set the short description */
  105. virtual void SetShortDescription(const std::string& short_description)
  106. {
  107. short_description_ = short_description;
  108. }
  109. /** Set the long description */
  110. virtual void SetLongDescription(const std::string& long_description)
  111. {
  112. long_description_ = long_description;
  113. }
  114. /** Get the registering class */
  115. virtual const std::string& RegisteringCategory() const
  116. {
  117. return registering_category_;
  118. }
  119. /** Set the registering class */
  120. virtual void SetRegisteringCategory(const std::string& registering_category)
  121. {
  122. registering_category_ = registering_category;
  123. }
  124. /** Get the Option's type */
  125. virtual const RegisteredOptionType& Type() const
  126. {
  127. return type_;
  128. }
  129. /** Get the Option's type */
  130. virtual void SetType(const RegisteredOptionType& type)
  131. {
  132. type_ = type;
  133. }
  134. /** Counter */
  135. virtual Index Counter() const
  136. {
  137. return counter_;
  138. }
  139. //@}
  140. /** @name Get / Set methods valid for specific types - NOTE: the Type
  141. * must be set before calling these methods.
  142. */
  143. //@{
  144. /** check if the option has a lower bound - can be called for
  145. * OT_Number & OT_Integer*/
  146. virtual const bool& HasLower() const
  147. {
  148. DBG_ASSERT(type_ == OT_Number || type_ == OT_Integer);
  149. return has_lower_;
  150. }
  151. /** check if the lower bound is strict - can be called for
  152. OT_Number */
  153. virtual const bool& LowerStrict() const
  154. {
  155. DBG_ASSERT(type_ == OT_Number && has_lower_ == true);
  156. return lower_strict_;
  157. }
  158. /** get the Number version of the lower bound - can be called for
  159. * OT_Number */
  160. virtual Number LowerNumber() const
  161. {
  162. DBG_ASSERT(has_lower_ == true && type_ == OT_Number);
  163. return lower_;
  164. }
  165. /** set the Number version of the lower bound - can be called for
  166. * OT_Number */
  167. virtual void SetLowerNumber(const Number& lower, const bool& strict)
  168. {
  169. DBG_ASSERT(type_ == OT_Number);
  170. lower_ = lower;
  171. lower_strict_ = strict, has_lower_ = true;
  172. }
  173. /** get the Integer version of the lower bound can be called for
  174. * OT_Integer*/
  175. virtual Index LowerInteger() const
  176. {
  177. DBG_ASSERT(has_lower_ == true && type_ == OT_Integer);
  178. return (Index)lower_;
  179. }
  180. /** set the Integer version of the lower bound - can be called for
  181. * OT_Integer */
  182. virtual void SetLowerInteger(const Index& lower)
  183. {
  184. DBG_ASSERT(type_ == OT_Integer);
  185. lower_ = (Number)lower;
  186. has_lower_ = true;
  187. }
  188. /** check if the option has an upper bound - can be called for
  189. * OT_Number & OT_Integer*/
  190. virtual const bool& HasUpper() const
  191. {
  192. DBG_ASSERT(type_ == OT_Number || type_ == OT_Integer);
  193. return has_upper_;
  194. }
  195. /** check if the upper bound is strict - can be called for
  196. * OT_Number */
  197. virtual const bool& UpperStrict() const
  198. {
  199. DBG_ASSERT(type_ == OT_Number && has_upper_ == true);
  200. return upper_strict_;
  201. }
  202. /** get the Number version of the upper bound - can be called for
  203. * OT_Number */
  204. virtual Number UpperNumber() const
  205. {
  206. DBG_ASSERT(has_upper_ == true && type_ == OT_Number);
  207. return upper_;
  208. }
  209. /** set the Number version of the upper bound - can be called for
  210. * OT_Number */
  211. virtual void SetUpperNumber(const Number& upper, const bool& strict)
  212. {
  213. DBG_ASSERT(type_ == OT_Number);
  214. upper_ = upper;
  215. upper_strict_ = strict;
  216. has_upper_ = true;
  217. }
  218. /** get the Integer version of the upper bound - can be called for
  219. * OT_Integer*/
  220. virtual Index UpperInteger() const
  221. {
  222. DBG_ASSERT(has_upper_ == true && type_ == OT_Integer);
  223. return (Index)upper_;
  224. }
  225. /** set the Integer version of the upper bound - can be called for
  226. * OT_Integer */
  227. virtual void SetUpperInteger(const Index& upper)
  228. {
  229. DBG_ASSERT(type_ == OT_Integer);
  230. upper_ = (Number)upper;
  231. has_upper_ = true;
  232. }
  233. /** method to add valid string entries - can be called for
  234. * OT_String */
  235. virtual void AddValidStringSetting(const std::string value,
  236. const std::string description)
  237. {
  238. DBG_ASSERT(type_ == OT_String);
  239. valid_strings_.push_back(string_entry(value, description));
  240. }
  241. /** get the default as a Number - can be called for OT_Number */
  242. virtual Number DefaultNumber() const
  243. {
  244. DBG_ASSERT(type_ == OT_Number);
  245. return default_number_;
  246. }
  247. /** Set the default as a Number - can be called for OT_Number */
  248. virtual void SetDefaultNumber(const Number& default_value)
  249. {
  250. DBG_ASSERT(type_ == OT_Number);
  251. default_number_ = default_value;
  252. }
  253. /** get the default as an Integer - can be called for OT_Integer*/
  254. virtual Index DefaultInteger() const
  255. {
  256. DBG_ASSERT(type_ == OT_Integer);
  257. return (Index)default_number_;
  258. }
  259. /** Set the default as an Integer - can be called for
  260. OT_Integer */
  261. virtual void SetDefaultInteger(const Index& default_value)
  262. {
  263. DBG_ASSERT(type_ == OT_Integer);
  264. default_number_ = (Number)default_value;
  265. }
  266. /** get the default as a string - can be called for OT_String */
  267. virtual std::string DefaultString() const
  268. {
  269. DBG_ASSERT(type_ == OT_String);
  270. return default_string_;
  271. }
  272. /** get the default as a string, but as the index of the string in
  273. * the list - helps map from a string to an enum- can be called
  274. * for OT_String */
  275. virtual Index DefaultStringAsEnum() const
  276. {
  277. DBG_ASSERT(type_ == OT_String);
  278. return MapStringSettingToEnum(default_string_);
  279. }
  280. /** Set the default as a string - can be called for OT_String */
  281. virtual void SetDefaultString(const std::string& default_value)
  282. {
  283. DBG_ASSERT(type_ == OT_String);
  284. default_string_ = default_value;
  285. }
  286. /** get the valid string settings - can be called for OT_String */
  287. virtual std::vector<string_entry> GetValidStrings() const
  288. {
  289. DBG_ASSERT(type_ == OT_String);
  290. return valid_strings_;
  291. }
  292. /** Check if the Number value is a valid setting - can be called
  293. * for OT_Number */
  294. virtual bool IsValidNumberSetting(const Number& value) const
  295. {
  296. DBG_ASSERT(type_ == OT_Number);
  297. if (has_lower_ && ((lower_strict_ == true && value <= lower_) ||
  298. (lower_strict_ == false && value < lower_))) {
  299. return false;
  300. }
  301. if (has_upper_ && ((upper_strict_ == true && value >= upper_) ||
  302. (upper_strict_ == false && value > upper_))) {
  303. return false;
  304. }
  305. return true;
  306. }
  307. /** Check if the Integer value is a valid setting - can be called
  308. * for OT_Integer */
  309. virtual bool IsValidIntegerSetting(const Index& value) const
  310. {
  311. DBG_ASSERT(type_ == OT_Integer);
  312. if (has_lower_ && value < lower_) {
  313. return false;
  314. }
  315. if (has_upper_ && value > upper_) {
  316. return false;
  317. }
  318. return true;
  319. }
  320. /** Check if the String value is a valid setting - can be called
  321. * for OT_String */
  322. virtual bool IsValidStringSetting(const std::string& value) const;
  323. /** Map a user setting (allowing any case) to the case used when
  324. * the setting was registered.
  325. */
  326. virtual std::string MapStringSetting(const std::string& value) const;
  327. /** Map a user setting (allowing any case) to the index of the
  328. * matched setting in the list of string settings. Helps map a
  329. * string setting to an enumeration.
  330. */
  331. virtual Index MapStringSettingToEnum(const std::string& value) const;
  332. //@}
  333. /** output a description of the option */
  334. virtual void OutputDescription(const Journalist& jnlst) const;
  335. /** output a more concise version */
  336. virtual void OutputShortDescription(const Journalist& jnlst) const;
  337. /** output a latex version */
  338. virtual void OutputLatexDescription(const Journalist& jnlst) const;
  339. private:
  340. std::string name_;
  341. std::string short_description_;
  342. std::string long_description_;
  343. std::string registering_category_;
  344. RegisteredOptionType type_;
  345. bool has_lower_;
  346. bool lower_strict_;
  347. Number lower_;
  348. bool has_upper_;
  349. bool upper_strict_;
  350. Number upper_;
  351. Number default_number_;
  352. void MakeValidLatexString(std::string source, std::string& dest) const;
  353. std::string MakeValidLatexNumber(Number value) const;
  354. /** Compare two strings and return true if they are equal (case
  355. insensitive comparison) */
  356. bool string_equal_insensitive(const std::string& s1,
  357. const std::string& s2) const;
  358. std::vector<string_entry> valid_strings_;
  359. std::string default_string_;
  360. /** Has the information as how many-th option this one was
  361. * registered. */
  362. const Index counter_;
  363. };
  364. /** Class for storing registered options. Used for validation and
  365. * documentation.
  366. */
  367. class RegisteredOptions : public ReferencedObject
  368. {
  369. public:
  370. /** Constructors / Destructors */
  371. //@{
  372. /** Standard Constructor */
  373. RegisteredOptions()
  374. :
  375. next_counter_(0),
  376. current_registering_category_("Uncategorized")
  377. {}
  378. /** Standard Destructor */
  379. virtual ~RegisteredOptions()
  380. {}
  381. //@}
  382. DECLARE_STD_EXCEPTION(OPTION_ALREADY_REGISTERED);
  383. /** Methods to interact with registered options */
  384. //@{
  385. /** set the registering class. All subsequent options will be
  386. * added with the registered class */
  387. virtual void SetRegisteringCategory(const std::string& registering_category)
  388. {
  389. current_registering_category_ = registering_category;
  390. }
  391. /** retrieve the value of the current registering category */
  392. virtual std::string RegisteringCategory()
  393. {
  394. return current_registering_category_;
  395. }
  396. /** Add a Number option (with no restrictions) */
  397. virtual void AddNumberOption(const std::string& name,
  398. const std::string& short_description,
  399. Number default_value,
  400. const std::string& long_description="");
  401. /** Add a Number option (with a lower bound) */
  402. virtual void AddLowerBoundedNumberOption(const std::string& name,
  403. const std::string& short_description,
  404. Number lower, bool strict,
  405. Number default_value,
  406. const std::string& long_description="");
  407. /** Add a Number option (with a upper bound) */
  408. virtual void AddUpperBoundedNumberOption(const std::string& name,
  409. const std::string& short_description,
  410. Number upper, bool strict,
  411. Number default_value,
  412. const std::string& long_description="");
  413. /** Add a Number option (with a both bounds) */
  414. virtual void AddBoundedNumberOption(const std::string& name,
  415. const std::string& short_description,
  416. Number lower, bool lower_strict,
  417. Number upper, bool upper_strict,
  418. Number default_value,
  419. const std::string& long_description="");
  420. /** Add a Integer option (with no restrictions) */
  421. virtual void AddIntegerOption(const std::string& name,
  422. const std::string& short_description,
  423. Index default_value,
  424. const std::string& long_description="");
  425. /** Add a Integer option (with a lower bound) */
  426. virtual void AddLowerBoundedIntegerOption(const std::string& name,
  427. const std::string& short_description,
  428. Index lower, Index default_value,
  429. const std::string& long_description="");
  430. /** Add a Integer option (with a upper bound) */
  431. virtual void AddUpperBoundedIntegerOption(const std::string& name,
  432. const std::string& short_description,
  433. Index upper, Index default_value,
  434. const std::string& long_description="");
  435. /** Add a Integer option (with a both bounds) */
  436. virtual void AddBoundedIntegerOption(const std::string& name,
  437. const std::string& short_description,
  438. Index lower, Index upper,
  439. Index default_value,
  440. const std::string& long_description="");
  441. /** Add a String option (with no restrictions) */
  442. virtual void AddStringOption(const std::string& name,
  443. const std::string& short_description,
  444. const std::string& default_value,
  445. const std::vector<std::string>& settings,
  446. const std::vector<std::string>& descriptions,
  447. const std::string& long_description="");
  448. /** Methods that make adding string options with only a few
  449. * entries easier */
  450. virtual void AddStringOption1(const std::string& name,
  451. const std::string& short_description,
  452. const std::string& default_value,
  453. const std::string& setting1,
  454. const std::string& description1,
  455. const std::string& long_description="");
  456. virtual void AddStringOption2(const std::string& name,
  457. const std::string& short_description,
  458. const std::string& default_value,
  459. const std::string& setting1,
  460. const std::string& description1,
  461. const std::string& setting2,
  462. const std::string& description2,
  463. const std::string& long_description="");
  464. virtual void AddStringOption3(const std::string& name,
  465. const std::string& short_description,
  466. const std::string& default_value,
  467. const std::string& setting1,
  468. const std::string& description1,
  469. const std::string& setting2,
  470. const std::string& description2,
  471. const std::string& setting3,
  472. const std::string& description3,
  473. const std::string& long_description="");
  474. virtual void AddStringOption4(const std::string& name,
  475. const std::string& short_description,
  476. const std::string& default_value,
  477. const std::string& setting1,
  478. const std::string& description1,
  479. const std::string& setting2,
  480. const std::string& description2,
  481. const std::string& setting3,
  482. const std::string& description3,
  483. const std::string& setting4,
  484. const std::string& description4,
  485. const std::string& long_description="");
  486. virtual void AddStringOption5(const std::string& name,
  487. const std::string& short_description,
  488. const std::string& default_value,
  489. const std::string& setting1,
  490. const std::string& description1,
  491. const std::string& setting2,
  492. const std::string& description2,
  493. const std::string& setting3,
  494. const std::string& description3,
  495. const std::string& setting4,
  496. const std::string& description4,
  497. const std::string& setting5,
  498. const std::string& description5,
  499. const std::string& long_description="");
  500. virtual void AddStringOption6(const std::string& name,
  501. const std::string& short_description,
  502. const std::string& default_value,
  503. const std::string& setting1,
  504. const std::string& description1,
  505. const std::string& setting2,
  506. const std::string& description2,
  507. const std::string& setting3,
  508. const std::string& description3,
  509. const std::string& setting4,
  510. const std::string& description4,
  511. const std::string& setting5,
  512. const std::string& description5,
  513. const std::string& setting6,
  514. const std::string& description6,
  515. const std::string& long_description="");
  516. virtual void AddStringOption7(const std::string& name,
  517. const std::string& short_description,
  518. const std::string& default_value,
  519. const std::string& setting1,
  520. const std::string& description1,
  521. const std::string& setting2,
  522. const std::string& description2,
  523. const std::string& setting3,
  524. const std::string& description3,
  525. const std::string& setting4,
  526. const std::string& description4,
  527. const std::string& setting5,
  528. const std::string& description5,
  529. const std::string& setting6,
  530. const std::string& description6,
  531. const std::string& setting7,
  532. const std::string& description7,
  533. const std::string& long_description="");
  534. virtual void AddStringOption8(const std::string& name,
  535. const std::string& short_description,
  536. const std::string& default_value,
  537. const std::string& setting1,
  538. const std::string& description1,
  539. const std::string& setting2,
  540. const std::string& description2,
  541. const std::string& setting3,
  542. const std::string& description3,
  543. const std::string& setting4,
  544. const std::string& description4,
  545. const std::string& setting5,
  546. const std::string& description5,
  547. const std::string& setting6,
  548. const std::string& description6,
  549. const std::string& setting7,
  550. const std::string& description7,
  551. const std::string& setting8,
  552. const std::string& description8,
  553. const std::string& long_description="");
  554. virtual void AddStringOption9(const std::string& name,
  555. const std::string& short_description,
  556. const std::string& default_value,
  557. const std::string& setting1,
  558. const std::string& description1,
  559. const std::string& setting2,
  560. const std::string& description2,
  561. const std::string& setting3,
  562. const std::string& description3,
  563. const std::string& setting4,
  564. const std::string& description4,
  565. const std::string& setting5,
  566. const std::string& description5,
  567. const std::string& setting6,
  568. const std::string& description6,
  569. const std::string& setting7,
  570. const std::string& description7,
  571. const std::string& setting8,
  572. const std::string& description8,
  573. const std::string& setting9,
  574. const std::string& description9,
  575. const std::string& long_description="");
  576. virtual void AddStringOption10(const std::string& name,
  577. const std::string& short_description,
  578. const std::string& default_value,
  579. const std::string& setting1,
  580. const std::string& description1,
  581. const std::string& setting2,
  582. const std::string& description2,
  583. const std::string& setting3,
  584. const std::string& description3,
  585. const std::string& setting4,
  586. const std::string& description4,
  587. const std::string& setting5,
  588. const std::string& description5,
  589. const std::string& setting6,
  590. const std::string& description6,
  591. const std::string& setting7,
  592. const std::string& description7,
  593. const std::string& setting8,
  594. const std::string& description8,
  595. const std::string& setting9,
  596. const std::string& description9,
  597. const std::string& setting10,
  598. const std::string& description10,
  599. const std::string& long_description="");
  600. /** Get a registered option - this will return NULL if the option
  601. * does not exist */
  602. virtual SmartPtr<const RegisteredOption> GetOption(const std::string& name);
  603. /** Output documentation for the options - gives a description,
  604. * etc. */
  605. virtual void OutputOptionDocumentation(const Journalist& jnlst, std::list<std::string>& categories);
  606. /** Output documentation in Latex format to include in a latex file */
  607. virtual void OutputLatexOptionDocumentation(const Journalist& jnlst, std::list<std::string>& categories);
  608. //@}
  609. typedef std::map<std::string, SmartPtr<RegisteredOption> > RegOptionsList;
  610. /** Giving access to iteratable representation of the registered
  611. * options */
  612. virtual const RegOptionsList& RegisteredOptionsList () const
  613. {
  614. return registered_options_;
  615. }
  616. private:
  617. Index next_counter_;
  618. std::string current_registering_category_;
  619. std::map<std::string, SmartPtr<RegisteredOption> > registered_options_;
  620. };
  621. } // namespace Ipopt
  622. #endif