Skip to content

Commit

Permalink
Parallel using Classes
Browse files Browse the repository at this point in the history
  • Loading branch information
bhushan23 committed Apr 30, 2018
1 parent cc74f9a commit c42f398
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions admm.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,22 @@
from numpy.linalg import inv
from numpy.linalg import norm
from joblib import Parallel, delayed
import multiprocessing
from multiprocessing import Process, Manager, cpu_count


class SolveIndividual:
def solve(self, A, b, nu, rho, Z):
t1 = A.dot(A.T)
A = A.reshape(-1, 1)
tX = (A * b + rho * Z - nu) / (t1 + rho)
return tX

class CombineSolution:
def combine(self, nuBar, xBar, Z, rho):
t = nuBar.reshape(-1, 1)
t = t + rho * (xBar.reshape(-1, 1) - Z)
return t.T


class ADMM:
def __init__(self, A, b, parallel = False):
Expand All @@ -19,7 +34,7 @@ def __init__(self, A, b, parallel = False):
self.b = b
self.alpha = 0.01
self.parallel = parallel
self.numberOfThreads = multiprocessing.cpu_count()
self.numberOfThreads = cpu_count()

def step(self):
if self.parallel:
Expand All @@ -34,19 +49,12 @@ def step(self):
self.nu = self.nu + self.rho * (self.X - self.Z)

def solveIndividual(self, i):
A = self.A[i]
b = np.asscalar(self.b[i])
nu = self.nuBar[i].reshape(-1, 1)
t1 = A.dot(A.T)
A = A.reshape(-1, 1)
tX = (A * b + self.rho * self.Z - nu) / (t1 + self.rho)
return tX
solve = SolveIndividual()
return solve.solve(self.A[i], np.asscalar(self.b[i]), self.nuBar[i].reshape(-1, 1), self.rho, self.Z)

def combineSolution(self, i):
t = self.nuBar[i].reshape(-1, 1)
t = t + self.rho * (self.XBar[i].reshape(-1, 1) - self.Z)
self.nuBar[i] = t.T

combine = CombineSolution()
return combine.combine(self.nuBar[i].reshape(-1, 1), self.XBar[i].reshape(-1, 1), self.Z, self.rho)

def step_parallel(self):
# Solve for X_t+1
Expand Down

0 comments on commit c42f398

Please sign in to comment.