Ipopt
trunk
|
00001 // Copyright (C) 2004, 2007 International Business Machines and others. 00002 // All Rights Reserved. 00003 // This code is published under the Eclipse Public License. 00004 // 00005 // $Id$ 00006 // 00007 // Authors: Carl Laird, Andreas Waechter IBM 2005-06-18 00008 00009 #ifndef __IPREGOPTIONS_HPP__ 00010 #define __IPREGOPTIONS_HPP__ 00011 00012 #include "IpUtils.hpp" 00013 #include "IpReferenced.hpp" 00014 #include "IpException.hpp" 00015 #include "IpSmartPtr.hpp" 00016 00017 #include <map> 00018 00019 namespace Ipopt 00020 { 00021 00022 enum RegisteredOptionType 00023 { 00024 OT_Number, 00025 OT_Integer, 00026 OT_String, 00027 OT_Unknown 00028 }; 00029 00033 class RegisteredOption : public ReferencedObject 00034 { 00035 public: 00037 class string_entry 00038 { 00039 public: 00040 string_entry(const std::string& value, const std::string& description) 00041 : value_(value), description_(description) 00042 {} 00043 std::string value_; 00044 std::string description_; 00045 }; 00046 00049 RegisteredOption(Index counter) 00050 : 00051 type_(OT_Unknown), 00052 has_lower_(false), 00053 has_upper_(false), 00054 counter_(counter) 00055 {} 00056 00057 RegisteredOption(const std::string& name, 00058 const std::string& short_description, 00059 const std::string& long_description, 00060 const std::string& registering_category, 00061 Index counter) 00062 : 00063 name_(name), 00064 short_description_(short_description), 00065 long_description_(long_description), 00066 registering_category_(registering_category), 00067 type_(OT_Unknown), 00068 has_lower_(false), 00069 has_upper_(false), 00070 counter_(counter) 00071 {} 00072 00073 RegisteredOption(const RegisteredOption& copy) 00074 : 00075 name_(copy.name_), 00076 short_description_(copy.short_description_), 00077 long_description_(copy.long_description_), 00078 registering_category_(copy.registering_category_), 00079 type_(copy.type_), 00080 has_lower_(copy.has_lower_), 00081 lower_(copy.lower_), 00082 has_upper_(copy.has_upper_), 00083 upper_(copy.upper_), 00084 valid_strings_(copy.valid_strings_), 00085 counter_(copy.counter_) 00086 {} 00087 00088 virtual ~RegisteredOption() 00089 {} 00091 00092 DECLARE_STD_EXCEPTION(ERROR_CONVERTING_STRING_TO_ENUM); 00093 00097 virtual const std::string& Name() const 00098 { 00099 return name_; 00100 } 00102 virtual void SetName(const std::string& name) 00103 { 00104 name_ = name; 00105 } 00107 virtual const std::string& ShortDescription() const 00108 { 00109 return short_description_; 00110 } 00112 virtual const std::string& LongDescription() const 00113 { 00114 return long_description_; 00115 } 00117 virtual void SetShortDescription(const std::string& short_description) 00118 { 00119 short_description_ = short_description; 00120 } 00122 virtual void SetLongDescription(const std::string& long_description) 00123 { 00124 long_description_ = long_description; 00125 } 00127 virtual const std::string& RegisteringCategory() const 00128 { 00129 return registering_category_; 00130 } 00132 virtual void SetRegisteringCategory(const std::string& registering_category) 00133 { 00134 registering_category_ = registering_category; 00135 } 00137 virtual const RegisteredOptionType& Type() const 00138 { 00139 return type_; 00140 } 00142 virtual void SetType(const RegisteredOptionType& type) 00143 { 00144 type_ = type; 00145 } 00147 virtual Index Counter() const 00148 { 00149 return counter_; 00150 } 00152 00159 virtual const bool& HasLower() const 00160 { 00161 DBG_ASSERT(type_ == OT_Number || type_ == OT_Integer); 00162 return has_lower_; 00163 } 00166 virtual const bool& LowerStrict() const 00167 { 00168 DBG_ASSERT(type_ == OT_Number && has_lower_ == true); 00169 return lower_strict_; 00170 } 00173 virtual Number LowerNumber() const 00174 { 00175 DBG_ASSERT(has_lower_ == true && type_ == OT_Number); 00176 return lower_; 00177 } 00180 virtual void SetLowerNumber(const Number& lower, const bool& strict) 00181 { 00182 DBG_ASSERT(type_ == OT_Number); 00183 lower_ = lower; 00184 lower_strict_ = strict, has_lower_ = true; 00185 } 00188 virtual Index LowerInteger() const 00189 { 00190 DBG_ASSERT(has_lower_ == true && type_ == OT_Integer); 00191 return (Index)lower_; 00192 } 00195 virtual void SetLowerInteger(const Index& lower) 00196 { 00197 DBG_ASSERT(type_ == OT_Integer); 00198 lower_ = (Number)lower; 00199 has_lower_ = true; 00200 } 00203 virtual const bool& HasUpper() const 00204 { 00205 DBG_ASSERT(type_ == OT_Number || type_ == OT_Integer); 00206 return has_upper_; 00207 } 00210 virtual const bool& UpperStrict() const 00211 { 00212 DBG_ASSERT(type_ == OT_Number && has_upper_ == true); 00213 return upper_strict_; 00214 } 00217 virtual Number UpperNumber() const 00218 { 00219 DBG_ASSERT(has_upper_ == true && type_ == OT_Number); 00220 return upper_; 00221 } 00224 virtual void SetUpperNumber(const Number& upper, const bool& strict) 00225 { 00226 DBG_ASSERT(type_ == OT_Number); 00227 upper_ = upper; 00228 upper_strict_ = strict; 00229 has_upper_ = true; 00230 } 00233 virtual Index UpperInteger() const 00234 { 00235 DBG_ASSERT(has_upper_ == true && type_ == OT_Integer); 00236 return (Index)upper_; 00237 } 00240 virtual void SetUpperInteger(const Index& upper) 00241 { 00242 DBG_ASSERT(type_ == OT_Integer); 00243 upper_ = (Number)upper; 00244 has_upper_ = true; 00245 } 00248 virtual void AddValidStringSetting(const std::string value, 00249 const std::string description) 00250 { 00251 DBG_ASSERT(type_ == OT_String); 00252 valid_strings_.push_back(string_entry(value, description)); 00253 } 00255 virtual Number DefaultNumber() const 00256 { 00257 DBG_ASSERT(type_ == OT_Number); 00258 return default_number_; 00259 } 00261 virtual void SetDefaultNumber(const Number& default_value) 00262 { 00263 DBG_ASSERT(type_ == OT_Number); 00264 default_number_ = default_value; 00265 } 00267 virtual Index DefaultInteger() const 00268 { 00269 DBG_ASSERT(type_ == OT_Integer); 00270 return (Index)default_number_; 00271 } 00274 virtual void SetDefaultInteger(const Index& default_value) 00275 { 00276 DBG_ASSERT(type_ == OT_Integer); 00277 default_number_ = (Number)default_value; 00278 } 00280 virtual std::string DefaultString() const 00281 { 00282 DBG_ASSERT(type_ == OT_String); 00283 return default_string_; 00284 } 00288 virtual Index DefaultStringAsEnum() const 00289 { 00290 DBG_ASSERT(type_ == OT_String); 00291 return MapStringSettingToEnum(default_string_); 00292 } 00294 virtual void SetDefaultString(const std::string& default_value) 00295 { 00296 DBG_ASSERT(type_ == OT_String); 00297 default_string_ = default_value; 00298 } 00300 virtual std::vector<string_entry> GetValidStrings() const 00301 { 00302 DBG_ASSERT(type_ == OT_String); 00303 return valid_strings_; 00304 } 00307 virtual bool IsValidNumberSetting(const Number& value) const 00308 { 00309 DBG_ASSERT(type_ == OT_Number); 00310 if (has_lower_ && ((lower_strict_ == true && value <= lower_) || 00311 (lower_strict_ == false && value < lower_))) { 00312 return false; 00313 } 00314 if (has_upper_ && ((upper_strict_ == true && value >= upper_) || 00315 (upper_strict_ == false && value > upper_))) { 00316 return false; 00317 } 00318 return true; 00319 } 00322 virtual bool IsValidIntegerSetting(const Index& value) const 00323 { 00324 DBG_ASSERT(type_ == OT_Integer); 00325 if (has_lower_ && value < lower_) { 00326 return false; 00327 } 00328 if (has_upper_ && value > upper_) { 00329 return false; 00330 } 00331 return true; 00332 } 00335 virtual bool IsValidStringSetting(const std::string& value) const; 00336 00340 virtual std::string MapStringSetting(const std::string& value) const; 00341 00346 virtual Index MapStringSettingToEnum(const std::string& value) const; 00348 00350 virtual void OutputDescription(const Journalist& jnlst) const; 00352 virtual void OutputShortDescription(const Journalist& jnlst) const; 00354 virtual void OutputLatexDescription(const Journalist& jnlst) const; 00355 00356 private: 00357 std::string name_; 00358 std::string short_description_; 00359 std::string long_description_; 00360 std::string registering_category_; 00361 RegisteredOptionType type_; 00362 00363 bool has_lower_; 00364 bool lower_strict_; 00365 Number lower_; 00366 bool has_upper_; 00367 bool upper_strict_; 00368 Number upper_; 00369 Number default_number_; 00370 00371 void MakeValidLatexString(std::string source, std::string& dest) const; 00372 std::string MakeValidLatexNumber(Number value) const; 00373 00376 bool string_equal_insensitive(const std::string& s1, 00377 const std::string& s2) const; 00378 00379 std::vector<string_entry> valid_strings_; 00380 std::string default_string_; 00381 00384 const Index counter_; 00385 }; 00386 00390 class RegisteredOptions : public ReferencedObject 00391 { 00392 public: 00396 RegisteredOptions() 00397 : 00398 next_counter_(0), 00399 current_registering_category_("Uncategorized") 00400 {} 00401 00403 virtual ~RegisteredOptions() 00404 {} 00406 00407 DECLARE_STD_EXCEPTION(OPTION_ALREADY_REGISTERED); 00408 00413 virtual void SetRegisteringCategory(const std::string& registering_category) 00414 { 00415 current_registering_category_ = registering_category; 00416 } 00417 00419 virtual std::string RegisteringCategory() 00420 { 00421 return current_registering_category_; 00422 } 00423 00425 virtual void AddNumberOption(const std::string& name, 00426 const std::string& short_description, 00427 Number default_value, 00428 const std::string& long_description=""); 00430 virtual void AddLowerBoundedNumberOption(const std::string& name, 00431 const std::string& short_description, 00432 Number lower, bool strict, 00433 Number default_value, 00434 const std::string& long_description=""); 00436 virtual void AddUpperBoundedNumberOption(const std::string& name, 00437 const std::string& short_description, 00438 Number upper, bool strict, 00439 Number default_value, 00440 const std::string& long_description=""); 00442 virtual void AddBoundedNumberOption(const std::string& name, 00443 const std::string& short_description, 00444 Number lower, bool lower_strict, 00445 Number upper, bool upper_strict, 00446 Number default_value, 00447 const std::string& long_description=""); 00449 virtual void AddIntegerOption(const std::string& name, 00450 const std::string& short_description, 00451 Index default_value, 00452 const std::string& long_description=""); 00454 virtual void AddLowerBoundedIntegerOption(const std::string& name, 00455 const std::string& short_description, 00456 Index lower, Index default_value, 00457 const std::string& long_description=""); 00459 virtual void AddUpperBoundedIntegerOption(const std::string& name, 00460 const std::string& short_description, 00461 Index upper, Index default_value, 00462 const std::string& long_description=""); 00464 virtual void AddBoundedIntegerOption(const std::string& name, 00465 const std::string& short_description, 00466 Index lower, Index upper, 00467 Index default_value, 00468 const std::string& long_description=""); 00469 00471 virtual void AddStringOption(const std::string& name, 00472 const std::string& short_description, 00473 const std::string& default_value, 00474 const std::vector<std::string>& settings, 00475 const std::vector<std::string>& descriptions, 00476 const std::string& long_description=""); 00479 virtual void AddStringOption1(const std::string& name, 00480 const std::string& short_description, 00481 const std::string& default_value, 00482 const std::string& setting1, 00483 const std::string& description1, 00484 const std::string& long_description=""); 00485 virtual void AddStringOption2(const std::string& name, 00486 const std::string& short_description, 00487 const std::string& default_value, 00488 const std::string& setting1, 00489 const std::string& description1, 00490 const std::string& setting2, 00491 const std::string& description2, 00492 const std::string& long_description=""); 00493 virtual void AddStringOption3(const std::string& name, 00494 const std::string& short_description, 00495 const std::string& default_value, 00496 const std::string& setting1, 00497 const std::string& description1, 00498 const std::string& setting2, 00499 const std::string& description2, 00500 const std::string& setting3, 00501 const std::string& description3, 00502 const std::string& long_description=""); 00503 virtual void AddStringOption4(const std::string& name, 00504 const std::string& short_description, 00505 const std::string& default_value, 00506 const std::string& setting1, 00507 const std::string& description1, 00508 const std::string& setting2, 00509 const std::string& description2, 00510 const std::string& setting3, 00511 const std::string& description3, 00512 const std::string& setting4, 00513 const std::string& description4, 00514 const std::string& long_description=""); 00515 virtual void AddStringOption5(const std::string& name, 00516 const std::string& short_description, 00517 const std::string& default_value, 00518 const std::string& setting1, 00519 const std::string& description1, 00520 const std::string& setting2, 00521 const std::string& description2, 00522 const std::string& setting3, 00523 const std::string& description3, 00524 const std::string& setting4, 00525 const std::string& description4, 00526 const std::string& setting5, 00527 const std::string& description5, 00528 const std::string& long_description=""); 00529 virtual void AddStringOption6(const std::string& name, 00530 const std::string& short_description, 00531 const std::string& default_value, 00532 const std::string& setting1, 00533 const std::string& description1, 00534 const std::string& setting2, 00535 const std::string& description2, 00536 const std::string& setting3, 00537 const std::string& description3, 00538 const std::string& setting4, 00539 const std::string& description4, 00540 const std::string& setting5, 00541 const std::string& description5, 00542 const std::string& setting6, 00543 const std::string& description6, 00544 const std::string& long_description=""); 00545 virtual void AddStringOption7(const std::string& name, 00546 const std::string& short_description, 00547 const std::string& default_value, 00548 const std::string& setting1, 00549 const std::string& description1, 00550 const std::string& setting2, 00551 const std::string& description2, 00552 const std::string& setting3, 00553 const std::string& description3, 00554 const std::string& setting4, 00555 const std::string& description4, 00556 const std::string& setting5, 00557 const std::string& description5, 00558 const std::string& setting6, 00559 const std::string& description6, 00560 const std::string& setting7, 00561 const std::string& description7, 00562 const std::string& long_description=""); 00563 virtual void AddStringOption8(const std::string& name, 00564 const std::string& short_description, 00565 const std::string& default_value, 00566 const std::string& setting1, 00567 const std::string& description1, 00568 const std::string& setting2, 00569 const std::string& description2, 00570 const std::string& setting3, 00571 const std::string& description3, 00572 const std::string& setting4, 00573 const std::string& description4, 00574 const std::string& setting5, 00575 const std::string& description5, 00576 const std::string& setting6, 00577 const std::string& description6, 00578 const std::string& setting7, 00579 const std::string& description7, 00580 const std::string& setting8, 00581 const std::string& description8, 00582 const std::string& long_description=""); 00583 virtual void AddStringOption9(const std::string& name, 00584 const std::string& short_description, 00585 const std::string& default_value, 00586 const std::string& setting1, 00587 const std::string& description1, 00588 const std::string& setting2, 00589 const std::string& description2, 00590 const std::string& setting3, 00591 const std::string& description3, 00592 const std::string& setting4, 00593 const std::string& description4, 00594 const std::string& setting5, 00595 const std::string& description5, 00596 const std::string& setting6, 00597 const std::string& description6, 00598 const std::string& setting7, 00599 const std::string& description7, 00600 const std::string& setting8, 00601 const std::string& description8, 00602 const std::string& setting9, 00603 const std::string& description9, 00604 const std::string& long_description=""); 00605 virtual void AddStringOption10(const std::string& name, 00606 const std::string& short_description, 00607 const std::string& default_value, 00608 const std::string& setting1, 00609 const std::string& description1, 00610 const std::string& setting2, 00611 const std::string& description2, 00612 const std::string& setting3, 00613 const std::string& description3, 00614 const std::string& setting4, 00615 const std::string& description4, 00616 const std::string& setting5, 00617 const std::string& description5, 00618 const std::string& setting6, 00619 const std::string& description6, 00620 const std::string& setting7, 00621 const std::string& description7, 00622 const std::string& setting8, 00623 const std::string& description8, 00624 const std::string& setting9, 00625 const std::string& description9, 00626 const std::string& setting10, 00627 const std::string& description10, 00628 const std::string& long_description=""); 00629 00632 virtual SmartPtr<const RegisteredOption> GetOption(const std::string& name); 00633 00636 virtual void OutputOptionDocumentation(const Journalist& jnlst, std::list<std::string>& categories); 00637 00639 virtual void OutputLatexOptionDocumentation(const Journalist& jnlst, std::list<std::string>& categories); 00641 00642 typedef std::map<std::string, SmartPtr<RegisteredOption> > RegOptionsList; 00643 00646 virtual const RegOptionsList& RegisteredOptionsList () const 00647 { 00648 return registered_options_; 00649 } 00650 00651 private: 00652 Index next_counter_; 00653 std::string current_registering_category_; 00654 std::map<std::string, SmartPtr<RegisteredOption> > registered_options_; 00655 }; 00656 } // namespace Ipopt 00657 00658 #endif