0

I would like to know if I can have the objective function of CPLEX called from external software such as R. I built the optimisation model in CPLEX using its OPL and I would like to run the CPLEX model to optimise an external objective function. Is there a way to do so?

duckmayr
  • 16,303
  • 3
  • 35
  • 53
  • Perhaps the package [`Rcplex`](https://cran.r-project.org/web/packages/Rcplex/index.html) would be of use? Not *super* familiar with this tool, but could be worth checking out – duckmayr Apr 16 '20 at 16:11

1 Answers1

1

Let me give the 3 ways from "How to call CPLEX from R" at here

  1. use system in r
  2. use docplexcloud api
  3. use doopl

More details about the first method:

diet.r

sink("diet.dat")

# function to turn a csv dat file into an opl dat file
translateCsvToOpl <- function(csvfilename,setNameInOpl) {

value<-0


cat(setNameInOpl,"={")


t <-read.table(csvfilename)



for (j in 1:nrow(t)){

cat("<")


for (i in 1:ncol(t)){
     #print(t[j,i]);
       value<-paste(t[j,i]);
     if (i==1) cat("\"");
        cat(value)
if (i==1) cat("\"");

    #cat(",")


}
cat(">,\n")

}  

cat("};")
cat("\n\n")
}


translateCsvToOpl("food.dat","FOODS")
translateCsvToOpl("nutrients.dat","NUTRIENTS")
translateCsvToOpl("foodnutrients.dat","FOOD_NUTRIENTS")

system("oplrun diet.mod diet.dat")

diet.mod

tuple Food
{
    key string name;
    float unit_cost;
    float qmin;
    float qmax;
};

{Food} FOODS=...;



tuple Nutrient 
{
    key string name;
    float qmin;
    float qmax;
}

{Nutrient} NUTRIENTS=...;

tuple food_nutrients
{
    key string name;
    float q1;
    float q2;
    float q3;
    float q4;
    float q5;
    float q6;
    float q7;
}

{food_nutrients} FOOD_NUTRIENTS=...;

float array_FOOD_NUTRIENTS[f in FOODS][n in NUTRIENTS];

// turn tuple set into an array
execute
{
for(var fn in FOOD_NUTRIENTS) 
    for(var n in NUTRIENTS)
        array_FOOD_NUTRIENTS[FOODS.find(fn.name)][n]=fn[fn.getFieldName(1+Opl.ord(NUTRIENTS,n))];
}

// Decision variables
dvar float qty[f in FOODS] in f.qmin .. f.qmax;

// cost
dexpr float cost=sum (f in FOODS) qty[f]*f.unit_cost;

// KPI
dexpr float amount[n in NUTRIENTS] = sum(f in FOODS)
qty[f] * array_FOOD_NUTRIENTS[f,n];

minimize cost;
subject to
{
forall(n in NUTRIENTS) n.qmin<=amount[n]<=n.qmax;
}

execute
{
var f=new IloOplOutputFile("dietoutput.txt");
f.writeln("quantity = ",qty);
f.writeln("cost = ",cost);
f.writeln("amount = ",amount);
f.close();
}

and then when you run Rscript.exe diet.r you will get

quantity =  [0 2.1552 0 0 0 10 1.8312 0 0.9297]
cost = 2.690409172
amount =  [2000 800 11.278 8518.4 25 256.81 51.174] 
Alex Fleischer
  • 9,276
  • 2
  • 12
  • 15
  • Thanks for your kind help. I would like to ask is it possible to have the objective function written in R and the rest of the model in CPLEX. In other words, can I have: minimize cost; in R not in CPLEX. In my case, I would like to manipulate the objective function externally. Is it possible? – Mohamed Menessy Apr 25 '20 at 17:06