# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

import numpy as np
import matplotlib.pyplot as plt
import pickle

f = open('/home/xw17070/Documents/data_caching_coding/finished_jobs_bl.txt', 'rb')
finished_jobs = pickle.load(f)
f = open('/home/xw17070/Documents/data_caching_coding/finished_jobs_size_bl.txt', 'rb')
finished_job_size = pickle.load(f)
f = open('/home/xw17070/Documents/data_caching_coding/exceed_deadlines_bl.txt', 'rb')
exceed_deadlines = pickle.load(f)
f = open('/home/xw17070/Documents/data_caching_coding/exceed_deadline_size_bl.txt', 'rb')
exceed_deadline_size = pickle.load(f)

# +
num_ep = 20
print('average finished jobs in an ep:', finished_jobs[0]/num_ep)
print('ave TP in an ep:', np.sum(finished_job_size)/num_ep*1)

print('average number of lost jobs:', exceed_deadlines[0]/num_ep)
print('average TP of lost jobs:', np.sum(exceed_deadline_size)/num_ep*1)
print('finished rate (in number of jobs):', finished_jobs[0]/(finished_jobs[0]+exceed_deadlines[0]))
print('finished rate (in TP):', np.sum(finished_job_size)/(np.sum(finished_job_size)+np.sum(exceed_deadline_size)))
# -









a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

a[3:-2]

a = np.reshape([0, 0, 1, 1, 2, 2, 3, 3], (2, 4))

a

a[0][1]







f = open('/home/xw17070/Documents/data_caching_hie_2/finished_jobs_DQN.txt', 'rb')
finished_jobs = pickle.load(f)
f = open('/home/xw17070/Documents/data_caching_hie_2/lost_jobs_DQN.txt', 'rb')
lost_jobs = pickle.load(f)

# +
finished_eps = []
finished_TP = []
for ii in range(0, len(finished_jobs)):
    finished_eps.append(len(finished_jobs[ii]))
    finished_TP.append(sum(finished_jobs[ii]))

plt.plot(finished_eps)
plt.xlabel('eps')
plt.title('number of finished jobs')
print('average finished jobs in an ep:', np.mean(finished_eps))

plt.figure()
plt.plot(finished_TP)
plt.xlabel('eps')
plt.title('throughput of finished jobs')
print('ave TP in an ep:', np.mean(finished_TP))

range_lost = []
ddl_lost = []
for jj in range(0, len(lost_jobs)):
    cur_range_lost = lost_jobs[jj][0]
    cur_ddl_lost = lost_jobs[jj][1]
    range_lost.append(len(cur_range_lost))
    ddl_lost.append(len(cur_ddl_lost))

plt.figure()
plt.plot(range_lost)
print('average lost due to range:', np.mean(range_lost))
plt.plot(ddl_lost)
print('average lost due to deadline:', np.mean(ddl_lost))
# -

np.mean(finished_eps) / (np.mean(range_lost) + np.mean(ddl_lost) + np.mean(finished_eps))





# +
f = open('/home/xw17070/Documents/data_caching_hie/rewards_list.txt', 'rb')
rewards_list = pickle.load(f)

def running_mean(x, N):
    cumsum = np.cumsum(np.insert(x, 0, 0)) 
    return (cumsum[N:] - cumsum[:-N]) / N

rewards_list = rewards_list[10:]
eps, rews = np.array(rewards_list).T
smoothed_rews = running_mean(rews, 20)
plt.plot(eps[-len(smoothed_rews):], smoothed_rews)
plt.plot(eps, rews, color='grey', alpha=0.3)
plt.xlabel('Episode')
plt.ylabel('Total Reward')

# f = open('/home/xw17070/Documents/data_caching_hie/losses_list.txt', 'rb')
# loss_list = pickle.load(f)

# plt.figure()
# plt.plot(range(0,len(loss_list)), loss_list)
# -

np.mean(rewards_list[-50:])

rewards_list[-50:2:]





# +
def running_mean(x, N):
    cumsum = np.cumsum(np.insert(x, 0, 0)) 
    return (cumsum[N:] - cumsum[:-N]) / N

rewards_list = rewards_list[10:]
eps, rews = np.array(rewards_list).T
smoothed_rews = running_mean(rews, 20)
plt.plot(eps[-len(smoothed_rews):], smoothed_rews)
plt.plot(eps, rews, color='grey', alpha=0.3)
plt.xlabel('Episode')
plt.ylabel('Total Reward')

plt.figure()
plt.plot(range(0,len(loss_list)), loss_list)

StarcoderdataJupyterScriptsDedupFiltered