source: trunk/Test/CbcParam.cpp @ 122

Last change on this file since 122 was 122, checked in by forrest, 16 years ago

major changes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.6 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  switch(type_) {
320  case DUALTOLERANCE:
321    assert(model->getDblParam(OsiDualTolerance,value));
322    break;
323  case PRIMALTOLERANCE:
324    assert(model->getDblParam(OsiPrimalTolerance,value));
325    break;
326  default:
327    abort();
328  }
329  return value;
330}
331int 
332CbcParam::setIntParameter (OsiSolverInterface * model,int value) const
333{
334  if (value<lowerIntValue_||value>upperIntValue_) {
335    std::cout<<value<<" was provided for "<<name_<<
336      " - valid range is "<<lowerIntValue_<<" to "<<
337      upperIntValue_<<std::endl;
338    return 1;
339  } else {
340    int oldValue;
341    switch(type_) {
342    case LOGLEVEL:
343      model->messageHandler()->setLogLevel(value);
344      break;
345    default:
346      oldValue=0; // to avoid compiler message
347      abort();
348    }
349    std::cout<<name_<<" was changed from "<<oldValue<<" to "
350             <<value<<std::endl;
351    return 0;
352  }
353}
354int 
355CbcParam::intParameter (OsiSolverInterface * model) const
356{
357  int value=0;
358  switch(type_) {
359  case LOGLEVEL:
360    //value=model->logLevel();
361    break;
362  default:
363    abort();
364  }
365  return value;
366}
367int
368CbcParam::setDoubleParameter (CbcModel &model,double value) const
369{
370  if (value<lowerDoubleValue_||value>upperDoubleValue_) {
371    std::cout<<value<<" was provided for "<<name_<<
372      " - valid range is "<<lowerDoubleValue_<<" to "<<
373      upperDoubleValue_<<std::endl;
374    return 1;
375  } else {
376    double oldValue;
377    switch(type_) {
378    case INFEASIBILITYWEIGHT:
379      oldValue=model.getDblParam(CbcModel::CbcInfeasibilityWeight);
380      model.setDblParam(CbcModel::CbcInfeasibilityWeight,value);
381      break;
382    case INTEGERTOLERANCE:
383      oldValue=model.getDblParam(CbcModel::CbcIntegerTolerance);
384      model.setDblParam(CbcModel::CbcIntegerTolerance,value);
385      break;
386    case INCREMENT:
387      oldValue=model.getDblParam(CbcModel::CbcCutoffIncrement);
388      model.setDblParam(CbcModel::CbcCutoffIncrement,value);
389    case ALLOWABLEGAP:
390      oldValue=model.getDblParam(CbcModel::CbcAllowableGap);
391      model.setDblParam(CbcModel::CbcAllowableGap,value);
392      break;
393    case TIMELIMIT:
394    { oldValue = model.getDblParam(CbcModel::CbcMaximumSeconds) ;
395      model.setDblParam(CbcModel::CbcMaximumSeconds,value) ;
396      break ; }
397    case DUALTOLERANCE:
398    case PRIMALTOLERANCE:
399      setDoubleParameter(model.solver(),value);
400      return 0; // to avoid message
401    default:
402      oldValue=0.0; // to avoid compiler message
403      abort();
404    }
405    std::cout<<name_<<" was changed from "<<oldValue<<" to "
406             <<value<<std::endl;
407    return 0;
408  }
409}
410double 
411CbcParam::doubleParameter (CbcModel &model) const
412{
413  double value;
414  switch(type_) {
415  case INFEASIBILITYWEIGHT:
416    value=model.getDblParam(CbcModel::CbcInfeasibilityWeight);
417    break;
418  case INTEGERTOLERANCE:
419    value=model.getDblParam(CbcModel::CbcIntegerTolerance);
420    break;
421  case INCREMENT:
422    value=model.getDblParam(CbcModel::CbcCutoffIncrement);
423  case ALLOWABLEGAP:
424    value=model.getDblParam(CbcModel::CbcAllowableGap);
425    break;
426  case TIMELIMIT:
427  { value = model.getDblParam(CbcModel::CbcMaximumSeconds) ;
428    break ; }
429  case DUALTOLERANCE:
430  case PRIMALTOLERANCE:
431    value=doubleParameter(model.solver());
432    break;
433  default:
434    abort();
435  }
436  return value;
437}
438int 
439CbcParam::setIntParameter (CbcModel &model,int value) const
440{
441  if (value<lowerIntValue_||value>upperIntValue_) {
442    std::cout<<value<<" was provided for "<<name_<<
443      " - valid range is "<<lowerIntValue_<<" to "<<
444      upperIntValue_<<std::endl;
445    return 1;
446  } else {
447    int oldValue;
448    switch(type_) {
449    case LOGLEVEL:
450      oldValue = model.messageHandler()->logLevel();
451      model.messageHandler()->setLogLevel(value);
452      break;
453    case SOLVERLOGLEVEL:
454      oldValue = model.solver()->messageHandler()->logLevel();
455      model.solver()->messageHandler()->setLogLevel(value);
456      break;
457    case MAXNODES:
458      oldValue=model.getIntParam(CbcModel::CbcMaxNumNode);
459      model.setIntParam(CbcModel::CbcMaxNumNode,value);
460      break;
461    case STRONGBRANCHING:
462      oldValue=model.numberStrong();
463      model.setNumberStrong(value);
464      break;
465    default:
466      oldValue=0; // to avoid compiler message
467      abort();
468    }
469    std::cout<<name_<<" was changed from "<<oldValue<<" to "
470             <<value<<std::endl;
471    return 0;
472  }
473}
474int 
475CbcParam::intParameter (CbcModel &model) const
476{
477  int value;
478  switch(type_) {
479  case LOGLEVEL:
480    value = model.messageHandler()->logLevel();
481      break;
482  case SOLVERLOGLEVEL:
483    value = model.solver()->messageHandler()->logLevel();
484      break;
485  case MAXNODES:
486    value = model.getIntParam(CbcModel::CbcMaxNumNode);
487    break;
488  case STRONGBRANCHING:
489    value=model.numberStrong();
490    break;
491  default:
492    abort();
493  }
494  return value;
495}
Note: See TracBrowser for help on using the repository browser.