In addition, you can also see a list of code examples, across a range of programming languages, on our code examples page.
Any program that uses optimization must start by building an optimization model. While the exact details of how models are built depend on the programming language, the general concepts are similar in all of our supported languages. We’ll focus on the concepts, and provide details when appropriate. The first two steps in describing an optimization problem, in both CPLEX and Gurobi, are to create an environment and a model. These are objects in our object-oriented interfaces, created using the GRBEnv and GRBModel constructors, respectively. They are pointers in our C interface, returned by our GRBloadenv() and GRBnewmodel() routines. The next step is generally to list your decision variables. This is done using the GRBaddvar() routine in C, and the addVar() method in our object-oriented interfaces. You can add one variable at a time, or you can add multiple variables (using GRBaddvars() or addVars()). We generally find it more convenient to add one variable at a time, and there is no performance penalty for doing so, but you may want to add multiple variables if your CPLEX program already does so.
One difference between Gurobi and CPLEX is that we use a “lazy update” approach. After making changes to a model, you need to call GRBupdatemodel/GRBModel.update in order for those changes to be visible. To be more specific, you’ll need to call “update” after adding variables to the model, in order to use those variables in constraints. Our lazy update approach makes it easier and more efficient to build or modify a model, since you have complete control over when the actual changes take place. However, this difference between our interface and the CPLEX interface is something you’ll need to remember. Decision variables have a number of attributes, including variable type (continuous, binary, etc.), lower bound, upper bound, etc. You have two choices for specifying these. The first is to input the desired attribute values when you create the variable (i.e., as arguments to addVar()). The second is to modify the attributes after the variable has been added to the model, using one of the various Gurobi set routines (e.g., GRBsetintattr() in C, GRBVar.set() in C++). Attributes are an important concept in the Gurobi interface. Rather than providing dozens of different routines for accessing and modifying the various attributes of a model, as is done in CPLEX, we handle them through a single interface. To give an example, below is the command, shown in a range of languages, you would use to change the upper bound on variable x to 1.0:
Language | Command |
---|---|
C | GRBsetdblattrelement(model, GRB_DBL_ATTR_UB, x_index, 1.0); |
C++ | x.set(GRB_DoubleAttr_UB, 1.0); |
C# | x.Set(GRB.DoubleAttr.UB, 1.0); |
Java | x.set(GRB.DoubleAttr.UB, 1.0); |
Python | x.ub = 1.0 |
Similarly, to change the lower bound:
Language | Command |
---|---|
C | GRBsetdblattrelement(model, GRB_DBL_ATTR_LB, x_index, 1.0); |
C++ | x.set(GRB_DoubleAttr_LB, 1.0); |
C# | x.Set(GRB.DoubleAttr.LB, 1.0); |
Java | x.set(GRB.DoubleAttr.LB, 1.0); |
Python | x.lb = 1.0 |
This attribute interface serves to both unify and simplify the various Gurobi interfaces. In general, if you are searching for a routine to match a CPLEX get/set routine, you are likely to find that capability in our attribute interface.
The next step in building an optimization model is generally to describe the linear constraints on your decision variables. Depending on the interface you are using, you can describe these constraints using a single constraint matrix, by adding groups of constraints, or by adding constraints one at a time. Again, we find it more convenient to add constraints one at a time, but we understand that it may simplify migration if you mimic your existing CPLEX approach. You will generally only use a single constraint matrix to specify your constraints if you are using the Gurobi C interface. You would do this with the GRBloadmodel() routine. The arguments to this routine are quite similar to the arguments to the CPLEX CPXcopylp() routine. Note that you can also add constraints individually in our C interface, using GRBaddconstr(). In our object-oriented interfaces, you can add individual linear constraints using the addConstr() method. The details depend on the language you are using. Our C++, .NET, and Python interfaces allow operator overloading, so you can add a linear constraint as follows:
Language | Command |
---|---|
C | int ind[ ] = {1, 3, 4}; double val[ ] = {1.0, 2.0, 1.0}; error = GRBaddconstr(model, 3, ind, val, GRB_EQUAL, 1.0, “New”); |
C++ | model.addConstr(x + y + 2*z <= 2); |
C# | model.AddConstr(x + y + 2*z <= 2); |
Python | model.addConstr(x + y + 2*z <= 2) |
You can also build linear expression (GRBExpr) objects, add linear terms to these expressions (expr.addTerm()), and then add constraints using these expressions. In Java:
GRBLinExpr expr = new GRBLinExpr();
expr.addTerm(1.0, x); expr.addTerm(1.0, y); expr.addTerm(2.0, z);
model.addConstr(expr, GRB.LESS_EQUAL, 2.0);
Your model might contain other constraint types, including Special Ordered Set (SOS) constraints or quadratic constraints. The Gurobi interface contains routines that are quite similar to those in the CPLEX interface for each of these. We encourage you to browse our examples for details.
When migrating an optimization model from CPLEX to Gurobi, you may need to set certain Gurobi parameters to match the parameters you have modified in CPLEX. One important point we’d like to make is that you shouldn’t assume that you’ll need to find a matching Gurobi parameter for every CPLEX parameter you’ve changed. Gurobi and CPLEX use different strategies and algorithms. Gurobi strategy tuning may differ from the CPLEX tuning you’ve done, and often it may not be necessary at all. We recommend that you start with default settings, and only change parameters when you observe specific behavior that you’d like to modify. The following table gives a high-level mapping for the most commonly used CPLEX parameters:
CPLEX parameter (C API) | Gurobi Parameter |
---|---|
CPX_PARAM_BARALG | BarHomogeneous |
CPX_PARAM_BARCROSSALG | Crossover |
CPX_PARAM_BAREPCOMP | BarConvTol |
CPX_PARAM_BARQCPEPCOMP | BarQCPConvTol |
CPX_PARAM_BRDIR | BranchDir |
CPX_PARAM_CLIQUES | CliqueCuts |
CPX_PARAM_COVERS | CoverCuts |
CPX_PARAM_CUTPASS | CutPasses |
CPX_PARAM_EPGAP | MIPGap |
CPX_PARAM_EPAGAP | MIPGapAbs |
CPX_PARAM_EPINT | IntFeasTol |
CPX_PARAM_EPOPT | OptimalityTol |
CPX_PARAM_FLOWCOVERS | FlowCoverCuts |
CPX_PARAM_FPHEUR | PumpPasses |
CPX_PARAM_FRACPASS | GomoryPasses |
CPX_PARAM_GUBCOVERS | GUBCoverCuts |
CPX_PARAM_HEURFREQ | Heuristics |
CPX_PARAM_INTSOLLIM | SolutionLimit |
CPX_PARAM_LPMETHOD | Method |
CPX_PARAM_MIPEMPHASIS | MIPFocus |
CPX_PARAM_MIRCUTS | MIRCuts |
CPX_PARAM_NODEFILEIND | NodeFileStart |
CPX_PARAM_POLISHAFTEREPGAP | ImproveStartGap* |
CPX_PARAM_POLISHAFTERTIME | ImproveStartTime* |
CPX_PARAM_PREDUAL | PreDual |
CPX_PARAM_PREIND | Presolve |
CPX_PARAM_RINSHEUR | RINS |
CPX_PARAM_STARTALG | Method |
CPX_PARAM_SUBALG | NodeMethod |
CPX_PARAM_THREADS | Threads |
CPX_PARAM_TIMELIMIT | TimeLimit |
CPX_PARAM_VARSEL | VarBranch |
CPX_PARAM_ZEROHALFCUTS | ZeroHalfCuts |
* Gurobi uses a different algorithm, but achieves a similar result. Again, this is just a partial list of the parameters you might be using in CPLEX. If you are modifying a parameter that isn’t on this list, we encourage you to browse the list of Gurobi parameters in our Reference Manual
Once you know which parameters you would like to change, the code required to change them is straightforward. In C, you call GRBsetintparam/GRBsetdblparam/GRBsetstringparam. In our object-oriented interfaces, you call the set() method on the GRBEnv object. One point that sometimes trips people up when migrating from CPLEX to Gurobi is that Gurobi gives each model its own copy of a Gurobi environment, thus allowing each model to have its own parameter settings. In CPLEX, all models use the same parameter values. The simplest approach to managing this is to set parameters for a model as follows:
Language | Command |
---|---|
C | error = GRBsetintparam(GRBgetenv(model), GRB_INT_PAR_SOLUTIONLIMIT, 1); |
C++ | model.getEnv().set(GRB_IntParam_SolutionLimit, 1); |
C# | model.GetEnv().Set(GRB.IntParam.SolutionLimit, 1); |
Java | model.getEnv().set(GRB.IntParam.SolutionLimit, 1); |
Python | model.params.solutionLimit = 1 |
Once you have formulated your optimization model and set solver parameters, the last step is to compute a solution. You do this by calling GRBoptimize() in C, or GRBModel.optimize() in our object-oriented interfaces. The optimization will complete when one of your requested termination conditions (optimality gap, time limit, solution limit, etc.) are met. You should always check the status of the optimization once it completes. Again, rather than providing dozens of get/set routines, we return the status as an attribute of the model. You query it with the appropriate attribute get routine. For example:
Language | Command |
---|---|
C | error = GRBgetintattr(model, GRB_INT_ATTR_STATUS, &status); if (status == GRB_OPTIMAL) … |
C++ | if (model.get(GRB_IntAttr_Status) == GRB_OPTIMAL) … |
C# | if (model.Get(GRB.IntAttr.Status) == GRB.OPTIMAL) … |
Java | if (model.get(GRB.IntAttr.Status) == GRB.OPTIMAL) … |
Python | if model.status == GRB.OPTIMAL: … |
The computed values for the decision variables are also attributes:
Language | Command |
---|---|
C | error = GRBgetdblattrelement(model, GRB_DBL_ATTR_X, x_index, &xvalue); printf(“Value for variable x is %g\n”, xvalue); |
C++ | cout << “Value for variable x is: ” << x.get(GRB_DoubleAttr_X) << endl; |
C# | Console.WriteLine(“Value for variable x is: ” + x.Get(GRB.DoubleAttr.X)); |
Java | System.out.println(“Value for variable x is: ” + x.get(GRB.DoubleAttr.X)); |
Python | print(’Value for variable x is:‘,x.x) |
You can also retrieve other solution information, including the objective value, constraint slacks, and dual variables (when available), all using the attribute interface. Let us reiterate that if you are looking to map a CPLEX get/set function to Gurobi, you will probably find its equivalent in our attribute interface. Of course, finding a proven optimal solution isn’t the only possible outcome. For example, you may find that your model is infeasible. If you are using the CPLEX conflict refiner to help diagnose infeasibility, Gurobi offers the same functionality through our Irreducible Inconsistent Subsystem (IIS) routines (GRBcomputeIIS() in C; model.computeIIS() in the object-oriented interfaces). If you are using the CPLEX feasopt feature to find a minimum-cost relaxation of the constraints in the model, you can use the Gurobi feasRelax feature (GRBfeasrelax() in C; model.feasRelax in the object-oriented interfaces).
We encourage you to browse the Gurobi examples in the Example Tour to get a more concrete sense of how the Gurobi interfaces are structured. Customers have told us that, once you understand a few key concepts, the migration process is straightforward and typically quite quick.
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. |