Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  Scripting runtime error: cannot convert to a number, "[a IloNumVar]".

    Posted 10/02/14 08:27 AM

    Originally posted by: saeeds


    Hi,

    I am gonna write a code in OPL for lagrangian relaxation. anyway, I defined some two dimensional arrays in my main script and used them. When I run the project, this error occurs:

    Scripting runtime error: cannot convert to a number, "[a IloNumVar]".

    The 2-D arrays I defined are:

     var xn = new Array(12);
      for(var i = 0; i < 12; i++){
        xn[i] = new Array(125);
      }
     
      
       var xntemp = new Array(12);
      for(var i = 0; i < 12; i++){
        xntemp[i]=new Array(125);
      }

    And the line which faces with errors is:

    for (i in thisOplModel.product){
          for (t in thisOplModel.period){
        xn[i][t]=alpha*(m2.X[i][t])+(1-alpha)*(xntemp[i][t]);
         
        }
        }

    m2.X[i][t] are the variables I used in my main model. After generation m2, I used X[i][t] values in above equation. Could anyone please help me to solve this error?

    I think if I can somehow define xn[i][t] and xntemp[i][t] as 2-d float arrays, it may become alright. But it is just an opinion, and I don't know how to define 2-d arrays in which each value is a double or float number, not an integer one!

    I would appreciate any idea ! Thanks

    Saeed,


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 2.  Re: Scripting runtime error: cannot convert to a number, "[a IloNumVar]".

    Posted 10/02/14 12:47 PM

    Hi

    {int} product = {1,2};
     {int} period = {1,2};
     
     float X[i in 1..10][j in 1..10]=2.0;
     
     execute
     {
     alpha=1.0;
     
    var xn = new Array(12);
      for(var i = 0; i < 12; i++){
        xn[i] = new Array(125);
      }
     
     
       var xntemp = new Array(12);
      for(var i = 0; i < 12; i++){
        xntemp[i]=new Array(125);
      }


    for (i in thisOplModel.product){
          for (t in thisOplModel.period){
        xn[i][t]=alpha*(thisOplModel.X[i][t])+(1-alpha)*(xntemp[i][t]);
         
        }
        }
      }  

    works fine

    Can you send a small code that does not work ?

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 3.  Re: Scripting runtime error: cannot convert to a number, "[a IloNumVar]".

    Posted 10/06/14 09:05 AM

    Originally posted by: saeeds


    Dear AlexFleischer,

    Thank you so much for your response. I deleted some excess codes and I copied the remaining code here:

    range product = 0..11;
    int maxperiod = ...;
    range period = 0..maxperiod;
    int maxarray=...;
     
    int ReleaseDate[product] =...;
    int ProcessTime[product] =...;
    int DueDate[product] =...;
    int Deadline[product] =...;
    int MaximumRevenue[product] =...;
    float ImportanceWeight[product] =...;
    int SetupTime[product][product] =...;
     
    main {
        
      thisOplModel.settings.mainEndEnabled = true;
      thisOplModel.generate();
      var data = thisOplModel.dataElements;
     
        var m2Source = new IloOplModelSource("UpperBound.mod");
      var m2Cplex = new IloCplex();
      var m2Def = new IloOplModelDefinition(m2Source);
      // model used to retrieve data common at each iteration
      var m2_init = new IloOplModel(m2Def,m2Cplex);
      m2_init.addDataSource(data);
      var dataLambda = new IloOplDataSource("Lambda.dat");
      m2_init.addDataSource(dataLambda);
      m2_init.generate();
      var data2 = m2_init.dataElements;  
      
     
      
      // begin the lagrangian calculation here   
      writeln();
      writeln(" beginning the lagrangian calculation here... ");
     
     // maximum number of iteration we want to run the loop  
      var iter_limit = 1000;
      
      // initialize arrays and variables used in the loop that follows
      var same = 0;
      var same_limit = 10;
      
      var slack = new Array(12);
      for (var i = 0; i < 12; i++) {
        slack[i] = new Array(12);
      }
      
      var slackbar = new Array(12);
      for (var i = 0; i < 12; i++) {
        slackbar[i] = new Array(12);
      }
      
      var temp = new Array(12);
      for (var i = 0; i < 12; i++) {
        temp[i] = new Array(12);
      }
      
      var lambda = new Array(12);
      for (var i = 0; i < 12; i++) {
        lambda[i] = new Array(12);
      }
      
      var xn = new Array(12);
      for(var i = 0; i < 12; i++){
        xn[i] = new Array(125);
      }
      
      var yn = new Array(12);
      for (var i = 0; i < 12; i++) {
        yn[i] = new Array(12);
      }
      
       var xntemp = new Array(12);
      for(var i = 0; i < 12; i++){
        xntemp[i]=new Array(125);
      }
      
      var yntemp = new Array(12);
      for (var i = 0; i < 12; i++) {
        yntemp[i] = new Array(12);
      }
      
     
      for (var i in thisOplModel.product) 
      {
        for (var t in thisOplModel.period)
        {    
      xn[i][t]=0.00000;
      xntemp[i][t]=0.00000;
    }
    }
      
        
      var LB = 90;
     
     for (var i in thisOplModel.product) 
      {
        for (var j in thisOplModel.product)
        {
        slack[i][j] = 0.0;
        slackbar[i][j]=0.0;
        temp[i][j] = 0.0;
        lambda[i][j] = 0.0;
        yn[i][j]=0.0;
        yntemp[i][j]=0.0;
      }    
      } 
      var scale = 2;
      var norm = 0.0;
      var step = 0.0;
      var alpha=0.1;
      
      //arrays to store the UB, scale and step values at each iteration
     
      var UBlog = new Array(iter_limit);
      var scalelog = new Array(iter_limit);
      var steplog = new Array(iter_limit);
     
    var m2 = new IloOplModel(m2Def,m2Cplex);
        for (i in thisOplModel.product){
          for (j in thisOplModel.product){
          data2.lambda[i][j] = lambda[i][j];
          temp[i][j]=lambda[i][j];
        }      
        }
        m2.addDataSource(data2);  
        m2.generate();
        var Lagrangian;
        if (m2Cplex.solve()) { 
          Lagrangian = m2Cplex.getObjValue();
          UB = Lagrangian;
        }
        
        for (i in thisOplModel.product){
          for (t in thisOplModel.period){
            xntemp[i][t]=m2.X[i][t];
          }
        }
        for (i in thisOplModel.product){
          for (j in thisOplModel.product){
            yntemp[i][j]=m2.Y[i][j];
          }
        }
                      
      // executes UpperBound model 
      for(var k=1; k<=iter_limit;k++) {
     
        UBlog[k] = 0.0;
        scalelog[k] = 0.0;
        steplog[k] = 0.0;
        writeln();
        writeln(" ITERATION:  " , k );  
        
        
        norm = 0;
        for(i in thisOplModel.product) {
          for (j in thisOplModel.product){
          slack[i][j] = 0;
           if(i==j){
            slack[i][j] = 0;}
            if(i==11)
            {slack[i][j] = 0;}
            if (j==0)
            {slack[i][j] = 0;}
            else{
          for (var t=0;t<=thisOplModel.maxperiod;t++)
          {
          slack[i][j]+=t*(xntemp[j][t]);
          slack[i][j]-=t*(xntemp[i][t]);
          }
          slack[i][j]-=thisOplModel.ProcessTime[i];
          slack[i][j]-=(thisOplModel.SetupTime[i][j])*(yntemp[i][j]);
          slack[i][j]-=(thisOplModel.Deadline[i])*((yntemp[i][j])-1);
          }      
          norm += Opl.pow(slack[i][j],2);   
          }     
          }
        
          step = scale * (( Lagrangian - LB) / norm);
          
             // update Lambda to pass it as input data to UpperBound model in next iteration
        for(i in thisOplModel.product) {
          for(j in thisOplModel.product) {
          if(i==j)
          {lambda[i][j] = 0;
        }
        if(i==11){
          lambda[i][j] = 0;
        }
        if (j==0){
          lambda[i][j] = 0;
        }    
        else
        {      
           
          if (temp[i][j] - (step * slack[i][j]) > 0 )
            lambda[i][j] = temp[i][j] - (step * slack[i][j]) ;
          else 
            lambda[i][j] = 0; 
          }        
        }
      }
      
      var m2 = new IloOplModel(m2Def,m2Cplex);
        for (i in thisOplModel.product){
          for (j in thisOplModel.product){
          data2.lambda[i][j] = lambda[i][j];
        }      
        }
        m2.addDataSource(data2);  
        m2.generate();
     
        if (m2Cplex.solve()) { 
          Lagrangian = m2Cplex.getObjValue();
        }
        
        for(i in thisOplModel.product) {
          for (j in thisOplModel.product){
          slackbar[i][j] = 0;
           if(i==j){
            slackbar[i][j] = 0;}
            if(i==11)
            {slackbar[i][j] = 0;}
            if (j==0)
            {slackbar[i][j] = 0;}
            else{
          for (var t=0;t<=thisOplModel.maxperiod;t++)
          {
          slackbar[i][j]+=t*(m2.X[j][t]);
          slackbar[i][j]-=t*(m2.X[i][t]);
          }
          slackbar[i][j]-=thisOplModel.ProcessTime[i];
          slackbar[i][j]-=(thisOplModel.SetupTime[i][j])*(m2.Y[i][j]);
          slackbar[i][j]-=(thisOplModel.Deadline[i])*((m2.Y[i][j])-1);
          }      
          }     
          }
        
        for (i in thisOplModel.product){
          for (t in thisOplModel.period){
       xn[i][t]=alpha*(m2.X[i][t])+(1-alpha)*(xntemp[i][t]);
          
        
          }
        }
        
        for (i in thisOplModel.product){
          for (j in thisOplModel.product){
       yn[i][j]=alpha*m2.Y[i][j]+(1-alpha)*yntemp[i][j];
       
        
          }
        }
       
     
        if (Lagrangian < UB ) {
          UB = Lagrangian;
           for(i in thisOplModel.product) {
          for(j in thisOplModel.product) {
          temp[i][j] = lambda[i][j];
        }
      }          
        } else {   
          same ++; 
        }
        if (same == same_limit) {
         scale = scale/2;
         same = 0;    
        }
        
        
        LBlog[k] = LB;
        UBlog[k] = UB; 
        scalelog[k] = scale;
        steplog[k] = step; 
        
        m2.end();
        
    for (i in thisOplModel.product){
          for (t in thisOplModel.period){
            xntemp[i][t]=xn[i][t];
          }
        }
        for (i in thisOplModel.product){
          for (j in thisOplModel.product){
            yntemp[i][j]=yn[i][j];
          }
        }
        
      } //end of main "for loop"
      dataLambda.end();
      m2_init.end();
     
      m2Def.end();
      m2Cplex.end();
      m2Source.end();    
          
       }

    Actually, the first lines are the parameters I used in my main model in UpperBound.mod . After solving UpperBound.mod, I take X and Y values and use them for generating new Lambda values. X's are the variables which are float between 0 and 1. Y values are binary variables. I ran this small code, and I saw the same error again for the line I wrote in red color in the code. Simply, if I ignore that line, again the error will be shown for blue line.

    I would appreciate if you can help me. 

    Best,


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 4.  Re: Scripting runtime error: cannot convert to a number, "[a IloNumVar]".

    Posted 10/06/14 09:33 AM

    Hi

    can you also attach UpperBound.mod and lambda.dat so that other users of the forum may help you ?

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 5.  Re: Scripting runtime error: cannot convert to a number, "[a IloNumVar]".

    Posted 10/06/14 09:52 AM

    Originally posted by: saeeds


    Yes, sure. I've attached all of them here.

    best,


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 6.  Re: Scripting runtime error: cannot convert to a number, "[a IloNumVar]".

    Posted 10/06/14 10:21 AM

    Hi

    line 142

    replace

    xntemp[i][t]=m2.X[i][t];

    by

    xntemp[i][t]=1.0*m2.X[i][t];

    line 147

    replace

    yntemp[i][j]=m2.Y[i][j];

    by

    yntemp[i][j]=1.0*m2.Y[i][j];

    and

    line 277 add something like

    var LBlog= new Array(10000); var UBlog= new Array(10000);

    and your model should run better

    regards

     


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 7.  Re: Scripting runtime error: cannot convert to a number, "[a IloNumVar]".

    Posted 10/06/14 10:32 AM

    Originally posted by: saeeds


    The error is fixed. I really don't know how to thank you Mr. Fleischer. I'll go forward on the code.

    Thank you a zillion,

    Sincerely,


    #DecisionOptimization
    #OPLusingCPLEXOptimizer