首页 > 解决方案 > 如何在 C++ 上添加 SOCP 约束而不发生内存泄漏?

问题描述

我之前曾在这里提出过“增加循环解决时间”的问题;https://www.ibm.com/developerworks/community/forums/html/topic?id=0ef4c99b-3bcc-4105-ac98-57867d5427da但该论坛目前不可用。

后来,我意识到二次、二阶锥编程约束会造成内存泄漏。这是代码的摘要版本(源代码和标题可以在上面的论坛链接中找到):

#include <header.h>
int main() {
 const int n = 10;
 const int ins = 30;
 // Parameters are read

 for (int i = 0; i < ins; i++) {
   IloEnv env;
   IloModel model(env);
   IloNumVarArray d(env, n + 1, 0, IloInfinity, ILOFLOAT);
   IloNumVarArray A(env, n + 1, -IloInfinity, IloInfinity, ILOFLOAT);
   IloNumVarArray C(env, n + 1, -IloInfinity, IloInfinity, ILOFLOAT);
   // Other variables are defined
   model.add((d[1] * d[1]) >= (A[1] * A[1]) + (C[1] * C[1])); // SOCP constraint 1
   model.add((d[2] * d[2]) >= (A[2] * A[2]) + (C[2] * C[2])); // SOCP constraint 2
   // Linear constraints are added according to the parameters of the instance
   // Solve method of IloCplex is called and outputs are collected
   env.end();
 }
}

执行此代码时,求解时间会随着增加而增加,i并且 Visual Studio 的内存使用工具显示每个循环都存在内存泄漏。如果注释掉 SOCP 约束,内存泄漏就会消失。奇怪的是,如果只添加这些 SOCP 约束中的一个,无论是 1 还是 2,内存都不会泄漏。仅当同时添加它们(或更多其他 SOCP 约束)时才会泄漏。

我添加这些二次约束的方式有问题吗?

谢谢你。

编辑:要重现文件,您可以使用以下链接:https ://drive.google.com/open?id=1AAPaSUhGwdGZ15c3MEb0atRQsrAel-mJ 。它包括标题、源和输入。

标签: c++memory-leakscplexgurobi

解决方案


通过从 CPLEX 转换为 GUROBI,此问题得到部分解决。显然,输入有一些数字问题,GUROBI 将其报告为警告,这可能是 CPLEX 中类似实例的时间增加的原因。在优化参数进行了几次更改后,模型成功求解,而 GUROBI 中连续实例的时间没有增加。但我想提一下,即使有数字问题,GUROBI 仍然设法提供次优结果,即使在这些问题中,连续实例的时间也没有任何增加。

此外,我想补充一点,有人建议我更改 MKL_DISABLE_FAST_MM = 0,这是我在 CPLEX 中的一些类似问题的解决方案。但是,它并没有解决我的问题。


推荐阅读