Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

SbbParam.cpp

00001 // Copyright (C) 2002, International Business Machines
00002 // Corporation and others.  All Rights Reserved.
00003 #if defined(_MSC_VER)
00004 // Turn off compiler warning about long names
00005 #  pragma warning(disable:4786)
00006 #endif
00007 
00008 #include <string>
00009 #include <iostream>
00010 #include <cassert>
00011 
00012 #include "SbbParam.hpp"
00013 
00014 //#############################################################################
00015 // Constructors / Destructor / Assignment
00016 //#############################################################################
00017 
00018 //-------------------------------------------------------------------
00019 // Default Constructor 
00020 //-------------------------------------------------------------------
00021 SbbParam::SbbParam () 
00022   : type_(INVALID),
00023     lowerDoubleValue_(0.0),
00024     upperDoubleValue_(0.0),
00025     lowerIntValue_(0),
00026     upperIntValue_(0),
00027     lengthName_(0),
00028     lengthMatch_(0),
00029     definedKeyWords_(),
00030     name_(),
00031     shortHelp_(),
00032     longHelp_(),
00033     action_(INVALID),
00034     currentKeyWord_(-1)
00035 {
00036 }
00037 // Other constructors
00038 SbbParam::SbbParam (std::string name, std::string help,
00039            double lower, double upper, SbbParameterType type)
00040   : type_(type),
00041     lowerIntValue_(0),
00042     upperIntValue_(0),
00043     definedKeyWords_(),
00044     name_(name),
00045     shortHelp_(help),
00046     longHelp_(),
00047     action_(type),
00048     currentKeyWord_(-1)
00049 {
00050   lowerDoubleValue_ = lower;
00051   upperDoubleValue_ = upper;
00052   gutsOfConstructor();
00053 }
00054 SbbParam::SbbParam (std::string name, std::string help,
00055            int lower, int upper, SbbParameterType type)
00056   : type_(type),
00057     lowerDoubleValue_(0.0),
00058     upperDoubleValue_(0.0),
00059     definedKeyWords_(),
00060     name_(name),
00061     shortHelp_(help),
00062     longHelp_(),
00063     action_(type),
00064     currentKeyWord_(-1)
00065 {
00066   gutsOfConstructor();
00067   lowerIntValue_ = lower;
00068   upperIntValue_ = upper;
00069 }
00070 // Other strings will be added by append
00071 SbbParam::SbbParam (std::string name, std::string help, 
00072                   std::string defaultValue,
00073                   SbbParameterType type)
00074   : type_(type),
00075     lowerDoubleValue_(0.0),
00076     upperDoubleValue_(0.0),
00077     lowerIntValue_(0),
00078     upperIntValue_(0),
00079     definedKeyWords_(),
00080     name_(name),
00081     shortHelp_(help),
00082     longHelp_(),
00083     action_(type),
00084     currentKeyWord_(0)
00085 {
00086   gutsOfConstructor();
00087   definedKeyWords_.push_back(defaultValue);
00088 }
00089 // Action
00090 SbbParam::SbbParam (std::string name, std::string help,
00091            SbbParameterType type)
00092   : type_(type),
00093     lowerDoubleValue_(0.0),
00094     upperDoubleValue_(0.0),
00095     lowerIntValue_(0),
00096     upperIntValue_(0),
00097     definedKeyWords_(),
00098     name_(name),
00099     shortHelp_(help),
00100     longHelp_(),
00101     action_(type),
00102     currentKeyWord_(-1)
00103 {
00104   gutsOfConstructor();
00105 }
00106 
00107 //-------------------------------------------------------------------
00108 // Copy constructor 
00109 //-------------------------------------------------------------------
00110 SbbParam::SbbParam (const SbbParam & rhs) 
00111 {  
00112   type_ = rhs.type_;
00113   lowerDoubleValue_ = rhs.lowerDoubleValue_;
00114   upperDoubleValue_ = rhs.upperDoubleValue_;
00115   lowerIntValue_ = rhs.lowerIntValue_;
00116   upperIntValue_ = rhs.upperIntValue_;
00117   lengthName_ = rhs.lengthName_;
00118   lengthMatch_ = rhs.lengthMatch_;
00119   definedKeyWords_ = rhs.definedKeyWords_;
00120   name_ = rhs.name_;
00121   shortHelp_ = rhs.shortHelp_;
00122   longHelp_ = rhs.longHelp_;
00123   action_ = rhs.action_;
00124   currentKeyWord_ = rhs.currentKeyWord_;
00125   
00126 }
00127 
00128 //-------------------------------------------------------------------
00129 // Destructor 
00130 //-------------------------------------------------------------------
00131 SbbParam::~SbbParam ()
00132 {
00133 }
00134 
00135 //----------------------------------------------------------------
00136 // Assignment operator 
00137 //-------------------------------------------------------------------
00138 SbbParam &
00139 SbbParam::operator=(const SbbParam& rhs)
00140 {
00141   if (this != &rhs) {
00142     type_ = rhs.type_;
00143     lowerDoubleValue_ = rhs.lowerDoubleValue_;
00144     upperDoubleValue_ = rhs.upperDoubleValue_;
00145     lowerIntValue_ = rhs.lowerIntValue_;
00146     upperIntValue_ = rhs.upperIntValue_;
00147     lengthName_ = rhs.lengthName_;
00148     lengthMatch_ = rhs.lengthMatch_;
00149     definedKeyWords_ = rhs.definedKeyWords_;
00150     name_ = rhs.name_;
00151     shortHelp_ = rhs.shortHelp_;
00152     longHelp_ = rhs.longHelp_;
00153     action_ = rhs.action_;
00154     currentKeyWord_ = rhs.currentKeyWord_;
00155   }
00156   return *this;
00157 }
00158 void 
00159 SbbParam::gutsOfConstructor()
00160 {
00161   unsigned int  shriekPos = name_.find('!');
00162   lengthName_ = name_.length();
00163   if ( shriekPos==std::string::npos ) {
00164     //does not contain '!'
00165     lengthMatch_= lengthName_;
00166   } else {
00167     lengthMatch_=shriekPos;
00168     name_ = name_.substr(0,shriekPos)+name_.substr(shriekPos+1);
00169     lengthName_--;
00170   }
00171 }
00172 // Insert string (only valid for keywords)
00173 void 
00174 SbbParam::append(std::string keyWord)
00175 {
00176   definedKeyWords_.push_back(keyWord);
00177 }
00178 
00179 int 
00180 SbbParam::matches (std::string input) const
00181 {
00182   // look up strings to do more elegantly
00183   if (input.length()>lengthName_) {
00184     return 0;
00185   } else {
00186     unsigned int i;
00187     for (i=0;i<input.length();i++) {
00188       if (tolower(name_[i])!=tolower(input[i])) 
00189         break;
00190     }
00191     if (i<input.length()) {
00192       return 0;
00193     } else if (i>=lengthMatch_) {
00194       return 1;
00195     } else {
00196       // matched but too short
00197       return 2;
00198     }
00199   }
00200 }
00201 // Returns name which could match
00202 std::string 
00203 SbbParam::matchName (  ) const
00204 { 
00205   if (lengthMatch_==lengthName_) 
00206     return name_;
00207   else
00208     return name_.substr(0,lengthMatch_)+"("+name_.substr(lengthMatch_)+")";
00209 }
00210 
00211 // Returns parameter option which matches (-1 if none)
00212 int 
00213 SbbParam::parameterOption ( std::string check ) const
00214 {
00215   int numberItems = definedKeyWords_.size();
00216   if (!numberItems) {
00217     return -1;
00218   } else {
00219     int whichItem=0;
00220     unsigned int it;
00221     for (it=0;it<definedKeyWords_.size();it++) {
00222       std::string thisOne = definedKeyWords_[it];
00223       unsigned int  shriekPos = thisOne.find('!');
00224       unsigned int length1 = thisOne.length();
00225       unsigned int length2 = length1;
00226       if ( shriekPos!=std::string::npos ) {
00227         //contains '!'
00228         length2 = shriekPos;
00229         thisOne = thisOne.substr(0,shriekPos)+
00230           thisOne.substr(shriekPos+1);
00231         length1 = thisOne.length();
00232       }
00233       if (check.length()<=length1) {
00234         unsigned int i;
00235         for (i=0;i<check.length();i++) {
00236           if (tolower(thisOne[i])!=tolower(check[i])) 
00237             break;
00238         }
00239         if (i<check.length()) {
00240           whichItem++;
00241         } else if (i>=length2) {
00242           break;
00243         } 
00244       } else {
00245         whichItem++;
00246       }
00247     }
00248     if (whichItem<numberItems)
00249       return whichItem;
00250     else
00251       return -1;
00252   }
00253 }
00254 // Prints parameter options
00255 void 
00256 SbbParam::printOptions (  ) const
00257 {
00258   std::cout<<"Possible options for "<<name_<<" are:"<<std::endl;
00259   unsigned int it;
00260   for (it=0;it<definedKeyWords_.size();it++) {
00261     std::string thisOne = definedKeyWords_[it];
00262     unsigned int  shriekPos = thisOne.find('!');
00263     if ( shriekPos!=std::string::npos ) {
00264       //contains '!'
00265       thisOne = thisOne.substr(0,shriekPos)+
00266         "("+thisOne.substr(shriekPos+1)+")";
00267     }
00268     std::cout<<thisOne<<std::endl;
00269   }
00270 }
00271 int
00272 SbbParam::setDoubleParameter (OsiSolverInterface * model,double value) const
00273 {
00274   if (value<lowerDoubleValue_||value>upperDoubleValue_) {
00275     std::cout<<value<<" was provided for "<<name_<<
00276       " - valid range is "<<lowerDoubleValue_<<" to "<<
00277       upperDoubleValue_<<std::endl;
00278     return 1;
00279   } else {
00280     double oldValue;
00281     switch(type_) {
00282     case DUALTOLERANCE:
00283       model->getDblParam(OsiDualTolerance,oldValue);
00284       model->setDblParam(OsiDualTolerance,value);
00285       break;
00286     case PRIMALTOLERANCE:
00287       model->getDblParam(OsiPrimalTolerance,oldValue);
00288       model->setDblParam(OsiPrimalTolerance,value);
00289       break;
00290     default:
00291       oldValue=0.0; // to avoid compiler message
00292       abort();
00293     }
00294     std::cout<<name_<<" was changed from "<<oldValue<<" to "
00295              <<value<<std::endl;
00296     return 0;
00297   }
00298 }
00299 int
00300 SbbParam::checkDoubleParameter (double value) const
00301 {
00302   if (value<lowerDoubleValue_||value>upperDoubleValue_) {
00303     std::cout<<value<<" was provided for "<<name_<<
00304       " - valid range is "<<lowerDoubleValue_<<" to "<<
00305       upperDoubleValue_<<std::endl;
00306     return 1;
00307   } else {
00308     return 0;
00309   }
00310 }
00311 double 
00312 SbbParam::doubleParameter (OsiSolverInterface * model) const
00313 {
00314   double value;
00315   switch(type_) {
00316   case DUALTOLERANCE:
00317     assert(model->getDblParam(OsiDualTolerance,value));
00318     break;
00319   case PRIMALTOLERANCE:
00320     assert(model->getDblParam(OsiPrimalTolerance,value));
00321     break;
00322   default:
00323     abort();
00324   }
00325   return value;
00326 }
00327 int 
00328 SbbParam::setIntParameter (OsiSolverInterface * model,int value) const
00329 {
00330   if (value<lowerIntValue_||value>upperIntValue_) {
00331     std::cout<<value<<" was provided for "<<name_<<
00332       " - valid range is "<<lowerIntValue_<<" to "<<
00333       upperIntValue_<<std::endl;
00334     return 1;
00335   } else {
00336     int oldValue;
00337     switch(type_) {
00338     case LOGLEVEL:
00339       model->messageHandler()->setLogLevel(value);
00340       break;
00341     default:
00342       oldValue=0; // to avoid compiler message
00343       abort();
00344     }
00345     std::cout<<name_<<" was changed from "<<oldValue<<" to "
00346              <<value<<std::endl;
00347     return 0;
00348   }
00349 }
00350 int 
00351 SbbParam::intParameter (OsiSolverInterface * model) const
00352 {
00353   int value=0;
00354   switch(type_) {
00355   case LOGLEVEL:
00356     //value=model->logLevel();
00357     break;
00358   default:
00359     abort();
00360   }
00361   return value;
00362 }
00363 int
00364 SbbParam::setDoubleParameter (SbbModel &model,double value) const
00365 {
00366   if (value<lowerDoubleValue_||value>upperDoubleValue_) {
00367     std::cout<<value<<" was provided for "<<name_<<
00368       " - valid range is "<<lowerDoubleValue_<<" to "<<
00369       upperDoubleValue_<<std::endl;
00370     return 1;
00371   } else {
00372     double oldValue;
00373     switch(type_) {
00374     case INFEASIBILITYWEIGHT:
00375       oldValue=model.getDblParam(SbbModel::SbbInfeasibilityWeight);
00376       model.setDblParam(SbbModel::SbbInfeasibilityWeight,value);
00377       break;
00378     case INTEGERTOLERANCE:
00379       oldValue=model.getDblParam(SbbModel::SbbIntegerTolerance);
00380       model.setDblParam(SbbModel::SbbIntegerTolerance,value);
00381       break;
00382     case INCREMENT:
00383       oldValue=model.getDblParam(SbbModel::SbbCutoffIncrement);
00384       model.setDblParam(SbbModel::SbbCutoffIncrement,value);
00385     case ALLOWABLEGAP:
00386       oldValue=model.getDblParam(SbbModel::SbbAllowableGap);
00387       model.setDblParam(SbbModel::SbbAllowableGap,value);
00388       break;
00389     case TIMELIMIT:
00390     { oldValue = model.getDblParam(SbbModel::SbbMaximumSeconds) ;
00391       model.setDblParam(SbbModel::SbbMaximumSeconds,value) ;
00392       break ; }
00393     default:
00394       oldValue=0.0; // to avoid compiler message
00395       abort();
00396     }
00397     std::cout<<name_<<" was changed from "<<oldValue<<" to "
00398              <<value<<std::endl;
00399     return 0;
00400   }
00401 }
00402 double 
00403 SbbParam::doubleParameter (SbbModel &model) const
00404 {
00405   double value;
00406   switch(type_) {
00407   case INFEASIBILITYWEIGHT:
00408     value=model.getDblParam(SbbModel::SbbInfeasibilityWeight);
00409     break;
00410   case INTEGERTOLERANCE:
00411     value=model.getDblParam(SbbModel::SbbIntegerTolerance);
00412     break;
00413   case INCREMENT:
00414     value=model.getDblParam(SbbModel::SbbCutoffIncrement);
00415   case ALLOWABLEGAP:
00416     value=model.getDblParam(SbbModel::SbbAllowableGap);
00417     break;
00418   case TIMELIMIT:
00419   { value = model.getDblParam(SbbModel::SbbMaximumSeconds) ;
00420     break ; }
00421   default:
00422     abort();
00423   }
00424   return value;
00425 }
00426 int 
00427 SbbParam::setIntParameter (SbbModel &model,int value) const
00428 {
00429   if (value<lowerIntValue_||value>upperIntValue_) {
00430     std::cout<<value<<" was provided for "<<name_<<
00431       " - valid range is "<<lowerIntValue_<<" to "<<
00432       upperIntValue_<<std::endl;
00433     return 1;
00434   } else {
00435     int oldValue;
00436     switch(type_) {
00437     case LOGLEVEL:
00438       oldValue = model.messageHandler()->logLevel();
00439       model.messageHandler()->setLogLevel(value);
00440       break;
00441     case SOLVERLOGLEVEL:
00442       oldValue = model.solver()->messageHandler()->logLevel();
00443       model.solver()->messageHandler()->setLogLevel(value);
00444       break;
00445     case MAXNODES:
00446       oldValue=model.getIntParam(SbbModel::SbbMaxNumNode);
00447       model.setIntParam(SbbModel::SbbMaxNumNode,value);
00448       break;
00449     case STRONGBRANCHING:
00450       oldValue=model.numberStrong();
00451       model.setNumberStrong(value);
00452       break;
00453     default:
00454       oldValue=0; // to avoid compiler message
00455       abort();
00456     }
00457     std::cout<<name_<<" was changed from "<<oldValue<<" to "
00458              <<value<<std::endl;
00459     return 0;
00460   }
00461 }
00462 int 
00463 SbbParam::intParameter (SbbModel &model) const
00464 {
00465   int value;
00466   switch(type_) {
00467   case LOGLEVEL:
00468     value = model.messageHandler()->logLevel();
00469       break;
00470   case SOLVERLOGLEVEL:
00471     value = model.solver()->messageHandler()->logLevel();
00472       break;
00473   case MAXNODES:
00474     value = model.getIntParam(SbbModel::SbbMaxNumNode);
00475     break;
00476   case STRONGBRANCHING:
00477     value=model.numberStrong();
00478     break;
00479   default:
00480     abort();
00481   }
00482   return value;
00483 }

Generated on Wed Dec 3 14:36:21 2003 for Sbb by doxygen 1.3.5