0

I implemented a CPLEX model as .mod, which uses data from a .dat file. Three example sets look like this in the .dat file:

Set1 from SheetRead(Data1, "Sheet1!A2:A5");
Set2 from SheetRead(Data2, "Sheet1!A2:A3");
Set3 from SheetRead(Data3, "Sheet1!A2:A250");

Now I am investigating different data using the same model. That means that in one case Set1 has 4 data entries and therefore goes from A2:A5 but in another case it might include data from A2:A100. Is there a possibility so that I don´t have to put in the excel ranges manually every time? I tried to put a very high upper limit so that in every case all data is included, but then CPLEX includes a zero into the set in case the actual range is smaller.

Thanks a lot! Best Regards

Addition: I tried to use oplrun -D option and it does not really work out yet, but I could not figure out the reason because there is no error message or something.

Assume in that there is the following written in the .mod file called RCPLEX.mod:

tuple TEST{
 key int i;
 key int j;
}
{TEST} A=...;

float p[A]=...;

...It follows an optimization using set A and parameter p.

A is the result of several operations in R. It is a data.frame. p represents the second column of this data.frame A.

So what I did is:

Result <- system("oplrun -DA=A -Dp=A[,2] RCPLEX.mod")

This gives a value of 5. But this is not the correct result of the CPLEX .mod. It also does change with the input. I think it does not run the CPLEX .mod because it also takes only 1 second of time. The CPLEX .mod normally takes like 10 minutes. Did I do something wrong? Maybe in application of the -D option on data.frames/sets?

A321
  • 17
  • 4

1 Answers1

0

you could so that in a main (flow control) See How to SheetRead in Cplex with variable column length?

But if you call oplrun from R as you seem to do in Run CPLEX mod within in R script

then I recommend oplrun -D option

oplrun -D can help you send to oplrun the right SheetRead string

PS:

Here an example of oplrun -D

Take https://github.com/AlexFleischerParis/zooopl/blob/master/zoodat.mod

int nbKids=...;
float costBus40=...;
float costBus30=...;
 
dvar int+ nbBus40;
dvar int+ nbBus30;
 
minimize
 costBus40*nbBus40  +nbBus30*costBus30;
 
subject to
{
 40*nbBus40+nbBus30*30>=nbKids;
} 

execute DISPLAY_After_SOLVE
    {
    writeln("The minimum cost is ",cplex.getObjValue());
    writeln("We will use ",nbBus40," 40 seats buses and ",nbBus30," 30 seats buses ");
    }

then if in the command line you do

oplrun -DnbKids=300 -DcostBus30=400 -DcostBus40=500 zoodat.mod

You will get

OBJECTIVE: 3800
The minimum cost is 3800
We will use 6 40 seats buses and 2 30 seats buses
Alex Fleischer
  • 9,276
  • 2
  • 12
  • 15
  • Thanks a lot for your answer! Do you have a hint where I can find more information about the oplrun -D option and its application? – A321 Jul 19 '21 at 19:38
  • in PS I gave an example of oplrun -D – Alex Fleischer Jul 20 '21 at 08:46
  • Ahh, thank you! So I do not need a .dat file anymore. Should that also work for larger Sets? I tried and it give the same value 5 no matter what I am changing in the oplrum command line. I post an example below Addition in the initial thread. – A321 Jul 20 '21 at 17:23
  • Well, in order to do whatever you need you may build the .dat file from R and then call .mod and .dat – Alex Fleischer Jul 21 '21 at 12:46
  • So instead of implementing the .dat file in CPLEX using the data from R (as excel sheets; requiring information about ranges), it is also possible to build the .dat file from R directly? That would be exactly what I am looking for I think. Do you have a hint where I could look up how this works? – A321 Jul 21 '21 at 13:44
  • See https://stackoverflow.com/questions/2470248/write-lines-of-text-to-a-file-in-r for writing a file in R and again let me share https://github.com/AlexFleischerParis/oplexcel/blob/main/mainvariablesheetreadstring.mod which shows how to set the SheetRead string from a main block – Alex Fleischer Jul 22 '21 at 07:27