Minimize Z Subject Z y W X B I Rows Z W X B y I Rows
Minimize Z Subject Z y W X B I Rows Z W X B y I Rows
(a)
Minimize z subject ¿
z ≥ y i−∑ w j x j −bi ∀ i ∈rows
j
z ≥ ∑ w j x j +b i− y i ∀ i∈ rows
j
Code:
import pandas as pd
from gurobipy import *
import os
os.chdir('/Users/z001ys6/Downloads/PA')
d = pd.read_csv("dataLR.txt",header=None)
d.columns =["y","x1","x2","x3","x4","x5","x6","x7","x8","x9","x10"]
rows, cols = d.shape
## 1(a)
model_1a = Model("Akhil_1a")
##Declaring Variables for 1(a)
w = model_1a.addVars(cols-1,lb = -GRB.INFINITY,ub = GRB.INFINITY,name="w")
b = model_1a.addVar(lb = -GRB.INFINITY,ub = GRB.INFINITY,name="b")
e = model_1a.addVars(rows,lb = -GRB.INFINITY,ub = GRB.INFINITY,name="e")
z_a = model_1a.addVar(lb = -GRB.INFINITY,ub = GRB.INFINITY,name="z_a")
model_1a.update()
def Model_Output_Display(model):
if model.status == GRB.Status.OPTIMAL:
print('\nObjective_Value: %g' % model.objVal)
w_x = model.getAttr('x', w)
for f in range(1,cols):
print('%s %g' % ('w'+str(f)+': ', w_x[f-1]))
print('%s %g' % ('b: ', b.x))
Answer:
Objective_Value: 50
w1: -9
w2: 1.04376e-13
w3: -12
w4: 28
w5: -25
w6: 6
w7: 21
w8: 64
w9: 36
w10: 100
b: 77
1.(b) Minimize ∑ zi subject ¿
i
z i ≥ y i−∑ w j x j −bi ∀ i ∈rows
j
z i ≥ ∑ w j x j +bi − y i ∀ i∈ rows
j
Code:
import pandas as pd
from gurobipy import *
import os
os.chdir('/Users/z001ys6/Downloads/PA')
d = pd.read_csv("dataLR.txt",header=None)
d.columns =["y","x1","x2","x3","x4","x5","x6","x7","x8","x9","x10"]
rows, cols = d.shape
def Model_Output_Display(model):
if model.status == GRB.Status.OPTIMAL:
print('\nObjective_Value: %g' % model.objVal)
w_x = model.getAttr('x', w)
for f in range(1,cols):
print('%s %g' % ('w'+str(f)+': ', w_x[f-1]))
print('%s %g' % ('b: ', b.x))
## 1(b)
model_1b = Model("Akhil_1b")
##Declaring Variables for 1(b)
w = model_1b.addVars(cols-1,lb = -GRB.INFINITY,ub = GRB.INFINITY,name="w")
b = model_1b.addVar(lb = -GRB.INFINITY,ub = GRB.INFINITY,name="b")
e = model_1b.addVars(rows,lb = -GRB.INFINITY,ub = GRB.INFINITY,name="e")
z_b = model_1b.addVars(rows,lb = -GRB.INFINITY,ub = GRB.INFINITY,name="z_b")
## Adding Constraints for Question (1b)
model_1b.addConstrs((quicksum(row[j+1]*w[j-1] for j in range(1,cols))+b+e[row.Index] == row[1] for row in
d.itertuples()), "Error_Expression")
model_1b.addConstrs((z_b[row]+e[row] >= 0) for row in range(rows))
model_1b.addConstrs((z_b[row]-e[row] >= 0) for row in range(rows))
## Solving for 1(b)
model_1b.setObjective(sum(z_b), GRB.MINIMIZE)
model_1b.update()
model_1b.optimize()
Model_Output_Display(model_1b)
Output:
Objective_Value: 1000
w1: -9
w2: 1.04311e-13
w3: -12
w4: 28
w5: -25
w6: 6
w7: 21
w8: 64
w9: 36
w10: 100
b: 27
∑ (r i + si )=1
i
∑ (r i + si )=1 ∀ i ∈columns
i
From the code, both primal and duals of 1(a) and 1(b) match. Dual and Primal of 1(a)
matches to an objective value of 50, whereas both dual and primal of 1(b) matches to
1000.