source: branches/devel/Cbc/src/CbcGenCbcParamUtils.cpp @ 608

Last change on this file since 608 was 608, checked in by lou, 12 years ago

Cbc-generic: Add message handler, separate libCbc and cbc-generic main log
level parameters.

  • Property svn:eol-style set to native
File size: 12.7 KB
Line 
1/*
2  Copyright (C) 2007, Lou Hafer, International Business Machines Corporation
3  and others.  All Rights Reserved.
4
5  This file is part of cbc-generic.
6*/
7
8#if defined(_MSC_VER)
9// Turn off compiler warning about long names
10#  pragma warning(disable:4786)
11#endif
12
13#include <string>
14#include <cassert>
15
16#include "CoinParam.hpp"
17
18#include "CbcModel.hpp"
19
20#include "CbcGenCtlBlk.hpp"
21#include "CbcGenParam.hpp"
22#include "CbcGenCbcParam.hpp"
23
24/*! \file CbcGenParamUtils
25    \brief Implementation functions for CbcGenParam parameters.
26*/
27
28namespace {
29
30  char svnid[] = "$Id$" ;
31
32}
33
34namespace CbcCbcParamUtils
35{
36
37
38/* Function to set up cbc (CbcModel) parameters.  */
39
40void addCbcCbcParams (int &numberParameters, CoinParamVec &parameters,
41                      CbcModel *model)
42
43{ CbcCbcParam *param ;
44
45  param = new CbcCbcParam(CbcCbcParam::ALLOWABLEGAP,
46        "allow!ableGap",
47        "Stop when gap between best possible and incumbent is less than this",
48        0.0,1.0e20) ;
49  param->setDblVal(0.0) ;
50  param->setPushFunc(pushCbcCbcDbl) ;
51  param->setObj(model) ;
52  param->setLongHelp(
53        "If the gap between best solution and best possible solution is less than this then the search will be terminated. Also see ratioGap."
54        ) ;
55  parameters.push_back(param) ;
56
57  param = new CbcCbcParam(CbcCbcParam::CUTOFF,
58        "cuto!ff","All solutions must be better than this", -1.0e60,1.0e60) ;
59  param->setDblVal(1.0e50) ;
60  param->setPushFunc(pushCbcCbcDbl) ;
61  param->setObj(model) ;
62  param->setLongHelp(
63        "All solutions must be better than this value (in a minimization sense).  This is also set by cbc whenever it obtains a solution and is set to the value of the objective for the solution minus the cutoff increment."
64        ) ;
65  parameters.push_back(param) ;
66
67  param = new CbcCbcParam(CbcCbcParam::DIRECTION,
68                          "direction","Minimize or maximize","min!imize",0) ;
69  param->appendKwd("max!imize") ;
70  param->appendKwd("zero") ;
71  param->setObj(model) ;
72  param->setLongHelp(
73        "The default is minimize - use 'direction maximize' for maximization.\nYou can also use the parameters 'maximize' or 'minimize'."
74        ) ;
75  parameters.push_back(param) ;
76
77  param = new CbcCbcParam(CbcCbcParam::INCREMENT,
78        "inc!rement",
79        "A new solution must be at least this much better than the incumbent",
80        -1.0e20,1.0e20,model->getDblParam(CbcModel::CbcCutoffIncrement)) ;
81  param->setPushFunc(pushCbcCbcDbl) ;
82  param->setObj(model) ;
83  param->setLongHelp(
84        "Whenever a solution is found the bound on future solutions is set to the objective of the solution (in a minimization sense) plus the specified increment.  If this option is not specified, the code will try and work out an increment.  E.g., if all objective coefficients are multiples of 0.01 and only integer variables have entries in objective then the increment can be set to 0.01.  Be careful if you set this negative!"
85        ) ;
86  parameters.push_back(param) ;
87
88  param = new CbcCbcParam(CbcCbcParam::INFEASIBILITYWEIGHT,
89        "inf!easibilityWeight",
90        "Each integer infeasibility is expected to cost this much",
91        0.0,1.0e20,model->getDblParam(CbcModel::CbcInfeasibilityWeight)) ;
92  param->setPushFunc(pushCbcCbcDbl) ;
93  param->setObj(model) ;
94  param->setLongHelp(
95        "A primitive way of deciding which node to explore next.  Satisfying each integer infeasibility is expected to cost this much."
96        ) ;
97  parameters.push_back(param) ;
98
99  param = new CbcCbcParam(CbcCbcParam::INTEGERTOLERANCE,
100        "integerT!olerance",
101        "For an optimal solution, no integer variable may be farther than this from an integer value",
102        1.0e-20,0.5,model->getDblParam(CbcModel::CbcIntegerTolerance)) ;
103  param->setPushFunc(pushCbcCbcDbl) ;
104  param->setObj(model) ;
105  param->setLongHelp(
106        "When checking a solution for feasibility, if the difference between the value of a variable and the nearest integer is less than the integer tolerance, the value is considered to be integral. Beware of setting this smaller than the primal tolerance."
107        ) ;
108  parameters.push_back(param) ;
109
110  param = new CbcCbcParam(CbcCbcParam::LOGLEVEL,
111        "bclog!Level","Level of detail in Coin branch and Cut output",
112        -1,63,model->messageHandler()->logLevel()) ;
113  param->setPushFunc(pushCbcCbcInt) ;
114  param->setObj(model) ;
115  param->setLongHelp(
116        "If set to 0 then there should be no output in normal circumstances. A value of 1 is probably the best value for most uses, while 2 and 3 give more information."
117        ) ;
118  parameters.push_back(param) ;
119
120  param = new CbcCbcParam(CbcCbcParam::MAXIMIZE,
121        "max!imize","Set optimization direction to maximize") ;
122  param->setObj(model) ;
123  param->setLongHelp(
124        "The default is minimize - use 'maximize' for maximization.\n A synonym for 'direction maximize'."
125        ) ;
126  parameters.push_back(param) ;
127
128  param = new CbcCbcParam(CbcCbcParam::MAXNODES,
129        "maxN!odes","Maximum number of nodes to evaluate",1,2147483647) ;
130  param->setObj(model) ;
131  param->setLongHelp(
132        "This is a repeatable way to limit search.  Normally using time is easier but then the results may not be repeatable."
133        ) ;
134  parameters.push_back(param) ;
135
136  param = new CbcCbcParam(CbcCbcParam::MINIMIZE,
137        "min!imize","Set optimization direction to minimize") ;
138  param->setObj(model) ;
139  param->setLongHelp(
140        "The default is minimize - use 'maximize' for maximization.\nThis should only be necessary if you have previously set maximization. A synonym for 'direction minimize'."
141        ) ;
142  parameters.push_back(param) ;
143
144  param = new CbcCbcParam(CbcCbcParam::MIPOPTIONS,
145        "mipO!ptions","Dubious options for mip",0,INT_MAX,0,false) ;
146  parameters.push_back(param) ;
147
148  param = new CbcCbcParam(CbcCbcParam::MOREMIPOPTIONS,
149        "more!MipOptions","More dubious options for mip",-1,INT_MAX,0,false) ;
150  parameters.push_back(param) ;
151
152  param = new CbcCbcParam(CbcCbcParam::NUMBERMINI,
153        "miniT!ree","Size of fast mini tree",0,INT_MAX,0,false) ;
154  param->setObj(model) ;
155  param->setLongHelp(
156        "The idea is that I can do a small tree fast. This is a first try and will hopefully become more sophisticated."
157        ) ;
158  parameters.push_back(param) ;
159
160  param = new CbcCbcParam(CbcCbcParam::NUMBERANALYZE,
161        "numberA!nalyze",
162        "Number of analysis iterations",-INT_MAX,INT_MAX,false) ;
163  param->setObj(model) ;
164  param->setLongHelp(
165        "This says how many iterations to spend at the root node analyzing the problem.  This is a first try and will hopefully become more sophisticated."
166        ) ; 
167  parameters.push_back(param) ;
168
169  param = new CbcCbcParam(CbcCbcParam::CUTPASS,
170        "passC!uts","Number of cut passes at root node",
171        -999999,999999,model->getMaximumCutPassesAtRoot()) ;
172  param->setObj(model) ;
173  param->setLongHelp(
174        "The default is 100 passes if less than 500 columns, 100 passes (but stop if the drop is small) if less than 5000 columns, 20 otherwise."
175        ) ;
176  parameters.push_back(param) ;
177
178  param = new CbcCbcParam(CbcCbcParam::GAPRATIO,
179        "ratio!Gap",
180        "Stop when the gap between the best possible solution and the incumbent is less than this fraction of the larger of the two",
181        0.0,1.0e20,model->getDblParam(CbcModel::CbcAllowableFractionGap)) ;
182  param->setPushFunc(pushCbcCbcDbl) ;
183  param->setObj(model) ;
184  param->setLongHelp(
185        "If the gap between the best solution and the best possible solution is less than this fraction of the objective value at the root node then the search will terminate.  See 'allowableGap' for a way of using absolute value rather than fraction."
186        ) ;
187  parameters.push_back(param) ;
188
189  param = new CbcCbcParam(CbcCbcParam::TIMELIMIT_BAB,
190        "sec!onds","Maximum seconds for branch and cut",-1.0,1.0e12) ;
191  param->setPushFunc(pushCbcCbcDbl) ;
192  param->setObj(model) ;
193  param->setLongHelp(
194        "After this many seconds the program will act as if maximum nodes had been reached."
195        ) ;
196  parameters.push_back(param) ;
197
198  param = new CbcCbcParam(CbcCbcParam::STRONGBRANCHING,
199        "strong!Branching",
200        "Number of variables to look at in strong branching",0,999999,
201        model->numberStrong()) ;
202  param->setObj(model) ;
203  param->setLongHelp(
204        "In order to decide which variable to branch on, the code will choose up to this number of unsatisfied variables and try mini up and down branches.  The most effective one is chosen. If a variable is branched on many times then the previous average up and down costs may be used - see number before trust."
205        ) ;
206  parameters.push_back(param) ;
207
208  param = new CbcCbcParam(CbcCbcParam::NUMBERBEFORE,
209        "trust!PseudoCosts","Number of branches before we trust pseudocosts",
210        -1,2000000,model->numberBeforeTrust()) ;
211  param->setObj(model) ;
212  param->setPushFunc(pushCbcCbcInt) ;
213  param->setLongHelp(
214        "Using strong branching computes pseudo-costs.  After this many times for a variable we just trust the pseudo costs and do not do any more strong branching."
215        ) ;
216  parameters.push_back(param) ;
217
218  numberParameters = parameters.size() ;
219
220  assert (numberParameters <= parameters.capacity()) ;
221
222}
223
224void loadCbcParamObj (const CoinParamVec paramVec, int first, int last,
225                      CbcModel *obj)
226
227{ int i ;
228
229/*
230  Load the CbcModel object into the parameters
231*/
232  for (i = first ; i <= last ; i++)
233  { CbcCbcParam *cbcParam = dynamic_cast<CbcCbcParam *>(paramVec[i]) ;
234    assert (cbcParam != 0) ;
235    cbcParam->setObj(obj) ; }
236
237  return ; }
238
239/*
240  Set CbcModel defaults appropriate for cbc-generic.
241*/
242
243void setCbcModelDefaults (CbcModel *model)
244
245{ model->setIntParam(CbcModel::CbcMaxNumNode,(INT_MAX/2)) ;
246  model->setIntParam(CbcModel::CbcMaxNumSol,999999) ;
247  model->setIntParam(CbcModel::CbcFathomDiscipline,0) ;
248
249  model->setDblParam(CbcModel::CbcIntegerTolerance,1.0e-6) ;
250  model->setDblParam(CbcModel::CbcInfeasibilityWeight,0.0) ;
251  model->setDblParam(CbcModel::CbcCutoffIncrement,1.0e-5) ;
252  model->setDblParam(CbcModel::CbcAllowableGap,1.0e-10) ;
253  model->setDblParam(CbcModel::CbcAllowableFractionGap,0.0) ;
254  // One year is 60x60x24x365 = 31,536,000 seconds.
255  model->setDblParam(CbcModel::CbcMaximumSeconds,3.0e7) ;
256  model->setDblParam(CbcModel::CbcCurrentCutoff,1.0e100) ;
257  model->setDblParam(CbcModel::CbcOptimizationDirection,1.0) ;
258  model->setDblParam(CbcModel::CbcCurrentObjectiveValue,1.0e100) ;
259  model->setDblParam(CbcModel::CbcCurrentMinimizationObjectiveValue,1.0e100) ;
260  model->setDblParam(CbcModel::CbcStartSeconds,0.0) ;
261
262  model->setNumberBeforeTrust(5) ;
263  model->setNumberStrong(5) ;
264
265  return ; }
266
267
268/*
269  Function to push a double parameter.
270*/
271
272int pushCbcCbcDbl (CoinParam *param)
273
274{ assert (param != 0) ;
275
276  CbcCbcParam *cbcParam = dynamic_cast<CbcCbcParam *>(param) ;
277  assert (cbcParam != 0) ;
278
279  CbcModel *model = cbcParam->obj() ;
280  double val = cbcParam->dblVal() ;
281  CbcCbcParam::CbcCbcParamCode code = cbcParam->paramCode() ;
282
283  assert (model != 0) ;
284
285  int retval = 0 ;
286/*
287  Translate the parameter code from CbcCbcParamCode into the correct key for
288  CbcDblParam.
289*/
290  CbcModel::CbcDblParam key ;
291  switch (code)
292  { case CbcCbcParam::INTEGERTOLERANCE:
293    { key = CbcModel::CbcIntegerTolerance ;
294      break ; }
295    case CbcCbcParam::INFEASIBILITYWEIGHT:
296    { key = CbcModel::CbcInfeasibilityWeight ;
297      break ; }
298    case CbcCbcParam::INCREMENT:
299    { key = CbcModel::CbcCutoffIncrement ;
300      break ; }
301    case CbcCbcParam::ALLOWABLEGAP:
302    { key = CbcModel::CbcAllowableGap ;
303      break ; }
304    case CbcCbcParam::GAPRATIO:
305    { key = CbcModel::CbcAllowableFractionGap ;
306      break ; }
307    case CbcCbcParam::TIMELIMIT_BAB:
308    { key = CbcModel::CbcMaximumSeconds ;
309      break ; }
310    case CbcCbcParam::CUTOFF:
311    { key = CbcModel::CbcCurrentCutoff ;
312      break ; }
313    default:
314    { std::cerr << "pushCbcCbcDbl: no equivalent CbcDblParam for "
315                << "parameter code `" << code << "'." << std::endl ;
316      retval = -1 ;
317      break ; } }
318
319  bool result = model->setDblParam(key,val) ;
320  if (result == false)
321  { retval = -1 ; }
322
323  return (retval) ; }
324
325
326/*
327  Function to push an integer parameter.
328*/
329
330int pushCbcCbcInt (CoinParam *param)
331
332{ assert (param != 0) ;
333
334  CbcCbcParam *cbcParam = dynamic_cast<CbcCbcParam *>(param) ;
335  assert (cbcParam != 0) ;
336
337  CbcModel *model = cbcParam->obj() ;
338  int val = cbcParam->intVal() ;
339  CbcCbcParam::CbcCbcParamCode code = cbcParam->paramCode() ;
340
341  assert (model != 0) ;
342
343  int retval = 0 ;
344/*
345  Translate the parameter code from CbcCbcParamCode into the correct key for
346  CbcIntParam, or call the appropriate method directly.
347*/
348  CbcModel::CbcIntParam key = CbcModel::CbcLastIntParam ;
349  switch (code)
350  { case CbcCbcParam::CUTPASS:
351    { model->setMaximumCutPassesAtRoot(val) ;
352      break ; }
353    case CbcCbcParam::LOGLEVEL:
354    { CoinMessageHandler *hndl = model->messageHandler() ;
355      assert (hndl != 0) ;
356      hndl->setLogLevel(val) ;
357      break ; }
358    case CbcCbcParam::NUMBERBEFORE:
359    { model->setNumberBeforeTrust(val) ;
360      break ; }
361    default:
362    { std::cerr << "pushCbcCbcInt: no equivalent CbcIntParam for "
363                << "parameter code `" << code << "'." << std::endl ;
364      retval = -1 ;
365      break ; } }
366
367  if (key != CbcModel::CbcLastIntParam)
368  { bool result = model->setIntParam(key,val) ;
369    if (result == false)
370    { retval = -1 ; } }
371
372  return (retval) ; }
373
374} // end namespace CbcCbcParamUtils
375
Note: See TracBrowser for help on using the repository browser.