How to write this problem in R? I have it in Python.
AnsweredHi,
I am new to Gurobi, and I would like to use it in R. There is only one very short example on the website where a very simple example is shown. However, I have no idea how to formulate a more complex problem where several variables can be added one by one. Here is an example in Python. What should I write in order to use the same code in R?
This is Python:
X = np.array([250000,1700000,160000,145000,185000,55000,85000])
Y = np.array([38400000,2700000,500000,600000,237000000,
218800000,24700000,3300000,800000,1770000000,
24000000,1200000,1900000,0,170000000,
21500000,900000,400000,0,76000000,
24600000,1200000,300000,300000,115000000,
5700000,1000000,0,400000,65000000,
9100000,700000,700000,300000,35000000])
Y = np.reshape(Y, (7,5))
K = 46
N = 5
my_model = gp.Model('Mergers')
MyLambdas = my_model.addVars(K, vtype = gp.GRB.CONTINUOUS, lb = 0.0, ub = 1, name = 'lambda')
MyMergers= my_model.addVars(K, vtype = gp.GRB.BINARY, name = 'mergers')
S = my_model.addVar(vtype = gp.GRB.CONTINUOUS, lb = -GRB.INFINITY, name = 'Slack')
#Define the objective function:
my_model.setObjective(S, gp.GRB.MAXIMIZE)
#Constraints
my_model.addConstr(gp.quicksum(MyMergers[i]*X[i] for i in range(K)) >= gp.quicksum(MyLambdas[j]*X[j] for j in range(K)) + S)
my_model.addConstrs((gp.quicksum(MyMergers[i]*Y[i][j] for i in range(K)) <= gp.quicksum(MyLambdas[i]*Y[i][j] for i in range(K))) for j in range(N))
my_model.addConstr(gp.quicksum(MyLambdas[i] for i in range(K) )==1)
#Solve model
my_model.optimize()
#Print the solution for v in my_model.getVars():
print(v.varName, v.x)
I can start to import the gurobi library and define the values of X and Y. But how should I define the model itself? Thanks a lot in advance!
library(gurobi)
X=c(250000,1700000,160000,145000,185000,55000,85000)
Y=c(38400000,2700000,500000,600000,237000000,
218800000,24700000,3300000,800000,1770000000,
24000000,1200000,1900000,0,170000000,
21500000,900000,400000,0,76000000,
24600000,1200000,300000,300000,115000000,
5700000,1000000,0,400000,65000000,
9100000,700000,700000,300000,35000000)
Y <- matrix(Y,nrow=7,ncol=5,byrow=TRUE)
K = 46
N = 5
-
Hi Aleksandrs,
All Gurobi functional examples are available in R API as well. Please see the R Examples list.
There are three examples of diet.R, facility.R, and workforce1.R that you would find useful looking over.
Feel free to reach out again here if you encounter any issue implementing your model in R after checking the examples above.
Best regards,
Maliheh
1 -
Ok, thank you! The examples in R definitely help. However, to me it looks like the implementation in Python is much easier. For example in order to set the objective function, in Python ne can write:
my_model.setObjective(S, gp.GRB.MAXIMIZE)
So, here we just say which variable needs to be maximized without thinking about the other variables. In one of your R examples, namely diet.r , the objective is maximized in the following way:
model$obj <- c(rep(0, nCategories), cost)
So, here even though the cost is the only variable that we want in the objective function, we still need to write 0s for all other variables that we have. That is, while in Python we can add everything step by step, in R we will need to think in terms of
max vT*x
Ax <= b
Is my understanding correct?0 -
Yes, your understanding is correct. The R and MATLAB APIs are matrix-oriented where decision variables are defined as a vector and there should be a coefficient for each variable in the objective vector \(\texttt{model\$obj}\).
The Gurobi Python API is definitely the most intuitive, easy-to-use, and popular Gurobi API as you mentioned.
Best regards,
Maliheh
1
Please sign in to leave a comment.
Comments
3 comments