0

I am trying to adapt an OPL formulation using the Java API in Eclipse IDE. Extraction of the original OPL code is the following:

using CP;

int nbJobs = ...;                               
int nbMchs = ...;       

range Jobs = 0..nbJobs-1;                                                   
range Mchs = 0..nbMchs-1; 

int OpDurations[j in Jobs][m in Mchs] = ...;                                

dvar interval itvs[j in Jobs][m in Mchs] size OpDurations[j][m];                    
dvar sequence mchs[m in Mchs] in all(j in Jobs) itvs[j][m] types all(j in Jobs) j;      
dvar sequence jobs[j in Jobs] in all(m in Mchs) itvs[j][m];

I want to replicate the same above but now using the Java API. I had tried the following: (filename is a file with the values of an instance where is specified the number of jobs, number of machines and the processing time of every job in every machine):

IloCP cp = new IloCP();
DataReader data = new DataReader(filename);

int nbJobs = data.next();
int nbMachines = data.next();
int OpDurations = data.next();

IloIntRange Jobs = cp.intRange(0,nbJobs-1);
IloIntRange Mchs = cp.intRange(0,nbMachines-1);

But I don't know if that is correct and also how to replicate in Java the definition of the interval and sequences variables previously defined in OPL.

Any help would be highly appreciated.

Fco.Jav.Y
  • 65
  • 9
  • 1
    Hi, do you know that you can also embed an OPL model within a java program ? https://www.ibm.com/support/knowledgecenter/en/SSSA5P_12.8.0/ilog.odms.ide.help/OPL_Studio/usroplinterfaces/topics/opl_interfaces.html – Alex Fleischer Apr 30 '20 at 07:33
  • @AlexFleischer Thank you Alex. I can do that and is working!. Just last question: I get the following output on the Eclipse console: 23 65 58 55 78 99 14 58 92 (3x3 array). It is possible to save this output as a 2D array to use this information for other methods in Java?. Thanks – Fco.Jav.Y Apr 30 '20 at 21:12
  • *DO* please keep using the Java API - there are lots of things that you can do using Java to manipulate your data and the solution(s) that are much easierin Java than using OPL or OPL Script. I have found that it is easier to deliver real business systems and solutions using the API (from Java, C# C++ etc) than to try to embed and interface with OPL. People make lots of claims about the extra benefits of a 'proper' modelling language like OPL, but we find there is no real loss in expressiveness and real benefits through tighter integration from directly accessing CPLEX through the APIs. – TimChippingtonDerrick May 01 '20 at 19:21

1 Answers1

2

Your code looks correct at first glance.

For creation of variables take a look at the reference documentation of IloIntervalVar and IloIntervalSequenceVar as well as functions IloCP.intervalVar() and IloCP.intervalSequenceVar() here.

Moreover, in your distribution you have a folder cpoptimizer/examples/src/java in which you can find examples Sched*.java. These use interval variables and you can learn from them how to do scheduling with interval variables.

Daniel Junglas
  • 5,830
  • 1
  • 5
  • 22