Tutorial_Gurobi
Copyright By PowCoder代写 加微信 powcoder
Tutorial: Optimization Modeling with Gurobi Python¶
from gurobipy import *
from math import sqrt
import pandas as pd
import numpy as np
First Example: Wyndor¶
$$\begin{align*}
Z^*=\max\quad & 3x_1+5x_2\\
\mbox{s.t.} \quad & x_1\le 4\\
& 2x_2\le 12\\
&3x_1+2x_2\le 18\\
&x_1\ge 0, x_2 \ge 0
\end{align*}$$
# Create a new model
m = Model(“Wyndor”)
# Create variables
# default is nonnegative: lb=0
x1 = m.addVar(name=”x1″)
x2 = m.addVar(name=”x2″)
# Set objective
m.setObjective(3*x1 + 5 * x2 , GRB.MAXIMIZE)
# Add constraint:
m.addConstr(x1<=4, "c1")
# Add constraint:
m.addConstr(2*x2<=12, "c2")
# Add constraint:
m.addConstr(3*x1+2*x2<=18, "c3")
m.optimize()
# print optimal solutions
for v in m.getVars():
print('Optimal Solution: %s = %g' % (v.varName, v.x))
# print optimal value
print('Optimal Objective Value: %g' % m.objVal)
# print dual values to all constraints
print('Dual values:', m.getAttr("Pi", m.getConstrs()))
# Lower and Upper Bounds of sensitivity ranges
print('Sensitivity ranges of constraints have Lower Bounds ', m.getAttr("SARHSLow", m.getConstrs()), ' and Upper Bounds', m.getAttr("SARHSUp", m.getConstrs()), '.')
print('Sensitivity ranges of objective coefficients have Lower Bounds ', m.getAttr("SAObjLow", m.getVars()), ' and Upper Bounds', m.getAttr("SAObjUp", m.getVars()), '.')
Gurobi Optimizer version 9.5.0 build v9.5.0rc5 (mac64[x86])
Thread count: 4 physical cores, 8 logical processors, using up to 8 threads
Optimize a model with 3 rows, 2 columns and 4 nonzeros
Model fingerprint: 0xc030cf3a
Coefficient statistics:
Matrix range [1e+00, 3e+00]
Objective range [3e+00, 5e+00]
Bounds range [0e+00, 0e+00]
RHS range [4e+00, 2e+01]
Presolve removed 2 rows and 0 columns
Presolve time: 0.01s
Presolved: 1 rows, 2 columns, 2 nonzeros
Iteration Objective Primal Inf. Dual Inf. Time
0 4.5000000e+01 1.500000e+00 0.000000e+00 0s
1 3.6000000e+01 0.000000e+00 0.000000e+00 0s
Solved in 1 iterations and 0.02 seconds (0.00 work units)
Optimal objective 3.600000000e+01
Optimal Solution: x1 = 2
Optimal Solution: x2 = 6
Optimal Objective Value: 36
Dual values: [0.0, 1.5, 1.0]
Sensitivity ranges of constraints have Lower Bounds [2.0, 6.0, 12.0] and Upper Bounds [inf, 18.0, 24.0] .
Sensitivity ranges of objective coefficients have Lower Bounds [-0.0, 2.0] and Upper Bounds [7.5, inf] .
Alternative way: Wyndor¶
# Create a new model
m = Model("Wyndor")
# Create variables
x = m.addVars(2, name="x")
# Set objective
c = [3, 5]
m.setObjective(sum(x[i] * c[i] for i in range(2)) , GRB.MAXIMIZE)
# Add constraints:
b = [4,12,18]
A = np.array([[1,0], [0,2], [3,2]])
m.addConstrs( quicksum(A[i,j] * x[j] for j in range(2)) <= b[i] for i in range(3))
m.optimize()
# print optimal solutions
for v in m.getVars():
print('Optimal Solution: %s = %g' % (v.varName, v.x))
# print optimal value
print('Optimal Objective Value: %g' % m.objVal)
# print dual values to all constraints
print('Dual values:', m.getAttr("Pi", m.getConstrs()))
# Lower and Upper Bounds of sensitivity ranges
print('Sensitivity ranges of constraints have Lower Bounds ', m.getAttr("SARHSLow", m.getConstrs()), ' and Upper Bounds', m.getAttr("SARHSUp", m.getConstrs()), '.')
print('Sensitivity ranges of objective coefficients have Lower Bounds ', m.getAttr("SAObjLow", m.getVars()), ' and Upper Bounds', m.getAttr("SAObjUp", m.getVars()), '.')
Gurobi Optimizer version 9.5.0 build v9.5.0rc5 (mac64[x86])
Thread count: 4 physical cores, 8 logical processors, using up to 8 threads
Optimize a model with 3 rows, 2 columns and 4 nonzeros
Model fingerprint: 0xc030cf3a
Coefficient statistics:
Matrix range [1e+00, 3e+00]
Objective range [3e+00, 5e+00]
Bounds range [0e+00, 0e+00]
RHS range [4e+00, 2e+01]
Presolve removed 2 rows and 0 columns
Presolve time: 0.01s
Presolved: 1 rows, 2 columns, 2 nonzeros
Iteration Objective Primal Inf. Dual Inf. Time
0 4.5000000e+01 1.500000e+00 0.000000e+00 0s
1 3.6000000e+01 0.000000e+00 0.000000e+00 0s
Solved in 1 iterations and 0.02 seconds (0.00 work units)
Optimal objective 3.600000000e+01
Optimal Solution: x[0] = 2
Optimal Solution: x[1] = 6
Optimal Objective Value: 36
Dual values: [0.0, 1.5, 1.0]
Sensitivity ranges of constraints have Lower Bounds [2.0, 6.0, 12.0] and Upper Bounds [inf, 18.0, 24.0] .
Sensitivity ranges of objective coefficients have Lower Bounds [-0.0, 2.0] and Upper Bounds [7.5, inf] .
Nurse Scheduling Problem¶
Let $x_{i}:$ be the number of nurses starting their week on day $i$.
$$\begin{align*}
\min \quad & x_1+x_2+x_3+x_4+x_5+x_6+x_7\\
\mbox{s.t.} \quad & x_1+x_4+x_5+x_6+x_7 \ge d_1\\
& x_1+x_2+x_5+x_6+x_7\ge d_2\\
& x_1+x_2+x_3+x_6+x_7\ge d_3\\
& x_1+x_2+x_3+x_4+x_7\ge d_4\\
& x_1+x_2+x_3+x_4+x_5\ge d_5\\
& x_2+x_3+x_4+x_5+x_6\ge d_6\\
& x_3+x_4+x_5+x_6+x_7\ge d_7\\
& x_i\ge 0, \mbox{for } i=1,2,3,4,5,6,7
\end{align*}$$
# Scheduling Problem
m_sch = Model('scheduling')
# Demand for nurses
d=np.array([80,26,33,44,55,62,71])
# Declaring variables
x = m_sch.addVars(np.arange(1,8))
# Setting the objective
m_sch.setObjective(sum(x))
# Demand constraints
m_sch.addConstr( x[1] + x[4] + x[5] + x[6] + x[7] >= d[0])
m_sch.addConstr( x[1] + x[2] + x[5] + x[6] + x[7] >= d[1])
m_sch.addConstr( x[1] + x[2] + x[3] + x[6] + x[7] >= d[2])
m_sch.addConstr( x[1] + x[2] + x[3] + x[4] + x[7] >= d[3])
m_sch.addConstr( x[1] + x[2] + x[3] + x[4] + x[5] >= d[4])
m_sch.addConstr( x[2] + x[3] + x[4] + x[5] + x[6] >= d[5])
m_sch.addConstr( x[3] + x[4] + x[5] + x[6] + x[7] >= d[6])
# Solving the optimization problem
m_sch.optimize()
# Printing the optimal solutions obtained
print(“Optimal Solutions:”)
for i, val in x.items():
print(“Number of nurses starting their week on day %g = %g” %(i,val.getAttr(“x”)))
Gurobi Optimizer version 9.5.0 build v9.5.0rc5 (mac64[x86])
Thread count: 4 physical cores, 8 logical processors, using up to 8 threads
Optimize a model with 7 rows, 7 columns and 35 nonzeros
Model fingerprint: 0x064cb98b
Coefficient statistics:
Matrix range [1e+00, 1e+00]
Objective range [0e+00, 0e+00]
Bounds range [0e+00, 0e+00]
RHS range [3e+01, 8e+01]
Presolve removed 7 rows and 7 columns
Presolve time: 0.01s
Presolve: All rows and columns removed
Iteration Objective Primal Inf. Dual Inf. Time
0 2.8000000e+01 0.000000e+00 0.000000e+00 0s
Solved in 0 iterations and 0.01 seconds (0.00 work units)
Optimal objective 2.800000000e+01
Optimal Solutions:
Number of nurses starting their week on day 1 = 80
Number of nurses starting their week on day 2 = 0
Number of nurses starting their week on day 3 = 71
Number of nurses starting their week on day 4 = 0
Number of nurses starting their week on day 5 = 0
Number of nurses starting their week on day 6 = 0
Number of nurses starting their week on day 7 = 0
Transportation Problem¶
# Transportation Problem
m_tran = Model(‘transportation’)
# Shipping costs
c = np.array([[5, 3, 2, 6],
[7, 7, 8, 10],
[6, 5, 3, 8]])
s = np.array([1700, 2000, 1700])
d = np.array([1700, 1000, 1500, 1200])
# Declaring variables
var = m_tran.addVars(3,4)
# Setting the objective
m_tran.setObjective(sum(c[i,j] * var[i,j] for i in range(3) for j in range(4)), GRB.MINIMIZE)
# Demand constraints
for j in range(4):
m_tran.addConstr(sum(var[i,j] for i in range(3)) == d[j])
# Supply constraints
for i in range(3):
m_tran.addConstr(sum(var[i,j] for j in range(4)) <= s[i])
# Solving the optimization problem
m_tran.optimize()
# Printing the optimal solutions obtained
print("Optimal Solutions:")
for i, val in var.items():
print("Number of units from plant %g to outlet %g:\t %g " %(i[0]+1, i[1]+1, val.getAttr("x")))
Gurobi Optimizer version 9.5.0 build v9.5.0rc5 (mac64[x86])
Thread count: 4 physical cores, 8 logical processors, using up to 8 threads
Optimize a model with 7 rows, 12 columns and 24 nonzeros
Model fingerprint: 0x0164a657
Coefficient statistics:
Matrix range [1e+00, 1e+00]
Objective range [2e+00, 1e+01]
Bounds range [0e+00, 0e+00]
RHS range [1e+03, 2e+03]
Presolve time: 0.01s
Presolved: 7 rows, 12 columns, 24 nonzeros
Iteration Objective Primal Inf. Dual Inf. Time
0 2.1700000e+04 3.700000e+03 0.000000e+00 0s
5 2.8200000e+04 0.000000e+00 0.000000e+00 0s
Solved in 5 iterations and 0.01 seconds (0.00 work units)
Optimal objective 2.820000000e+04
Optimal Solutions:
Number of units from plant 1 to outlet 1: 0
Number of units from plant 1 to outlet 2: 500
Number of units from plant 1 to outlet 3: 0
Number of units from plant 1 to outlet 4: 1200
Number of units from plant 2 to outlet 1: 1700
Number of units from plant 2 to outlet 2: 300
Number of units from plant 2 to outlet 3: 0
Number of units from plant 2 to outlet 4: 0
Number of units from plant 3 to outlet 1: 0
Number of units from plant 3 to outlet 2: 200
Number of units from plant 3 to outlet 3: 1500
Number of units from plant 3 to outlet 4: 0
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com