0% found this document useful (0 votes)
67 views8 pages

HW4 - Question7 - Jupyter Notebook

This document discusses solving optimization problems to maximize and minimize objectives subject to constraints. It presents three problems: 1) maximizing price with constraints on portfolio quantities, 2) minimizing portfolio cost with price and quantity constraints, and 3) minimizing price with portfolio quantity constraints. The problems are modeled and solved using the Gurobi optimization solver, and optimal solutions and objective values are reported.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views8 pages

HW4 - Question7 - Jupyter Notebook

This document discusses solving optimization problems to maximize and minimize objectives subject to constraints. It presents three problems: 1) maximizing price with constraints on portfolio quantities, 2) minimizing portfolio cost with price and quantity constraints, and 3) minimizing price with portfolio quantity constraints. The problems are modeled and solved using the Gurobi optimization solver, and optimal solutions and objective values are reported.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

10/31/21, 11:01 PM 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

In [1]: import numpy as np


import gurobipy as grb
import matplotlib.pyplot as plt
import pandas as pd
executed in 312ms, finished 23:00:04 2021-10-31

In [2]: ▾ ## Question 7 (b)



nx = 1
nq = 3


# Gurobi Model
#
m = grb.Model("q7")

P = m.addVar(lb=0.0, name="P")
m.update()

q = {}
▾ for j in range(1,nq+1):
q[j] = m.addVar(lb=0.0001, name="q["+str(j)+"]")
m.update()
m
executed in 9ms, finished 23:00:04 2021-10-31

Academic license - for non-commercial use only - expires 2022-08-30

Using license file /Users/suvrasoumya.mohanty/gurobi.lic

Out[2]: <gurobi.Model Continuous instance q7: 0 constrs, 4 vars, No parameter cha


nges>

localhost:8888/notebooks/Downloads/HW4_Question7.ipynb 1/8
10/31/21, 11:01 PM HW4_Question7 - Jupyter Notebook

In [3]: ▾ # Add objective


m.setObjective(P, grb.GRB.MAXIMIZE)
m.update()

# add constraints
m.addConstr(10/9*q[1] + 10/9*q[2] + 10/9*q[3] == 1, name = "const1")
m.addConstr(20/3*q[1] + 49/9*q[2] + 10/3*q[3] == 5, name = "const2")
m.addConstr(3.5*q[1] + 5*q[2] + 2*q[3] == P, name = "const3")

m.update()

m.optimize()

▾ for v in m.getVars():
print("%s=%f"%(v.varName, v.x))

print('Obj:', m.objVal)
executed in 14ms, finished 23:00:04 2021-10-31

Gurobi Optimizer version 9.1.2 build v9.1.2rc0 (mac64)


Thread count: 8 physical cores, 8 logical processors, using up to 8 threa
ds

Optimize a model with 3 rows, 4 columns and 10 nonzeros

Model fingerprint: 0xef0ffdab

Coefficient statistics:

Matrix range [1e+00, 7e+00]

Objective range [1e+00, 1e+00]

Bounds range [1e-04, 1e-04]

RHS range [1e+00, 5e+00]

Presolve removed 1 rows and 1 columns

Presolve time: 0.00s

Presolved: 2 rows, 3 columns, 6 nonzeros

Iteration Objective Primal Inf. Dual Inf. Time

0 4.4995500e+00 1.251111e-02 0.000000e+00 0s

1 4.3767136e+00 0.000000e+00 0.000000e+00 0s

Solved in 1 iterations and 0.01 seconds

Optimal objective 4.376713636e+00

P=4.376714

q[1]=0.081991

q[2]=0.817909

q[3]=0.000100

Obj: 4.376713636363637

 1.2  Minimizing portfolio cost

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

Out[4]: <gurobi.Model Continuous instance q7b: 0 constrs, 2 vars, No parameter ch


anges>

localhost:8888/notebooks/Downloads/HW4_Question7.ipynb 3/8
10/31/21, 11:01 PM HW4_Question7 - Jupyter Notebook

In [5]: ▾ # Add objective


m.setObjective(1*x[1]+5*x[2], grb.GRB.MINIMIZE)
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()
m.optimize()


▾ for v in m.getVars():
print("%s=%f"%(v.varName, v.x))

print('Obj:', m.objVal)
executed in 9ms, finished 23:00:04 2021-10-31

Gurobi Optimizer version 9.1.2 build v9.1.2rc0 (mac64)


Thread count: 8 physical cores, 8 logical processors, using up to 8 threa
ds

Optimize a model with 3 rows, 2 columns and 6 nonzeros


Model fingerprint: 0x16807eed

Coefficient statistics:

Matrix range [1e+00, 7e+00]

Objective range [1e+00, 5e+00]

Bounds range [0e+00, 0e+00]

RHS range [2e+00, 5e+00]

Presolve removed 3 rows and 2 columns

Presolve time: 0.00s

Presolve: All rows and columns removed

Iteration Objective Primal Inf. Dual Inf. Time

0 4.3772727e+00 0.000000e+00 0.000000e+00 0s

Solved in 0 iterations and 0.00 seconds

Optimal objective 4.377272727e+00

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

In [6]: ▾ ## Question 7 (c)



nx = 1
nq = 3


# Gurobi Model
#
m = grb.Model("q7c")

P = m.addVar(lb=0.0, name="P")
m.update()

q = {}
▾ for j in range(1,nq+1):
q[j] = m.addVar(lb=0.000001, name="q["+str(j)+"]")
m.update()

executed in 2ms, finished 23:00:04 2021-10-31

localhost:8888/notebooks/Downloads/HW4_Question7.ipynb 5/8
10/31/21, 11:01 PM HW4_Question7 - Jupyter Notebook

In [7]: ▾ # Add objective


m.setObjective(P, grb.GRB.MINIMIZE)
m.update()

# add constraints
m.addConstr(10/9*q[1] + 10/9*q[2] + 10/9*q[3] == 1, name = "const1")
m.addConstr(20/3*q[1] + 49/9*q[2] + 10/3*q[3] == 5, name = "const2")
m.addConstr(3.5*q[1] + 5*q[2] + 2*q[3] == P, name = "const3")

m.update()
m.optimize()

▾ for v in m.getVars():
print("%s=%f"%(v.varName, v.x))

print('Obj:', m.objVal)
executed in 9ms, finished 23:00:04 2021-10-31

Gurobi Optimizer version 9.1.2 build v9.1.2rc0 (mac64)


Thread count: 8 physical cores, 8 logical processors, using up to 8 threa
ds

Optimize a model with 3 rows, 4 columns and 10 nonzeros

Model fingerprint: 0xa27891b7

Coefficient statistics:

Matrix range [1e+00, 7e+00]

Objective range [1e+00, 1e+00]

Bounds range [1e-06, 1e-06]

RHS range [1e+00, 5e+00]

Presolve removed 1 rows and 1 columns

Presolve time: 0.00s

Presolved: 2 rows, 3 columns, 6 nonzeros

Iteration Objective Primal Inf. Dual Inf. Time

0 1.8000045e+00 2.499993e-01 0.000000e+00 0s

1 2.7000020e+00 0.000000e+00 0.000000e+00 0s

Solved in 1 iterations and 0.00 seconds

Optimal objective 2.700002050e+00

P=2.700002

q[1]=0.599999

q[2]=0.000001

q[3]=0.300000

Obj: 2.7000020499999997

 2.2  Maximizing portfolio cost

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

Gurobi Optimizer version 9.1.2 build v9.1.2rc0 (mac64)


Thread count: 8 physical cores, 8 logical processors, using up to 8 threa
ds

Optimize a model with 3 rows, 2 columns and 6 nonzeros


Model fingerprint: 0xd6598379

Coefficient statistics:

Matrix range [1e+00, 7e+00]

Objective range [1e+00, 5e+00]

Bounds range [0e+00, 0e+00]

RHS range [2e+00, 5e+00]

Presolve removed 3 rows and 2 columns

Presolve time: 0.00s

Presolve: All rows and columns removed

Iteration Objective Primal Inf. Dual Inf. Time

0 2.7000000e+00 0.000000e+00 0.000000e+00 0s

Solved in 0 iterations and 0.00 seconds

Optimal objective 2.700000000e+00

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

You might also like