Answer To: Microsoft Word - Assignment 2.docx MATH7232OperationsResearch&MathematicalPlanning2018...
Abr Writing answered on Apr 09 2020
Optimization.html
Importing Packages¶
In [1]:
import pandas as pd
import numpy as np
from gurobipy import *
import warnings
warnings.filterwarnings("ignore")
Data¶
In [2]:
nrows = 8
ncols = 3
init = [3200, 4000, 3800]
fileHandle = 'Demand.xlsx'
df = pd.read_excel(fileHandle)
df
Out[2]:
Quarter Brisbane Melbourne Adelaide Cost
0 Q1 1700 1950 2750 832
1 Q2 2550 3000 2850 965
2 Q3 2900 2400 1200 968
3 Q4 2800 1600 2250 874
4 Q5 1850 2200 2900 966
5 Q6 2050 3200 1950 1007
6 Q7 3300 3150 900 827
7 Q8 2650 2150 1700 914
Variables¶
In [3]:
m = Model("mip1")
var = {}
for row in range(nrows):
for col in range(ncols):
name = str(row) + str(col)
var[name] = m.addVar(ub=10000, name=name)
var[name].vType = GRB.INTEGER
m.update()
In [4]:
idx = 0
for row in range(nrows):
for col in range(ncols):
name = str(row) + str(col)
colname = df.columns[col+1]
if idx-3 >= 0:
init.append(init[idx-3] + var[name] - df.iloc[row][colname])
idx += 1
else:
init[idx] = init[idx] + var[name] - df.iloc[row][colname]
idx += 1
Objective¶
In [5]:
obj = 0
idx = 0
for row in range(nrows):
for col in range(ncols):
name = str(row) + str(col)
colname = df.columns[col+1]
obj += var[name]*df.Cost[row] + init[idx]*35
idx += 1
m.setObjective(obj)
Constraints¶
In [6]:
contraint = 0
for row in range(nrows):
m.addConstr(var[str(row) + str(0)] + var[str(row) + str(1)] + var[str(row) + str(2)] <= 10000, "c"+str(contraint))
contraint += 1
idx = 0
for row in range(nrows):
for col in range(ncols):
name = str(row) + str(col)
colname = df.columns[col+1]
m.addConstr(init[idx] >= 0, "c"+str(contraint))
contraint += 1
idx += 1
Optimization¶
In [7]:
m.optimize()
for v in m.getVars():
print('%s %g' % (v.varName, v.x))
print('Obj: %g' % obj.getValue())
Optimize a model with 32 rows, 24 columns and 132 nonzeros
Variable types: 0 continuous, 24 integer (0 binary)
Coefficient statistics:
Matrix range [1e+00, 1e+00]
Objective range [9e+02, 1e+03]
Bounds range [1e+04, 1e+04]
RHS range [1e+03, 2e+04]
Found heuristic solution: objective 4.279545e+07
Presolve removed 3 rows and 0 columns
Presolve time: 0.00s
Presolved: 29 rows, 24 columns, 129 nonzeros
Variable types: 0 continuous, 24 integer (0 binary)
Root relaxation: objective 4.076690e+07, 19 iterations, 0.00 seconds
Nodes | Current Node | Objective Bounds | Work
Expl Unexpl | Obj Depth IntInf | Incumbent BestBd Gap | It/Node Time
* 0 0 0 4.076690e+07 4.0767e+07 0.00% - 0s
Explored 0 nodes (19 simplex iterations) in 0.08 seconds
Thread count was 4 (of 4 available processors)
Solution count 2: 4.07669e+07 4.27955e+07
Optimal solution found (tolerance 1.00e-04)
Best objective 4.076690000000e+07, best bound 4.076690000000e+07, gap 0.0000%
00 3650
01 3350
02 3000
10 -0
11 -0
12 -0
20 300
21 -0
22 -0
30 5900
31 1850
32 2250
40 -0
41 5150
42 4850
50 800
51 -0
52 -0
60 3300
61 4100
62 2600
70 2650
71 1200
72 -0
Obj: 4.07669e+07
Result¶
In [8]:
db = df
for v in m.getVars():
row = int(list(str(v.varName))[0])
col = int(list(str(v.varName))[1])
colname = db.columns[col+1]
db[colname][row] = v.x
db
Out[8]:
Quarter Brisbane Melbourne Adelaide Cost
0 Q1 3650 3350 3000 832
1 Q2 0 0 0 965
2 Q3 300 0 0 968
3 Q4 5900 1850 2250 874
4 Q5 0 5150 4850 966
5 Q6 800 0 0 1007
6 Q7 3300 4100 2600 827
7 Q8 2650 1200 0 914
Boss.docx
Importing Packages
import pandas as pd
import numpy as np
from gurobipy import *
import warnings
warnings.filterwarnings("ignore")
Data
nrows = 8
ncols = 3
init = [3200, 4000, 3800]
fileHandle = 'Demand.xlsx'
df = pd.read_excel(fileHandle)
df
Quarter
Brisbane
Melbourne
Adelaide
Cost
0
Q1
1700
1950
2750
832
1
Q2
2550
3000
2850
965
2
Q3
2900
2400
1200
968
3
Q4
2800
1600
2250
874
4
Q5
1850
2200
2900
966
5
Q6
2050
3200
1950
1007
6
Q7
3300
3150
900
827
7
Q8
2650
2150
1700
914
Variables¶
Defining the variables. There are in total 24 (3 X 8) variables as there are 3 cities and 8 quarters.
m = Model("mip1")
var = {} for row in range(nrows):
for col in range(ncols):
name = str(row) + str(col)
var[name] = m.addVar(ub=10000, name=name)
var[name].vType = GRB.INTEGER
m.update()
Defining the number of barrels left at every city at the end of every quarter.
idx = 0
for row in range(nrows):
for col in range(ncols):
name = str(row) + str(col)
colname = df.columns[col+1]
if idx-3 >= 0:
init.append(init[idx-3] + var[name] - df.iloc[row][colname])
idx += 1
else:
init[idx] = init[idx] + var[name] - df.iloc[row][colname]
idx += 1
Objective
Objective is to minimize the total cost of buying new barrels and concentrating the existing ones with a summation over every city and every quarter.
obj = 0
idx = 0
for row in range(nrows):
for col in range(ncols):
name = str(row) + str(col)
colname = df.columns[col+1]
obj += var[name]*df.Cost[row] + init[idx]*35
idx += 1
m.setObjective(obj)
Constraints
There are two types of constraints:
1. Total number of barrels imported in any quarter (sum over all the cities) should be less than, equal to 10000.
2. The number of barrels left after any quarter in any city should be greater than or equal to zero.
constraint = 0
for row in range(nrows):
m.addConstr(var[str(row) + str(0)] + var[str(row) + str(1)] + var[str(row) + str(2)] <= 10000, "c"+str(constraint))
constraint += 1
idx = 0
for row in range(nrows):
for col in range(ncols):
name = str(row) + str(col)
colname = df.columns[col+1]
m.addConstr(init[idx] >= 0, "c"+str(constraint))
constraint += 1
idx += 1
Optimization¶
m.optimize()
for v in m.getVars():
print('%s %g' % (v.varName, v.x)
print('Obj: %g' % obj.getValue())
Optimize a model with 32 rows, 24 columns and 132 nonzeros
Variable types: 0 continuous, 24 integer (0 binary)
Coefficient statistics:
Matrix range [1e+00, 1e+00]
Objective range [9e+02, 1e+03]
Bounds range [1e+04, 1e+04]
RHS range [1e+03, 2e+04]
Found heuristic solution: objective 4.279545e+07
Presolve removed 3 rows and 0 columns
Presolve time: 0.00s
Presolved: 29 rows, 24 columns, 129 nonzeros
Variable types: 0 continuous, 24 integer (0 binary)
Root relaxation: objective 4.076690e+07, 19 iterations, 0.00 seconds
Nodes | Current Node | Objective Bounds | ...