gpt4 book ai didi

C++:错误:使用空句柄

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:40:44 24 4
gpt4 key购买 nike

我使用 C++ 和 CPLEX 编写了一个模型,如下所示。没有错误 写作时。但是,当我运行它时,它显示错误:使用空句柄。我是 疑惑哪里出错了,如何解决。谢谢你的帮助!


这是这个模型的主要部分[在此处输入图片描述][1]


该模型是计算最优序列,将不同的作业分配到子批处理中,并确定子批处理的数量


如果可以,我想知道如何解决这个问题

ILOSTLBEGIN

//params
#define I 3 //job classification
#define M 1000 //a big number
#define K 6 //machine number,0 for dummy machine
#define minS 10 //minimum sublot number

//types of params
typedef IloArray<IloNumArray> Float2D;
typedef IloArray<Float2D> Float3D;
//typedef IloArray<IloNumArray> Float3D;

//types of vars
typedef IloArray<IloIntVarArray> IntVar2D;
typedef IloArray<IloNumVarArray> FloatVar2D;
typedef IloArray<IloBoolVarArray> BoolVar2D;
typedef IloArray<FloatVar2D> FloatVar3D;
typedef IloArray<FloatVar3D> FloatVar4D;
typedef IloArray<BoolVar2D> BoolVar3D;
typedef IloArray<BoolVar3D> BoolVar4D;

int main(int argc, char **argv)
{
IloEnv env;
try
{
IloModel model(env);

//params
Float2D processtime(env, I);
IloNumArray limit(env, I);
Float3D setuptime(env, I);
IloNumArray totalnum(env, I);


totalnum[0] = 70; totalnum[1] = 50; totalnum[2] = 100;

for (int i = 0; i < I; i++)
{
limit[i] = totalnum[i] / minS;
}

for (int i = 0; i < I; i++)
{
processtime[i] = IloNumArray(env, K);
setuptime[i] = Float2D(env, I);
}

for (int i = 0; i < I; i++)
{
for (int j = 0; j < I; j++)
{
setuptime[i][j] = IloNumArray(env, K);
}
}

processtime[0][0] = 0; processtime[0][1] = 10; processtime[0][2] =20;
processtime[0][3] = 0; processtime[0][4] = 30; processtime[0][5] = 0;
processtime[1][0] = 0; processtime[1][1] = 0; processtime[1][2] =40;
processtime[1][3] = 50; processtime[1][4] = 0; processtime[1][5] =70;
processtime[2][0] = 0; processtime[2][1] = processtime[2][2] =
processtime[2][3] = processtime[2][4] = processtime[2][5] = 45;
// setuptime for different job
for (int i = 0; i < I; i++)
{
for (int j = 0; j < I; j++)
{
for (int k = 1; k < K; k++)
{
setuptime[i][j][k] = abs(i - j)*k / 2;

}
}
}
//variables
BoolVar2D y(env, I);
IntVar2D S(env, I);
IntVar2D n(env, I);
BoolVar4D x(env, I);
FloatVar3D t(env, I);
IloNumVar Cmax(env);
//matrix definition
for (int i = 0; i < I; i++)
{
y[i] = IloBoolVarArray(env, limit[i]);
S[i] = IloIntVarArray(env, limit[i]);
n[i] = IloIntVarArray(env, limit[i], 0, IloInfinity);
x[i] = BoolVar3D(env, limit[i]);
t[i] = FloatVar2D(env, limit[i]);
for (int j = 0; j < limit[i]; j++)
{
x[i][j] = BoolVar2D(env, I);
t[i][j] = IloNumVarArray(env, K, 0, IloInfinity, ILOFLOAT);
for (int g = 0; g < I; g++)
{
x[i][j][g] = IloBoolVarArray(env, limit[g]);
}
}
}

//objective
IloExpr obj(env);
obj = Cmax;
model.add(IloMinimize(env, obj));
obj.end();


//constraint
for (int i = 0; i < I; i++)
{
for (int j = 0; j < limit[i]; j++)
{
model.add(t[i][j][0] == 0);
model.add(S[i][j] == n[i][j] * minS);
model.add(S[i][j] >= y[i][j] * minS);
model.add(S[i][j] <= y[i][j] * M);
}
}
/constraint
for (int k = 1; k < K; k++)
{
for (int i = 0; i < I; i++)
{
for (int j = 0; j < limit[i]; j++)
{
for (int g = 0; g < I; g++)
{
for (int h = 0; h < limit[g]; h++)
{
if (g != i && h != j)
model.add(x[i][j][g][h] + x[g][h][i][j] ==
1);
model.add(t[i][j][k] >= t[g][h][k] + S[i][j]
* processtime[i][k] + setuptime[g][i][k] -
(2 + x[i][j][g][h] - y[i][j] - y[g][h])*M);
}
}
}
}
}
//constraint
for (int i = 0; i < I; i++)
{
for (int j = 0; j < limit[i]; j++)
{
for (int k = 1; k < K; k++)
{
model.add(t[i][j][k] >= t[i][j][k - 1] + S[i][j]
*processtime[i][k]);
}
}
}
for (int i = 0; i < I; i++)
{
IloExpr con4(env);
for (int j = 0; j < limit[i]; j++)
{
con4 += S[i][j];
}
model.add(con4 == totalnum[i]);
con4.end();
}

for (int i = 0; i < I; i++)
{
for (int j = 0; j < limit[i]; j++)
{
model.add(Cmax >= t[i][j][5]);
}
}

IloCplex cplex(model);

cplex.solve();

if (cplex.getStatus() == IloAlgorithm::Infeasible)
env.out() << "No Solution" << endl;
else
for (int i = 0; i < I; i++)
{
for (int j = 0; j < limit[i]; j++)
{
for (int g = 0; g < I; g++)
{
for (int h = 0; h < limit[g]; h++)
{
env.out() << cplex.getValue(x[i][j][g][h]) << endl;
}
}
}
}

感谢您的帮助!

最佳答案

至少在 Linux 上,通过 gdb,您可以使用 catch throw(例如,参见 Run an Application in GDB Until an Exception Occurs)

问题来自以下代码块:

//variables
BoolVar2D y(env, I);
IntVar2D S(env, I);
IntVar2D n(env, I);
BoolVar4D x(env, I);
FloatVar3D t(env, I);
IloNumVar Cmax(env);
//matrix definition
for (int i = 0; i < I; i++)
{
y[i] = IloBoolVarArray(env, limit[i]);
S[i] = IloIntVarArray(env, limit[i]); // <-- HERE
n[i] = IloIntVarArray(env, limit[i], 0, IloInfinity);
x[i] = BoolVar3D(env, limit[i]);
t[i] = FloatVar2D(env, limit[i]);
for (int j = 0; j < limit[i]; j++)
{
x[i][j] = BoolVar2D(env, I);
t[i][j] = IloNumVarArray(env, K, 0, IloInfinity, ILOFLOAT);
for (int g = 0; g < I; g++)
{
x[i][j][g] = IloBoolVarArray(env, limit[g]);
}
}
}

您需要替换以下行:

S[i] = IloIntVarArray(env, limit[i]);

用类似的东西:

S[i] = IloIntVarArray(env, limit[i], 0, IloIntMax);

关键是双参数构造函数创建了一个空句柄数组。因此,您必须初始化每个插槽或使用其他构造函数重载之一(如上所述)。

IloIntVarArray(IloEnv, IloInt) 的文档说:

This constructor creates an extensible array of n integer variables.

但是,它应该说:

This constructor creates an extensible array of n integer variables. Initially, the n elements are empty handles.

对于 IloNumVarArray(IloEnv, IloInt) 也是如此.

关于C++:错误:使用空句柄,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51246772/

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