/home/coin/SVN-release/OS-2.1.1/Bonmin/src/Interfaces/BonRegisteredOptions.cpp

Go to the documentation of this file.
00001 // (C) Copyright International Business Machines Corporation 2007
00002 //
00003 // All Rights Reserved.
00004 // This code is published under the Common Public License.
00005 //
00006 // Authors :
00007 // Pierre Bonami, International Business Machines,
00008 //
00009 // Date : 27/08/2007
00010 
00011 #include "BonRegisteredOptions.hpp"
00012 #include <IpSmartPtr.hpp>
00013 #include <sstream>
00014 #include <climits>
00015 #include <cfloat>
00016 
00017 namespace Bonmin{
00018   struct optionsCmp{
00019   bool operator()( Ipopt::RegisteredOption * a,
00020                    Ipopt::RegisteredOption * b){
00021     if(a->RegisteringCategory() == b->RegisteringCategory()){
00022        return a->Name() < b->Name();
00023     }
00024     return a->RegisteringCategory() < b->RegisteringCategory(); 
00025   }
00026   };
00027 
00028 
00029   static std::string makeLatex(const std::string &s){
00030     std::string ret_val;
00031     std::string::const_iterator i = s.begin();
00032     for(; i != s.end() ; i++){
00033        switch (*i) {
00034          case '_':
00035          case '-':
00036            ret_val +='\\';
00037          default:
00038            ret_val += *i;
00039        }
00040     }
00041    return ret_val;
00042   }
00043 
00044   static std::string makeSpaceLess(const std::string &s){
00045     std::string ret_val;
00046     std::string::const_iterator i = s.begin();
00047     for(; i != s.end() ; i++){
00048        switch (*i) {
00049          case ' ':
00050          case '\t':
00051            ret_val +='_';
00052            break;
00053          default:
00054            ret_val += *i;
00055        }
00056     }
00057    return ret_val;
00058   }
00059 
00060 #if 0
00061   static std::string makeLatex(double value){
00062     std::string ret_val = "$";
00063     std::stringstream s_val;
00064     s_val<<value;
00065     
00066     unsigned int i = s_val.str().find_first_of('e');
00067     if(i != s_val.str().size()){
00068       ret_val += s_val.str().substr(0,i-1);
00069       ret_val += " \\cdot 10^{";
00070       ret_val += s_val.str().substr(i+1);
00071       ret_val += '}';
00072     }
00073     else ret_val += s_val.str();
00074     ret_val += '$';
00075     return ret_val;
00076   }
00077 #endif
00078 
00079   static std::string makeString(int value){
00080     std::string ret_val;
00081     if(value >= INT_MAX){
00082      ret_val="INT_MAX";}
00083     else if(value <= - INT_MAX){
00084       ret_val="-INT_MAX";}
00085     else{
00086       std::stringstream s_val;
00087       s_val<<value;
00088       ret_val = s_val.str();
00089     }
00090     return ret_val;
00091   }
00092 
00093   static std::string makeString(double value){
00094     std::string ret_val;
00095     if(value >= DBL_MAX){
00096      ret_val="DBL_MAX";}
00097     else if(value <= - DBL_MAX){
00098       ret_val="-DBL_MAX";}
00099     else{
00100       std::stringstream s_val;
00101       s_val<<value;
00102       ret_val = s_val.str();
00103     }
00104     return ret_val;
00105   }
00106 
00107  static std::string makeNumber(std::string value){
00108    if(value == "DBL_MAX") {
00109      std::stringstream s_val;
00110      s_val<<DBL_MAX;
00111      return s_val.str();
00112    }
00113    if(value == "-DBL_MAX") {
00114      std::stringstream s_val;
00115      s_val<<-DBL_MAX;
00116      return s_val.str();
00117    }
00118    if(value == "INT_MAX") {
00119      std::stringstream s_val;
00120      s_val<<INT_MAX;
00121      return s_val.str();
00122    }
00123    if(value == "-INT_MAX") {
00124      std::stringstream s_val;
00125      s_val<<-INT_MAX;
00126      return s_val.str();
00127    }
00128    return value;
00129  }
00130 #if 0
00131   static std::string makeLatex(int value){
00132     std::string ret_val = "$";
00133     std::stringstream s_val;
00134     s_val<<value;
00135     ret_val += s_val.str();
00136     ret_val += "$";
00137     return ret_val;
00138   }
00139 #endif
00140 
00141   static char OptionType2Char(const Ipopt::RegisteredOptionType &T){
00142     switch(T){
00143       case Ipopt::OT_Number: return 'F';
00144       case Ipopt::OT_Integer: return 'I';
00145       case Ipopt::OT_String: return 'S';
00146       case Ipopt::OT_Unknown: 
00147       default: return 'U';
00148     }
00149   }
00150 
00151   static std::string defaultAsString(Ipopt::SmartPtr< Ipopt::RegisteredOption > opt){
00152     Ipopt::RegisteredOptionType T = opt->Type();
00153     switch(T){
00154       case Ipopt::OT_Number: return makeString(opt->DefaultNumber());
00155       case Ipopt::OT_Integer: return makeString(opt->DefaultInteger());
00156       case Ipopt::OT_String: return (opt->DefaultString());
00157       case Ipopt::OT_Unknown: 
00158       default:
00159          return "Unknown type of option";
00160     }
00161   }
00163   void 
00164   RegisteredOptions::writeLatexOptionsTable(std::ostream &of, ExtraCategoriesInfo which){
00165   std::map<std::string, Ipopt::SmartPtr<Ipopt::RegisteredOption> > 
00166            registered_options = RegisteredOptionsList();
00167 
00168   //Print table header
00169   //of<<"\\begin{threeparttable}"<<std::endl
00170   of<<"\\topcaption{\\label{tab:options} "<<std::endl
00171     <<"List of options and compatibility with the different algorithms."<<std::endl
00172     <<"}"<<std::endl;
00173   of<<"\\tablehead{\\hline "<<std::endl
00174     <<"Option & type &  default & {\\tt B-BB} & {\\tt B-OA} & {\\tt B-QG} & {\\tt B-Hyb} & {\\tt B-Ecp} & {\\tt B-iFP} & {\\tt Cbc\\_Par} \\\\"<<std::endl
00175     <<"\\hline"<<std::endl
00176     <<"\\hline}"<<std::endl;
00177   of<<"\\tabletail{\\hline \\multicolumn{10}{|c|}{continued on next page}\\\\"
00178     <<"\\hline}"<<std::endl; 
00179   of<<"\\tablelasttail{\\hline}"<<std::endl;
00180   of<<"{\\tiny"<<std::endl;
00181   of<<"\\begin{xtabular}{|l|r|r|r|r|r|r|r|r|r|}"<<std::endl;
00182 
00183   //sort options by categories and alphabetical order
00184   std::list< Ipopt::RegisteredOption * > sortedOptions;
00185  
00186   for(std::map<std::string, Ipopt::SmartPtr<Ipopt::RegisteredOption > >::iterator i = 
00187           registered_options.begin(); i != registered_options.end() ; i++){
00188      if(categoriesInfo(i->second->RegisteringCategory()) == which)
00189      sortedOptions.push_back(GetRawPtr(i->second));
00190      }
00191 
00192    sortedOptions.sort(optionsCmp());
00193    std::string registeringCategory = "";
00194    for(std::list< Ipopt::RegisteredOption * >::iterator i = sortedOptions.begin();
00195        i != sortedOptions.end() ; i++)
00196    {
00197      if((*i)->RegisteringCategory() != registeringCategory){
00198      registeringCategory = (*i)->RegisteringCategory();
00199      of<<"\\hline"<<std::endl
00200        <<"\\multicolumn{1}{|c}{} & \\multicolumn{9}{l|}{"
00201        <<registeringCategory<<"}\\\\"<<std::endl
00202        <<"\\hline"<<std::endl;
00203      }
00204      
00205      of<<makeLatex((*i)->Name())<<"& "<<OptionType2Char((*i)->Type())<<"& "
00206        <<makeLatex(defaultAsString(*i))
00207        <<"& "<<( (isValidForBBB((*i)->Name()))? "$\\surd$" : "-" )
00208        <<"& "<<( (isValidForBOA((*i)->Name()))? "$\\surd$" : "-" )
00209        <<"& "<<( (isValidForBQG((*i)->Name()))? "$\\surd$" : "-" )
00210        <<"& "<<( (isValidForHybrid((*i)->Name()))? "$\\surd$" : "-" )
00211        <<"& "<<( (isValidForBEcp((*i)->Name()))? "$\\surd$" : "-" )
00212        <<"& "<<( (isValidForBiFP((*i)->Name()))? "$\\surd$" : "-" )
00213        <<"& "<<( (isValidForCbc((*i)->Name()))? "$\\surd$" : "-" )
00214        <<"\\\\"<<std::endl;
00215    }
00216    //Print table end
00217   of<<"\\hline"<<std::endl
00218     <<"\\end{xtabular}"<<std::endl;
00219   of<<"}"<<std::endl;
00220 #if 0
00221   of<<"\\begin{tablenotes}"<<std::endl
00222     <<"\\item $\\strut^*$ option is available"<<std::endl
00223     <<"        for MILP subsolver (it is only passed if the {\\tt milp\\_subsolver} optio"<<std::endl
00224     <<"        see Subsection \\ref{sec:milp_opt})."<<std::endl
00225     <<"       \\item $\\strut^1$ disabled for stability reasons."<<std::endl
00226     <<"\\end{tablenotes}"<<std::endl
00227     <<"\\end{threeparttable} "<<std::endl;
00228 #endif
00229   }
00230 
00232   void 
00233   RegisteredOptions::chooseOptions(ExtraCategoriesInfo which,
00234                                             std::list< Ipopt::RegisteredOption * >& sortedOptions)
00235   {
00236   std::map<std::string, Ipopt::SmartPtr<Ipopt::RegisteredOption> > 
00237            registered_options = RegisteredOptionsList();
00238 
00239   for(std::map<std::string, Ipopt::SmartPtr<Ipopt::RegisteredOption > >::iterator i = 
00240           registered_options.begin(); i != registered_options.end() ; i++){
00241      if(categoriesInfo(i->second->RegisteringCategory()) == which)
00242      sortedOptions.push_back(GetRawPtr(i->second));
00243      }
00244    sortedOptions.sort(optionsCmp());
00245   }
00247   void 
00248   RegisteredOptions::writeHtmlOptionsTable(std::ostream &of, ExtraCategoriesInfo which){
00249 
00250   //Print table header
00251   of<<"<table border=\"1\">"<<std::endl;
00252   //sort options by categories and alphabetical order
00253   std::list< Ipopt::RegisteredOption * > sortedOptions;
00254   chooseOptions(which, sortedOptions); 
00255   writeHtmlOptionsTable(of, sortedOptions);
00256   }
00257 
00258    
00260    void 
00261    RegisteredOptions::writeHtmlOptionsTable(std::ostream &os, std::list<Ipopt::RegisteredOption *> &options)
00262    {
00263    os<<"<table border=\"1\">"<<std::endl;
00264    os<<"<tr>"<<std::endl;
00265    os<<"<td>Option </td>"<<std::endl;
00266    os<<"<td> type </td>"<<std::endl;
00267    os<<"<td> default </td>"<<std::endl;
00268    os<<"<td> B-BB</td>"<<std::endl;
00269    os<<"<td> B-OA</td>"<<std::endl;
00270    os<<"<td> B-QG</td>"<<std::endl;
00271    os<<"<td> B-Hyb</td>"<<std::endl;
00272    os<<"</tr>"<<std::endl;
00273    std::string registeringCategory = "";
00274    for(std::list< Ipopt::RegisteredOption * >::iterator i = options.begin();
00275        i != options.end() ; i++)
00276    {
00277      if((*i)->RegisteringCategory() != registeringCategory){
00278      registeringCategory = (*i)->RegisteringCategory();
00279      os<<"<tr>"
00280        <<"   <th colspan=7>"
00281        <<" <a href=\"#sec:"<<makeSpaceLess(registeringCategory)<<"\">"
00282        <<registeringCategory<<"</a> </th>"<<std::endl
00283        <<"</tr>"<<std::endl;
00284      }
00285      
00286      os<<"<tr>"<<std::endl
00287        <<"<td> <a href=\"#sec:"<<makeSpaceLess((*i)->Name())<<"\">"
00288        <<((*i)->Name())<<"</a> </td>"<<std::endl
00289        <<"<td>"<<OptionType2Char((*i)->Type())<<"</td>"<<std::endl
00290        <<"<td>"<<defaultAsString(*i)<<"</td>"<<std::endl
00291        <<"<td> "<<( (isValidForBBB((*i)->Name()))? '+' : '-' )<<"</td>"<<std::endl
00292        <<"<td>"<<( (isValidForBOA((*i)->Name()))? '+' : '-' )<<"</td>"<<std::endl
00293        <<"<td>"<<( (isValidForBQG((*i)->Name()))? '+' : '-' )<<"</td>"<<std::endl
00294        <<"<td>"<<( (isValidForHybrid((*i)->Name()))? '+' : '-' )<<"</td>"<<std::endl
00295        <<"</tr>"<<std::endl;
00296    }
00297    //Print table end
00298   os<<"</tr>"<<std::endl
00299     <<"</table>"<<std::endl;
00300   }
00301 
00303    void 
00304    RegisteredOptions::writeLatexHtmlDoc(std::ostream &os, ExtraCategoriesInfo which){
00305       std::list< Ipopt::RegisteredOption * > options;
00306       chooseOptions(which, options);
00307       os<<"\\latexhtml{}{"<<std::endl;
00308       os<<"\\begin{rawhtml}"<<std::endl;
00309       writeHtmlOptionsTable(os, options);
00310       os<<"\\end{rawhtml}\n}"<<std::endl;
00311 
00312       //Create journalist to write to os
00313       Ipopt::Journalist jnlst;
00314       Ipopt::SmartPtr<Ipopt::StreamJournal> J = new Ipopt::StreamJournal("options_journal", Ipopt::J_ALL);
00315       J->SetOutputStream(&os);
00316       J->SetPrintLevel(Ipopt::J_DOCUMENTATION, Ipopt::J_SUMMARY);
00317       jnlst.AddJournal(GetRawPtr(J));
00318 
00319       std::string registeringCategory = "";
00320       for(std::list< Ipopt::RegisteredOption * >::iterator i = options.begin();
00321            i != options.end() ; i++)
00322        {
00323           if((*i)->RegisteringCategory() != registeringCategory){
00324            registeringCategory = (*i)->RegisteringCategory();
00325              os<<"\\subsection{"<<registeringCategory<<"}"<<std::endl;      
00326              os<<"\\label{sec:"<<makeSpaceLess(registeringCategory)<<"}"<<std::endl;
00327            }
00328        
00329            (*i)->OutputLatexDescription(jnlst);
00330        }
00331     }
00332 
00334   void
00335   RegisteredOptions::writeBonminOpt(std::ostream &os, ExtraCategoriesInfo which){
00336       std::list< Ipopt::RegisteredOption * > options;
00337       chooseOptions(which, options);
00338 
00339       //Create journalist to write to os
00340       Ipopt::Journalist jnlst;
00341       Ipopt::SmartPtr<Ipopt::StreamJournal> J = new Ipopt::StreamJournal("options_journal", Ipopt::J_ALL);
00342       J->SetOutputStream(&os);
00343       J->SetPrintLevel(Ipopt::J_DOCUMENTATION, Ipopt::J_SUMMARY);
00344       jnlst.AddJournal(GetRawPtr(J));
00345 
00346       std::string registeringCategory = "";
00347       for(std::list< Ipopt::RegisteredOption * >::iterator i = options.begin();
00348            i != options.end() ; i++)
00349        {
00350           if((*i)->RegisteringCategory() != registeringCategory){
00351            registeringCategory = (*i)->RegisteringCategory();
00352              os<<std::endl<<"# registering category: "<<registeringCategory<<std::endl<<std::endl;
00353            }
00354            os<<"bonmin.";
00355            os.setf(std::ios::left);
00356            os.width(37);
00357            os<<(*i)->Name()<<" ";
00358            os.width(10);
00359            os<<makeNumber(defaultAsString(*i))<<"\t#";
00360            os<<(*i)->ShortDescription();
00361            os<<std::endl;            
00362        }
00363     }
00364 
00365 }/*Ends bonmin namespace.*/

Generated on Mon May 3 03:05:17 2010 by  doxygen 1.4.7