Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  add a new constraint into an existing model - JAVA interface

    Posted 08/01/12 09:43 AM

    Originally posted by: CplexUser1453


    Hello

    I would like to manipulate an existing OPL model through the JAVA interface - solve it, add constraints and solve it again for example.

    Alex Fleischer posted a nice example on how to do it with script here

    It seems to me that there should be a way to do the equivalent through the JAVA interface, right? Is there an equivalent JAVA example (have not managed to find one in the documentation or on the forum)

    When trying to do this, I have trouble with adding constraints - I am getting a ilog.concert.Exception.

    My OPL representation :
    
    tuple TripleIndex
    { 
    
    int t; 
    
    int u; 
    
    int g; 
    } 
    {TripleIndex
    } Index = 
    {<t,u,g> | t in Time, u in Urange, g in 1..Garray[t][u]
    }; dvar 
    
    boolean X[s in Index];
    

    Here is an example :
    A .mod file and a .dat file is imported through OplFactory just like in the java examples in the documentation. Then :
    
    (1 ) IloTupleSet index = opl.getElement(
    "Index").asTupleSet(); (2 ) IloTupleBuffer buf = opl.getElement(
    "Index").asTupleSet().makeTupleBuffer(-1); (3 ) buf.setIntValue(
    "t", t); (4 ) buf.setIntValue(
    "u", u); (5 ) buf.setIntValue(
    "g", g); (6 ) buf.commit(); (7 ) IloTuple indexTuple = index.find(buf); (8 ) val = (
    
    double) XvalMap.get(indexTuple); (9 ) val = Math.round(val); (10) cplex.addEq(XvarMap.get(indexTuple), val); (11) buf = opl.getElement(
    "Index").asTupleSet().makeTupleBuffer(-1); (12) buf.setIntValue(
    "t", t); (13) buf.setIntValue(
    "u", u); (14) buf.setIntValue(
    "g", g+1); (15) buf.commit(); (16) indexTuple = index.find(buf); (17) val = (
    
    double) XvalMap.get(indexTuple); (18) val = Math.round(val); (19) cplex.addEq(XvarMap.get(indexTuple), val, name);
    


    I am getting the following ilog.concert.IloExeption on line 17 :
    
    ilog.concert.IloException: Type 
    {TripleIndex
    } expected 
    
    for element 
    "Index".
    


    I am really not sure why the exact same code on line 8 did not throw an exception while line 17 causes an exception. Anybody has an idea?

    I would also like to know if there are some fundemtal limitations of the JAVA OPL interface. Is there anything I really cannot do if I am working with a model that was originally done in OPL and imported through a .mod file?

    Thanks a lot!
    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 2.  Re: add a new constraint into an existing model - JAVA interface

    Posted 08/01/12 11:17 AM

    Originally posted by: CplexUser1453


    I replaced lines (8) and (17) by
    val = cplex.getValue(XvarMap.get(indexTuple));
    


    and I am getting an exception
    ilog.concert.IloException: CPLEX Error  1217: No solution exists.
    


    Considering that I have just solved my model and I am trying to add constraints, this seems strange to me...
    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 3.  Re: add a new constraint into an existing model - JAVA interface

    Posted 08/01/12 02:31 PM
    Hi,

    if what you need is the java equivalent of

    thisOplModel.ctEmpty[1].UB=5;
       thisOplModel.ctEmpty[1].setCoef(thisOplModel.Gas,-1);
       thisOplModel.ctEmpty[1].setCoef(thisOplModel.Chloride,1);
       thisOplModel.ctEmpty[2].UB=10;
       thisOplModel.ctEmpty[2].setCoef(thisOplModel.Gas,-3);
       thisOplModel.ctEmpty[2].setCoef(thisOplModel.Chloride,4);
       cplex.solve();
    


    then what you could do is

    IloForAllRange ctEmpty = (IloForAllRange) oplModel.getElement("ctEmpty").asConstraint();
    


    and then on the class IloForAllRange you have access to setUB and setLB

    Regards
    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 4.  Re: add a new constraint into an existing model - JAVA interface

    Posted 08/02/12 05:48 AM

    Originally posted by: CplexUser1453


    Thanks for the answer.

    Actually the strange behaviour I was getting was due to the fact that apparently cplex does not like if I solve a model, manipulate the constraints and try to solve if again using the same objects. So my question was actually not well posed...

    I was able to make my code work (sort of) by using run configurations just like in the mulprod_main.java

    So what is the better way to add an equality constraint var = val to an existing model imported from opl?

    IloForAllRange ctEmpty = (IloForAllRange) oplModel.getElement("ctEmpty").asConstraint();
    ctEmpty.setLinearCoef(var, 1);
    ctEmpty.setBounds(val, val);
    

    or
    cplex().addEq(var, val)
    


    Thanks a lot! (this forum is really a great tool...)
    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 5.  Re: add a new constraint into an existing model - JAVA interface

    Posted 08/02/12 11:00 AM
    Hi

    I think

    cplex().addEq(var, val)
    

    is more simple.

    Regards
    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 6.  Re: add a new constraint into an existing model - JAVA interface

    Posted 11/09/12 09:43 AM

    Originally posted by: SystemAdmin


    Hi all,

    I face the same problem as stated above - the difference is, I try to change an exisiting constraint in a .mod file using VB.NET.

    I came up with the following code:
    Dim ctPriceUB As IForAllRange = opl.GetElement("ctPriceUB").AsConstraint()
    


    After defining the variable ctPriceUB as IForAllRange I might use the method .UB for changing the right hand side of the constraint.

    However I get the following exception
    Type constraint[nbProducts] expected for element "ctPriceUB"
    
    .

    How is it possible to index a constraint? I don't find any examples for VB.NET...

    Thank you very much in advance.
    #DecisionOptimization
    #OPLusingCPLEXOptimizer