如何在 C++ 中完全省略 CBC 日志记录?

How to completely omit CBC loggings in C++?

我正在使用 CBC 求解器求解一个程序,方法 model.branchAndBound 被调用了多次(相当多)。因为日志消息必须写入文件,所以它实际上减慢了程序。我想知道是否可以完全忽略日志消息?这是在 c++ 中,我现在认为许多建议的答案纸浆只适用于 python。另外,如果纸浆是解决这个问题的方法,我不太确定纸浆是如何使用的。是否有任何好的文档可以帮助理解纸浆和 CBC 之间的关系,以及如何使用它们?如果有人能帮我解决这个问题,我将不胜感激!

例如(这是来自github的例子):

// Copyright (C) 2009, International Business Machines
// Corporation and others.  All Rights Reserved.
// This code is licensed under the terms of the Eclipse Public License (EPL).
// For Branch and bound
#include "OsiSolverInterface.hpp"
#include "CbcModel.hpp"

// Methods of building

#include "CoinBuild.hpp"
#include "CoinModel.hpp"

int main(int argc, const char *argv[])
{
    OsiClpSolverInterface model;
    double objValue[] = { 1.0, 0.0, 0.0, 0.0, 2.0, 0.0, 0.0, -1.0 };
    // Lower bounds for columns
    double columnLower[] = { 2.5, 0.0, 0.0, 0.0, 0.5, 0.0, 0.0, 0.0 };
    // Upper bounds for columns
    double columnUpper[] = { COIN_DBL_MAX, 4.1, 1.0, 1.0, 4.0,
    COIN_DBL_MAX, COIN_DBL_MAX, 4.3 };
    // Lower bounds for row activities
    double rowLower[] = { 2.5, -COIN_DBL_MAX, 4.0, 1.8, 3.0 };
    // Upper bounds for row activities
    double rowUpper[] = { COIN_DBL_MAX, 2.1, 4.0, 5.0, 15.0 };
    // Matrix stored packed
    int column[] = { 0, 1, 3, 4, 7,
    1, 2,
    2, 5,
    3, 6,
    0, 4, 7 };
    double element[] = { 3.0, 1.0, -2.0, -1.0, -1.0,
    2.0, 1.1,
    1.0, 1.0,
    2.8, -1.2,
    5.6, 1.0, 1.9 };
    int starts[] = { 0, 5, 7, 9, 11, 14 };
    // Integer variables (note upper bound already 1.0)
    int whichInt[] = { 2, 3 };
    int numberRows = (int)(sizeof(rowLower) / sizeof(double));
    int numberColumns = (int)(sizeof(columnLower) / sizeof(double));
    CoinModel build;
    // First do columns (objective and bounds)
    int i;
    for (i = 0; i < numberColumns; i++) {
    build.setColumnBounds(i, columnLower[i], columnUpper[i]);
    build.setObjective(i, objValue[i]);
    }
    // mark as integer
    for (i = 0; i < (int)(sizeof(whichInt) / sizeof(int)); i++)
    build.setInteger(whichInt[i]);
    // Now build rows
    for (i = 0; i < numberRows; i++) {
    int startRow = starts[i];
    int numberInRow = starts[i + 1] - starts[i];
    build.addRow(numberInRow, column + startRow, element + startRow,
        rowLower[i], rowUpper[i]);
    }
    // add rows into solver
    model.loadFromCoinModel(build);
    CbcModel model2(model);
    model2.branchAndBound();
    return 0;
}

日志输出为:

Clp0000I Optimal - objective value 3.2368421
Clp0000I Optimal - objective value 3.2368421
Node 0 depth 0 unsatisfied 0 sum 0 obj 3.23684 guess 3.23684 branching on -1
Clp0000I Optimal - objective value 3.2368421
Cbc0004I Integer solution of 3.2368421 found after 0 iterations and 0 nodes (0.13 seconds)
Cbc0001I Search completed - best objective 3.236842105263158, took 0 iterations and 0 nodes (0.14 seconds)
Cbc0035I Maximum depth 0, 0 variables fixed on reduced cost
Clp0000I Optimal - objective value 3.2368421
Clp0000I Optimal - objective value 3.2368421

将此添加到代码中即可解决所有问题:

model.setLogLevel(0);