This example builds a trivial MIP model, solves it, and prints the solution.
In the following sections, we will walk through the example, line by line, to understand how it achieves the desired result of optimizing the indicated model.
The example
Our example optimizes the following model:
maximize | x | + | y | + | 2 z | ||
subject to | x | + | 2 y | + | 3 z | <= | 4 |
x | + | y | >= | 1 | |||
x, y, z binary |
Python interface
The complete source code for our example can be found in:
- Online: Example mip1.py
- Distribution: <installdir>/examples/python/mip1.py
The example begins by importing Gurobi functions and classes:
import gurobipy as gp from gurobipy import GRB
The first line makes all Gurobi functions and classes available through a gp.
prefix (e.g., gp.Model()
). The second makes everything in class GRB
available without a prefix (e.g., GRB.OPTIMAL
). You can also start a program with from gurobipy import *
to remove the need for the gp.
prefix, but if your program uses multiple Python modules it is usually preferred to access each through its own prefix.
In order for these commands to succeed, the Python application needs to know how to find the Gurobi functions and classes. Recall that you have three options for installing them.
Python Matrix interface
The complete source code for our example can be found in:
- Online: Example matrix1.py
- Distribution: <installdir>/examples/python/matrix1.py>
The example begins by importing the Gurobi functions and classes, as well as the NumPy and SciPy packages:
import gurobipy as gp from gurobipy import GRB import numpy as np import scipy.sparse as sp
Gurobi Python applications should always start with the first two lines. If you want to use the matrix interface, the last two lines are needed as well.
C interface
The complete source code for our example can be found in:
- Online: Example mip1_c.c
- Distribution: <installdir>/examples/c/mip1_c.c
The example begins by including a few include files. Gurobi C applications should always start by including gurobi_c.h
, along with the standard C include files (stdlib.h
and stdio.h
).
C++ interface
The complete source code for our example can be found in:
- Online: Example mip1_cpp.cpp
- Distribution: <installdir>/examples/cpp/mip1_cpp.cpp
The example begins by including file gurobi_c++.h
. Gurobi C++ applications should always start by including this file.
Java interface
The complete source code for our example can be found in:
- Online: Example Mip1.java
- Distribution: <installdir>/examples/java/Mip1.java
The example begins by importing the Gurobi classes (import gurobi.*
). Gurobi Java applications should always start with this line.
.NET interface
The complete source code for our example can be found in:
- Online: Example mip1_cs.cs
- Distribution: <installdir>/examples/c#/mip1_cs.cs
The example begins by importing the Gurobi namespace (using Gurobi
). Gurobi .NET applications should always start with this line.
MATLAB interface
The complete source code for our example can be found in:
- Online: Example mip1.m
- Distribution: <installdir>/examples/matlab/mip1.m
R interface
The complete source code for our example can be found in:
- Online: Example mip1.R
- Distribution: <installdir>/examples/R/mip.R
The example begins by importing the Gurobi package (library('gurobi')). R programs that call Gurobi must include this line.
Comments
0 comments
Please sign in to leave a comment.