/home/coin/SVN-release/OS-2.0.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     <<"\\caption{\\label{tab:options} "<<std::endl
00171     <<"List of options and compatibility with the different algorithms."<<std::endl
00172     <<"}"<<std::endl
00173     <<"\\begin{tabular}{|l|r|r|r|r|r|r|}"
00174     <<"\\hline"<<std::endl
00175     <<"Option & type &  default & {\\tt B-BB} & {\\tt B-OA} & {\\tt B-QG} & {\\tt B-Hyb} \\\\"<<std::endl
00176     <<"\\hline"<<std::endl;
00177 
00178   //sort options by categories and alphabetical order
00179   std::list< Ipopt::RegisteredOption * > sortedOptions;
00180  
00181   for(std::map<std::string, Ipopt::SmartPtr<Ipopt::RegisteredOption > >::iterator i = 
00182           registered_options.begin(); i != registered_options.end() ; i++){
00183      if(categoriesInfo(i->second->RegisteringCategory()) == which)
00184      sortedOptions.push_back(GetRawPtr(i->second));
00185      }
00186 
00187    sortedOptions.sort(optionsCmp());
00188    std::string registeringCategory = "";
00189    for(std::list< Ipopt::RegisteredOption * >::iterator i = sortedOptions.begin();
00190        i != sortedOptions.end() ; i++)
00191    {
00192      if((*i)->RegisteringCategory() != registeringCategory){
00193      registeringCategory = (*i)->RegisteringCategory();
00194      of<<"\\hline"<<std::endl
00195        <<"\\multicolumn{1}{|c}{} & \\multicolumn{6}{l|}{"
00196        <<registeringCategory<<"}\\\\"<<std::endl
00197        <<"\\hline"<<std::endl;
00198      }
00199      
00200      of<<makeLatex((*i)->Name())<<"& "<<OptionType2Char((*i)->Type())<<"& "
00201        <<makeLatex(defaultAsString(*i))
00202        <<"& "<<( (isValidForBBB((*i)->Name()))? '+' : '-' )
00203        <<"& "<<( (isValidForBOA((*i)->Name()))? '+' : '-' )
00204        <<"& "<<( (isValidForBQG((*i)->Name()))? '+' : '-' )
00205        <<"& "<<( (isValidForHybrid((*i)->Name()))? '+' : '-' )
00206        <<"\\\\"<<std::endl;
00207    }
00208    //Print table end
00209   of<<"\\hline"<<std::endl
00210     <<"\\end{tabular}"<<std::endl
00211     <<"\\begin{tablenotes}"<<std::endl
00212     <<"\\item $\\strut^*$ option is available"<<std::endl
00213     <<"        for MILP subsolver (it is only passed if the {\\tt milp\\_subsolver} optio"<<std::endl
00214     <<"        see Subsection \\ref{sec:milp_opt})."<<std::endl
00215     <<"       \\item $\\strut^1$ disabled for stability reasons."<<std::endl
00216     <<"\\end{tablenotes}"<<std::endl
00217     <<"\\end{threeparttable} "<<std::endl;
00218   }
00219 
00221   void 
00222   RegisteredOptions::chooseOptions(ExtraCategoriesInfo which,
00223                                             std::list< Ipopt::RegisteredOption * >& sortedOptions)
00224   {
00225   std::map<std::string, Ipopt::SmartPtr<Ipopt::RegisteredOption> > 
00226            registered_options = RegisteredOptionsList();
00227 
00228   for(std::map<std::string, Ipopt::SmartPtr<Ipopt::RegisteredOption > >::iterator i = 
00229           registered_options.begin(); i != registered_options.end() ; i++){
00230      if(categoriesInfo(i->second->RegisteringCategory()) == which)
00231      sortedOptions.push_back(GetRawPtr(i->second));
00232      }
00233    sortedOptions.sort(optionsCmp());
00234   }
00236   void 
00237   RegisteredOptions::writeHtmlOptionsTable(std::ostream &of, ExtraCategoriesInfo which){
00238 
00239   //Print table header
00240   of<<"<table border=\"1\">"<<std::endl;
00241   //sort options by categories and alphabetical order
00242   std::list< Ipopt::RegisteredOption * > sortedOptions;
00243   chooseOptions(which, sortedOptions); 
00244   writeHtmlOptionsTable(of, sortedOptions);
00245   }
00246 
00247    
00249    void 
00250    RegisteredOptions::writeHtmlOptionsTable(std::ostream &os, std::list<Ipopt::RegisteredOption *> &options)
00251    {
00252    os<<"<table border=\"1\">"<<std::endl;
00253    os<<"<tr>"<<std::endl;
00254    os<<"<td>Option </td>"<<std::endl;
00255    os<<"<td> type </td>"<<std::endl;
00256    os<<"<td> default </td>"<<std::endl;
00257    os<<"<td> B-BB</td>"<<std::endl;
00258    os<<"<td> B-OA</td>"<<std::endl;
00259    os<<"<td> B-QG</td>"<<std::endl;
00260    os<<"<td> B-Hyb</td>"<<std::endl;
00261    os<<"</tr>"<<std::endl;
00262    std::string registeringCategory = "";
00263    for(std::list< Ipopt::RegisteredOption * >::iterator i = options.begin();
00264        i != options.end() ; i++)
00265    {
00266      if((*i)->RegisteringCategory() != registeringCategory){
00267      registeringCategory = (*i)->RegisteringCategory();
00268      os<<"<tr>"
00269        <<"   <th colspan=7>"
00270        <<" <a href=\"#sec:"<<makeSpaceLess(registeringCategory)<<"\">"
00271        <<registeringCategory<<"</a> </th>"<<std::endl
00272        <<"</tr>"<<std::endl;
00273      }
00274      
00275      os<<"<tr>"<<std::endl
00276        <<"<td> <a href=\"#sec:"<<makeSpaceLess((*i)->Name())<<"\">"
00277        <<((*i)->Name())<<"</a> </td>"<<std::endl
00278        <<"<td>"<<OptionType2Char((*i)->Type())<<"</td>"<<std::endl
00279        <<"<td>"<<defaultAsString(*i)<<"</td>"<<std::endl
00280        <<"<td> "<<( (isValidForBBB((*i)->Name()))? '+' : '-' )<<"</td>"<<std::endl
00281        <<"<td>"<<( (isValidForBOA((*i)->Name()))? '+' : '-' )<<"</td>"<<std::endl
00282        <<"<td>"<<( (isValidForBQG((*i)->Name()))? '+' : '-' )<<"</td>"<<std::endl
00283        <<"<td>"<<( (isValidForHybrid((*i)->Name()))? '+' : '-' )<<"</td>"<<std::endl
00284        <<"</tr>"<<std::endl;
00285    }
00286    //Print table end
00287   os<<"</tr>"<<std::endl
00288     <<"</table>"<<std::endl;
00289   }
00290 
00292    void 
00293    RegisteredOptions::writeLatexHtmlDoc(std::ostream &os, ExtraCategoriesInfo which){
00294       std::list< Ipopt::RegisteredOption * > options;
00295       chooseOptions(which, options);
00296       os<<"\\latexhtml{}{"<<std::endl;
00297       os<<"\\begin{rawhtml}"<<std::endl;
00298       writeHtmlOptionsTable(os, options);
00299       os<<"\\end{rawhtml}\n}"<<std::endl;
00300 
00301       //Create journalist to write to os
00302       Ipopt::Journalist jnlst;
00303       Ipopt::SmartPtr<Ipopt::StreamJournal> J = new Ipopt::StreamJournal("options_journal", Ipopt::J_ALL);
00304       J->SetOutputStream(&os);
00305       J->SetPrintLevel(Ipopt::J_DOCUMENTATION, Ipopt::J_SUMMARY);
00306       jnlst.AddJournal(GetRawPtr(J));
00307 
00308       std::string registeringCategory = "";
00309       for(std::list< Ipopt::RegisteredOption * >::iterator i = options.begin();
00310            i != options.end() ; i++)
00311        {
00312           if((*i)->RegisteringCategory() != registeringCategory){
00313            registeringCategory = (*i)->RegisteringCategory();
00314              os<<"\\subsection{"<<registeringCategory<<"}"<<std::endl;      
00315              os<<"\\label{sec:"<<makeSpaceLess(registeringCategory)<<"}"<<std::endl;
00316            }
00317        
00318            (*i)->OutputLatexDescription(jnlst);
00319        }
00320     }
00321 
00323   void
00324   RegisteredOptions::writeBonminOpt(std::ostream &os, ExtraCategoriesInfo which){
00325       std::list< Ipopt::RegisteredOption * > options;
00326       chooseOptions(which, options);
00327 
00328       //Create journalist to write to os
00329       Ipopt::Journalist jnlst;
00330       Ipopt::SmartPtr<Ipopt::StreamJournal> J = new Ipopt::StreamJournal("options_journal", Ipopt::J_ALL);
00331       J->SetOutputStream(&os);
00332       J->SetPrintLevel(Ipopt::J_DOCUMENTATION, Ipopt::J_SUMMARY);
00333       jnlst.AddJournal(GetRawPtr(J));
00334 
00335       std::string registeringCategory = "";
00336       for(std::list< Ipopt::RegisteredOption * >::iterator i = options.begin();
00337            i != options.end() ; i++)
00338        {
00339           if((*i)->RegisteringCategory() != registeringCategory){
00340            registeringCategory = (*i)->RegisteringCategory();
00341              os<<std::endl<<"# registering category: "<<registeringCategory<<std::endl<<std::endl;
00342            }
00343            os<<"bonmin.";
00344            os.setf(std::ios::left);
00345            os.width(37);
00346            os<<(*i)->Name();
00347            os.width(10);
00348            os<<makeNumber(defaultAsString(*i))<<"\t#";
00349            os<<(*i)->ShortDescription();
00350            os<<std::endl;            
00351        }
00352     }
00353 
00354 }/*Ends bonmin namespace.*/

Generated on Thu Oct 8 03:02:55 2009 by  doxygen 1.4.7