Skip to content

Commit

Permalink
conv_ana
Browse files Browse the repository at this point in the history
  • Loading branch information
Abdullah-Al-Zishan committed Jul 11, 2019
1 parent 16a9c75 commit b3cbc30
Show file tree
Hide file tree
Showing 10 changed files with 12,670 additions and 112 deletions.
6 changes: 4 additions & 2 deletions algo.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def __init__(self, DSSObj, env, P, Q, slot_len_in_min=10, start_hr=0, mode='chan
def get_connected(self):
connected = []
for i in range(0, self.env['evNumber']):
if self.current_slot >= self.arrival[i] and self.current_slot <= self.arrival[i] + self.duration[i] and self.remaining_demand[i]/self.slot_len_in_min >= util.tol:
if self.current_slot >= self.arrival[i] and self.current_slot <= self.arrival[i] + self.duration[i] and self.remaining_demand[i] >= util.tol:
connected.append(i)
return np.array(connected)

Expand Down Expand Up @@ -68,7 +68,9 @@ def get_urgent(self, connected):
def get_laxity(self, urgent, scale=1.0):
laxity = []
for u in urgent:
laxity.append((self.arrival[u]+self.claimed_duration[u]-self.current_slot) + scale*self.discrepancy[u] - self.remaining_demand[u]/self.max_rate[u])
l = (self.arrival[u]+self.claimed_duration[u]-self.current_slot) + scale*self.discrepancy[u]
l -= (self.remaining_demand[u]/self.max_rate[u])/self.slot_len_in_min
laxity.append(l)
return np.array(laxity)

def get_driver_type(self, connected):
Expand Down
4 changes: 2 additions & 2 deletions base_line_algo.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ def svf(self, connected, value, A, UB, evNode, phase):
#print(j)
#print(k)
if B[j]>=UB[index] and B[k]>=UB[index]:
power[index]=0.8*UB[index]
power[index]=1.0*UB[index]
else:
power[index]=0.8*np.minimum(B[j],B[k])
power[index]=1.0*np.minimum(B[j],B[k])
#print('bj')
#print(B[j])
#print('bk')
Expand Down
11 changes: 9 additions & 2 deletions central_algo.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def update(self, P, Q):
#LB = np.zeros(len(connected))
#urgent = self.get_urgent(connected)
x = []
w = []
'''
if len(urgent) > 0:
#print('yes')
Expand Down Expand Up @@ -37,7 +38,13 @@ def update(self, P, Q):
#print(A)
#print(self.get_UB(connected))
#x = lb.solve(self.get_driver_type(connected), self.get_claimed(connected), self.get_over_time(connected), self.get_UB(connected), A, T)
x = lb.solve(driver_type=self.get_driver_type(connected), UB=self.get_UB(connected), A=A, T=T)
w = util.f(self.get_driver_type(connected))

#laxity = self.get_laxity(connected)
#w = w * (144-laxity)

x = lb.solve(w=w, UB=self.get_UB(connected), A=A, T=T)
#print(x)
#x = primal.solve(np.zeros(len(connected)), 10*np.ones(len(connected)), A, T)
#x = primal.solve(np.zeros(len(connected)), self.get_UB(connected)-LB+util.tol, A, T)

Expand All @@ -55,7 +62,7 @@ def update(self, P, Q):

self.update_remaining_demand(ev_power)

result = {'trans_load':self.get_trans_load(ev_power, P, Q).tolist(), 'ev_power':ev_power.tolist(), 'x':x, 'connected':connected.tolist(), 'remaining_demand':self.remaining_demand.tolist()}
result = {'trans_load':self.get_trans_load(ev_power, P, Q).tolist(), 'ev_power':ev_power.tolist(), 'x':x, 'connected':connected.tolist(), 'remaining_demand':self.remaining_demand.tolist(), 'w':w}

self.current_slot += 1

Expand Down
2 changes: 1 addition & 1 deletion conv_ana.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def get_TAU(whole=0):

scale = 1e-4

for gamma in tqdm(range(5, 10)):
for gamma in tqdm(range(5, 100)):
gammas.append(gamma)
n_iter = 0

Expand Down
101 changes: 7 additions & 94 deletions decentral_algo.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@

class decentral_algo(algo):
def update(self, P, Q, central={}):

connected = self.get_connected()

w = util.f(self.get_driver_type(connected))
#laxity = self.get_laxity(connected)
#w = w * (144-laxity)

LB = np.zeros(len(connected))

n_iter = 0
Expand Down Expand Up @@ -45,7 +49,7 @@ def update(self, P, Q, central={}):
n_iter = i+1


x = np.minimum(np.maximum(LB, w/np.dot(T.T, lamda)), self.get_UB(connected))
x = np.minimum(np.maximum(LB, w/(np.dot(T.T, lamda)+util.tol)), self.get_UB(connected))

ev_power = np.zeros(self.env['evNumber'])
for j in range(0, len(connected)):
Expand Down Expand Up @@ -90,7 +94,7 @@ def update(self, P, Q, central={}):


print(n_iter)

#print(x)
ev_power = np.zeros(self.env['evNumber'])
for i in range(0, len(connected)):
ev_power[connected[i]] = x[i]
Expand All @@ -104,94 +108,3 @@ def update(self, P, Q, central={}):
#print('decentral')
#print(result['ev_power'])
return result

def convergence(self, P,Q, central={}):

connected = self.get_connected()
LB = np.zeros(len(connected))

n_iter = 0
result = {}

result['params'] = {}
result['gamma'] = {}
'''
urgent = self.get_urgent(connected)
if len(urgent)>0:
T, A, _ = self.get_TAU(urgent, P, Q)
ur_LB = lb.solve(self.get_laxity(urgent), self.params['theta'], self.get_UB(urgent), A, T)
for i in range(0, len(urgent)):
j, = np.where(connected == urgent[i])
LB[j[0]] = ur_LB[i]
'''
if len(connected)>0:
T, A, U = self.get_TAU(connected, P, Q)

m = (np.amax(self.get_UB(connected)))**2
L = np.amax(np.sum(T, axis=0))
S = np.amax(np.sum(T, axis=1))

gamma_star = 2.0/(m*L*S+util.tol)

result['params']['gamma_star'] = gamma_star

lamda = np.zeros(len(A))
x = np.zeros(len(connected))

scale = 0.6e-5
result['params']['scale'] = scale

for gamma in range(250, 500):
n_iter = 0
for i in range(0, self.params['max_iter']):
n_iter = i+1

x = np.minimum(np.maximum(LB, 1.0/np.dot(T.T, lamda), self.get_UB(connected))

ev_power = np.zeros(self.env['evNumber'])
for j in range(0, len(connected)):
ev_power[connected[j]] = x[j]

if self.params['x']==True:
lamda = np.maximum(0.0, lamda - gamma*scale*(A-np.dot(T,x)))
else:
g = np.array(self.env['transRating']) - self.get_trans_load(ev_power,P,Q)
g = np.array([g[e] for e in U])

lamda = np.maximum(0.0, lamda - gamma*scale*g)


#if np.allclose(self.get_trans_load(ev_power,P,Q), central['trans_load'], atol=0.0, rtol=self.params['tol'])==True:
# break
#print(ev_power)
#print(central['ev_power'])
"""
sub = [0,0]
temp = self.get_trans_load(ev_power,P,Q)
sub[0] = central['trans_load'][0]+central['trans_load'][1]+central['trans_load'][2]
sub[1] = temp[0]+temp[1]+temp[2]
#print(abs(sub[1]-sub[0])/sub[0])
if abs(sub[1]-sub[0]) <= self.params['tol']*sub[0]:
break
"""

c = sum(central['ev_power'])
d = sum(ev_power)

if abs(d-c) <= self.params['tol']*c:
break
#if np.allclose(ev_power, central['ev_power'], atol=0.0, rtol=self.params['tol'])==True:
# break
result['gamma'][gamma] = n_iter


#self.update_remaining_demand(ev_power)
#result = {'trans_load':self.get_trans_load(ev_power, P, Q).tolist(), 'ev_power':ev_power.tolist(), 'remaining_demand':self.remaining_demand.tolist(),'gamma':gamma, 'n_iter':n_iter}
#self.current_slot += 1

#print('decentral')
#print(result['ev_power'])
return result
17 changes: 10 additions & 7 deletions lower_bound.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@
import cvxpy as cp
import utility as util

def solve(driver_type, UB, A, T):
w = util.f(driver_type)
def solve(w, UB, A, T):
#w = util.f(driver_type)

x = cp.Variable(len(UB))
obj = cp.Maximize(sum(np.diag(w) * cp.log(x)))
z = cp.Variable(len(UB))

constraints = [0 <= x, x <= UB, T * x <= A]
print(A)
obj = cp.Maximize(sum(np.diag(w) * cp.log(z + 1000)))
#print(T)
#print(A)
constraints = [0 <= z, z <= UB, T * z <= A]
#print(A)
prob = cp.Problem(obj, constraints)

# Solve with MOSEK.
prob.solve(solver=cp.MOSEK, verbose=False)

return x.value
#print(z.value)
return z.value
4 changes: 2 additions & 2 deletions plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ def fig_conv_ana(result_path):
env = util.load_dict(simu_params['env_path'])

#fig_soc_vs_time(simu_params['save_path'], (env['evDriverType']), algo='central')
fig_trans_load_vs_time(simu_params['save_path'], trans=5, env=env)
#fig_compare(simu_params['save_path'], 0, 143, env)
#fig_trans_load_vs_time(simu_params['save_path'], trans=0, env=env)
fig_compare(simu_params['save_path'], 2, 143, env)
#fig_conv_ana('result/meta_large.txt')

12,633 changes: 12,633 additions & 0 deletions result/1000_mosek_3.txt

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion simu.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
DSSObj = DSSStartup.dssstartup('master33Full.dss')

# Initial P and Q
bl_scale = 1.2
bl_scale = 2.0
PQ_dict = util.load_dict(simu_params['base_load_path']+'h'+str(simu_params['start_hr'])+'.txt')
P = bl_scale*np.array(PQ_dict['P'])
Q = bl_scale*np.array(PQ_dict['Q'])
Expand Down
2 changes: 1 addition & 1 deletion simu_params.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
'env_path':'env/1000.txt',

'save_path':'result/1000_mosek_2.txt',
'save_path':'result/1000_mosek_3.txt',

'base_load_path':'base_load/10_min/',

Expand Down

0 comments on commit b3cbc30

Please sign in to comment.