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

Last change on this file since 2488 was 2488, checked in by unxusr, 20 months ago

initializations in CbcParam? and more getters and setters in Cbc_C_Interface

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