sos.m


% Copyright 2017, Gurobi Optimization, Inc.

% This example creates a very simple Special Ordered Set (SOS)
% model. The model consists of 3 continuous variables, no linear
% constraints, and a pair of SOS constraints of type 1.

try
    clear model;
    model.ub = [1 1 2];
    model.obj = [2 1 1];
    model.modelsense = 'Max';
    model.A = sparse(1,3);
    model.rhs = 0;
    model.sense = '=';

    % Add first SOS: x1 = 0 or x2 = 0
    model.sos(1).type   = 1;
    model.sos(1).index  = [1 2];
    model.sos(1).weight = [1 2];

    % Add second SOS: x1 = 0 or x3 = 0
    model.sos(2).type   = 1;
    model.sos(2).index  = [1 3];
    model.sos(2).weight = [1 2];

    % Write model to file
    gurobi_write(model, 'sos.lp');

    result = gurobi(model);

    for i=1:3
        fprintf('x%d %e\n', i, result.x(i))
    end

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

catch gurobiError
    fprintf('Encountered an error\n')
end

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.

Search