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

时间:2018-07-09 13:28:24

标签: c++ cplex

我使用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;
                        }
                    }
                }
            }

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

至少在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)的文档说:

  

此构造函数创建n个整数变量的可扩展数组。

但是,它应该说:

  

此构造函数创建n个整数变量的可扩展数组。   最初,n个元素是空句柄。

IloNumVarArray(IloEnv, IloInt)一样。