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 <span>$</span>\leq<span>$</span> 4
  x + y     <span>$</span>\geq<span>$</span> 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/matlab/mip1.m)...

function mip1()
% Copyright 2023, 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

names = {'x'; 'y'; 'z'};

model.A = sparse([1 2 3; 1 1 0]);
model.obj = [1 1 2];
model.rhs = [4; 1];
model.sense = '<>';
model.vtype = 'B';
model.modelsense = 'max';
model.varnames = names;

gurobi_write(model, 'mip1.lp');

params.outputflag = 0;

result = gurobi(model, params);


for v=1:length(names)
    fprintf('%s %d\n', names{v}, result.x(v));

fprintf('Obj: %e\n', result.objval);

Try Gurobi for Free

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

Evaluation License
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.
Academic License
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.