The Gurobi™ distribution includes an extensive set of examples that illustrate commonly used features of the Gurobi libraries. Most examples have versions for C, C++, C#, Java, Visual Basic and Python. A few, however, illustrate features that are specific to the Python interface. A brief note for users of the Gurobi MATLAB® and R interfaces: our interfaces to these languages are built around the assumption that you will use the rich matrix-oriented capabilities of the underlying languages to build your optimization models. Thus, our examples for these languages don’t attempt to show you how to build models.
We have instead chosen to provide a few simple examples that demonstrate how to pass matrices into our interface. We recommend that you begin by reading the overview of the examples (available here). However, if you’d like to dive directly into specific examples, the following is a list of all of the examples included in the Gurobi distribution, organized by basic function. The source for the examples can be found by following the provided links, or in the examples
 directory of the Gurobi distribution. If you would like further details on any of the Gurobi routines used in these examples, please consult the Gurobi Reference Manual.
Example | Description | Available Languages |
---|---|---|
lp | A very simple example that reads a continuous model from a file, optimizes it, and writes the solution to a file. If the model is infeasible, it writes an Irreducible Inconsistent Subsystem (IIS) instead. | C, C++, C#, Java, Python, VB |
mip2 | Reads a MIP model from a file, optimizes it, and then solves the fixed version of the MIP model. | C, C++, C#, Java, Python, VB |
Example | Description | Available Languages |
---|---|---|
mip1 | Builds a trivial MIP model, solves it, and prints the solution. | C, C++, C#, Java, MATLAB, Python, R, VB |
qp | Builds a trivial QP model, solves it, converts it to an MIQP model, and solves it again. | C, C++, C#, Java, MATLAB, Python, R, VB |
qcp | Builds and solves a trivial QCP model. | C, C++, C#, Java, MATLAB, Python, R, VB |
sos | Builds and solves a trivial SOS model. | C, C++, C#, Java, MATLAB, Python, R, VB |
dense | Solves a model stored using dense matrices. We don’t recommend using dense matrices, but this example may be helpful if your data is already in this format. | C, C++, C#, Java, Python, VB |
genconstr | Demonstrates the use of general constraints. | C, C++, C#, Java, Python, VB |
multiobj | Demonstrates the use of multi-objective optimization. | C, C++, C#, Java, Python, VB |
piecewise | Demonstrates the use of piecewise-linear objective functions. | C, C++, C#, Java, MATLAB, Python, R, VB |
poolsearch | Demonstrates the use of solution pools. | C, C++, C#, Java, Python, VB |
Example | Description | Available Languages |
---|---|---|
diet | Builds and solves the classic diet problem. Demonstrates model construction and simple model modification – after the initial model is solved, a constraint is added to limit the number of dairy servings. | C, C++, C#, Java, MATLAB, Python, VB |
diet2, diet3, diet4, dietmodel | Python-only variants of the diet example that illustrate model-data separation. | diet2.py, diet3.py, diet4.py, dietmodel.py |
facility | Simple facility location model: given a set of plants and a set of warehouses, with transportation costs between them, this example finds the least expensive set of plants to open in order to satisfy product demand. This example demonstrates the use of MIP starts — the example computes an initial, heuristic solution and passes that solution to the MIP solver. | C, C++, C#, Java, Python, VB |
netflow | A Python-only example that solves a multi-commodity network flow model. It demonstrates the use of several Python modeling constructs, including dictionaries, tuples, and tuplelist objects. | Python |
portfolio | A Python-only example that solves a financial portfolio optimization model, where the historical return data is stored using the pandas package and the result is plotted using the matplotlib package. It demonstrates the use of pandas, NumPy, and Matplotlib in conjunction with Gurobi. | Python |
sudoku | Reads a Sudoku puzzle dataset from a file, builds a MIP model to solve that model, solves it, and prints the solution. | C, C++, C#, Java, Python, VB |
workforce1 | Formulates and solves a workforce scheduling model. If the model is infeasible, the example computes and prints an Irreducible Inconsistent Subsystem (IIS). | C, C++, C#, Java, Python, VB |
workforce2 | An enhancement of workforce1 . This example solves the same workforce scheduling model, but if the model is infeasible, it computes an IIS, removes one of the associated constraints from the model, and re-solves. This process is repeated until the model becomes feasible. Demonstrates constraint removal. |
C, C++, C#, Java, Python, VB |
workforce3 | A different enhancement of workforce1 . This example solves the same workforce scheduling model, but if the model is infeasible, it adds artificial variables to each constraint and minimizes the sum of the artificial variables. This corresponds to finding the minimum total change in the right-hand side vector required in order to make the model feasible. Demonstrates variable addition. |
C, C++, C#, Java, Python, VB |
workforce4 | An enhancement of workforce3. This example solves the same workforce scheduling model, but it starts with artificial variables in each constraint. It first minimizes the sum of the artificial variables. Then, it introduces a new quadratic objective to balance the workload among the workers. Demonstrates optimization with multiple objective functions. | C, C++, C#, Java, Python, VB |
workforce5 | An alternative enhancement of workforce3. This example solves the same workforce scheduling model, but it starts with artificial variables in each constraint. It formulates a multi-objective model where the primary objective is to minimize the sum of the artificial variables (uncovered shifts), and the secondary objective is to minimize the maximum difference in the number of shifts worked between any pair of workers. Demonstrates multi-objective optimization. | C, C++, C#, Java, Python, VB |
Example | Description | Available Languages |
---|---|---|
feasopt | Reads a MIP model from a file, adds artificial slack variables to relax each constraint, and then minimizes the sum of the artificial variables. It then computes the same relaxation using the feasibility relaxation feature. The example demonstrates simple model modification by adding slack variables. It also demonstrates the feasibility relaxation feature. | C, C++, C#, Java, Python, VB |
lpmethod | Demonstrates the use of different LP algorithms. Reads a continuous model from a file and solves it using multiple algorithms, reporting which is the quickest for that model. | C, C++, C#, Java, Python, VB |
lpmod | Demonstrates the use of advanced starts in LP. Reads a continuous model from a file, solves it, and then modifies one variable bound. The resulting model is then solved in two different ways: starting from the solution of the original model, or restarting from scratch. | C, C++, C#, Java, Python, VB |
params | Demonstrates the use of Gurobi parameters. Reads a MIP model from a file, and then spends 5 seconds solving the model with each of four different values of the MIPFocus  parameter. It compares the optimality gaps for the four different runs, and continues with the MIPFocus  value that produced the smallest gap. |
C, C++, C#, Java, Python, VB |
sensitivity | MIP sensitivity analysis. Reads a MIP model, solves it, and then computes the objective impact of fixing each binary variable in the model to 0 or 1. Demonstrates simple MIP model modification by changing variable bounds. | C, C++, C#, Java, Python, VB |
tune | Uses the parameter tuning tool to search for improved parameter settings for a model. | C, C++, C#, Java, Python, VB |
fixanddive | Implements a simple MIP heuristic. It reads a MIP model from a file, relaxes the integrality conditions, and then solves the relaxation. It then chooses a set of integer variables that take integer or nearly integer values in the relaxation, fixes them to the nearest integer, and solves the relaxation again. This process is repeated until the relaxation is either integer feasible or linearly infeasible. The example demonstrates different types of model modification (relaxing integrality conditions, changing variable bounds, etc.). | C, C++, C#, Java, Python, VB |
matrix1.py | A Python-only example that formulates and solves a simple MIP model using the matrix API. | Python |
matrix2.py | A Python-only example that uses the Python matrix API to formulate the n-queens problem; it maximizes the number queens placed on an n x n chessboard without threatening each other. | Python |
Example | Description | Available Languages |
---|---|---|
tsp | Solves a traveling salesman problem using lazy constraints. | C, C++, C#, Java, Python, VB |
callback | Demonstrates the use of Gurobi callbacks. | C, C++, C#, Java, Python, VB |
GUROBI NEWSLETTER
Latest news and releases
Choose the evaluation license that fits you best, and start working with our Expert Team for technical guidance and support.
Request free trial hours, so you can see how quickly and easily a model can be solved on the cloud.
Cookie | Duration | Description |
---|---|---|
_biz_flagsA | 1 year | A Cloudflare cookie set to record users’ settings as well as for authentication and analytics. |
_biz_pendingA | 1 year | A Cloudflare cookie set to record users’ settings as well as for authentication and analytics. |
_biz_sid | 30 minutes | This cookie is set by Bizible, to store the user's session id. |
ARRAffinity | session | ARRAffinity cookie is set by Azure app service, and allows the service to choose the right instance established by a user to deliver subsequent requests made by that user. |
ARRAffinitySameSite | session | This cookie is set by Windows Azure cloud, and is used for load balancing to make sure the visitor page requests are routed to the same server in any browsing session. |
BIGipServersj02web-nginx-app_https | session | NGINX cookie |
cookielawinfo-checkbox-advertisement | 1 year | Set by the GDPR Cookie Consent plugin, this cookie is used to record the user consent for the cookies in the "Advertisement" category . |
cookielawinfo-checkbox-analytics | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics". |
cookielawinfo-checkbox-functional | 11 months | The cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional". |
cookielawinfo-checkbox-necessary | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary". |
cookielawinfo-checkbox-others | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other. |
cookielawinfo-checkbox-performance | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance". |
CookieLawInfoConsent | 1 year | Records the default button state of the corresponding category & the status of CCPA. It works only in coordination with the primary cookie. |
elementor | never | This cookie is used by the website's WordPress theme. It allows the website owner to implement or change the website's content in real-time. |
JSESSIONID | session | New Relic uses this cookie to store a session identifier so that New Relic can monitor session counts for an application. |
viewed_cookie_policy | 11 months | The cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data. |
Cookie | Duration | Description |
---|---|---|
__cf_bm | 30 minutes | This cookie, set by Cloudflare, is used to support Cloudflare Bot Management. |
_biz_nA | 1 year | Bizible sets this cookie to remember users’ settings as well as for authentication and analytics. |
_biz_uid | 1 year | This cookie is set by Bizible, to store user id on the current domain. |
_hjAbsoluteSessionInProgress | 30 minutes | Hotjar sets this cookie to detect a user's first pageview session, which is a True/False flag set by the cookie. |
_mkto_trk | 2 years | This cookie is set by Marketo. This allows a website to track visitor behavior on the sites on which the cookie is installed and to link a visitor to the recipient of an email marketing campaign, to measure campaign effectiveness. Tracking is performed anonymously until a user self-identifies by submitting a form. |
bcookie | 1 year | LinkedIn sets this cookie from LinkedIn share buttons and ad tags to recognize browser ID. |
bscookie | 1 year | LinkedIn sets this cookie to store performed actions on the website. |
doc_langsBB | 1 year | Documentation system cookie |
doc_version | 1 year | Documentation system cookie |
lang | session | LinkedIn sets this cookie to remember a user's language setting. |
lidc | 1 day | LinkedIn sets the lidc cookie to facilitate data center selection. |
UserMatchHistory | 1 month | LinkedIn sets this cookie for LinkedIn Ads ID syncing. |
whova_client_id | 10 years | Event agenda system cookie |
Cookie | Duration | Description |
---|---|---|
_gat_UA-5909815-1 | 1 minute | A variation of the _gat cookie set by Google Analytics and Google Tag Manager to allow website owners to track visitor behaviour and measure site performance. The pattern element in the name contains the unique identity number of the account or website it relates to. |
Cookie | Duration | Description |
---|---|---|
_an_uid | 7 days | 6Sense Cookie |
_BUID | 1 year | This cookie, set by Bizible, is a universal user id to identify the same user across multiple clients’ domains. |
_ga | 2 years | The _ga cookie, installed by Google Analytics, calculates visitor, session and campaign data and also keeps track of site usage for the site's analytics report. The cookie stores information anonymously and assigns a randomly generated number to recognize unique visitors. |
_ga_* | 1 year 1 month 4 days | Google Analytics sets this cookie to store and count page views. |
_gat_UA-* | 1 minute | Google Analytics sets this cookie for user behaviour tracking. |
_gcl_au | 3 months | Provided by Google Tag Manager to experiment advertisement efficiency of websites using their services. |
_gd_session | 4 hours | This cookie is used for collecting information on users visit to the website. It collects data such as total number of visits, average time spent on the website and the pages loaded. |
_gd_visitor | 2 years | This cookie is used for collecting information on the users visit such as number of visits, average time spent on the website and the pages loaded for displaying targeted ads. |
_gid | 1 day | Installed by Google Analytics, _gid cookie stores information on how visitors use a website, while also creating an analytics report of the website's performance. Some of the data that are collected include the number of visitors, their source, and the pages they visit anonymously. |
_hjFirstSeen | 30 minutes | Hotjar sets this cookie to identify a new user’s first session. It stores the true/false value, indicating whether it was the first time Hotjar saw this user. |
_hjIncludedInSessionSample_* | 2 minutes | Hotjar cookie that is set to determine if a user is included in the data sampling defined by a site's daily session limit. |
_hjRecordingEnabled | never | Hotjar sets this cookie when a Recording starts and is read when the recording module is initialized, to see if the user is already in a recording in a particular session. |
_hjRecordingLastActivity | never | Hotjar sets this cookie when a user recording starts and when data is sent through the WebSocket. |
_hjSession_* | 30 minutes | Hotjar cookie that is set when a user first lands on a page with the Hotjar script. It is used to persist the Hotjar User ID, unique to that site on the browser. This ensures that behavior in subsequent visits to the same site will be attributed to the same user ID. |
_hjSessionUser_* | 1 year | Hotjar cookie that is set when a user first lands on a page with the Hotjar script. It is used to persist the Hotjar User ID, unique to that site on the browser. This ensures that behavior in subsequent visits to the same site will be attributed to the same user ID. |
_hjTLDTest | session | To determine the most generic cookie path that has to be used instead of the page hostname, Hotjar sets the _hjTLDTest cookie to store different URL substring alternatives until it fails. |
6suuid | 2 years | 6Sense Cookie |
AnalyticsSyncHistory | 1 month | LinkedIn cookie |
BE_CLA3 | 1 year 1 month 4 days | BrightEdge sets this cookie to enable data aggregation, analysis and report creation to assess marketing effectiveness and provide solutions for SEO, SEM and website performance. |
CONSENT | 2 years | YouTube sets this cookie via embedded youtube-videos and registers anonymous statistical data. |
dj | 10 years | DemandJump cookie |
djaimid.a28e | 2 years | DemandJump cookiean |
djaimses.a28e | 30 minutes | DemandJump cookie |
li_gc | 5 months 27 days | LinkedIn Cookie |
ln_or | 1 day | LinkedIn Cookie |
vuid | 2 years | Vimeo installs this cookie to collect tracking information by setting a unique ID to embed videos to the website. |
Cookie | Duration | Description |
---|---|---|
__adroll | 1 year 1 month | This cookie is set by AdRoll to identify users across visits and devices. It is used by real-time bidding for advertisers to display relevant advertisements. |
__adroll_fpc | 1 year | AdRoll sets this cookie to target users with advertisements based on their browsing behaviour. |
__adroll_shared | 1 year 1 month | Adroll sets this cookie to collect information on users across different websites for relevant advertising. |
__ar_v4 | 1 year | This cookie is set under the domain DoubleClick, to place ads that point to the website in Google search results and to track conversion rates for these ads. |
_fbp | 3 months | This cookie is set by Facebook to display advertisements when either on Facebook or on a digital platform powered by Facebook advertising, after visiting the website. |
_te_ | session | Adroll cookie |
fr | 3 months | Facebook sets this cookie to show relevant advertisements to users by tracking user behaviour across the web, on sites that have Facebook pixel or Facebook social plugin. |
IDE | 1 year 24 days | Google DoubleClick IDE cookies are used to store information about how the user uses the website to present them with relevant ads and according to the user profile. |
li_sugr | 3 months | LinkedIn sets this cookie to collect user behaviour data to optimise the website and make advertisements on the website more relevant. |
test_cookie | 15 minutes | The test_cookie is set by doubleclick.net and is used to determine if the user's browser supports cookies. |
VISITOR_INFO1_LIVE | 5 months 27 days | A cookie set by YouTube to measure bandwidth that determines whether the user gets the new or old player interface. |
YSC | session | YSC cookie is set by Youtube and is used to track the views of embedded videos on Youtube pages. |
yt-remote-connected-devices | never | YouTube sets this cookie to store the video preferences of the user using embedded YouTube video. |
yt-remote-device-id | never | YouTube sets this cookie to store the video preferences of the user using embedded YouTube video. |
yt.innertube::nextId | never | This cookie, set by YouTube, registers a unique ID to store data on what videos from YouTube the user has seen. |
yt.innertube::requests | never | This cookie, set by YouTube, registers a unique ID to store data on what videos from YouTube the user has seen. |