source: branches/devel/Cbc/src/CbcParam.cpp @ 606

Last change on this file since 606 was 375, checked in by jpfasano, 13 years ago

Changed doubleParameter method. Code which must be executed was contained in an assert statement. When compiling with debug, some compilers will not generate this code. The fix was to move the code outside of the assert.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.8 KB
Line 
1// Copyright (C) 2002, International Business Machines
2// Corporation and others.  All Rights Reserved.
3#if defined(_MSC_VER)
4// Turn off compiler warning about long names
5#  pragma warning(disable:4786)
6#endif
7
8#include <string>
9#include <iostream>
10#include <cassert>
11
12#include "CbcParam.hpp"
13
14//#############################################################################
15// Constructors / Destructor / Assignment
16//#############################################################################
17
18//-------------------------------------------------------------------
19// Default Constructor
20//-------------------------------------------------------------------
21CbcParam::CbcParam () 
22  : type_(INVALID),
23    lowerDoubleValue_(0.0),
24    upperDoubleValue_(0.0),
25    lowerIntValue_(0),
26    upperIntValue_(0),
27    lengthName_(0),
28    lengthMatch_(0),
29    definedKeyWords_(),
30    name_(),
31    shortHelp_(),
32    longHelp_(),
33    action_(INVALID),
34    currentKeyWord_(-1)
35{
36}
37// Other constructors
38CbcParam::CbcParam (std::string name, std::string help,
39           double lower, double upper, CbcParameterType type,
40                    bool display)
41  : type_(type),
42    lowerIntValue_(0),
43    upperIntValue_(0),
44    definedKeyWords_(),
45    name_(name),
46    shortHelp_(help),
47    longHelp_(),
48    action_(type),
49    currentKeyWord_(-1)
50{
51  lowerDoubleValue_ = lower;
52  upperDoubleValue_ = upper;
53  gutsOfConstructor();
54}
55CbcParam::CbcParam (std::string name, std::string help,
56           int lower, int upper, CbcParameterType type,
57                    bool display)
58  : type_(type),
59    lowerDoubleValue_(0.0),
60    upperDoubleValue_(0.0),
61    definedKeyWords_(),
62    name_(name),
63    shortHelp_(help),
64    longHelp_(),
65    action_(type),
66    currentKeyWord_(-1)
67{
68  gutsOfConstructor();
69  lowerIntValue_ = lower;
70  upperIntValue_ = upper;
71}
72// Other strings will be added by append
73CbcParam::CbcParam (std::string name, std::string help, 
74                    std::string firstValue,
75                    CbcParameterType type,int defaultIndex,
76                    bool display)
77  : type_(type),
78    lowerDoubleValue_(0.0),
79    upperDoubleValue_(0.0),
80    lowerIntValue_(0),
81    upperIntValue_(0),
82    definedKeyWords_(),
83    name_(name),
84    shortHelp_(help),
85    longHelp_(),
86    action_(type),
87    currentKeyWord_(0)
88{
89  gutsOfConstructor();
90  definedKeyWords_.push_back(firstValue);
91}
92// Action
93CbcParam::CbcParam (std::string name, std::string help,
94                    CbcParameterType type,int indexNumber,
95                    bool display)
96  : type_(type),
97    lowerDoubleValue_(0.0),
98    upperDoubleValue_(0.0),
99    lowerIntValue_(0),
100    upperIntValue_(0),
101    definedKeyWords_(),
102    name_(name),
103    shortHelp_(help),
104    longHelp_(),
105    action_(type),
106    currentKeyWord_(-1)
107{
108  gutsOfConstructor();
109}
110
111//-------------------------------------------------------------------
112// Copy constructor
113//-------------------------------------------------------------------
114CbcParam::CbcParam (const CbcParam & rhs) 
115{ 
116  type_ = rhs.type_;
117  lowerDoubleValue_ = rhs.lowerDoubleValue_;
118  upperDoubleValue_ = rhs.upperDoubleValue_;
119  lowerIntValue_ = rhs.lowerIntValue_;
120  upperIntValue_ = rhs.upperIntValue_;
121  lengthName_ = rhs.lengthName_;
122  lengthMatch_ = rhs.lengthMatch_;
123  definedKeyWords_ = rhs.definedKeyWords_;
124  name_ = rhs.name_;
125  shortHelp_ = rhs.shortHelp_;
126  longHelp_ = rhs.longHelp_;
127  action_ = rhs.action_;
128  currentKeyWord_ = rhs.currentKeyWord_;
129 
130}
131
132//-------------------------------------------------------------------
133// Destructor
134//-------------------------------------------------------------------
135CbcParam::~CbcParam ()
136{
137}
138
139//----------------------------------------------------------------
140// Assignment operator
141//-------------------------------------------------------------------
142CbcParam &
143CbcParam::operator=(const CbcParam& rhs)
144{
145  if (this != &rhs) {
146    type_ = rhs.type_;
147    lowerDoubleValue_ = rhs.lowerDoubleValue_;
148    upperDoubleValue_ = rhs.upperDoubleValue_;
149    lowerIntValue_ = rhs.lowerIntValue_;
150    upperIntValue_ = rhs.upperIntValue_;
151    lengthName_ = rhs.lengthName_;
152    lengthMatch_ = rhs.lengthMatch_;
153    definedKeyWords_ = rhs.definedKeyWords_;
154    name_ = rhs.name_;
155    shortHelp_ = rhs.shortHelp_;
156    longHelp_ = rhs.longHelp_;
157    action_ = rhs.action_;
158    currentKeyWord_ = rhs.currentKeyWord_;
159  }
160  return *this;
161}
162void 
163CbcParam::gutsOfConstructor()
164{
165  std::string::size_type  shriekPos = name_.find('!');
166  lengthName_ = name_.length();
167  if ( shriekPos==std::string::npos ) {
168    //does not contain '!'
169    lengthMatch_= lengthName_;
170  } else {
171    lengthMatch_=shriekPos;
172    name_ = name_.substr(0,shriekPos)+name_.substr(shriekPos+1);
173    lengthName_--;
174  }
175}
176// Insert string (only valid for keywords)
177void 
178CbcParam::append(std::string keyWord)
179{
180  definedKeyWords_.push_back(keyWord);
181}
182
183int 
184CbcParam::matches (std::string input) const
185{
186  // look up strings to do more elegantly
187  if (input.length()>lengthName_) {
188    return 0;
189  } else {
190    unsigned int i;
191    for (i=0;i<input.length();i++) {
192      if (tolower(name_[i])!=tolower(input[i])) 
193        break;
194    }
195    if (i<input.length()) {
196      return 0;
197    } else if (i>=lengthMatch_) {
198      return 1;
199    } else {
200      // matched but too short
201      return 2;
202    }
203  }
204}
205// Returns name which could match
206std::string
207CbcParam::matchName (  ) const
208{ 
209  if (lengthMatch_==lengthName_) 
210    return name_;
211  else
212    return name_.substr(0,lengthMatch_)+"("+name_.substr(lengthMatch_)+")";
213}
214
215// Returns parameter option which matches (-1 if none)
216int 
217CbcParam::parameterOption ( std::string check ) const
218{
219  int numberItems = definedKeyWords_.size();
220  if (!numberItems) {
221    return -1;
222  } else {
223    int whichItem=0;
224    unsigned int it;
225    for (it=0;it<definedKeyWords_.size();it++) {
226      std::string thisOne = definedKeyWords_[it];
227      std::string::size_type  shriekPos = thisOne.find('!');
228      unsigned int length1 = thisOne.length();
229      unsigned int length2 = length1;
230      if ( shriekPos!=std::string::npos ) {
231        //contains '!'
232        length2 = shriekPos;
233        thisOne = thisOne.substr(0,shriekPos)+
234          thisOne.substr(shriekPos+1);
235        length1 = thisOne.length();
236      }
237      if (check.length()<=length1) {
238        unsigned int i;
239        for (i=0;i<check.length();i++) {
240          if (tolower(thisOne[i])!=tolower(check[i])) 
241            break;
242        }
243        if (i<check.length()) {
244          whichItem++;
245        } else if (i>=length2) {
246          break;
247        } 
248      } else {
249        whichItem++;
250      }
251    }
252    if (whichItem<numberItems)
253      return whichItem;
254    else
255      return -1;
256  }
257}
258// Prints parameter options
259void 
260CbcParam::printOptions (  ) const
261{
262  std::cout<<"Possible options for "<<name_<<" are:"<<std::endl;
263  unsigned int it;
264  for (it=0;it<definedKeyWords_.size();it++) {
265    std::string thisOne = definedKeyWords_[it];
266    std::string::size_type  shriekPos = thisOne.find('!');
267    if ( shriekPos!=std::string::npos ) {
268      //contains '!'
269      thisOne = thisOne.substr(0,shriekPos)+
270        "("+thisOne.substr(shriekPos+1)+")";
271    }
272    std::cout<<thisOne<<std::endl;
273  }
274}
275int
276CbcParam::setDoubleParameter (OsiSolverInterface * model,double value) const
277{
278  if (value<lowerDoubleValue_||value>upperDoubleValue_) {
279    std::cout<<value<<" was provided for "<<name_<<
280      " - valid range is "<<lowerDoubleValue_<<" to "<<
281      upperDoubleValue_<<std::endl;
282    return 1;
283  } else {
284    double oldValue;
285    switch(type_) {
286    case DUALTOLERANCE:
287      model->getDblParam(OsiDualTolerance,oldValue);
288      model->setDblParam(OsiDualTolerance,value);
289      break;
290    case PRIMALTOLERANCE:
291      model->getDblParam(OsiPrimalTolerance,oldValue);
292      model->setDblParam(OsiPrimalTolerance,value);
293      break;
294    default:
295      oldValue=0.0; // to avoid compiler message
296      abort();
297    }
298    std::cout<<name_<<" was changed from "<<oldValue<<" to "
299             <<value<<std::endl;
300    return 0;
301  }
302}
303int
304CbcParam::checkDoubleParameter (double value) const
305{
306  if (value<lowerDoubleValue_||value>upperDoubleValue_) {
307    std::cout<<value<<" was provided for "<<name_<<
308      " - valid range is "<<lowerDoubleValue_<<" to "<<
309      upperDoubleValue_<<std::endl;
310    return 1;
311  } else {
312    return 0;
313  }
314}
315double 
316CbcParam::doubleParameter (OsiSolverInterface * model) const
317{
318  double value;
319  bool getDblParamRetValue;
320  switch(type_) {
321  case DUALTOLERANCE:
322    getDblParamRetValue = model->getDblParam(OsiDualTolerance,value);
323    assert(getDblParamRetValue);
324    break;
325  case PRIMALTOLERANCE:
326    getDblParamRetValue = model->getDblParam(OsiPrimalTolerance,value);
327    assert(getDblParamRetValue);
328    break;
329  default:
330    abort();
331  }
332  return value;
333}
334int 
335CbcParam::setIntParameter (OsiSolverInterface * model,int value) const
336{
337  if (value<lowerIntValue_||value>upperIntValue_) {
338    std::cout<<value<<" was provided for "<<name_<<
339      " - valid range is "<<lowerIntValue_<<" to "<<
340      upperIntValue_<<std::endl;
341    return 1;
342  } else {
343    int oldValue;
344    switch(type_) {
345    case LOGLEVEL:
346      oldValue=model->messageHandler()->logLevel();
347      model->messageHandler()->setLogLevel(value);
348      break;
349    default:
350      oldValue=0; // to avoid compiler message
351      abort();
352    }
353    std::cout<<name_<<" was changed from "<<oldValue<<" to "
354             <<value<<std::endl;
355    return 0;
356  }
357}
358int 
359CbcParam::intParameter (OsiSolverInterface * model) const
360{
361  int value=0;
362  switch(type_) {
363  case LOGLEVEL:
364    //value=model->logLevel();
365    break;
366  default:
367    abort();
368  }
369  return value;
370}
371int
372CbcParam::setDoubleParameter (CbcModel &model,double value) const
373{
374  if (value<lowerDoubleValue_||value>upperDoubleValue_) {
375    std::cout<<value<<" was provided for "<<name_<<
376      " - valid range is "<<lowerDoubleValue_<<" to "<<
377      upperDoubleValue_<<std::endl;
378    return 1;
379  } else {
380    double oldValue;
381    switch(type_) {
382    case INFEASIBILITYWEIGHT:
383      oldValue=model.getDblParam(CbcModel::CbcInfeasibilityWeight);
384      model.setDblParam(CbcModel::CbcInfeasibilityWeight,value);
385      break;
386    case INTEGERTOLERANCE:
387      oldValue=model.getDblParam(CbcModel::CbcIntegerTolerance);
388      model.setDblParam(CbcModel::CbcIntegerTolerance,value);
389      break;
390    case INCREMENT:
391      oldValue=model.getDblParam(CbcModel::CbcCutoffIncrement);
392      model.setDblParam(CbcModel::CbcCutoffIncrement,value);
393    case ALLOWABLEGAP:
394      oldValue=model.getDblParam(CbcModel::CbcAllowableGap);
395      model.setDblParam(CbcModel::CbcAllowableGap,value);
396      break;
397    case TIMELIMIT:
398    { oldValue = model.getDblParam(CbcModel::CbcMaximumSeconds) ;
399      model.setDblParam(CbcModel::CbcMaximumSeconds,value) ;
400      break ; }
401    case DUALTOLERANCE:
402    case PRIMALTOLERANCE:
403      setDoubleParameter(model.solver(),value);
404      return 0; // to avoid message
405    default:
406      oldValue=0.0; // to avoid compiler message
407      abort();
408    }
409    std::cout<<name_<<" was changed from "<<oldValue<<" to "
410             <<value<<std::endl;
411    return 0;
412  }
413}
414double 
415CbcParam::doubleParameter (CbcModel &model) const
416{
417  double value;
418  switch(type_) {
419  case INFEASIBILITYWEIGHT:
420    value=model.getDblParam(CbcModel::CbcInfeasibilityWeight);
421    break;
422  case INTEGERTOLERANCE:
423    value=model.getDblParam(CbcModel::CbcIntegerTolerance);
424    break;
425  case INCREMENT:
426    value=model.getDblParam(CbcModel::CbcCutoffIncrement);
427  case ALLOWABLEGAP:
428    value=model.getDblParam(CbcModel::CbcAllowableGap);
429    break;
430  case TIMELIMIT:
431  { value = model.getDblParam(CbcModel::CbcMaximumSeconds) ;
432    break ; }
433  case DUALTOLERANCE:
434  case PRIMALTOLERANCE:
435    value=doubleParameter(model.solver());
436    break;
437  default:
438    abort();
439  }
440  return value;
441}
442int 
443CbcParam::setIntParameter (CbcModel &model,int value) const
444{
445  if (value<lowerIntValue_||value>upperIntValue_) {
446    std::cout<<value<<" was provided for "<<name_<<
447      " - valid range is "<<lowerIntValue_<<" to "<<
448      upperIntValue_<<std::endl;
449    return 1;
450  } else {
451    int oldValue;
452    switch(type_) {
453    case LOGLEVEL:
454      oldValue = model.messageHandler()->logLevel();
455      model.messageHandler()->setLogLevel(value);
456      break;
457    case SOLVERLOGLEVEL:
458      oldValue = model.solver()->messageHandler()->logLevel();
459      model.solver()->messageHandler()->setLogLevel(value);
460      break;
461    case MAXNODES:
462      oldValue=model.getIntParam(CbcModel::CbcMaxNumNode);
463      model.setIntParam(CbcModel::CbcMaxNumNode,value);
464      break;
465    case STRONGBRANCHING:
466      oldValue=model.numberStrong();
467      model.setNumberStrong(value);
468      break;
469    default:
470      oldValue=0; // to avoid compiler message
471      abort();
472    }
473    std::cout<<name_<<" was changed from "<<oldValue<<" to "
474             <<value<<std::endl;
475    return 0;
476  }
477}
478int 
479CbcParam::intParameter (CbcModel &model) const
480{
481  int value;
482  switch(type_) {
483  case LOGLEVEL:
484    value = model.messageHandler()->logLevel();
485      break;
486  case SOLVERLOGLEVEL:
487    value = model.solver()->messageHandler()->logLevel();
488      break;
489  case MAXNODES:
490    value = model.getIntParam(CbcModel::CbcMaxNumNode);
491    break;
492  case STRONGBRANCHING:
493    value=model.numberStrong();
494    break;
495  default:
496    abort();
497  }
498  return value;
499}
Note: See TracBrowser for help on using the repository browser.