File: CbcGenCbcParamUtils.cpp

package info (click to toggle)
coinor-cbc 2.9.9+repack1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 7,848 kB
  • ctags: 5,787
  • sloc: cpp: 104,337; sh: 8,921; xml: 2,950; makefile: 520; ansic: 491; awk: 197
file content (407 lines) | stat: -rw-r--r-- 15,028 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/*
  Copyright (C) 2007, Lou Hafer, International Business Machines Corporation
  and others.  All Rights Reserved.

  This code is licensed under the terms of the Eclipse Public License (EPL).

  $Id: CbcGenCbcParamUtils.cpp 1899 2013-04-09 18:12:08Z stefan $
*/
/*
  This file is part of cbc-generic.
*/

#if defined(_MSC_VER)
// Turn off compiler warning about long names
#  pragma warning(disable:4786)
#endif

#include <string>
#include <cassert>

#include "CoinFinite.hpp"
#include "CoinParam.hpp"

#include "CbcModel.hpp"

#include "CbcGenCtlBlk.hpp"
#include "CbcGenParam.hpp"
#include "CbcGenCbcParam.hpp"

/*! \file CbcGenParamUtils
    \brief Implementation functions for CbcGenParam parameters.
*/

namespace {

char svnid[] = "$Id: CbcGenCbcParamUtils.cpp 1899 2013-04-09 18:12:08Z stefan $" ;

}

namespace CbcCbcParamUtils {


/* Function to set up cbc (CbcModel) parameters.  */

void addCbcCbcParams (int &numberParameters, CoinParamVec &parameters,
                      CbcModel *model)

{
    CbcCbcParam *param ;

    param = new CbcCbcParam(CbcCbcParam::ALLOWABLEGAP,
                            "allow!ableGap",
                            "Stop when gap between best possible and incumbent is less than this",
                            0.0, 1.0e20) ;
    param->setDblVal(0.0) ;
    param->setPushFunc(pushCbcCbcDbl) ;
    param->setObj(model) ;
    param->setLongHelp(
        "If the gap between best solution and best possible solution is less than this then the search will be terminated. Also see ratioGap."
    ) ;
    parameters.push_back(param) ;

    param = new CbcCbcParam(CbcCbcParam::CUTOFF,
                            "cuto!ff", "All solutions must be better than this", -1.0e60, 1.0e60) ;
    param->setDblVal(1.0e50) ;
    param->setPushFunc(pushCbcCbcDbl) ;
    param->setObj(model) ;
    param->setLongHelp(
        "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."
    ) ;
    parameters.push_back(param) ;

    param = new CbcCbcParam(CbcCbcParam::DIRECTION,
                            "direction", "Minimize or maximize", "min!imize", 0) ;
    param->appendKwd("max!imize") ;
    param->appendKwd("zero") ;
    param->setObj(model) ;
    param->setLongHelp(
        "The default is minimize - use 'direction maximize' for maximization.\nYou can also use the parameters 'maximize' or 'minimize'."
    ) ;
    parameters.push_back(param) ;

    param = new CbcCbcParam(CbcCbcParam::INCREMENT,
                            "inc!rement",
                            "A new solution must be at least this much better than the incumbent",
                            -1.0e20, 1.0e20, model->getDblParam(CbcModel::CbcCutoffIncrement)) ;
    param->setPushFunc(pushCbcCbcDbl) ;
    param->setObj(model) ;
    param->setLongHelp(
        "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!"
    ) ;
    parameters.push_back(param) ;

    param = new CbcCbcParam(CbcCbcParam::INFEASIBILITYWEIGHT,
                            "inf!easibilityWeight",
                            "Each integer infeasibility is expected to cost this much",
                            0.0, 1.0e20, model->getDblParam(CbcModel::CbcInfeasibilityWeight)) ;
    param->setPushFunc(pushCbcCbcDbl) ;
    param->setObj(model) ;
    param->setLongHelp(
        "A primitive way of deciding which node to explore next.  Satisfying each integer infeasibility is expected to cost this much."
    ) ;
    parameters.push_back(param) ;

    param = new CbcCbcParam(CbcCbcParam::INTEGERTOLERANCE,
                            "integerT!olerance",
                            "For an optimal solution, no integer variable may be farther than this from an integer value",
                            1.0e-20, 0.5, model->getDblParam(CbcModel::CbcIntegerTolerance)) ;
    param->setPushFunc(pushCbcCbcDbl) ;
    param->setObj(model) ;
    param->setLongHelp(
        "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."
    ) ;
    parameters.push_back(param) ;

    param = new CbcCbcParam(CbcCbcParam::LOGLEVEL,
                            "bclog!Level", "Level of detail in Coin branch and Cut output",
                            -1, 63, model->messageHandler()->logLevel()) ;
    param->setPushFunc(pushCbcCbcInt) ;
    param->setObj(model) ;
    param->setLongHelp(
        "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."
    ) ;
    parameters.push_back(param) ;

    param = new CbcCbcParam(CbcCbcParam::MAXIMIZE,
                            "max!imize", "Set optimization direction to maximize") ;
    param->setObj(model) ;
    param->setLongHelp(
        "The default is minimize - use 'maximize' for maximization.\n A synonym for 'direction maximize'."
    ) ;
    parameters.push_back(param) ;

    param = new CbcCbcParam(CbcCbcParam::MAXNODES,
                            "maxN!odes", "Maximum number of nodes to evaluate", 1, 2147483647) ;
    param->setObj(model) ;
    param->setLongHelp(
        "This is a repeatable way to limit search.  Normally using time is easier but then the results may not be repeatable."
    ) ;
    parameters.push_back(param) ;

    param = new CbcCbcParam(CbcCbcParam::MINIMIZE,
                            "min!imize", "Set optimization direction to minimize") ;
    param->setObj(model) ;
    param->setLongHelp(
        "The default is minimize - use 'maximize' for maximization.\nThis should only be necessary if you have previously set maximization. A synonym for 'direction minimize'."
    ) ;
    parameters.push_back(param) ;

    param = new CbcCbcParam(CbcCbcParam::MIPOPTIONS,
                            "mipO!ptions", "Dubious options for mip", 0, COIN_INT_MAX, 0, false) ;
    parameters.push_back(param) ;

    param = new CbcCbcParam(CbcCbcParam::MOREMIPOPTIONS,
                            "more!MipOptions", "More dubious options for mip", -1, COIN_INT_MAX, 0, false) ;
    parameters.push_back(param) ;

    param = new CbcCbcParam(CbcCbcParam::NUMBERMINI,
                            "miniT!ree", "Size of fast mini tree", 0, COIN_INT_MAX, 0, false) ;
    param->setObj(model) ;
    param->setLongHelp(
        "The idea is that I can do a small tree fast. This is a first try and will hopefully become more sophisticated."
    ) ;
    parameters.push_back(param) ;

    param = new CbcCbcParam(CbcCbcParam::NUMBERANALYZE,
                            "numberA!nalyze",
                            "Number of analysis iterations", -COIN_INT_MAX, COIN_INT_MAX, false) ;
    param->setObj(model) ;
    param->setLongHelp(
        "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."
    ) ;
    parameters.push_back(param) ;

    param = new CbcCbcParam(CbcCbcParam::CUTPASS,
                            "passC!uts", "Number of cut passes at root node",
                            -999999, 999999, model->getMaximumCutPassesAtRoot()) ;
    param->setObj(model) ;
    param->setLongHelp(
        "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."
    ) ;
    parameters.push_back(param) ;

    param = new CbcCbcParam(CbcCbcParam::GAPRATIO,
                            "ratio!Gap",
                            "Stop when the gap between the best possible solution and the incumbent is less than this fraction of the larger of the two",
                            0.0, 1.0e20, model->getDblParam(CbcModel::CbcAllowableFractionGap)) ;
    param->setPushFunc(pushCbcCbcDbl) ;
    param->setObj(model) ;
    param->setLongHelp(
        "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."
    ) ;
    parameters.push_back(param) ;

    param = new CbcCbcParam(CbcCbcParam::TIMELIMIT_BAB,
                            "sec!onds", "Maximum seconds for branch and cut", -1.0, 1.0e12) ;
    param->setPushFunc(pushCbcCbcDbl) ;
    param->setObj(model) ;
    param->setLongHelp(
        "After this many seconds the program will act as if maximum nodes had been reached."
    ) ;
    parameters.push_back(param) ;

    param = new CbcCbcParam(CbcCbcParam::STRONGBRANCHING,
                            "strong!Branching",
                            "Number of variables to look at in strong branching", 0, 999999,
                            model->numberStrong()) ;
    param->setObj(model) ;
    param->setLongHelp(
        "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."
    ) ;
    parameters.push_back(param) ;

    param = new CbcCbcParam(CbcCbcParam::NUMBERBEFORE,
                            "trust!PseudoCosts", "Number of branches before we trust pseudocosts",
                            -1, 2000000, model->numberBeforeTrust()) ;
    param->setObj(model) ;
    param->setPushFunc(pushCbcCbcInt) ;
    param->setLongHelp(
        "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."
    ) ;
    parameters.push_back(param) ;

    numberParameters = parameters.size() ;

    assert (numberParameters <= parameters.capacity()) ;

}

void loadCbcParamObj (const CoinParamVec paramVec, int first, int last,
                      CbcModel *obj)

{
    int i ;

    /*
      Load the CbcModel object into the parameters
    */
    for (i = first ; i <= last ; i++) {
        CbcCbcParam *cbcParam = dynamic_cast<CbcCbcParam *>(paramVec[i]) ;
        assert (cbcParam != 0) ;
        cbcParam->setObj(obj) ;
    }

    return ;
}

/*
  Set CbcModel defaults appropriate for cbc-generic.
*/

void setCbcModelDefaults (CbcModel *model)

{
    model->setIntParam(CbcModel::CbcMaxNumNode, (COIN_INT_MAX / 2)) ;
    model->setIntParam(CbcModel::CbcMaxNumSol, 999999) ;
    model->setIntParam(CbcModel::CbcFathomDiscipline, 0) ;

    model->setDblParam(CbcModel::CbcIntegerTolerance, 1.0e-6) ;
    model->setDblParam(CbcModel::CbcInfeasibilityWeight, 0.0) ;
    model->setDblParam(CbcModel::CbcCutoffIncrement, 1.0e-5) ;
    model->setDblParam(CbcModel::CbcAllowableGap, 1.0e-10) ;
    model->setDblParam(CbcModel::CbcAllowableFractionGap, 0.0) ;
    // One year is 60x60x24x365 = 31,536,000 seconds.
    model->setDblParam(CbcModel::CbcMaximumSeconds, 3.0e7) ;
    model->setDblParam(CbcModel::CbcCurrentCutoff, 1.0e100) ;
    model->setDblParam(CbcModel::CbcOptimizationDirection, 1.0) ;
    model->setDblParam(CbcModel::CbcCurrentObjectiveValue, 1.0e100) ;
    model->setDblParam(CbcModel::CbcCurrentMinimizationObjectiveValue, 1.0e100) ;
    model->setDblParam(CbcModel::CbcStartSeconds, 0.0) ;

    model->setNumberBeforeTrust(5) ;
    model->setNumberStrong(5) ;

    return ;
}


/*
  Function to push a double parameter.
*/

int pushCbcCbcDbl (CoinParam *param)

{
    assert (param != 0) ;

    CbcCbcParam *cbcParam = dynamic_cast<CbcCbcParam *>(param) ;
    assert (cbcParam != 0) ;

    CbcModel *model = cbcParam->obj() ;
    double val = cbcParam->dblVal() ;
    CbcCbcParam::CbcCbcParamCode code = cbcParam->paramCode() ;

    assert (model != 0) ;

    int retval = 0 ;
    /*
      Translate the parameter code from CbcCbcParamCode into the correct key for
      CbcDblParam.
    */
    CbcModel::CbcDblParam key ;
    switch (code) {
    case CbcCbcParam::INTEGERTOLERANCE: {
        key = CbcModel::CbcIntegerTolerance ;
        break ;
    }
    case CbcCbcParam::INFEASIBILITYWEIGHT: {
        key = CbcModel::CbcInfeasibilityWeight ;
        break ;
    }
    case CbcCbcParam::INCREMENT: {
        key = CbcModel::CbcCutoffIncrement ;
        break ;
    }
    case CbcCbcParam::ALLOWABLEGAP: {
        key = CbcModel::CbcAllowableGap ;
        break ;
    }
    case CbcCbcParam::GAPRATIO: {
        key = CbcModel::CbcAllowableFractionGap ;
        break ;
    }
    case CbcCbcParam::TIMELIMIT_BAB: {
        key = CbcModel::CbcMaximumSeconds ;
        break ;
    }
    case CbcCbcParam::CUTOFF: {
        key = CbcModel::CbcCurrentCutoff ;
        break ;
    }
    default: {
        std::cerr << "pushCbcCbcDbl: no equivalent CbcDblParam for "
                  << "parameter code `" << code << "'." << std::endl ;
        retval = -1 ;
        break ;
    }
    }

    bool result = model->setDblParam(key, val) ;
    if (result == false) {
        retval = -1 ;
    }

    return (retval) ;
}


/*
  Function to push an integer parameter.
*/

int pushCbcCbcInt (CoinParam *param)

{
    assert (param != 0) ;

    CbcCbcParam *cbcParam = dynamic_cast<CbcCbcParam *>(param) ;
    assert (cbcParam != 0) ;

    CbcModel *model = cbcParam->obj() ;
    int val = cbcParam->intVal() ;
    CbcCbcParam::CbcCbcParamCode code = cbcParam->paramCode() ;

    assert (model != 0) ;

    int retval = 0 ;
    /*
      Translate the parameter code from CbcCbcParamCode into the correct key for
      CbcIntParam, or call the appropriate method directly.
    */
    CbcModel::CbcIntParam key = CbcModel::CbcLastIntParam ;
    switch (code) {
    case CbcCbcParam::CUTPASS: {
        model->setMaximumCutPassesAtRoot(val) ;
        break ;
    }
    case CbcCbcParam::LOGLEVEL: {
        CoinMessageHandler *hndl = model->messageHandler() ;
        assert (hndl != 0) ;
        hndl->setLogLevel(val) ;
        break ;
    }
    case CbcCbcParam::NUMBERBEFORE: {
        model->setNumberBeforeTrust(val) ;
        break ;
    }
    default: {
        std::cerr << "pushCbcCbcInt: no equivalent CbcIntParam for "
                  << "parameter code `" << code << "'." << std::endl ;
        retval = -1 ;
        break ;
    }
    }

    if (key != CbcModel::CbcLastIntParam) {
        bool result = model->setIntParam(key, val) ;
        if (result == false) {
            retval = -1 ;
        }
    }

    return (retval) ;
}

} // end namespace CbcCbcParamUtils