'CPLEX Error: Data element "xyz" has already been set

I found an error while coding in CPLEX opl using flow control. When I run the configuration file, I encounter error: Data element "x" has already been set. (Resource = 2.dat). I have copied the query from Sharing data on multiple models using "main" block, as I am trying to solve the same error.

I have list of questions:

1. If one has a defined variable in the model (e.g. "int x = 1;") and load external data (with .dat file or addDataSource()) with the same variable then he/she gets an error "Data element "x" has already been set.". How does one instruct OPL to ignore such cases, or alternatively, overwrite the existing value of x. I just want the code not to exit here.

2. If one has in the model "int x = ...;" and external data source does not supply it then one gets error "External data element "x" was not defined." on generation step. How to ignore such errors? It looks like it can be treated as "int x;" if not provided by the external data.

3. If one does not have any definition of the variable in the model, but the external data does have it, then error raised "Element "x" not defined.". Same question, how to ignore such errors, avoid exit.

I will be thankful to you for helping me. Below are my small sample code and .dat files description.

int a = 5;
range g1 = 1..a;
int x[g1] = ...;
int y[g1] = ...;

minimize sum (i in g1) 3*x[i];

subject to {
forall (i in g1)
  3*x[i] + 4*y[i] <= 300;
}

main{
var iteration=1;  
var piece = 5;

    while(iteration<=3)
    {
        var src = new IloOplModelSource("practice.mod");
        var def = new IloOplModelDefinition(src);    
        var opl = new IloOplModel(def,cplex);
        var filename=iteration;
        var data = new IloOplDataSource(filename+".dat");
        opl.addDataSource(data);
        var details=opl.dataElements;


        opl.generate();
        if(cplex.solve())
        {
            writeln(filename+"->"+1);
        }
        else
        {
            writeln(filename+"->"+0);
        }
        iteration++;
    }
}

The Data Files are:

1.dat

x = [218,60,156,221,211];
y = [0,0,0,0,0];

2.dat

 x = [218,60,156,221,211];
 y = [1,2,1,5,1];

3.dat

x = [218,60,156,221,211];
y = [2,3,5,2,6];


Solution 1:[1]

  1. If you have several data with the same name, which one is OPL supposed to use? The first one? The first one that happens to have the correct type? The last one? In order to avoid all ambiguities, triggering an error is the correct thing to do.

  2. There's not much that the program could do if you don't supply all the data that you say you would provide. So your question 2 has no answer...

  3. Not declaring variables before their use is simply impossible in the OPL. The language has not been designed with this in mind...

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Xavier Nodet