Sos Examples#
This section includes source code for all of the Gurobi sos examples.
The same source code can be found in the examples
directory of the
Gurobi distribution.
/* Copyright 2025, Gurobi Optimization, LLC */
/* 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. */
#include <stdlib.h>
#include <stdio.h>
#include "gurobi_c.h"
int
main(int argc,
char *argv[])
{
GRBenv *env = NULL;
GRBmodel *model = NULL;
int error = 0;
double x[3];
double obj[3];
double ub[3];
int sostype[2];
int sosbeg[2];
int sosind[4];
double soswt[4];
int optimstatus;
double objval;
/* Create environment */
error = GRBloadenv(&env, "sos.log");
if (error) goto QUIT;
/* Create an empty model */
error = GRBnewmodel(env, &model, "sos", 0, NULL, NULL, NULL, NULL, NULL);
if (error) goto QUIT;
/* Add variables */
obj[0] = -2; obj[1] = -1; obj[2] = -1;
ub[0] = 1.0; ub[1] = 1.0; ub[2] = 2.0;
error = GRBaddvars(model, 3, 0, NULL, NULL, NULL, obj, NULL, ub, NULL,
NULL);
if (error) goto QUIT;
/* Build first SOS1: x0=0 or x1=0 */
sosind[0] = 0; sosind[1] = 1;
soswt[0] = 1.0; soswt[1] = 2.0;
sosbeg[0] = 0; sostype[0] = GRB_SOS_TYPE1;
/* Build second SOS1: x0=0 or x2=0 */
sosind[2] = 0; sosind[3] = 2;
soswt[2] = 1.0; soswt[3] = 2.0;
sosbeg[1] = 2; sostype[1] = GRB_SOS_TYPE1;
/* Add SOSs to model */
error = GRBaddsos(model, 2, 4, sostype, sosbeg, sosind, soswt);
if (error) goto QUIT;
/* Optimize model */
error = GRBoptimize(model);
if (error) goto QUIT;
/* Write model to 'sos.lp' */
error = GRBwrite(model, "sos.lp");
if (error) goto QUIT;
/* Capture solution information */
error = GRBgetintattr(model, GRB_INT_ATTR_STATUS, &optimstatus);
if (error) goto QUIT;
error = GRBgetdblattr(model, GRB_DBL_ATTR_OBJVAL, &objval);
if (error) goto QUIT;
error = GRBgetdblattrarray(model, GRB_DBL_ATTR_X, 0, 3, x);
if (error) goto QUIT;
printf("\nOptimization complete\n");
if (optimstatus == GRB_OPTIMAL) {
printf("Optimal objective: %.4e\n", objval);
printf(" x=%.4f, y=%.4f, z=%.4f\n", x[0], x[1], x[2]);
} else if (optimstatus == GRB_INF_OR_UNBD) {
printf("Model is infeasible or unbounded\n");
} else {
printf("Optimization was stopped early\n");
}
QUIT:
/* Error reporting */
if (error) {
printf("ERROR: %s\n", GRBgeterrormsg(env));
exit(1);
}
/* Free model */
GRBfreemodel(model);
/* Free environment */
GRBfreeenv(env);
return 0;
}
/* Copyright 2025, Gurobi Optimization, LLC */
/* 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. */
#include "gurobi_c++.h"
using namespace std;
int
main(int argc,
char *argv[])
{
GRBEnv *env = 0;
GRBVar *x = 0;
try {
env = new GRBEnv();
GRBModel model = GRBModel(*env);
// Create variables
double ub[] = {1, 1, 2};
double obj[] = {-2, -1, -1};
string names[] = {"x0", "x1", "x2"};
x = model.addVars(NULL, ub, obj, NULL, names, 3);
// Add first SOS1: x0=0 or x1=0
GRBVar sosv1[] = {x[0], x[1]};
double soswt1[] = {1, 2};
model.addSOS(sosv1, soswt1, 2, GRB_SOS_TYPE1);
// Add second SOS1: x0=0 or x2=0 */
GRBVar sosv2[] = {x[0], x[2]};
double soswt2[] = {1, 2};
model.addSOS(sosv2, soswt2, 2, GRB_SOS_TYPE1);
// Optimize model
model.optimize();
for (int i = 0; i < 3; i++)
cout << x[i].get(GRB_StringAttr_VarName) << " "
<< x[i].get(GRB_DoubleAttr_X) << endl;
cout << "Obj: " << model.get(GRB_DoubleAttr_ObjVal) << endl;
} catch(GRBException e) {
cout << "Error code = " << e.getErrorCode() << endl;
cout << e.getMessage() << endl;
} catch(...) {
cout << "Exception during optimization" << endl;
}
delete[] x;
delete env;
return 0;
}
/* Copyright 2025, Gurobi Optimization, LLC */
/* 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. */
using System;
using Gurobi;
class sos_cs
{
static void Main()
{
try {
GRBEnv env = new GRBEnv();
GRBModel model = new GRBModel(env);
// Create variables
double[] ub = {1, 1, 2};
double[] obj = {-2, -1, -1};
string[] names = {"x0", "x1", "x2"};
GRBVar[] x = model.AddVars(null, ub, obj, null, names);
// Add first SOS1: x0=0 or x1=0
GRBVar[] sosv1 = {x[0], x[1]};
double[] soswt1 = {1, 2};
model.AddSOS(sosv1, soswt1, GRB.SOS_TYPE1);
// Add second SOS1: x0=0 or x2=0
GRBVar[] sosv2 = {x[0], x[2]};
double[] soswt2 = {1, 2};
model.AddSOS(sosv2, soswt2, GRB.SOS_TYPE1);
// Optimize model
model.Optimize();
for (int i = 0; i < 3; i++)
Console.WriteLine(x[i].VarName + " " + x[i].X);
// Dispose of model and env
model.Dispose();
env.Dispose();
} catch (GRBException e) {
Console.WriteLine("Error code: " + e.ErrorCode + ". " + e.Message);
}
}
}
/* Copyright 2025, Gurobi Optimization, LLC */
/* 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. */
import com.gurobi.gurobi.*;
public class Sos {
public static void main(String[] args) {
try {
GRBEnv env = new GRBEnv();
GRBModel model = new GRBModel(env);
// Create variables
double ub[] = {1, 1, 2};
double obj[] = {-2, -1, -1};
String names[] = {"x0", "x1", "x2"};
GRBVar[] x = model.addVars(null, ub, obj, null, names);
// Add first SOS1: x0=0 or x1=0
GRBVar sosv1[] = {x[0], x[1]};
double soswt1[] = {1, 2};
model.addSOS(sosv1, soswt1, GRB.SOS_TYPE1);
// Add second SOS1: x0=0 or x2=0
GRBVar sosv2[] = {x[0], x[2]};
double soswt2[] = {1, 2};
model.addSOS(sosv2, soswt2, GRB.SOS_TYPE1);
// Optimize model
model.optimize();
for (int i = 0; i < 3; i++)
System.out.println(x[i].get(GRB.StringAttr.VarName) + " "
+ x[i].get(GRB.DoubleAttr.X));
// Dispose of model and environment
model.dispose();
env.dispose();
} catch (GRBException e) {
System.out.println("Error code: " + e.getErrorCode() + ". " +
e.getMessage());
}
}
}
function sos()
% Copyright 2025, Gurobi Optimization, LLC
%
% 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.
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);
end
#!/usr/bin/env python3.11
# Copyright 2025, Gurobi Optimization, LLC
# 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.
import gurobipy as gp
from gurobipy import GRB
try:
# Create a new model
model = gp.Model("sos")
# Create variables
x0 = model.addVar(ub=1.0, name="x0")
x1 = model.addVar(ub=1.0, name="x1")
x2 = model.addVar(ub=2.0, name="x2")
# Set objective
model.setObjective(2 * x0 + x1 + x2, GRB.MAXIMIZE)
# Add first SOS: x0 = 0 or x1 = 0
model.addSOS(GRB.SOS_TYPE1, [x0, x1], [1, 2])
# Add second SOS: x0 = 0 or x2 = 0
model.addSOS(GRB.SOS_TYPE1, [x0, x2], [1, 2])
model.optimize()
for v in model.getVars():
print(f"{v.VarName} {v.X:g}")
print(f"Obj: {model.ObjVal:g}")
except gp.GurobiError as e:
print(f"Error code {e.errno}: {e}")
except AttributeError:
print("Encountered an attribute error")
# Copyright 2025, Gurobi Optimization, LLC
#
# This example formulates and solves the following simple SOS model:
# maximize
# 2 x + y + z
# subject to
# x1 = 0 or x2 = 0 (SOS1 constraint)
# x1 = 0 or x3 = 0 (SOS1 constraint)
# x1 <= 1, x2 <= 1, x3 <= 2
library(gurobi)
model <- list()
model$A <- matrix(c(0,0,0), nrow=1, byrow=T)
model$obj <- c(2,1,1)
model$modelsense <- 'max'
model$ub <- c(1,1,2)
model$rhs <- c(0)
model$sense <- c('=')
# First SOS: x1 = 0 or x2 = 0
sos1 <- list()
sos1$type <- 1
sos1$index <- c(1, 2)
sos1$weight <- c(1, 2)
# Second SOS: x1 = 0 or x3 = 0
sos2 <- list()
sos2$type <- 1
sos2$index <- c(1, 3)
sos2$weight <- c(1, 2)
model$sos <- list(sos1, sos2)
result <- gurobi(model)
print(result$objval)
print(result$x)
# Clear space
rm(model, result)
' Copyright 2025, Gurobi Optimization, LLC
'
' 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.
Imports System
Imports Gurobi
Class sos_vb
Shared Sub Main()
Try
Dim env As New GRBEnv()
Dim model As New GRBModel(env)
' Create variables
Dim ub As Double() = {1, 1, 2}
Dim obj As Double() = {-2, -1, -1}
Dim names As String() = {"x0", "x1", "x2"}
Dim x As GRBVar() = model.AddVars(Nothing, ub, obj, Nothing, names)
' Add first SOS1: x0=0 or x1=0
Dim sosv1 As GRBVar() = {x(0), x(1)}
Dim soswt1 As Double() = {1, 2}
model.AddSOS(sosv1, soswt1, GRB.SOS_TYPE1)
' Add second SOS1: x0=0 or x2=0
Dim sosv2 As GRBVar() = {x(0), x(2)}
Dim soswt2 As Double() = {1, 2}
model.AddSOS(sosv2, soswt2, GRB.SOS_TYPE1)
' Optimize model
model.Optimize()
For i As Integer = 0 To 2
Console.WriteLine(x(i).VarName & " " & x(i).X)
Next
' Dispose of model and env
model.Dispose()
env.Dispose()
Catch e As GRBException
Console.WriteLine("Error code: " & e.ErrorCode & ". " & e.Message)
End Try
End Sub
End Class