Decision Optimization

Decision Optimization

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

 View Only
  • 1.  Scripting runtime error: Undefined methode 'add'

    Posted Sat November 30, 2019 07:49 AM

    Originally posted by: Mathsg


    Hello seniors and IBM team,

     

    I am getting an error of (Scripting runtime error: Undefined methode 'add') while optimizing the network planing problem through column generation method. Master problem has been relaxed for the given set of lighpaths for a flow between source to distination while pricing problem give me the result 6 which seems to be not correct as pricing problem should display the results with negative number if there is a lightpaths which can improve the master problem but PP give me positive result while in declaring next iteration for the MP OPL give me the mentioned above error. Kindly guide me how to write next iteration part for master problem to put the value of sub problem and also how to get the value of sub problem with the negative values. I am looking forward to hearing from you. thank you


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 2.  Re: Scripting runtime error: Undefined methode 'add'



  • 3.  Re: Scripting runtime error: Undefined methode 'add'

    Posted Sun December 01, 2019 09:37 PM

    Originally posted by: Mathsg


    Hi Alex, 

    Thank you so much for your kind suggestions. Although the problem seems to be easy but i am unable to fix it. My idea is to consider subset of lightpaths and relax the master problem with it and then sub problem generate new lightpaths which can improve the objective of master problem. i am unable to produce the next iteration, as i guess these lightpaths were generated by flow conserveration constrain in the master problem. sub problem generated new lightpaths which we have added to master problem again but OPL give me an error here. Kindly help me in this regards. i am looking forward to hearing from you. thank you

     

    int nbnodes = ...;                 /* Total number of nodes*/
    range nodes =  1..nbnodes;        /*set of  all ECs*/

    int totallc = ...;                /*total number of LC*/
    int LC_Cost = ...;                /*total number of different cost of LC*/
    int LC_Capacties = ...; 

    tuple lincard{                     /*Tuple for LC*/
      key int id;                    /*S.N of LC starting from 0 to....*/
      int k_lc;                      /*different capacties of LC*/
      int costtype_lc;              /*different costs of LC*/
    }
    {lincard} Lincards = ...;         /*Taking tupple data of LC from data file*/

    int totalec = ...;                 /*total number of EC*/
    int EC_Cost = ...;              /*total number of different cost of EC*/
    int EC_Capacties = ...;         /* total number of different capacties fo EC*/

    tuple eccard{                     /*Tuple for EC*/
      key int id;                    /*S.N of EC starting from 0 to....*/
      int k_ec;                        /*different capacties of EC*/
      int costtype_ec;                 /*different cost of EC*/
    }  
    {eccard} ECcards = ...;         /*Taking tupple data of EC from data file*/

    int nmbr_routes = ...;             /*Totall number of routes*/
    range routes = 1..nmbr_routes;    /*range of routes*/
    int nmberflow = ...;            /*Totall number of flows*/
    int im_flow = ...;                /*important flows*/
    //{string} flow_type = {"ip_f", "np_f"};
    tuple flow{                     /*Tuple for flows*/
    key int id;                        /*S.N of flows starting from 0 to....*/
    int src;                        /*Source node of flows*/
    int dest;                        /*destination node of flows*/
    float flow_bw;                    /*BW of flows*/
    int khan;                        /*this int take two values, either 1, if flow is important or 0 if flow is non-important*/
    }
    {flow} flows = ...;                /*Taking tupple data of flows from data file*/
    float Duals[routes] = ...;
    int hope_count[routes][nodes][nodes] = ...;            /*hope count between source and destination*/
    int a[Lincards] = ...;                                 /*Set upper limit of LC in data file*/
    int b[ECcards] = ...;                                 /*Set upper limit of EC in data file*/
    int q[nodes][nodes] = ...;                            /* the boolean value that equals to 1 if nodes u and v are from different zones, and 0 otherwise*/
    int link [nodes][nodes] = ...;                        /*link between source and dist*/
    dvar boolean k[flows][nodes][nodes];                /*k=1, if flow "i" get routed b/w node u to v and "0", otherwise*/
    dvar boolean lemda[flows][routes][nodes][nodes];     /*lemda=1 if "jth" lighpath is used and "0", otherwise*/
    dvar boolean x[flows][Lincards][nodes][nodes];         /*x=1, if nth LC is used, and 0, otherwise*/
    dvar boolean y[flows][ECcards][nodes][nodes];          /*y=1, if nth EC is used, and 0, otherwise*/
    //dvar boolean w[flows][flows][Lincards][nodes][nodes];/*w=1, if different flows "i"and"m" share the nth LC, and 0, otherwise*/ 
    //dvar boolean z[flows][flows][ECcards][nodes][nodes]; /*z=1, if different flows "i"and"m" share the nth EC, and 0, otherwise*/ 
    dvar int n1[Lincards][nodes];                         /*n1=integer values indicates the total number of used LC at node v*/
    dvar int n2[ECcards][nodes];                        /*n2=integer values indicates the total number of used EC at node v*/
    dvar boolean f[Lincards][nodes][nodes];                /*f=1, if nth LC used for transmission over u to v, and 0 otherwise*/
    dvar boolean g[ECcards][nodes][nodes];                /*g=1, if nth EC used for transmission over u to v, and 0 otherwise*/
                                    


    minimize((sum(k in Lincards, u in nodes)(n1[k][u]*k.costtype_lc*2))+(sum(k in ECcards, u in nodes)(n2[k][u]*k.costtype_ec*2))+(sum(i in flows, j in routes,  u in nodes, v in nodes)lemda[i][j][u][v]*hope_count[j][u][v]*i.flow_bw));
    subject to{
    forall ( i in flows, u in nodes, v in nodes)
      Const1:
    k[i][u][v]<=link[u][v];
        
    forall(i in flows, u in nodes: u==i.src)
          Const2:
          sum(v in nodes)(k[i][u][v]-k[i][v][u])==1;
    forall(i in flows, u in nodes: u==i.dest)
          Const3:
          sum(v in nodes)(k[i][u][v]-k[i][v][u])==-1;
    forall(i in flows, u in nodes: u!=i.src && u!=i.dest)
          Const4:
          sum(v in nodes)(k[i][u][v]-k[i][v][u])==0; 
    forall ( i in flows,  u in nodes, v in nodes)
         Const5:
         sum(j in routes)lemda[i][j][u][v]== k[i][u][v];
    forall (i in flows, j in routes, u in nodes, v in nodes)
          Const6:
           sum(p in Lincards)x[i][p][u][v]==lemda[i][j][u][v];
    forall (i in flows, j in routes, u in nodes, v in nodes: 1==i.khan)
           Const7:
           sum(p in ECcards)y[i][p][u][v]==lemda[i][j][u][v]*q[u][v];
    forall (p in Lincards, u in nodes, v in nodes)
           Const8:
           sum(i in flows) x[i][p][u][v]*i.flow_bw<=p.k_lc;
         
    forall (p in ECcards, u in nodes, v in nodes)
           Const9:
           sum(i in flows) y[i][p][u][v]*i.flow_bw<=p.k_ec;
         
    forall (u in nodes, p in Lincards)
              Const19:
               sum(s in Lincards, v in nodes:s==p )f[s][u][v]<=n1[p][u];
    forall (p in ECcards, u in nodes)
             Const20:
             sum(s in ECcards, v in nodes:s==p)g[s][u][v]<=n2[p][u];
    forall ( i in flows, p in Lincards, u in nodes, v in nodes)  
            Const21:
            x[i][p][u][v]<= f[p][u][v];
    forall ( i in flows, p in ECcards, u in nodes, v in nodes) 
        Const22:
         y[i][p][u][v]<= g[p][u][v];
    forall (u in nodes, p in Lincards)
      Const23:
        n1[p][u]<=a[p];
        forall (u in nodes, p in ECcards)
      Const24:
        n2[p][u]<=b[p];
    }
    // dual values used to fill in the sub model.
    execute FillDuals {
      for(j in routes) {
         Duals[j] = Const5[j].dual;
       
       
      }
    }  
    main {
         
      var status = 0;
      thisOplModel.generate();
      
      // This is an epsilon value to check if reduced cost is strictly negative
      var RC_EPS = 1.0e-6;

    // Retrieving model definition, engine and data elements from this OPL model to reuse them later
      var masterDef = thisOplModel.modelDefinition;
      var masterCplex = cplex;
      var masterData = thisOplModel.dataElements;
      
      // Creating the master-model
        var masterOpl = new IloOplModel(masterDef, masterCplex);
        masterOpl.addDataSource(masterData);
        masterOpl.generate();
       masterOpl.convertAllIntVars();
      
      // Preparing sub-model source, definition and engine 
      var subSource = new IloOplModelSource("CG_2_PP.mod");
      var subDef = new IloOplModelDefinition(subSource);
      var subCplex = new IloCplex();

      var best;
      var curr = Infinity;

      while ( best != curr ) {
        best = curr;      
        writeln("Solve master.");
        if ( masterCplex.solve() ) {
          curr = masterCplex.getObjValue();
          writeln();
          writeln("OBJECTIVE: ",curr);
        } 
        else {
          writeln("No solution!");
          masterOpl.end();
          break;
        }
      // Ceating the sub model
    var subOpl = new IloOplModel(subDef, subCplex);

    // Using data elements from the master model.
    var subData = new IloOplDataElements();
      subData.nmbr_routes = masterOpl.nmbr_routes;
      subData.nbnodes = masterOpl.nbnodes;
      subData.flows = masterOpl.flows;
      subData.Lincards = masterOpl.Lincards;
      subData.hope_count = masterOpl.hope_count;
      subData.link = masterOpl.link;
      subData.q = masterOpl.q;
      subData.ECcards = masterOpl.ECcards;
      subData.Duals = masterOpl.Duals;
        
       // for(var j in masterOpl.routes) {
       //   subData.Duals[j] = masterOpl.Const6[j].dual;
       //    subData.Duals[j] = masterOpl.Const7[j].dual;
      //  }
    // Creating the sub model
     
        
        subOpl.addDataSource(subData);
        subOpl.generate();
     
       // Previous master model is not needed anymore.
         masterOpl.end();
     
        writeln("Solve sub.");
        if ( subCplex.solve()&&
        subCplex.getObjValue() >= -RC_EPS ) {
          writeln();
          writeln("Sub OBJECTIVE: ",subCplex.getObjValue());
        }
        else {
          writeln("No solution!");
         subData.end();
          subOpl.end();
          break;
        }
        // Prepare the next iteration:
        //masterData.Patterns.add(masterData.Patterns.size,1,subOpl.Use.solutionValue);
        for(j in masterData.hope_count){
        masterData.hope_count.add(masterData.hope_count[j][1][1]); 
          
         }
        //masterData.Cost_of_Route.add(subOpl.getObjValue());
        
      masterOpl = new IloOplModel(masterDef,masterCplex);
      masterOpl.addDataSource(masterData);
      masterOpl.generate();   
       
       subData.end();
       subOpl.end();
    }  
     // writeln("Relaxed model search end.");

      

    //  writeln("Solve integer master.");  
     // if ( masterCplex.solve() ) {
       // writeln();
      //  writeln("OBJECTIVE: ",masterCplex.getObjValue());
        if (Math.abs(masterCplex.getObjValue() - 30.9)>=0.0001) {
          writeln("Unexpected objective value");
          status = -1;
          
        }
        /*for(i in  masterData.Patterns) {
          if (masterOpl.Cut[i].solutionValue > 0) {
            writeln("Pattern : ", i, " used ", masterOpl.Cut[i].solutionValue << " times");
          }
        }*/
      
      subDef.end();
      subCplex.end();
      subSource.end();
      
      status;
    }
      


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 4.  Re: Scripting runtime error: Undefined methode 'add'



  • 5.  Re: Scripting runtime error: Undefined methode 'add'

    Posted Mon December 02, 2019 06:45 AM

    Originally posted by: Mathsg


    Hi Alex, 

    So nice of you and thank you so much for your kind and prompt response. I have read your refrance several time but i am not getting the point exactly. i am very sorry to take much of your precious time but i will be very thankful if you help me to fix the problem. I am getting an errors two time in my scripting part and both the time when i am going to pass the value from master model to sub model and from sub model again to the next iteration. I have attached my both the model (i.e., Master and sub model) and data file. Kindly fix the errors. thank you so much


    #DecisionOptimization
    #OPLusingCPLEXOptimizer