如何在 C++ 中完全省略 CBC 日志记录?
Posted
技术标签:
【中文标题】如何在 C++ 中完全省略 CBC 日志记录?【英文标题】:How to completely omit CBC loggings in C++? 【发布时间】:2022-01-16 19:50:21 【问题描述】:我正在使用 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
【问题讨论】:
"有几个日志级别。将日志级别设置为 i 会生成级别 i 和所有低于 i 的级别的日志消息。日志级别 0:关闭所有 CBC 消息,但只有一个。" - 把它关掉? 哦,是的!我试图找到这个但没有。就像添加“model.setLogLevel(0)”一样简单。非常感谢! 【参考方案1】:将这个添加到代码中可以解决所有问题:
model.setLogLevel(0);
【讨论】:
正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center。以上是关于如何在 C++ 中完全省略 CBC 日志记录?的主要内容,如果未能解决你的问题,请参考以下文章