Decision Optimization

Decision Optimization

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

 View Only
Expand all | Collapse all

Usage of decision variables within preprocesing block

  • 1.  Usage of decision variables within preprocesing block

    Posted Fri February 02, 2018 03:34 AM

    Originally posted by: Srihitha Reddy


    Hi,

         I would like to know whether we can use decision variables within preprocessing block, that is, within the execute block.

    Here is the code snippet in which am getting errors. Please have a look at it and help me in sorting out this issue.

    /*********************************************
     * OPL 12.8.0.0 Model
     * Author: SRIHITHA
     * Creation Date: Feb 2, 2018 at 9:30:39 AM
     *********************************************/


     //parameters
     int n=...; //no. of requested VMs
     int m=...; //no. of servers
     
     range VMs=1..n;
     range servers=1..m;
     
     float maxpower=...; //max power consumed by server
     float power[VMs]=...; 
     float currentpower[servers]=...;

    //variables
    dvar boolean x[VMs][servers];
    dvar boolean e[servers];
    int count;
     
    execute
    {
     for(var j=1;j<=m;j++)
     {
         count = 0;
         for(var i=1;i<=n;i++)
         {
           if(x[i][j]==1)
              count++;  
         }
         if(count!=0) 
            e[j]=1; 
         else
            e[j]=0;
     } 

     
     
     //expression
     dexpr float z=sum(j in servers) e[j];
     minimize z;
     
     //constraints
     subject to{
        forall(j in servers)
           cons1: 
           sum(i in VMs) power[i]*x[i][j] <= maxpower*e[j] - currentpower[j];
        
        forall(i in VMs)
           cons2:
           sum(j in servers) x[i][j] == 1;
           
        cons3:
        sum(j in servers) e[j] >= ceil ((sum(j in servers) currentpower[j])/maxpower);
            }

     

    Thanks in advance.


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 2.  Re: Usage of decision variables within preprocesing block

    Posted Fri February 02, 2018 09:37 AM

    Hi,

    let me give you a small example:

    dvar int x[1..2] in 0..10;

    execute
    {
    x[1].UB=1;
    x[1].LB=1;
    }

    maximize x[1]+x[2];
    subject to
    {

    }

    gives

    x = [1
             10];

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 3.  Re: Usage of decision variables within preprocesing block

    Posted Sat February 03, 2018 10:48 AM

    Originally posted by: Srihitha Reddy


    Hi, Alex!

    I would like to know what does x[1].UB and x[1].LB mean in the above example.

    And in my above code, I was struck with an error, "Scripting runtime error: Cannot assign to decision variable in 'e'." Can you please help me out with this issue.

     

    Thanks in advance.


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 4.  Re: Usage of decision variables within preprocesing block

    Posted Sat February 03, 2018 12:27 PM

    Hi,

    UB and LB set the Upper and Lower bounds so in my small example I was able to write x[1] should be 1 in preprocessing which was what you were looking for.

    In your code you should do that

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 5.  Re: Usage of decision variables within preprocesing block

    Posted Mon February 05, 2018 07:09 AM

    Originally posted by: Srihitha Reddy


    Hi,

          I understood your example well. Now I tried my code as follows. But in the if loop, in which I was inserting the condition as x[i][j] ==1, can I use simply as if(x[i][j] ==1) or should I use UB and LB as if (x[i][j].UB ==1 && x[i][j].LB ==1). 

    Also for the given data files, all the values of e[j] should not be equal to 1. 

    And when am trying to print the values of x[i][j], am encountered an error saying "IloNumVar." What does it mean? How should I overcome it?

     

     

    /*********************************************
     * OPL 12.8.0.0 Model
     * Author: SRIHITHA
     * Creation Date: Feb 2, 2018 at 9:30:39 AM
     *********************************************/

     //parameters
     int n=...; //no. of requested VMs
     int m=...; //no. of servers
     
     range VMs=1..n;
     range servers=1..m;
     
     float maxpower=...; //max power consumed by server
     float power[VMs]=...; 
     float currentpower[servers]=...;

    //variables
    dvar boolean x[VMs][servers];
    dvar boolean e[servers];
    int count;
     
    execute
    {
     for(var j=1;j<=m;j=j+1)
     {
         count=0;
         writeln("Value:",count);
         for(var i=1;i<=n;i=i+1)
         { 
           if(x[i][j]==1)                                        //SHOULD I INCLUDE UB AND LB ADDRESSING x[i][j].
              count=count+1;  
         }
         writeln("Value of count:",count);         //THE VALUE IS NOT CHANGING. I GUESS IF CONDITION IS NOT GETTING EXECUTED.
         if(count==0)
         {
           e[j].UB=0;
           e[j].LB=0;
         } 
         else
         {
           e[j].UB=1;
           e[j].LB=1;   
         }       
     } 

     
     //expression
     dexpr float z=sum(j in servers) e[j];
     minimize z;
     
     //constraints
     subject to{
        forall (j in servers)
           cons1: 
           sum(i in VMs) power[i]*x[i][j] <= maxpower*e[j] - currentpower[j];
        
        forall(i in VMs)
           cons2:
           sum(j in servers) x[i][j] == 1;
           
        cons3:
        sum(j in servers) e[j] >= ceil ((sum(j in servers) currentpower[j])/maxpower);
            }

     

    Thanks in advance.


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 6.  Re: Usage of decision variables within preprocesing block

    Posted Mon February 05, 2018 07:23 AM

    Hi,

    in preprocessing you can get the value of a decision variable since this is still unknown but you may get its LB and UB!

    So you could use

    dvar int x[1..2] in 0..10;

        execute
        {
        x[1].UB=1;
        x[1].LB=1;
        if (x[1].LB==1) writeln("ok");
        }

        maximize x[1]+x[2];
        subject to
        {

        }

    which gives

    ok

    as a way to do what you want to do

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 7.  Re: Usage of decision variables within preprocesing block

    Posted Mon February 05, 2018 09:36 AM

    Originally posted by: Srihitha Reddy


    Hi, Alex!

    Sorry for troubling you.

    In the above example, you have already assigned integer "1" to both UB and LB which means the value of x[1]=1. Then there what is the point of putting the "if" condition?

    And my problem is different from yours, am having two decision variables, in which one(e[j]) is dependent on another(x[i][j]). My condition is: if x[i][j]==1 then the count increases. If count!=0, then e[j]=1 otherwise e[j]=0.

    I couldn't solve this issue for two weeks. Please help in writing correct code for this case.

     

    Please help me in correcting my code.

    execute
    {
     for(var j=1;j<=m;j=j+1)
     {
         count=0;
         writeln("Value:",count);
         for(var i=1;i<=n;i=i+1)
         { 
          // writeln("x[i][j]",(x[i][j]));
           if(x[i][j]==1)
              count=count+1;  
         }
         writeln("Value of count:",count);
         if(count==0)
         {
           e[j].UB=0;
           e[j].LB=0;
         } 
         else
         {
           e[j].UB=1;
           e[j].LB=1;   
         }       
     } 

     

     

    Thank you so much, Alex.


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 8.  Re: Usage of decision variables within preprocesing block

    Posted Mon February 05, 2018 09:47 AM

    Hi,

    in the model why don t you change

    execute
    {
     for(var j=1;j<=m;j++)
     {
         count = 0;
         for(var i=1;i<=n;i++)
         {
           if(x[i][j]==1)
              count++;  
         }
         if(count!=0) 
            e[j]=1; 
         else
            e[j]=0;
     } 

    into

    execute
    {
     for(var j=1;j<=m;j++)
     {
         count = 0;
         for(var i=1;i<=n;i++)
         {
           if(x[i][j].LB==1)
              count++;  
         }
         if(count!=0)
            e[j].LB=1;
         else
            e[j].UB=0;
     }
    }

    ?

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 9.  Re: Usage of decision variables within preprocesing block

    Posted Mon February 05, 2018 09:54 AM

    Originally posted by: Srihitha Reddy


    Hi, Alex!

    Just now I changed it and run the code.

     execute
    {
     for(var j=1;j<=m;j++)
     {
         count = 0;
         writeln("Count=",count);
         for(var i=1;i<=n;i++)
         {
           if(x[i][j].LB==1)
              count++;  
         }
         writeln("Value of increased count:",count);
         if(count!=0)
            e[j].LB=1;
         else
            e[j].UB=0;
     }
    }

     

    Count=0
    Value of increased count:0
    Count=0
    Value of increased count:0
    Count=0
    Value of increased count:0
    Count=0
    Value of increased count:0

    This is the result am getting. And also logically speaking, if count==0, then e[j] =0 but in the results obtained, e[j]=1.

     

    Results obtained:

    x = [[1 0 0 0]
                 [1 0 0 0]
                 [1 0 0 0]
                 [1 0 0 0]
                 [1 0 0 0]];
    e = [1 1 1 1]; 


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 10.  Re: Usage of decision variables within preprocesing block

    Posted Mon February 05, 2018 10:05 AM

    Ok what you do is a bi strange since you do not know yet in postprocessing the values of the decision variables.

    What you could try is:

    /*********************************************
     * OPL 12.8.0.0 Model
     * Author: SRIHITHA
     * Creation Date: Feb 2, 2018 at 9:30:39 AM
     *********************************************/


     //parameters
     int n=...; //no. of requested VMs
     int m=...; //no. of servers
     
     range VMs=1..n;
     range servers=1..m;
     
     float maxpower=...; //max power consumed by server
     float power[VMs]=...;
     float currentpower[servers]=...;

    //variables
    dvar boolean x[VMs][servers];
    dvar boolean e[servers];

     

     
     dvar int s[1..m];
     
     //expression
     dexpr float z=sum(j in servers) e[j];
     minimize z;
     
     //constraints
     subject to{
     
     forall(j in 1..m) s[j]==sum(i in 1..n) x[i][j];
     
     forall(j in 1..m) (s[j]>=1) =>  (e[j]==1);
     forall(j in 1..m) (s[j]==0) =>  (e[j]==0);
     
     
        forall(j in servers)
           cons1:
           sum(i in VMs) power[i]*x[i][j] <= maxpower*e[j] - currentpower[j];
        
        forall(i in VMs)
           cons2:
           sum(j in servers) x[i][j] == 1;
           
        cons3:
        sum(j in servers) e[j] >= ceil ((sum(j in servers) currentpower[j])/maxpower);
            }

     

    and then it would work better

     

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 11.  Re: Usage of decision variables within preprocesing block

    Posted Mon February 05, 2018 10:48 AM

    Originally posted by: Srihitha Reddy


    Thank you, Alex!

    It worked out.

     


    #DecisionOptimization
    #OPLusingCPLEXOptimizer