Filter Content By
Version

## Example

Let us now turn our attention to an example of using Gurobi to solve a simple MIP model. Our example optimizes the following model:

 maximize x + y + 2 z subject to x + 2 y + 3 z 4 x + y 1 x, y, z binary
Note that this is the same model that was modeled and optimized in the C Interface section.

This is the complete source code for our example (also available in <installdir>/examples/R/mip.R)...

# Copyright 2021, Gurobi Optimization, LLC
#
# This example formulates and solves the following simple MIP model:
#  maximize
#        x +   y + 2 z
#  subject to
#        x + 2 y + 3 z <= 4
#        x +   y       >= 1
#        x, y, z binary

library(gurobi)

model <- list()

model$A <- matrix(c(1,2,3,1,1,0), nrow=2, ncol=3, byrow=T) model$obj        <- c(1,1,2)
model$modelsense <- 'max' model$rhs        <- c(4,1)
model$sense <- c('<', '>') model$vtype      <- 'B'

params <- list(OutputFlag=0)

result <- gurobi(model, params)

print('Solution:')
print(result$objval) print(result$x)

# Clear space
rm(model, result, params)


Choose the evaluation license that fits you best, and start working with our Expert Team for technical guidance and support.

Get a free, full-featured license of the Gurobi Optimizer to experience the performance, support, benchmarking and tuning services we provide as part of our product offering.
Gurobi supports the teaching and use of optimization within academic institutions. We offer free, full-featured copies of Gurobi for use in class, and for research.
##### Cloud Trial

Request free trial hours, so you can see how quickly and easily a model can be solved on the cloud.