Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  How I can access decision variables values?

    Posted 02/27/12 06:24 PM

    Originally posted by: Rommelds


    I'm programming in Java language...

    Firstly, I defined the model with IloOplModelSource modelSource = oplF.createOplModelSource(DATADIR + "/model.mod") and then created the model using IloOplModel opl=oplF.createOplModel(def,cplex).

    But, I want to access the values os the decision variables. It is defined as var[a][b][c].

    How can I do it?

    Thanks.

    Rommel.
    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: How I can access decision variables values?

    Posted 02/27/12 06:47 PM

    Originally posted by: SystemAdmin


    I would suggest you to go through the Iterators.java example (located in the <CPLEX_StudioXXX>\opl\examples\opl_interfaces\java\iterators\src\iterators directory). It has a method called 'sample2' which talks about retrieving values of a 2D variable called "x" indexed over "s1" and "s2". You will need to extend this idea to retrieve the values of your 3D "var" variable. Here's a code snippet from the sample:

    
    IloOplFactory oplF = 
    
    new IloOplFactory();   IloOplRunConfiguration rc = oplF.createOplRunConfiguration(DATADIR + 
    "/iterators.mod"); IloOplModel opl = rc.getOplModel(); opl.generate();   
    // Get the x, s1 and s2 elements from the OplModel. IloIntMap x = opl.getElement(
    "x").asIntMap(); IloSymbolSet s1 = opl.getElement(
    "s1").asSymbolSet(); IloSymbolSet s2 = opl.getElement(
    "s2").asSymbolSet();   
    // Iterate on the first indexer. 
    
    for (java.util.Iterator it1 = s1.iterator(); it1.hasNext();) 
    { String sub1 = (String) it1.next(); 
    // Get the 2nd dimension array from the 1st dimension. IloIntMap sub = x.getSub(sub1); 
    // Iterate on the second indexer of x (that is the indexer of the 
    // sub array). 
    
    for (java.util.Iterator it2 = s2.iterator(); it2.hasNext();) 
    { String sub2 = (String) it2.next(); 
    // We are in the last dimension of the array, so we can directly 
    // use the get method. System.out.println(sub1 + 
    " " + sub2 + 
    " " + sub.get(sub2)); 
    } 
    }
    


    Ofcourse, in your case, you should retrieve the values after the 'solve' call.
    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: How I can access decision variables values?

    Posted 02/27/12 06:58 PM

    Originally posted by: Rommelds


    I've seen it.

    But, how do I use the Iterator with integer types?
    #CPLEXOptimizers
    #DecisionOptimization


  • 4.  Re: How I can access decision variables values?

    Posted 02/27/12 06:59 PM

    Originally posted by: Rommelds


    Or with range types, in this case.
    #CPLEXOptimizers
    #DecisionOptimization


  • 5.  Re: How I can access decision variables values?

    Posted 02/28/12 12:29 PM

    Originally posted by: SystemAdmin


    Yes, in the sample, the iterator goes over a string set. For integer ranges, you will need to map the indexes as asIntRange(). Here's a code snippet to do the same:

    IloIntMap var = opl.getElement("var").asIntMap();
                    IloIntRange i = opl.getElement("a").asIntRange();
                    IloIntRange j = opl.getElement("b").asIntRange();
                    IloIntRange k = opl.getElement("c").asIntRange();
            
                    for (java.util.Iterator it1 = i.iterator(); it1.hasNext();) {
                        Integer sub1 = (Integer)it1.next();
                        IloIntMap sub1M = var.getSub(sub1);
                        for (java.util.Iterator it2 = j.iterator(); it2.hasNext();) {
                            Integer sub2 = (Integer) it2.next();
                            IloIntMap sub2M = sub1M.getSub(sub2);
                            for (java.util.Iterator it3 = k.iterator(); it3.hasNext();) {
                                    Integer sub3 = (Integer) it3.next();
                                    System.out.println("var["+sub1 + "][" + sub2 + "][" + sub3+ "]="+ sub2M.get(sub3));
                            }
                        }
                    }
    


    Hope this helps.
    #CPLEXOptimizers
    #DecisionOptimization


  • 6.  Re: How I can access decision variables values?

    Posted 02/28/12 01:26 PM

    Originally posted by: Rommelds


    Oh man, thanks.

    Problem solved :-)
    #CPLEXOptimizers
    #DecisionOptimization