Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  Cannot Access Decision Variable Values In Main Script

    Posted 05/25/16 11:47 AM

    Originally posted by: C.Magno


    Hi everyone!

     

    I'm coding a heuristic with exact local search.

    In my main block script code I'm trying to access the value of a decision variable.

    My model is in a separated file "LP-CA.mod".

    This is the important segments of the code in this file:

    ...
    
    dvar boolean ypc[P][C]; //decision variable that i'm trying access
    
    ...
    
    minimize sum (d in Demands) xd[d]*d.bandwidth;
     
     subject to{
      
      //ypc is used in the constraints
      
      forall(d in Demands)
        sum(p in pat[d])
             sum (c in channels[d])
                ypc[p][c] + xd[d] == 1;     
                      
      forall(e in Links)
       forall(s in Slots)
        sum (d in Demands)
          sum(p in pat[d]: deltape[p][e] == 1 ) 
            sum (c in channels[d]: gamacs[c][s] == 1) 
               deltape[p][e]*ypc[p][c]*gamacs[c][s] <= 1;
            
      forall(d in Demands: selected[d] == 1)
            xd[d] == 1;  
     }
    

    As we can see. The ypc[ ][ ] is used in the constraints. In my main script block I'm checking the cplex status before access the ypc[ ][ ] variable. Like this:

    main{
        
        ... //load model and data files...
        
        function runHeuristic(opl){ //opl is an instance of IloOplModel
            
            ...
            
            if(cplex.solve()){
                            
                            if(cplex.status == 1) //Optimal solution is available.
                                writeln(opl.ypc); //Not returning a string like [[0,0,0,0,][0,0,1,0][1,0,0,0]]
                            
                            objAux = cplex.getObjValue();
                            writeln("Obj. Value: " + objAux);
                            if(pertubationIdx >= 0){
                            localSearchCounter++;
                    }                               
            }
            
            ...
        }
        
        ...
    }
    

    But i'm getting as result just a string whit the name of the variable: "ypc".

    Someone can help me with this issue?

     

    Thanks in advanced.

     

    Update 1:

    When I try access element by element like this:

    for(var p in opl2.P)
       for(var c in opl2.C)
          writeln(opl2.ypc[p][c]);
    

    I get this string "[a IloNumVar]".

     

    Update 2:

    Since I'm using the ILOG IDE version 12.5, I must access the decision variable value like this:

    opl2.ypc[p][c].solutionValue
    

    But now, I'm getting a new problem.
    I'm getting a error message saying that "has no available solution" and it's "impossible to process".

    But how is it possible if the cplex status says "1" that means "Optimal solution is available"?


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 2.  Re: Cannot Access Decision Variable Values In Main Script

    Posted 05/25/16 02:58 PM

    Hi,

    you may notice in the example at

    https://www.ibm.com/developerworks/community/forums/html/topic?id=a63c5e3e-b205-4707-b63f-86ee3b78a544&ps=25

    that I wrote

    sol[w]=thisOplModel.Open[w].solutionValue

    in order to get all the values and then later on I use those values to update the model lower and upper bounds.

    You should do the same since as soon as you change a bound the previous solution gets invalid and that is why you get an error

    regards

     


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 3.  Re: Cannot Access Decision Variable Values In Main Script

    Posted 05/25/16 04:25 PM

    Originally posted by: C.Magno


    Hi Alex, thanks for the support.

     

    I've noticed that the error is caused because I make changes in the external data and generate again the opl model before solve.

    So, this is a more detailed code:

    main{
        
        var cplex = new IloCplex();
        var modSource =  new IloOplModelSource(".../file.mod");
        var def = new IloOplModelDefinition(modSource);
        var data = new IloOplDataSource(".../file.dat");
        var opl = new   IloOplModel(def, cplex);
    
        opl.addDataSource(data);
            
        runHeuristic(opl, cplex);
        
        function runHeuristic(opl, cplex){
              
           opl.generate();
                    
           cplex.tilim = 1; //I'm limiting the time because the heuristic
                    
           var obj = Number.MAX_VALUE;
           var objAux = Number.MAX_VALUE;
                    
           /*In this part I put this code as a test
           **and it worked well. No problems.*/
                    
           write("Solving... ");
           if(cplex.solve()){
              objAux = cplex.getObjValue();
              writeln("Obj. Value: " + objAux);
                      
              if(cplex.status == 1)
                 for(var p in  opl.P)
                    for(var c in opl.C)
                       writeln(opl.ypc[p][c].solutionValue);                               
              }
            
            /*****************************************/
                    
            while(Some Condition){
                        
               /*This function changes external data elements of the model
               **and generate it again.*/
               selectPercentOfDemandsRandomly(cplex, opl);
                            
               write("Solving... ");
               if(cplex.solve()){
                   objAux = cplex.getObjValue();
                   writeln("Obj. Value: " + objAux);
                   if(cplex.status == 1)
                      for(var p in  opl.P)
                          for(var c in opl.C)
                              writeln(opl.ypc[p][c].solutionValue); //This line is getting the error.
               }
               ...
         }
    }
    

    The external data I change  is a vector called selected[ ] :

     minimize sum (d in Demands) xd[d]*d.bandwidth;
     
     subject to{
     
      Only_one_path_and_channel:
      forall(d in Demands)
        sum(p in pat[d])
             sum (c in channels[d])
                 ypc[p][c] + xd[d] == 1;  
      
      Only_one_demand_per_channel:    
      forall(e in Links)
       forall(s in Slots)
        sum (d in Demands)
          sum(p in pat[d]: deltape[p][e] == 1 ) 
            sum (c in channels[d]: gamacs[c][s] == 1) 
              deltape[p][e] * ypc[p][c] * gamacs[c][s] <= 1;
            
      forall(d in Demands: selected[d]==1) //I change this vector before solve.
            xd[d] == 1; //that changes this dvar boolean array too, indirectly.
     }
    

    This is the function where i make the changes and generate the model:

    function selectPercentOfDemandsRandomly(cplex, opl){
        var dat = opl.dataElements;
        
        ...
        /*So, some times, for a given [d] I do:*/
        dat.selected[d] = 1;
        /*or*/
        dat.selected[d] = 0;
        /*And finally*/
        
        cplex.clearModel();
        opl = new IloOplModel(mod, cplex);
        opl.addDataSource(dat);
        opl.generate();
    }
    

    Is there something that i can do to be able to access the decision variable value without error after make the changes on the data?

    Regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 4.  Re: Cannot Access Decision Variable Values In Main Script



  • 5.  Re: Cannot Access Decision Variable Values In Main Script

    Posted 05/27/16 09:55 AM

    Originally posted by: C.Magno


    Hi Alex,

     

    Thank you very much!

     

    I studied the example code you mentioned.

    I made changes in my code and it worked for me!

     

    Regards.


    #DecisionOptimization
    #OPLusingCPLEXOptimizer