IloExtractable:使用空句柄

时间:2015-09-06 22:11:33

标签: c++ cplex

我正在使用Visual Studio 2012和IBM ILOG CPLEX Optimization Studio V12.6。 当我跑到循环中时:

for (l=0; l<num_l; ++l)
{
    sprintf(buf,"Delta1(%d,%d,%d,%d)",k,j,l,mu);
    Delta1[k][j][l][mu].setName(buf);
    Del1_del1 = Delta1[k][j][l][mu] - delta1[k][mu];                        
    cst_Del1_del1.add(IloRange(env,-IloInfinity,Del1_del1,0));
    Del1_y = Delta1[k][j][l][mu] - (D_k[k] * y[j][l]);
    cst_Del1_del1.add(IloRange(env,-IloInfinity,Del1_y,0));
    Del1_Dydel1 = Delta1[k][j][l][mu] + D_k[k] - D_k[k] * y[j][l] + delta1[k][mu];
    cst_Del1_Dydel1.add(IloRange(env,0,Del1_y,+IloInfinity));
    for(a=0;a<num_a;++a)
    {
        utility_l += beta_a[a] * r_al[a][l];
        for (b=0; b<num_b; ++b)
        {
            utility_l += (gamma_ab[a][b]*s_bk[b][k]*r_al[a][l]*time_jk[j][k]);
        }
    }
    sel1 += (-Delta1[k][mu][j][l]*exp(utility_l));
}

我的错误如下所示:

IloExtractable:使用空句柄

  

断言失败:(getImpl()!= 0)|| (ILOSTD(cerr)&lt;&lt;   “IloExtractable”“:使用空句柄”&lt;&lt; ILOSTD(ENDL)   ilo_stop_assert()),文件c:\ program files   (86)\ IBM \ ILOG \ cplex_studio_preview126 \音乐会\包括\ ilconcert \ iloextractable.h,   第267行

这是我要添加的约束(cst_sel1)。

<a href="https://www.codecogs.com/eqnedit.php?latex=\sum\limits_{j&space;\in&space;J}&space;\sum\limits_{l&space;\in&space;L}&space;\left(e^{&space;u_{jkl}}&space;\cdot&space;\Delta_{jkl\mu}^1&space;\right)&space;&\le&space;D_k^{0}&space;\cdot&space;(\sum\limits_{j&space;\in&space;J}&space;e^{&space;u_{jk\mu}}&space;\cdot&space;y_{j\mu})\\&space;\Delta_{jkl\mu}^1&space;&\le&space;\delta_{k\mu}^1\\&space;\Delta_{jkl\mu}^1&space;&\le&space;D_k^{0}&space;\cdot&space;y_{jl}\\&space;\Delta_{jkl\mu}^1&space;&\ge&space;-D_k^{0}&space;\cdot&space;(1-y_{jl})&space;&plus;&space;\delta_{k\mu}^1" target="_blank"><img src="https://latex.codecogs.com/gif.latex?\sum\limits_{j&space;\in&space;J}&space;\sum\limits_{l&space;\in&space;L}&space;\left(e^{&space;u_{jkl}}&space;\cdot&space;\Delta_{jkl\mu}^1&space;\right)&space;&\le&space;D_k^{0}&space;\cdot&space;(\sum\limits_{j&space;\in&space;J}&space;e^{&space;u_{jk\mu}}&space;\cdot&space;y_{j\mu})\\&space;\Delta_{jkl\mu}^1&space;&\le&space;\delta_{k\mu}^1\\&space;\Delta_{jkl\mu}^1&space;&\le&space;D_k^{0}&space;\cdot&space;y_{jl}\\&space;\Delta_{jkl\mu}^1&space;&\ge&space;-D_k^{0}&space;\cdot&space;(1-y_{jl})&space;&plus;&space;\delta_{k\mu}^1" title="\sum\limits_{j \in J} \sum\limits_{l \in L} \left(e^{ u_{jkl}} \cdot \Delta_{jkl\mu}^1 \right) &\le D_k^{0} \cdot (\sum\limits_{j \in J} e^{ u_{jk\mu}} \cdot y_{j\mu})\\ \Delta_{jkl\mu}^1 &\le \delta_{k\mu}^1\\ \Delta_{jkl\mu}^1 &\le D_k^{0} \cdot y_{jl}\\ \Delta_{jkl\mu}^1 &\ge -D_k^{0} \cdot (1-y_{jl}) + \delta_{k\mu}^1" /></a>

这是整个cpp文件。

#include "stdafx.h"
#include <ilcplex/ilocplex.h> 
#include <math.h> 
#include <iostream> 
#include <fstream> 
#include <sstream>

ILOSTLBEGIN

    typedef IloArray<IloNumVarArray> IloNumVarArray2;
    typedef IloArray<IloNumVarArray2> IloNumVarArray3;
    typedef IloArray<IloNumVarArray3> IloNumVarArray4;
    typedef IloArray<IloNumArray2> IloNumArray3;
    typedef IloArray<IloBoolVarArray> IloBoolVarArray2;
    typedef IloArray<IloIntVarArray> IloIntVarArray2;
    typedef IloArray<IloIntVarArray2> IloIntVarArray3;
    typedef IloArray<IloIntVarArray3> IloIntVarArray4;
    typedef IloArray<IloRangeArray> IloRangeArray2;


int main(int argc, char **argv)
{
    IloEnv env;
    char buf[300];
    try
    {
        const char* filename = "PPL_PARA.txt"; //input file
        if (argc >=2) filename = argv[1];
        ifstream file(filename);
        if( !file)
        {
            cerr << "No such file: " << filename << endl;
            throw(-1);
        }
        IloInt num_j; //number of outreach clinics;
        IloInt num_k; //number of patient groups
        IloInt num_l; //number of physicians
        IloInt num_a; //number of catagories for physician's attributes
        IloInt num_b; //number of catagories for patient's characteristics

        IloNum T; //working hour of physician
        IloNum alpha; //coefficient of patients' travel time for utility function
        IloNum omega1; //coefficient of 1st choice
        IloNum omega2; //coefficient of 2nd choice
        IloNum omega3; //coefficient of 3rd choice
        IloNum omega4; //coefficient of 4th choice



        IloInt j,k,l,mu,nu,a,b; //different index for loops
        IloNum M = 100000.0; //Big M
        IloModel mod(env);
        IloCplex cplex(mod);

        //reading the input file
        file >> num_j
            >> num_k
            >> num_l
            >> num_a
            >> num_b
            >> T
            >> alpha
            >> omega1
            >> omega2
            >> omega3
            >> omega4;

        IloNumArray beta_a(env, num_a); //coefficient of physician's attributes
        IloNumArray beta_b(env,num_b); //coefficient of patient's characteristics
        IloNumArray time_l(env,num_l); //diagnosis time of physician l
        IloNumArray D_k(env,num_k); //population of patient group k

        IloNumArray2 gamma_ab = IloNumArray2(env,num_a); //coefficient of the untility function's last term
        for(int a=0; a<num_a; ++a){
            gamma_ab[a] = IloNumArray(env,num_b);
        }
        IloNumArray2 r_al = IloNumArray2(env,num_a); //charaticristics of physician
        for(int a=0; a<num_a; ++a){
            r_al[a] = IloNumArray(env,num_l);
        }
        IloNumArray2 s_bk = IloNumArray2(env,num_b); //charaticristics of patient
        for(int b=0; b<num_b; ++b){
            s_bk[b] = IloNumArray(env,num_k);
        }
        IloNumArray2 time_jk = IloNumArray2(env,num_j); //travel time of patient group k to outreach clinic j;
        for(int j=0; j<num_j; ++j){
            time_jk[j] = IloNumArray(env,num_k);
        }
        IloNumArray2 time_jl = IloNumArray2(env,num_j); //travel time of physician l to outreach clinic j;
        for(int j=0; j<num_j; ++j){
            time_jl[j] = IloNumArray(env,num_l);
        }
            for (a=0; a<num_a; a++)
                file >> beta_a[a];  
            for (b=0; b<num_b; b++)
                file >> beta_b[b];
            for (l=0; l<num_l; l++)
                file >> time_l[l];
            for (k=0; k<num_k; k++)
                file >> D_k[k];
            for (a=0; a<num_a; a++)
                for (b=0; b<num_b; b++)
                    file >> gamma_ab[a][b];
            for (a=0; a<num_a; a++)
                for (l=0; l<num_l; l++)
                    file >>r_al[a][l];
            for (b=0; b<num_b; b++)
                for (k=0; k<num_k; k++)
                    file >>s_bk[b][k];
            for (j=0; j<num_j; j++)
                for (k=0; k<num_k; k++)
                    file>>time_jk[j][k];
            for (j=0; j<num_j; j++)
                for (l=0; l<num_l; l++)
                    file >>time_jl[j][l];


            file.close();

        //variables
        //Binary variable for physician l is assigned to clinic j

        IloBoolVarArray2 y(env,num_j);
        for (j=0; j<num_j; j++)
        {
            y[j] = IloBoolVarArray(env, num_l);         
            for(l=0;l<num_l;++l){
                sprintf(buf,"y(%d,%d)",j,l);
                y[j][l].setName(buf);
            }
        }

        //Integar variable for capacity of physician l
        IloNumVarArray capacity(env,num_l);
        IloExpr xpr_capacity(env);
        for (l=0;l<num_l; l++)
        {
            capacity[l] = IloNumVar(env,0.0,+IloInfinity);          
            for (j=0; j<num_j; j++)
                xpr_capacity += ((T-time_jl[j][l]*y[j][l]/time_l[l]));
        }

        //Integar variable for population of patient group who is assigned to clinic j under 1st,2nd,3rd,4th choice
        //population of patient group k which is assigned to clinics j under 1st choices
        IloNumVarArray2 delta1(env,num_k);
        for (k=0; k<num_k; k++)
        {
            delta1[k] = IloNumVarArray(env, num_l, 0, +IloInfinity, ILOINT);
            for(l=0;l<num_l;++l){
                sprintf(buf,"delta1(%d,%d)",k,l);
                delta1[k][l].setName(buf);
            }
        }

        //population of patient group k which is assigned to clinics j under 2nd choices
        IloNumVarArray2 delta2(env,num_k);
        for (k=0; k<num_k; k++)
        {
            delta2[k] = IloNumVarArray(env, num_l, 0, +IloInfinity, ILOINT);
            for(l=0;l<num_l;++l){
                sprintf(buf,"delta2(%d,%d)",k,l);
                delta2[k][l].setName(buf);
            }
        }

        //population of patient group k which is assigned to clinics j under 3rd choices
        IloNumVarArray2 delta3(env,num_k);
        for (k=0; k<num_k; k++)
        {
            delta3[k] = IloNumVarArray(env, num_l, 0, +IloInfinity, ILOINT);
            for(l=0;l<num_l;++l){
                sprintf(buf,"delta3(%d,%d)",k,l);
                delta3[k][l].setName(buf);
            }
        }

        IloNumVarArray2 delta4(env,num_k);
        for (k=0; k<num_k; k++)
        {
            delta4[k] = IloNumVarArray(env, num_l, 0, +IloInfinity, ILOINT);
            for(l=0;l<num_l;++l){
                sprintf(buf,"delta4(%d,%d)",k,l);
                delta4[k][l].setName(buf);
            }
        }

        // define objective function and constraints
        IloObjective mod_obj = IloAdd(mod, IloMaximize(env));

        IloExpr obj_xpr(env);
        double utility;
        for(k=0;k<num_k;++k){   
            for(l=0;l<num_l;++l){
                IloExpr Utility(env);
                utility = 0;
                for(j=0;j<num_j;++j)
                {
                    utility += alpha * time_jk[j][k];
                    for(a=0;a<num_a;++a){
                        utility += beta_a[a] * r_al[a][l];
                        for (b=0; b<num_b; ++b)
                            utility += (gamma_ab[a][b]*s_bk[b][k]*r_al[a][l]*time_jk[j][k]);
                    }
                    for (b=0; b<num_b; ++b)
                    {
                        utility += (beta_b[b]*s_bk[b][k]*time_jk[j][k]);
                    }
                    Utility += utility * y[j][l];
                }           
                obj_xpr += omega1 * Utility * delta1[k][l];
                obj_xpr += omega2 * Utility * delta2[k][l];
                obj_xpr += omega3 * Utility * delta3[k][l];
                obj_xpr += omega4 * Utility * delta4[k][l];
                Utility.end();
            }
        }
        mod_obj.setExpr(obj_xpr);
        //cout << obj_xpr << endl;
        obj_xpr.end();

        //constraints
        IloRangeArray cst_y(env);
        //a physician can only pick one clinic.
        for (j=0; j<num_j; ++j)
        {
            IloExpr sum_y(env);
            for(l=0; l<num_l; ++l)
            {
                sum_y += (y[j][l]);
            }   
            cst_y.add(IloRange(env,1,sum_y,1));
            sum_y.end();
        }
        mod.add(cst_y); 

        //
        IloRangeArray cst_sel1(env);
        IloRangeArray cst_Del1_del1(env);
        IloRangeArray cst_Del1_y(env);
        IloRangeArray cst_Del1_Dydel1(env);
        IloIntVarArray4 Delta1(env,num_l);      
        IloExpr Del1_del1(env);
        IloExpr Del1_y(env);
        IloExpr Del1_Dydel1(env);
        double utility_mu;
        double utility_l;
        utility_mu = 0;
        utility_l = 0;
        for (k=0; k<num_k; ++k)
        {
            Delta1[k] = IloIntVarArray3(env, num_l);
            for (mu=0; mu<num_l; ++mu)
            {               
                Delta1[k][mu] = IloIntVarArray2(env, num_j);
                IloExpr sel1(env);
                for (j=0; j<num_j; ++j)
                {
                    Delta1[k][j][mu] = IloIntVarArray(env, num_l);
                    utility_mu += alpha * time_jk[j][k];
                    for(a=0;a<num_a;++a)
                    {
                        utility_mu += beta_a[a] * r_al[a][mu];
                        for (b=0; b<num_b; ++b)
                        {
                            utility_mu += (gamma_ab[a][b]*s_bk[b][k]*r_al[a][mu]*time_jk[j][k]);
                        }
                    }
                    for (b=0; b<num_b; ++b)
                    {
                        utility_mu += (beta_b[b]*s_bk[b][k]*time_jk[j][k]);
                    }
                    utility_l += alpha * time_jk[j][k];
                    for (b=0; b<num_b; ++b)
                    {
                        utility_l += (beta_b[b]*s_bk[b][k]*time_jk[j][k]);
                    }
                    for (l=0; l<num_l; ++l)
                    {
                        sprintf(buf,"Delta1(%d,%d,%d,%d)",k,j,l,mu);
                        Delta1[k][j][l][mu].setName(buf);
                        Del1_del1 = Delta1[k][j][l][mu] - delta1[k][mu];                        
                        cst_Del1_del1.add(IloRange(env,-IloInfinity,Del1_del1,0));
                        Del1_y = Delta1[k][j][l][mu] - (D_k[k] * y[j][l]);
                        cst_Del1_del1.add(IloRange(env,-IloInfinity,Del1_y,0));
                        Del1_Dydel1 = Delta1[k][j][l][mu] + D_k[k] - D_k[k] * y[j][l] + delta1[k][mu];
                        cst_Del1_Dydel1.add(IloRange(env,0,Del1_y,+IloInfinity));
                        for(a=0;a<num_a;++a)
                        {
                            utility_l += beta_a[a] * r_al[a][l];
                            for (b=0; b<num_b; ++b)
                            {
                                utility_l += (gamma_ab[a][b]*s_bk[b][k]*r_al[a][l]*time_jk[j][k]);
                            }
                        }
                        sel1 += (-Delta1[k][mu][j][l]*exp(utility_l));
                    }
                    sel1 += D_k[k]*y[j][mu]*exp(utility_mu);                    
                }
                cst_sel1.add(IloRange(env,-IloInfinity,sel1,0));
                sel1.end();
            }
        }
        mod.add(cst_Del1_del1);
        mod.add(cst_sel1);  

        cplex.exportModel("mod.lp");

    }   
    catch(IloException& ex)
    { //for any exceptions caught
        cerr << "Error : " << ex << endl;
    }
    catch(...)
    {
        cerr << "Error : Unknown exception caught!" << endl;
    }
    env.end();
    return 0;
}

感谢。

1 个答案:

答案 0 :(得分:0)

我知道这个帖子已经快5年了,但是几天前我也遇到了同样的问题。我仅在IloNumVarArray构造函数中插入了三个参数就解决了这个问题。例如,看下面的代码:

IloNumVarArray x = IloNumVarArray (env, size, 0, 1, IloNumVar::Int);

在此示例中,我将创建一个名为x的变量数组,其中x中每个变量的值从0到1,并且它们是整数。

所以也许您应该对Delta1[k][j][mu]进行初始化,例如:

Delta1[k][j][mu] = IloIntVarArray(env, num_l, lb, ub);

其中lbub是表示Delta1[k][j][mu]中每个变量的下限和上限的整数。有关提供的IloIntVarArray构造函数的更多详细信息,可以检查类IloIntVarArray的{​​{3}}。尽管该链接来自IBM ILOG CPLEX Optimization Studio V12.8,但我认为它可以正常工作。