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

Last change on this file since 2464 was 2464, checked in by unxusr, 10 months ago

.clang-format with proposal for formatting code

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