Topic
  • 4 replies
  • Latest Post - ‏2014-03-20T08:50:40Z by xoftmade
saeed62s
saeed62s
22 Posts

Pinned topic From CPLEX to Eclipse

‏2014-02-22T03:46:46Z |

I wrote the code (CP) in ILOG CPLEX and now I want to convert it to JAVA which I am new in it. my first problem is the structure of input data, i don't know how should i define it, since in ILOG environment all my input variable and parameters were defined in Excel sheet, would you please help me. I have attached my code and my input data.

Thanks.

 

 

Attachments

  • ChrisBr
    ChrisBr
    74 Posts

    Re: From CPLEX to Eclipse

    ‏2014-02-28T18:12:43Z  

    Hello Mahmood,

    I suggest you to have look at the distributed samples in
          cpoptimizer/examples/src/java
    You will find how to declare your data, and especially those where there are "int[][]" which can be compared to your different uses of 2D maps in OPL (int Q[routes][order], int V[routes][stops], ...)

    For reading excel file, you can use a free dedicated package or convert your excel file into a CSV file.

    Below is how your model should look like (not in details).

    import ilog.cp.*;
    import ilog.concert.*;
    import java.io.*;
    
    public class Sample {
      public static int max(int[] x) {
        int m = x[0];
        for (int i = 1; i < x.length; i++) {
            if (m < x[i])
                m = x[i];
        }
        return m;
      }
        
      public static void main(String[] args) throws IOException {
        try {
          IloCP cp = new IloCP();
          int x, u, n, i, s1, s2;
          int f= getFromSheet();
          int maxholdtime= getFromSheet();
          int nbroutes= getFromSheet();
          int nbphysicalstops= getFromSheet();
          //int T[routes]=...;
          int T[]= new int[nbroutes];
          for (x = 0; x < nbroutes; x++)
            T[x] = getFromSheet();
          // same for k, g, lasttransfer, h, m
          //int c[routes][stops]=...;
          int[][] c = new int[nbroutes][];
          for (x = 0; x < nbroutes; x++) {
            c[x] =  new int[nbphysicalstops];
            for (n = 0; n < nbphysicalstops; n++)
              c[x][n] = getFromSheet();
          }
          //float e[routes][stops]=...;
          double[][] e = new double[nbroutes][];
          for (x = 0; x < nbroutes; x++) {
            e[x] =  new double[nbphysicalstops];
            for (n = 0; n < nbphysicalstops; n++)
              e[x][n] = getFromSheet();
          }
          // same for other 2D data (l,d,Q,V, p)
          //range order=1..max(i in routes) m[i]+1;
          int nborders = max(m)+1;
          //int t[x in routes][u in routes][n in stops]=0;
          int[][][] t = new int[nbroutes][][];
          for (x = 0; x < nbroutes; x++) {
            t[x] =  new int[nbroutes][];
            for (u = 0; u < nbroutes; u++) {
              t[x][u] =  new int[nbphysicalstops];
              for (n = 0; n < nbphysicalstops; n++)
                t[x][u][n] = 0;
            }
          }
          //t[1][2][24]=9;
          t[0][1][23]=9;
          //t[2][1][24]=1;
          t[1][0][23]=1;
    
          int[][][] Y = new int[nbroutes][][];
          for (x = 0; x < nbroutes; x++) {
            Y[x] =  new int[nbroutes][];
            for (u = 0; u < nbroutes; u++) {
              Y[x][u] =  new int[nbphysicalstops];
              for (n = 0; n < nbphysicalstops; n++)
                Y[x][u][n] = 0;
            }
          }
          for (x = 0; x < nbroutes; x++)
            for (u = 0; u < nbroutes; u++)
              for (n = 0; n < nbphysicalstops; n++)
                if (t[x][u][n]+t[u][x][n] >= 1) {
                  s1=-g[u]+d[u][n];
                  for (i = k[u]; i <= V[u][n]; i++)
                    s1 += c[u][Q[u][i-1]];
                  s2=-g[x];
                  for (i = k[x]; i <= V[x][n]; i++)
                    s2 += c[x][Q[x][i-1]];              
                  if (s1 <= s2)
                    Y[x][u][n] = 1;
                }
          // same for alpha
    
          //dexpr float h1[x in routes][n in stops] = sum(u in routes) ((1-Y[u][x][n])*t[u][x][n]-t[x][u][n]);
          int[][] h1 = new int[nbroutes][];
          for (x = 0; x < nbroutes; x++) {
            h1[x] =  new int[nbphysicalstops];
            for (n = 0; n < nbphysicalstops; n++) {
              h1[x][n] = 0;
              h1[x][n] += ((1-Y[u][x][n])*t[u][x][n]-t[x][u][n]);
            }
          }
    
          //Variables
          //dvar boolean Z[routes][routes][stops];
          IloIntVar[][][] Z = new IloIntVar[nbroutes][][];
          for (x = 0; x < nbroutes; x++) {
            Z[x] =  new IloIntVar[nbroutes][];
            for (u = 0; u < nbroutes; u++) {
              Z[x][u] =  cp.intVarArray(nbphysicalstops, 0, 1);
          // same for dvar boolean S[routes][stops];
          //dvar int W[routes][stops] in holdoptions;
          IloIntVar[][] W = new IloIntVar[nbroutes][];
          for (x = 0; x < nbroutes; x++)
            W[x] = cp.intVarArray(nbroutes, 0, maxholdtime);
    
          //Decision Expressions
          // int-expr (st3, transfer)
          // dexpr float st3[x in routes][n in stops] =
          //   sum(i in k[x]..V[x][n]-1 : V[x][n]>1)
          //       ( (S[x][Q[x][i]] * d[x][Q[x][i]]) - W[x][i]);
          IloIntExpr[][] st3 = new IloIntExpr[nbroutes][];
          for (x = 0; x < nbroutes; x++) {
            st3[x] =  new IloIntExpr[nbphysicalstops];
            for (n = 0; n < nbphysicalstops; n++) {
              IloIntExpr exp = cp.intExpr();
              for (i = k[x]; i < V[x][n]; i++) {
                if (V[x][n]>1)
                  exp = cp.sum(exp,
                               cp.diff(cp.prod(S[x][Q[x][i]], d[x][Q[x][i]]),
                                       W[x][i]) );
              }
              st3[x][n] = exp;
            }
          }
    
          // same for float-expr (sh2, hold, skip)
          //   use IloNumExpr
    
          //Objective
          //minimize sum(x in routes, n in stops)
          //    (hold[x][n] + skip [x][n] + transfer[x][n]);
          IloNumExpr objExp = cp.numExpr();
          for (x = 0; x < nbroutes; x++)
            for (n = 0; n < nbphysicalstops; n++)
              objExp = cp.sum(objExp,
                              cp.sum(hold[x][n], skip[x][n], transfer[x][n]));
          IloObjective obj = cp.minimize(objExp);
          cp.add(obj);
    
          //Constraints
          //forall ( x in routes , n in stops )
          //  S[x][n]*(sum(u in routes) (t[x][u][n] + t[u][x][n])) == 0;
          for (x = 0; x < nbroutes; x++)
            for (n = 0; n < nbphysicalstops; n++) {
              s1 = 0;
              for (u = 0; u < nbroutes; u++)
                s1 += (t[x][u][n] + t[u][x][n]);
              cp.add(cp.eq(cp.prod(S[x][n], s1),0));
            }
    
          // same for other constraints
        }
    }
    


    I hope that helps,

    Chris.
     

  • saeed62s
    saeed62s
    22 Posts

    Re: From CPLEX to Eclipse

    ‏2014-03-16T22:21:31Z  
    • ChrisBr
    • ‏2014-02-28T18:12:43Z

    Hello Mahmood,

    I suggest you to have look at the distributed samples in
          cpoptimizer/examples/src/java
    You will find how to declare your data, and especially those where there are "int[][]" which can be compared to your different uses of 2D maps in OPL (int Q[routes][order], int V[routes][stops], ...)

    For reading excel file, you can use a free dedicated package or convert your excel file into a CSV file.

    Below is how your model should look like (not in details).

    <pre class="html dw" data-editor-lang="js" data-pbcklang="html" dir="ltr">import ilog.cp.*; import ilog.concert.*; import java.io.*; public class Sample { public static int max(int[] x) { int m = x[0]; for (int i = 1; i < x.length; i++) { if (m < x[i]) m = x[i]; } return m; } public static void main(String[] args) throws IOException { try { IloCP cp = new IloCP(); int x, u, n, i, s1, s2; int f= getFromSheet(); int maxholdtime= getFromSheet(); int nbroutes= getFromSheet(); int nbphysicalstops= getFromSheet(); //int T[routes]=...; int T[]= new int[nbroutes]; for (x = 0; x < nbroutes; x++) T[x] = getFromSheet(); // same for k, g, lasttransfer, h, m //int c[routes][stops]=...; int[][] c = new int[nbroutes][]; for (x = 0; x < nbroutes; x++) { c[x] = new int[nbphysicalstops]; for (n = 0; n < nbphysicalstops; n++) c[x][n] = getFromSheet(); } //float e[routes][stops]=...; double[][] e = new double[nbroutes][]; for (x = 0; x < nbroutes; x++) { e[x] = new double[nbphysicalstops]; for (n = 0; n < nbphysicalstops; n++) e[x][n] = getFromSheet(); } // same for other 2D data (l,d,Q,V, p) //range order=1..max(i in routes) m[i]+1; int nborders = max(m)+1; //int t[x in routes][u in routes][n in stops]=0; int[][][] t = new int[nbroutes][][]; for (x = 0; x < nbroutes; x++) { t[x] = new int[nbroutes][]; for (u = 0; u < nbroutes; u++) { t[x][u] = new int[nbphysicalstops]; for (n = 0; n < nbphysicalstops; n++) t[x][u][n] = 0; } } //t[1][2][24]=9; t[0][1][23]=9; //t[2][1][24]=1; t[1][0][23]=1; int[][][] Y = new int[nbroutes][][]; for (x = 0; x < nbroutes; x++) { Y[x] = new int[nbroutes][]; for (u = 0; u < nbroutes; u++) { Y[x][u] = new int[nbphysicalstops]; for (n = 0; n < nbphysicalstops; n++) Y[x][u][n] = 0; } } for (x = 0; x < nbroutes; x++) for (u = 0; u < nbroutes; u++) for (n = 0; n < nbphysicalstops; n++) if (t[x][u][n]+t[u][x][n] >= 1) { s1=-g[u]+d[u][n]; for (i = k[u]; i <= V[u][n]; i++) s1 += c[u][Q[u][i-1]]; s2=-g[x]; for (i = k[x]; i <= V[x][n]; i++) s2 += c[x][Q[x][i-1]]; if (s1 <= s2) Y[x][u][n] = 1; } // same for alpha //dexpr float h1[x in routes][n in stops] = sum(u in routes) ((1-Y[u][x][n])*t[u][x][n]-t[x][u][n]); int[][] h1 = new int[nbroutes][]; for (x = 0; x < nbroutes; x++) { h1[x] = new int[nbphysicalstops]; for (n = 0; n < nbphysicalstops; n++) { h1[x][n] = 0; h1[x][n] += ((1-Y[u][x][n])*t[u][x][n]-t[x][u][n]); } } //Variables //dvar boolean Z[routes][routes][stops]; IloIntVar[][][] Z = new IloIntVar[nbroutes][][]; for (x = 0; x < nbroutes; x++) { Z[x] = new IloIntVar[nbroutes][]; for (u = 0; u < nbroutes; u++) { Z[x][u] = cp.intVarArray(nbphysicalstops, 0, 1); // same for dvar boolean S[routes][stops]; //dvar int W[routes][stops] in holdoptions; IloIntVar[][] W = new IloIntVar[nbroutes][]; for (x = 0; x < nbroutes; x++) W[x] = cp.intVarArray(nbroutes, 0, maxholdtime); //Decision Expressions // int-expr (st3, transfer) // dexpr float st3[x in routes][n in stops] = // sum(i in k[x]..V[x][n]-1 : V[x][n]>1) // ( (S[x][Q[x][i]] * d[x][Q[x][i]]) - W[x][i]); IloIntExpr[][] st3 = new IloIntExpr[nbroutes][]; for (x = 0; x < nbroutes; x++) { st3[x] = new IloIntExpr[nbphysicalstops]; for (n = 0; n < nbphysicalstops; n++) { IloIntExpr exp = cp.intExpr(); for (i = k[x]; i < V[x][n]; i++) { if (V[x][n]>1) exp = cp.sum(exp, cp.diff(cp.prod(S[x][Q[x][i]], d[x][Q[x][i]]), W[x][i]) ); } st3[x][n] = exp; } } // same for float-expr (sh2, hold, skip) // use IloNumExpr //Objective //minimize sum(x in routes, n in stops) // (hold[x][n] + skip [x][n] + transfer[x][n]); IloNumExpr objExp = cp.numExpr(); for (x = 0; x < nbroutes; x++) for (n = 0; n < nbphysicalstops; n++) objExp = cp.sum(objExp, cp.sum(hold[x][n], skip[x][n], transfer[x][n])); IloObjective obj = cp.minimize(objExp); cp.add(obj); //Constraints //forall ( x in routes , n in stops ) // S[x][n]*(sum(u in routes) (t[x][u][n] + t[u][x][n])) == 0; for (x = 0; x < nbroutes; x++) for (n = 0; n < nbphysicalstops; n++) { s1 = 0; for (u = 0; u < nbroutes; u++) s1 += (t[x][u][n] + t[u][x][n]); cp.add(cp.eq(cp.prod(S[x][n], s1),0)); } // same for other constraints } } </pre>


    I hope that helps,

    Chris.
     

    Thanks Chris

    I have started to do it, hope i can make it,

    I was wondering in the ILOG invironment I have used float , here you used double, is it OK?

    And the other question is how to use the following command to get data, i mean how to address this to read the value from my excel sheet, and how address the excel sheet location.

     "int maxholdtime= getFromSheet();"

     Cheers,

     

    Cheers,

  • ChrisBr
    ChrisBr
    74 Posts

    Re: From CPLEX to Eclipse

    ‏2014-03-17T16:44:18Z  
    • saeed62s
    • ‏2014-03-16T22:21:31Z

    Thanks Chris

    I have started to do it, hope i can make it,

    I was wondering in the ILOG invironment I have used float , here you used double, is it OK?

    And the other question is how to use the following command to get data, i mean how to address this to read the value from my excel sheet, and how address the excel sheet location.

     "int maxholdtime= getFromSheet();"

     Cheers,

     

    Cheers,

    Hello Mahmood,

    Since CPOptimizer and OPL JAVA APIs use double, it's safer to use double to avoid any problem. Moreover using float where double are expected could cause compile errors.

    About the way to read your excel sheet, you may have a look at Apache POI for example (here).

    Regards,

    Chris.
     

  • xoftmade
    xoftmade
    1 Post

    Re: From CPLEX to Eclipse

    ‏2014-03-20T08:50:40Z  

    Xoftmade offers shared, business, dedicated web hosting solutions, web development and domain registration services in Pakistan. We offer lowest prices for hosting and website development.

    vps hosting