source: trunk/Cbc/src/CbcParam.cpp

Last change on this file was 2490, checked in by unxusr, 5 months ago

duplicate initializer

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