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

Last change on this file since 607 was 591, checked in by lou, 13 years ago

Initial commit of cbc-generic source.

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