gpt4 book ai didi

c++ - CPLEX - 访问解决方案 C++ 时出错

转载 作者:行者123 更新时间:2023-11-28 01:46:17 25 4
gpt4 key购买 nike

我在访问 LP 问题的解决方案时遇到问题。

这是调用 cplex.solve() 后 CPLEX 的输出;

 CPXPARAM_MIP_Strategy_CallbackReducedLP          0
Found incumbent of value 0.000000 after 0.00 sec. (0.70 ticks)
Tried aggregator 1 time.
MIP Presolve eliminated 570 rows and 3 columns.
MIP Presolve modified 88 coefficients.
Reduced MIP has 390 rows, 29291 columns, and 76482 nonzeros.
Reduced MIP has 29291 binaries, 0 generals, 0 SOSs, and 0 indicators.
Presolve time = 0.06 sec. (49.60 ticks)
Tried aggregator 1 time.
Reduced MIP has 390 rows, 29291 columns, and 76482 nonzeros.
Reduced MIP has 29291 binaries, 0 generals, 0 SOSs, and 0 indicators.
Presolve time = 0.04 sec. (31.47 ticks)
Probing time = 0.02 sec. (1.36 ticks)
MIP emphasis: balance optimality and feasibility.
MIP search method: dynamic search.
Parallel mode: deterministic, using up to 8 threads.
Root relaxation solution time = 0.03 sec. (17.59 ticks)

Nodes Cuts/
Node Left Objective IInf Best Integer Best Bound ItCnt Gap

* 0+ 0 0.0000 -395.1814 ---
* 0+ 0 -291.2283 -395.1814 35.69%
* 0 0 integral 0 -372.2283 -372.2283 201 0.00%
Elapsed time = 0.21 sec. (131.64 ticks, tree = 0.00 MB, solutions = 3)

Root node processing (before b&c):
Real time = 0.21 sec. (133.18 ticks)
Parallel b&c, 8 threads:
Real time = 0.00 sec. (0.00 ticks)
Sync time (average) = 0.00 sec.
Wait time (average) = 0.00 sec.
------------
Total (root+branch&cut) = 0.21 sec. (133.18 ticks)

但是当我调用 cplex.getValues(values, variables);程序给出一个 SIGABRT 信号并抛出以下异常:

libc++abi.dylib: terminating with uncaught exception of type IloAlgorithm::NotExtractedException

这是我的代码。我做错了什么?

std::vector<links_t> links(pointsA.size()*pointsB.size());

std::unordered_map<int, std::vector<std::size_t> > point2DToLinks;

for(std::size_t i=0; i<pointsA.size(); ++i){

for(std::size_t j=0; j<pointsB.size(); ++j){

std::size_t index = (i*pointsA.size()) + j;

links[index].from = i;
links[index].to = j;

links[index].value = cv::norm(pointsA[i] - pointsB[j]);

point2DToLinks[pointsA[i].point2D[0]->id].push_back(index);
point2DToLinks[pointsA[i].point2D[1]->id].push_back(index);
point2DToLinks[pointsA[i].point2D[2]->id].push_back(index);

point2DToLinks[pointsB[j].point2D[0]->id].push_back(index);
point2DToLinks[pointsB[j].point2D[1]->id].push_back(index);
point2DToLinks[pointsB[j].point2D[2]->id].push_back(index);

}

}

std::size_t size = links.size() + point2DToLinks.size();

IloEnv environment;

IloNumArray coefficients(environment, size);

for(std::size_t i=0; i<links.size(); ++i) coefficients[i] = links[i].value;
for(std::size_t i=links.size(); i<size; ++i) coefficients[i] = -lambda;

IloNumVarArray variables(environment, size, 0, 1, IloNumVar::Bool);

IloObjective objective(environment, 0.0, IloObjective::Minimize);

objective.setLinearCoefs(variables, coefficients);

IloRangeArray constrains = IloRangeArray(environment);

std::size_t counter = 0;

for(auto point=point2DToLinks.begin(); point!=point2DToLinks.end(); point++){

IloExpr expression(environment);

const std::vector<std::size_t> & inLinks = point->second;

for(std::size_t j=0; j<inLinks.size(); j++) expression += variables[inLinks[j]];

expression -= variables[links.size() + counter];

constrains.add(IloRange(environment, 0, expression));

expression.end();

++counter;

}

IloModel model(environment);

model.add(objective);
model.add(constrains);

IloCplex cplex(model);

cplex.solve();

if(cplex.getStatus() != IloAlgorithm::Optimal){
fprintf(stderr, "error: cplex terminate with an error.\n");
abort();
}

IloNumArray values(environment, size);

cplex.getValues(values, variables);

for(std::size_t i=0; i<links.size(); ++i)
if(values[i] > 0) pairs.push_back(links[i]);

environment.end();

最佳答案

如果您向 CPLEX 询问 CPLEX 的模型中没有的变量值,则会发生此错误。当您构建模型时,仅声明和定义要包含在模型中的变量是不够的。它还必须是模型中约束或目标之一的一部分。因此,您声明/定义的任何未包含在其中一个约束或目标中的变量将不会包含在提取到 CPLEX 内部工作中的变量集中。您可以采取两种明显的措施来解决此问题。

首先,您可以尝试在变量循环内获取变量值,并测试每个变量值是否实际上在 cplex 模型中 - 我认为它类似于 cplex.isExtracted(var)。做一些简单的事情,比如当您遇到未提取的变量时打印一条消息,告诉您哪个变量导致了问题。

其次,您可以将模型从 CPLEX 导出为 LP 格式文件并手动检查。这是一种非常有用的方法,可以查看模型中的实际内容,而不是您认为模型中的内容。

关于c++ - CPLEX - 访问解决方案 C++ 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44955130/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com