| (ILOSTD(cerr) << "IloExtractable" ": Using empty handle" << ILOSTD(endl), ilo_stop_assert()), file c:\program files (x86)\ibm\ilog\cplex_studio_preview126\concert\include\ilconcert\iloextractable.h, line 267

问题描述 投票:0回答:1
This is the constraint(cst_sel1) I want to add.

This is the whole cpp file.

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));
}

Thanks.

I'm using Visual studio 2012 and IBM ILOG CPLEX Optimization Studio V12.6. When I run to the loop: for (l=0; l

I know that this post has almost 5 years, but I had this same problem a few days ago. I solved this problem in my code just by inserting three more parameters at the

<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>

constructor. For example, take a look at the code presented below:

#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;
}

In this example, I am creating an array of variables called

, where the values of each variable in
c++ cplex
1个回答
0
投票

, for example:IloNumVarArrayWhere

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

and x are integers representing the lower bound and upper bound of every variable in x. For more details about the presented

constructor, you can check the Delta1[k][j][mu]page

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

of class lb. Although the link is from the IBM ILOG CPLEX Optimization Studio V12.8 I think that it will work fine.ub Delta1[k][j][mu]我使用Visual studio 2012和IBM ILOG CPLEX Optimization Studio V12.6.当我运行到循环时。IloIntVarArray当我运行到循环时:出现了如下所示的错误。IloExtractable: 使用空句柄 IloIntVarArray断言失败:(getImpl() != 0)

© www.soinside.com 2019 - 2024. All rights reserved.