source: stable/2.4/Cbc/src/CbcParam.cpp @ 1271

Last change on this file since 1271 was 1271, checked in by forrest, 10 years ago

Creating new stable branch 2.4 from trunk (rev 1270)

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