Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
Expand all | Collapse all

Assigning data to multidimensional array in main flow with CPLEX OPL gives error: error Data element does not exist

  • 1.  Assigning data to multidimensional array in main flow with CPLEX OPL gives error: error Data element does not exist

    Posted 02/18/25 07:02 AM

    Hi,

    I'm a PhD student trying to run multiple models in sequence with CPLEX OPL. As part of my scripting I would like to change som data between runs. This is no problem for scalars, i.e. one value parameters. But when I want to change data with multiple dimensions, I don't get it to work. I've tried googling, reading the CPLEX documentation and asking ChatGPT but I have been unsuccessful. I would be greatful if anyone could point me in the right direction. I've provided an example below. 

    However I try, I get the error Data element "target" does not exist. What am I doing wrong?


    My model in the file test.mod that looks like this:

    range A = 0..1;

    range B = 0..1;

    float value[A][B] = ...;

    float target[a in A][b in B] = ...;

    dvar float+ x[A][B]in 0 .. 2;

    dvar float Z;

    maximize

    Z;

    subject to {

    Z == sum ( a in A, b in B )

    ( value[a,b] * x[a, b] ) ;

    forall (a in A )

    sum ( b in B )

    x[a,b] == 2;

    forall (a in A, b in B )

    x[a,b] >= target[a,b];

    }

    The test.dat file looks like this:

    value = [[1,2],[3,4]];

    The main_test.mod file looks like this:

    main {

    var data = new IloOplDataSource("test.dat");

    var def = new IloOplModelDefinition(new IloOplModelSource("test.mod"));

    var cplex = new IloCplex();

    var opl = new IloOplModel(def, cplex);

    opl.addDataSource(data);

    var loop = new IloOplDataElements();

    var newTarget = new Array();

    for (var a = 0; a <= 1; a++) {

    newTarget[a] = new Array();

    for (var b = 0; b <= 1; b++) {

    newTarget[a][b] = 1;

    }

    }

    loop.target = newTarget;

    opl.addDataSource(loop);

    opl.generate();

    cplex.solve()

    writeln(cplex.getObjValue());

    writeln(opl.value);

    writeln(opl.target);

    opl.end();

    }






    ------------------------------
    Patrik Ulvdal
    ------------------------------


  • 2.  RE: Assigning data to multidimensional array in main flow with CPLEX OPL gives error: error Data element does not exist
    Best Answer

    Posted 02/19/25 08:30 AM

    Hello Patrik,

    You need to add the target declaration in your test.dat file like this for instance: target = [[0,1],[1,0]];. Normally you should be able to run the test.mod file.

    Regarding the main_test.mod file, I updated/simplified your code (in green).

    main {

    var data = new IloOplDataSource("test.dat");

    var def = new IloOplModelDefinition(new IloOplModelSource("test.mod"));

    var cplex = new IloCplex();

    var opl = new IloOplModel(def, cplex);

    opl.addDataSource(data);

    //var loop = new IloOplDataElements();

    data = opl.dataElements;

    //var newTarget = new Array();

    for (var a = 0; a <= 1; a++) {

    //newTarget[a] = new Array();

    for (var b = 0; b <= 1; b++) {

    //newTarget[a][b] = 1;

    data.target[a][b] = 1;

     }

    }

    //loop.target = newTarget;

    //opl.addDataSource(loop);

    opl.addDataSource(data);

    opl.generate();

    cplex.solve()

    writeln(cplex.getObjValue());

    writeln(opl.value);

    writeln(opl.target);

    opl.end();

    }

    Best regards,



    ------------------------------
    Thierry Sola
    ------------------------------



  • 3.  RE: Assigning data to multidimensional array in main flow with CPLEX OPL gives error: error Data element does not exist

    Posted 02/19/25 09:09 AM

    Thanks Thierry Sola!

    It works like a charm! If I understand it correctly, the data needs to be complete to be able to run the model - and, in the main flow I need to add that data first and then change som part of it?



    ------------------------------
    Patrik Ulvdal
    ------------------------------