Christof
Christof

Reputation: 3

How to add an Array to a datasource in Cplex with Java API using an OPL model?

I'm a newbie with cplex. I have a model which is written in opl and I want to run the model from my java application. To do that I want to fill the external data directly from my application instead of using .dat files.

How can I add arrays or even multidimensional arrays ? As far as I can see from the API I only can int, double or Tuples but nothing that is similar to an array ?

I assume I'm currently misunderstanding the the concept behind this, can anyone help me out here ?

I have loaded the model successfully, and created a datasource. I could already add simple integer variables to the datasource:

IloOplDataElements dataSource = new IloOplDataElements(env);
dataSource.addElement(dataSource.makeElement("L", 1));
dataSource.addElement(dataSource.makeElement("M", l.getCopies()));

This works fine, because when i try to run the model, I don't see errors regarding this variables any longer. For arrays I would expect something like:

dataSource.addElement(dataSource.makeElement("c", "[1,1]"));

Upvotes: 0

Views: 32

Answers (1)

Alex Fleischer
Alex Fleischer

Reputation: 10062

See example in

CPLEX_Studio2211\opl\examples\opl_interfaces\java\customdatasource

in CustomDataSource.java

// initialize a 2-dimension int array 'a2DIntArray'
      handler.startElement("a2DIntArray");
      handler.startArray();
      for (int i=1;i<=2;i++) {
    handler.startArray();
    for (int j=1;j<=3;j++)
      handler.addIntItem(i * 10 + j);
    handler.endArray();
      }
      handler.endArray();
      handler.endElement();
    }

Upvotes: 0

Related Questions