Decision Optimization

Decision Optimization

Delivers prescriptive analytics capabilities and decision intelligence to improve decision-making.

 View Only
  • 1.  Problems Solving a VRP Satellite Depots

    Posted Thu May 20, 2010 05:14 AM

    Originally posted by: Dmartinc


    Hello,

    I´am trying to solve a VRP with satellite depots, until now I just write my model in source code, when I try to execute it, I have this problem:

    Empty Handle in IloArray::operator[]
    Assertation failed::(_impl)||(ILOSTD(cerr)<<"Empty handle in IloArray::operator[]"<< ILOSTD(endl), ilo_stop_assert()), file: iloenv.h

    What does it mean?

    I add my source code, thanks.

    #include <ilcplex/ilocplex.h>

    ILOSTLBEGIN

    typedef IloArray<IloNumArray> NumMatrix; //numbers matrix
    typedef IloArray<IloBoolVarArray> BoolVarMatrix; //variables matrix

    // VRP-Satellite Depots

    int main(int argc, char** argv) {

    IloEnv env;
    try {

    IloInt i,j,l,m,k;

    IloModel modelo(env);

    IloInt V1,V2,Vd,Vc;

    Vc=4;
    Vd=2;

    /*ifstream archivo("data.csv");

    archivo >> Vc;
    archivo.get();
    cout << "Numero de clientes:" << Vc << endl;
    archivo >> Vd;
    archivo.get();
    cout << "Numero de almacenes:" << Vd << endl;*/

    IloNumVarArray V(env,Vd,2,IloInfinity);

    V1=Vd+1;
    V2=Vc+Vd;
    for(i=0;i<Vd;i++)
    {
    V[i]==Vc+i;
    }

    //decision variable y[i][j]
    BoolVarMatrix y(env, V1);
    for(i = 0; i < Vd; i++)
    {
    y[i] = IloBoolVarArray(env, V1);
    for(int j=0; j<V1;++j){
    stringstream nn;
    nn << "y(" << 1+i << " " <<1+j << ")";
    y[i][j].setName( nn.str().c_str() );
    }
    }
    //decision variable x[l][m][j]
    IloArray<IloArray<IloBoolVarArray>> x(env, V2);
    for(l = 0; l < V2; l++)
    {
    x[l] = IloArray<IloBoolVarArray>(env, V2);
    for(m = 0; m < V2; m++)
    {
    x[l][m] = IloBoolVarArray(env, Vd);
    for(int j=0; j<Vd;++j)
    {
    stringstream ss;
    ss << "x(" <<1+l<< " " <<1+m<< " " <<1+j<< ")";
    x[l][m][j].setName( ss.str().c_str() );
    }
    }
    }

    //leangth of first-level trip

    NumMatrix c(env, V1);

    for(i = 0; i < V1; i++)
    {
    c[i] = IloNumArray(env, V1);

    }

    c[0][0]=0.00;
    c[0][1]=2.83;
    c[0][2]=4.47;
    c[1][0]=2.83;
    c[1][1]=0.00;
    c[1][2]=2.00;
    c[2][0]=4.47;
    c[2][1]=2.00;
    c[2][2]=0.00;
    //Creates matrix of first-level trip

    NumMatrix g(env, V2);

    for(l = 0; l < V2; l++)
    {
    g[l] = IloNumArray(env, V2);

    }

    g[0][0]=0.00;
    g[0][1]=2.00;
    g[0][2]=1.00;
    g[0][3]=4.00;
    g[0][4]=5.00;
    g[0][5]=3.00;
    g[1][0]=2.00;
    g[1][1]=0.00;
    g[1][2]=2.00;
    g[1][3]=4.00;
    g[1][4]=2.00;
    g[1][5]=6.00;
    g[2][0]=1.00;
    g[2][1]=2.00;
    g[2][2]=0.00;
    g[2][3]=2.00;
    g[2][4]=6.00;
    g[2][5]=4.50;
    g[3][0]=4.00;
    g[3][1]=4.00;
    g[3][2]=2.00;
    g[3][3]=0.00;
    g[3][4]=5.00;
    g[3][5]=6.00;
    g[4][0]=5.00;
    g[4][1]=2.00;
    g[4][2]=6.00;
    g[4][3]=5.00;
    g[4][4]=0.00;
    g[4][5]=5.50;
    g[5][0]=3.00;
    g[5][1]=6.00;
    g[5][2]=4.50;
    g[5][3]=6.00;
    g[5][4]=5.50;
    g[5][5]=0.00;
    //sets leangth matrix for first-level trip

    /* for (int i=0;i<V1;i++)
    {
    for(int j=0;j<V1;j++)
    {
    archivo >> c[i][j];
    archivo.get();
    }
    }*/

    cout << "vector c:" << c << endl <<endl;

    //sets leangth matrix for second-level trip

    /* ifstream archivo1("data1.csv");
    for (int l=0;l<V2;l++)
    {
    for(int m=0;m<V2;m++)
    {
    archivo >> g[l][m];
    archivo.get();
    }
    }*/

    cout << "vector g:" << g << endl <<endl;
    //Objetive Function

    IloExpr fo1(env);
    IloExpr fo2(env);
    IloExpr fo(env);

    for ( i = 0; i < V1; ++i)
    {
    for ( j = 0; j < V1; ++j)
    fo1 += c[i][j]*y[i][j];
    }
    for ( j = 0; j < Vd; ++j)
    {
    for ( l = 0; j < V2; ++l)
    {
    for ( m = 0; m < V2; ++m)
    fo2 += g[l][m]*x[l][m][j];
    }
    }
    fo=fo1+fo2;
    modelo.add(IloMinimize(env, fo));
    fo1.end();
    fo2.end();
    fo.end();
    //A trailer point is visited at most once in the first-level trip

    for(j = 0; j < Vd; ++j)
    {
    IloExpr Expr0(env);
    for(i = 0; i < V1; ++i)
    {
    if(i!=j)
    Expr0 = Expr0 + y[i][j];
    }
    modelo.add(Expr0 <= 1 );
    Expr0.end();
    }

    for(j = 0; j < Vd; ++j)
    {
    IloExpr Expr1(env);
    for(i = 0; i < V1; ++i)
    {
    if(i!=j)
    Expr1 = Expr1 + y[j][i];
    }
    modelo.add(Expr1 <= 1 );
    Expr1.end();
    }

    //Connectivity constraints for the first-level trip
    for(j = 0; j < Vd; j++)
    {
    IloExpr Expr2(env);
    IloExpr Expr3(env);
    for(i = 0; i < V1; ++i)
    {
    Expr2 = Expr2 + y[i][j];
    }
    for(k = 0; k < V1; ++k)
    {
    Expr3 = Expr3 + y[j][k];
    }
    modelo.add(Expr2 == Expr3);
    Expr2.end();
    Expr3.end();
    }

    //the first level trip departs and ends at the main depot

    IloExpr Expr4(env);
    for(i = 0; i < Vd; ++i)
    {
    Expr4 = Expr4 + y[i][0];
    }
    modelo.add(Expr4 == 1 );
    Expr4.end();

    IloExpr Expr5(env);
    for(j = 0; j < Vd; ++j)
    {
    Expr5 = Expr5 + y[0][j];
    }
    modelo.add(Expr5 == 1 );
    Expr5.end();

    //Subtour elimination
    /*for( l = 0; l < ; ++l)
    for(i = 0; i < V[l]; ++i)
    {
    IloExpr Expr6(env);
    for(j = 0; j < V[l]; ++j)
    {
    Expr6 = Expr6 + y[i][j];
    }
    modelo.add(Expr6 <= V[l]-1 );
    Expr6.end();
    }*/

    //Second level trips departs only from trailer points visited in the first level

    for ( j = 0; j < Vd; ++j)
    {
    IloExpr Expr7(env);
    IloExpr Expr8(env);
    for ( i = 0; i < V1; ++j)
    {
    Expr7 = Expr7 + y[i][j];
    }
    for ( l = 0; l < Vd; ++l)
    {
    for ( m = 0; m < Vd; ++m)
    Expr8 = x[l][m][j];
    }
    modelo.add(Expr8 <= Expr7);
    Expr7.end();
    Expr8.end();

    }
    //Each constumer must be visited exactly once

    for ( m = 0; m < Vc; ++m)
    {
    IloExpr Expr9(env);
    for ( l = 0; l < V2; ++l)
    {
    for ( j = 0; j < Vd; ++j)
    Expr9 = Expr9 + x[l][m][j];
    }
    modelo.add(Expr9 == 1);
    Expr9.end();

    }

    //Guarantee the connectivity of second level trips
    for( j = 0; j < Vd; j++)
    {
    IloExpr Expr10(env);
    IloExpr Expr11(env);
    for(m = 0; m < Vc; ++m)
    {
    for(l = 0; l < V2; ++l)
    Expr10 = Expr10 + x[l][m][j];
    for(k = 0; k < V2; ++k)
    Expr11 = Expr11 + x[m][k][j];
    }
    modelo.add(Expr10 - Expr11 == 0);
    Expr10.end();
    Expr11.end();
    }

    //All second level trips departing from trailer point return to it
    for(j = 0; j < Vd; j++)
    {
    IloExpr Expr12(env);
    IloExpr Expr13(env);
    for(l = 0; l < Vc; ++l)
    for(k = 0; k < Vc; ++k)
    Expr12 = Expr12 + x[j][l][j];
    Expr13 = Expr13 + x[k][j][j];
    modelo.add(Expr12 - Expr13 == 0);
    Expr12.end();
    Expr13.end();
    }

    //Prevents subtours in second level trips
    /* for( i=0; i< ; i++)
    for(j = 0; j < Vd; j++)
    {
    IloExpr Expr14(env);
    for(l = 0; l < V[i]; ++l)
    for(m = 0; m < V[i]; ++m)
    Expr14 = Expr14 + x[l][m][j];
    modelo.add(Expr14 <= V[i] - s*V[i]);
    Expr14.end();
    }
    */

    IloCplex cplex(env);
    cplex.extract(modelo);
    cplex.exportModel("VRPSD.lp");
    cplex.solve();

    if ( !cplex.solve() ) {
    env.error() << "It couldn´t solve it :-(" << endl;
    throw ( -1 );
    }

    env.out() << "Is it optimal ? = " << cplex.getStatus() << endl;
    env.out() << "Time solve = " << env.getTime() << endl;
    env.out() << " - Solution first level trip:" << endl;
    for(i = 0; i < V1; i++)
    {
    env.out() <<" "<<i<<":";

    for(j = 0; j < V1; j++)
    {
    env.out() <<cplex.getValue(y[i][j])<<"\t";
    }
    env.out() << endl;
    }
    env.out() << " - Solution second level trip:" << endl;
    for(j = 0; j < Vd; j++)
    {
    env.out() <<" "<<i<<":";
    for(l = 0; l < V2; l++)
    for(m = 0; m < V2; m++)
    env.out() <<cplex.getValue(x[l][m][j])<<"\t";
    env.out() << endl;
    }

    env.out() << " Minimal leangth = " << cplex.getObjValue() << endl;

    }
    catch (IloException& e) {
    cerr << "ERROR: " << e.getMessage() << endl;
    }
    catch (...) {
    cerr << "Error" << endl;
    }
    env.end();

    char c = getchar();
    return 0;
    }
    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: Problems Solving a VRP Satellite Depots

    Posted Wed May 26, 2010 02:12 AM

    Originally posted by: SystemAdmin


    Before you post lengthy code like yours please try to condense it to isolate the problem.
    Your problem is your usage of the matrix 'y'. You initialize this matrix to be a Vd x V1 matrix
    BoolVarMatrix y(env, V1);
          for(i = 0; i < Vd; i++)
          {
             y[i] = IloBoolVarArray(env, V1);
             for(int j=0; j<V1;++j){
                stringstream nn;
                nn << "y(" << 1+i << " " <<1+j << ")";
                y[i][j].setName( nn.str().c_str() );
             }
          }
    

    but use it as if it was a V1 x V1 matrix:
    for ( i = 0; i < V1; ++i)
          {
             for ( j = 0; j < V1; ++j)
                fo1 += c[i][j]*y[i][j];
          }
    

    Since V1=Vd+1 in your code this means that you eventually access a non-existing element in y which then throws the assertion.
    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: Problems Solving a VRP Satellite Depots

    Posted Wed May 26, 2010 06:20 AM

    Originally posted by: Dmartinc


    Firstly, thank you for your answer.

    I was thinking more deeply in the problem and my conclusion was that, I was trying to access in array without non-existing elements. I´am going to fix it, and I hope to run a first instance of my VRP.
    Thank you
    #CPLEXOptimizers
    #DecisionOptimization


  • 4.  Re: Problems Solving a VRP Satellite Depots

    Posted Thu July 09, 2015 07:56 AM

    Originally posted by: lorafrance


    fds


    #CPLEXOptimizers
    #DecisionOptimization