Skip to content
Snippets Groups Projects
Commit 9ade51c8 authored by Dominik Krupke's avatar Dominik Krupke
Browse files

gurobi basic usage

parent 018ae3c0
Branches main
No related tags found
No related merge requests found
......@@ -55,6 +55,83 @@ grbgetkey XXXXXXXX-YOUR-KEY-XXXXXXXXXX
to activate it.
You may have to be within the university's network (or use VPN) for this.
## Basic Usage
```python
import gurobipy as gp # API
from gurobipy import GRB # Symbols (e.g. GRB.BINARY)
model = gp.Model("mip1") # Create a model
model.Params.TimeLimit = 90 # 90s time limit
# Create variables x (bool), y (integer), z (fractional)
x = model.addVar(vtype=GRB.BINARY, name="x")
y = model.addVar(vtype=GRB.INTEGER, name="y", lb=-GRB.INFINITY)
z = model.addVar(vtype=GRB.CONTINUOUS, name="z")
# further options:
# * lb: float <= Lower Bound: Default Zero!!!
# * ub: float <= Upper Bound
# * obj: float <= Coefficient for the objective function.
# Objective function (Maximization)
model.setObjective(x + y + 2 * z, GRB.MAXIMIZE)
# Constraints
model.addConstr(x + 2 * y + 3 * z <= 4, name="Constraint1")
model.addConstr(x + y >= 1, name="Constraint2")
# AND GO!
model.optimize()
```
This will give us the following log.
```
Gurobi Optimizer version 9.1.2 build v9.1.2rc0 (mac64)
Thread count: 4 physical cores, 8 logical processors, using up to 8 threads
Optimize a model with 2 rows, 3 columns and 5 nonzeros
Model fingerprint: 0xd685009c
Model has 1 quadratic objective term
Variable types: 1 continuous, 2 integer (1 binary)
Coefficient statistics:
Matrix range [1e+00, 3e+00]
Objective range [1e+00, 2e+00]
QObjective range [2e+00, 2e+00]
Bounds range [1e+00, 1e+00]
RHS range [1e+00, 4e+00]
Found heuristic solution: objective 1.0000000
Presolve removed 2 rows and 3 columns
Presolve time: 0.00s
Presolve: All rows and columns removed
Explored 0 nodes (0 simplex iterations) in 0.01 seconds
Thread count was 1 (of 8 available processors)
Solution count 2: 3 1
Optimal solution found (tolerance 1.00e-04)
Best objective 3.000000000000e+00, best bound 3.000000000000e+00, gap 0.0000%
```
```python
if model.Status == GRB.OPTIMAL: # an optimal solution has been found
print("=== Optimal Solution ===")
print(f"x={x.X}")
print(f"y={y.X}")
print(f"z={z.X}")
elif model.SolCount > 0: # there exists a feasible solution
print("=== Suboptimal Solution ===")
print(f"x={x.X}")
print(f"y={y.X}")
print(f"z={z.X}")
else:
print(f"Bad solution (code {model.Status}).")
print(
"See status codes on https://www.gurobi.com/documentation/9.5/refman/optimization_status_codes.html#sec:StatusCodes"
)
```
> Caveat: Due to the underlying technique, the resulting variables may not be rounded. A boolean variable could take the value 0.00001 instead of 0, so you have to round to check them.
## Tasks
* Model the BTSP as a Mixed Integer Program on paper. Use the [constraint technique](https://en.wikipedia.org/wiki/Travelling_salesman_problem#Dantzig%E2%80%93Fulkerson%E2%80%93Johnson_formulation) already used for SAT to enforce a single tour and do not use the [technique](https://en.wikipedia.org/wiki/Travelling_salesman_problem#Miller%E2%80%93Tucker%E2%80%93Zemlin_formulation[21]) used with CP-SAT (as it performs poorly with MIP-solvers).
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment