source: trunk/Test/CbcParam.cpp @ 2

Last change on this file since 2 was 2, checked in by ladanyi, 16 years ago

Import of Coin Branch-and-Cut (formerly known as Sbb)

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