Topic
  • 5 replies
  • Latest Post - ‏2012-11-09T14:43:20Z by SystemAdmin
CplexUser1453
CplexUser1453
25 Posts

Pinned topic add a new constraint into an existing model - JAVA interface

‏2012-08-01T13:43:00Z |
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!
Updated on 2012-11-09T14:43:20Z at 2012-11-09T14:43:20Z by SystemAdmin
  • CplexUser1453
    CplexUser1453
    25 Posts

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

    ‏2012-08-01T15:17:23Z  
    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...
    Updated on 2014-03-26T05:53:53Z at 2014-03-26T05:53:53Z by iron-man
  • AlexFleischer
    AlexFleischer
    1248 Posts

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

    ‏2012-08-01T18:31:14Z  
    I replaced lines (8) and (17) by
    <pre class="java dw" data-editor-lang="java" data-pbcklang="java" dir="ltr">val = cplex.getValue(XvarMap.get(indexTuple)); </pre>

    and I am getting an exception
    <pre class="java dw" data-editor-lang="java" data-pbcklang="java" dir="ltr">ilog.concert.IloException: CPLEX Error 1217: No solution exists. </pre>

    Considering that I have just solved my model and I am trying to add constraints, this seems strange to me...
    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
    Updated on 2014-03-26T05:53:46Z at 2014-03-26T05:53:46Z by AlexFleischer
  • CplexUser1453
    CplexUser1453
    25 Posts

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

    ‏2012-08-02T09:48:23Z  
    Hi,

    if what you need is the java equivalent of

    <pre class="java dw" data-editor-lang="java" data-pbcklang="java" dir="ltr">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(); </pre>

    then what you could do is

    <pre class="java dw" data-editor-lang="java" data-pbcklang="java" dir="ltr">IloForAllRange ctEmpty = (IloForAllRange) oplModel.getElement("ctEmpty").asConstraint(); </pre>

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

    Regards
    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...)
    Updated on 2014-03-26T05:53:42Z at 2014-03-26T05:53:42Z by iron-man
  • AlexFleischer
    AlexFleischer
    1248 Posts

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

    ‏2012-08-02T15:00:23Z  
    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?

    <pre class="java dw" data-editor-lang="java" data-pbcklang="java" dir="ltr">IloForAllRange ctEmpty = (IloForAllRange) oplModel.getElement("ctEmpty").asConstraint(); ctEmpty.setLinearCoef(var, 1); ctEmpty.setBounds(val, val); </pre>
    or
    <pre class="java dw" data-editor-lang="java" data-pbcklang="java" dir="ltr">cplex().addEq(var, val) </pre>

    Thanks a lot! (this forum is really a great tool...)
    Hi

    I think

    cplex().addEq(var, val)
    

    is more simple.

    Regards
    Updated on 2014-03-26T05:53:38Z at 2014-03-26T05:53:38Z by AlexFleischer
  • SystemAdmin
    SystemAdmin
    1883 Posts

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

    ‏2012-11-09T14:43:20Z  
    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.
    Updated on 2014-03-26T05:52:35Z at 2014-03-26T05:52:35Z by iron-man