0% found this document useful (0 votes)
33 views3 pages

Minimize Z Subject Z y W X B I Rows Z W X B y I Rows

The document describes solving a linear regression problem using Gurobi. It presents the primal and dual formulations for problems 1(a) and 1(b). For 1(a) which minimizes residuals, the optimal objective is 50. For the dual, the objective is also 50. For 1(b) which minimizes the sum of residuals, the optimal objective is 1000, which also matches the dual formulation. Runtimes for both the primal and dual problems are provided.

Uploaded by

Soumojit Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views3 pages

Minimize Z Subject Z y W X B I Rows Z W X B y I Rows

The document describes solving a linear regression problem using Gurobi. It presents the primal and dual formulations for problems 1(a) and 1(b). For 1(a) which minimizes residuals, the optimal objective is 50. For the dual, the objective is also 50. For 1(b) which minimizes the sum of residuals, the optimal objective is 1000, which also matches the dual formulation. Runtimes for both the primal and dual problems are provided.

Uploaded by

Soumojit Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

1.

(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))

## Adding Constraints for Question (1a)


model_1a.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_1a.addConstrs((z_a+e[row] >= 0) for row in range(rows))
model_1a.addConstrs((z_a-e[row] >= 0) for row in range(rows))

## Solving for 1(a)


model_1a.update()
model_1a.setObjective(z_a, GRB.MINIMIZE)
model_1a.update()
model_1a.optimize()
Model_Output_Display(model_1a)

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

2.(a)Dual of 1(a): Maximize ∑ y i (r i −s i) subject ¿


i

∑ x ij (r i−si )=0 ∀ j ∈ columns


i
∑ (r i−s i)=0
i

∑ (r i + si )=1
i

2.(b) Dual of 1(b): Maximize ∑ y i (r i −s i) subject ¿


i

∑ x ij (r i−si )=0 ∀ j ∈ columns


i

∑ (r i−s i)=0 ∀ i∈ columns


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.

Runtime of 1(a): 0.01 secs


Runtime of dual of 1(a): 0.01 secs
Runtime of 1(b):0.07 secs
Runtime of dual of 1(b): 0.01 secs

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy