source: trunk/Cbc/src/CbcParam.cpp @ 373

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

Fixed uninitialized value oldValue in setIntParameter

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