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