Example

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 <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)...

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

try
    clear model;
    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';

    clear params;
    params.outputflag = 0;
    params.resultfile = 'mip1.lp';

    result = gurobi(model, params);

    disp(result)

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

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

catch gurobiError
    fprintf('Error reported\n');
end