Skip to main content

How to write this problem in R? I have it in Python.

Answered

Comments

3 comments

  • Maliheh Aramon
    • Gurobi Staff

    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
  • Aleksandrs Smilgins
    • Gurobi-versary
    • Conversationalist
    • Curious

    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
  • Maliheh Aramon
    • Gurobi Staff

    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.