Professional Documents
Culture Documents
HW4 - Question7 - Jupyter Notebook
HW4 - Question7 - Jupyter Notebook
Table of Contents
1 Problem 7 (b)
1.1 Maximising Price
1.2 Minimizing portfolio cost
2 Problem 7 (c)
2.1 Minimising Price
2.2 Maximizing portfolio cost
1 Problem 7 (b)
1.1 Maximising Price
localhost:8888/notebooks/Downloads/HW4_Question7.ipynb 1/8
10/31/21, 11:01 PM HW4_Question7 - Jupyter Notebook
print('Obj:', m.objVal)
executed in 14ms, finished 23:00:04 2021-10-31
Coefficient statistics:
P=4.376714
q[1]=0.081991
q[2]=0.817909
q[3]=0.000100
Obj: 4.376713636363637
localhost:8888/notebooks/Downloads/HW4_Question7.ipynb 2/8
10/31/21, 11:01 PM HW4_Question7 - Jupyter Notebook
In [4]: nx = 2
# Gurobi Model
#
m = grb.Model("q7b")
x = {}
▾ for j in range(1,nx+1):
x[j] = m.addVar(vtype = grb.GRB.CONTINUOUS,lb=-grb.GRB.INFINITY , name
m.update()
m
executed in 4ms, finished 23:00:04 2021-10-31
localhost:8888/notebooks/Downloads/HW4_Question7.ipynb 3/8
10/31/21, 11:01 PM HW4_Question7 - Jupyter Notebook
print('Obj:', m.objVal)
executed in 9ms, finished 23:00:04 2021-10-31
Coefficient statistics:
x[1]=10.513636
x[2]=-1.227273
Obj: 4.377272727272728
2 Problem 7 (c)
2.1 Minimising Price
localhost:8888/notebooks/Downloads/HW4_Question7.ipynb 4/8
10/31/21, 11:01 PM HW4_Question7 - Jupyter Notebook
localhost:8888/notebooks/Downloads/HW4_Question7.ipynb 5/8
10/31/21, 11:01 PM HW4_Question7 - Jupyter Notebook
print('Obj:', m.objVal)
executed in 9ms, finished 23:00:04 2021-10-31
Coefficient statistics:
P=2.700002
q[1]=0.599999
q[2]=0.000001
q[3]=0.300000
Obj: 2.7000020499999997
localhost:8888/notebooks/Downloads/HW4_Question7.ipynb 6/8
10/31/21, 11:01 PM HW4_Question7 - Jupyter Notebook
In [8]:
nx = 2
# Gurobi Model
#
m = grb.Model("q7c2")
x = {}
▾ for j in range(1,nx+1):
x[j] = m.addVar(vtype = grb.GRB.CONTINUOUS,lb=-grb.GRB.INFINITY ,name=
m.update()
# Add objective
m.setObjective(1*x[1]+5*x[2], grb.GRB.MAXIMIZE)
m.update()
# add constraints
m.addConstr(10/9*x[1] + 20/3*x[2] <= 3.5, name = "const1")
m.addConstr(10/9*x[1] + 49/9*x[2] <= 5, name = "const2")
m.addConstr(10/9*x[1] + 10/3*x[2] <=2, name = "const3")
m.update()
executed in 4ms, finished 23:00:04 2021-10-31
In [9]: m.optimize()
▾ for v in m.getVars():
print("%s=%f"%(v.varName, v.x))
print('Obj:', m.objVal)
executed in 6ms, finished 23:00:04 2021-10-31
Coefficient statistics:
x[1]=0.450000
x[2]=0.450000
Obj: 2.7
localhost:8888/notebooks/Downloads/HW4_Question7.ipynb 7/8
10/31/21, 11:01 PM HW4_Question7 - Jupyter Notebook
In [ ]:
localhost:8888/notebooks/Downloads/HW4_Question7.ipynb 8/8