CPLEX-访问解决方案C ++时出错 [英] CPLEX - Error in accessing solution C++

查看:211
本文介绍了CPLEX-访问解决方案C ++时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a problem in accessing the solution of a LP problem.

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

This is the output of CPLEX after calling cplex.solve();

 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信号,引发以下异常:

However when I call cplex.getValues(values, variables); the program gives a SIGABRT signal throwing the following exception:

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

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

This is my code. What I'm doing wrong?

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的内部工作原理的变量集中。有两个明显的方法可以解决此问题。

This is an error that happens if you ask CPLEX for the value of a variable that CPLEX does not have in its model. When you build the model, it is not enough to just declare and define the variable for it to be included in the model. It also has to be part of one of the constraints or the objective in the model. Any variable that you declare/define that is NOT included in one of the constraints or the objective will therefore not be in the set of variables that gets extracted into the inner workings of CPLEX. There are two obvious things that you can do to resolve this.

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

First you can try to get the variable values inside a loop over the variables, and test whether each is actually in the cplex model - I think is is something like cplex.isExtracted(var). Do something simple like print a message when you come across a variable that is not extracted, telling you which variable is causing the problem.

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

Secondly you can export the model from CPLEX as an LP format file and check it manually. This is a very useful way to see what is actually in your model rather than what you think is in your model.

这篇关于CPLEX-访问解决方案C ++时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆