Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. from operator import itemgetter
  2.  
  3. rawFile = open("kittens.txt", "r")
  4. fileCon = [lines.replace("\n", "").split(" ") for lines in rawFile]
  5. rawFile.close()
  6.  
  7. V = int(fileCon[0][0])
  8. E = int(fileCon[0][1])
  9. R = int(fileCon[0][2])
  10. C = int(fileCon[0][3])
  11. X = int(fileCon[0][4])
  12.  
  13. Vs = [int(x) for x in fileCon[1]]
  14. Ess = []
  15.  
  16. start = 2
  17. for i in range(E):
  18. temp = int(fileCon[start][1])
  19. temp2 = [-1 for x in range(C)]
  20. temp3 = (int(fileCon[start][0]))
  21. for x in range(temp):
  22. start += 1
  23. # print(fileCon[start])
  24. assert (int(fileCon[start][0]) < len(temp2))
  25. temp2[int(fileCon[start][0])] = int(fileCon[start][1])
  26. Ess.append((temp3, temp2))
  27. start += 1
  28.  
  29. Rs = [[-1 for x in range(E)] for y in range(V)]
  30. ARs = []
  31. counter = 0
  32. tempA = []
  33. for k in range(start, len(fileCon)):
  34. temp4 = [int(i) for i in fileCon[k]]
  35. Rs[temp4[0]][temp4[1]] = temp4[2]
  36. # assert(counter < len(Vs))
  37. tempA.append([temp4[2], Vs[temp4[0]], temp4[1], temp4[0]])
  38. counter += 1
  39.  
  40. tempA.sort()
  41. tempA.reverse()
  42.  
  43. for x in range(len(tempA)):
  44. tempA[x].reverse()
  45.  
  46. tempC = []
  47. key = tempA[0][3]
  48. tempB = []
  49. tempB.append([tempA[0][2], tempA[0][3], tempA[0][1], tempA[0][0]])
  50. for y in range(1, len(tempA) + 1):
  51. if y == len(tempA):
  52. key = -1
  53. y = 0
  54. if tempA[y][3] == key:
  55. tempB.append([tempA[y][2], tempA[y][3], tempA[y][1], tempA[y][0]])
  56. # print (tempB,"here")
  57. else:
  58. tempB.sort()
  59. for x in tempB:
  60. tempC.append(x)
  61. # tempC.append(tempB[:])
  62. # print (tempC)
  63. tempB = []
  64. tempB.append([tempA[y][2], tempA[y][3], tempA[y][1], tempA[y][0]])
  65. key = tempA[y][3]
  66.  
  67. # print(tempC)
  68. tempD = [[] for x in range(len(tempC))]
  69.  
  70. # print (len(tempC[0]))
  71. for x in range(len(tempC)):
  72. tempD[x] = [tempC[x][3], tempC[x][2], tempC[x][0], tempC[x][1]]
  73.  
  74. ARS = tempD[:]
  75.  
  76.  
  77.  
  78.  
  79. ##########################################################################
  80. solution = []
  81. center_lat=[] # list of all the latency values for the data center
  82.  
  83.  
  84.  
  85. cache_Num = C #number of caches
  86. end_Num = E #number of endpoints
  87. vid_Num = V # number of videos
  88.  
  89. #list of video size
  90. vid_size = Vs
  91. #size of the chahces
  92. cache_size = X
  93.  
  94. cache_count=0
  95. while cache_count < cache_Num:
  96. #solution for the problem
  97. solution.append([])
  98. #list just for this cache
  99.  
  100. force_list = []
  101.  
  102.  
  103. vid_count=0
  104. while vid_count<vid_Num:
  105. end_count = 0
  106.  
  107. force_list.append([vid_count, 0])
  108. while end_count < end_Num:
  109. #populate the force list table
  110. force_list[vid_count][1]+=Rs[vid_count][end_count]*(Ess[end_count][0]-Ess[end_count][1][cache_count])
  111. end_count+=1
  112.  
  113. vid_count += 1
  114.  
  115. #sort list based off second column
  116.  
  117. force_list = sorted(force_list, key=itemgetter(1), reverse=True)
  118.  
  119. force_count = 0
  120. mem_used=0
  121. while force_count < vid_Num:
  122. vid_indx = force_list[force_count][0] #should loop through each video index
  123. used = 0
  124. if (mem_used + vid_size[vid_indx])<= cache_size:
  125. used=1
  126. mem_used += vid_size[vid_indx]
  127. solution[cache_count].append(vid_indx)
  128.  
  129.  
  130.  
  131. force_count+=1
  132.  
  133. print( str(cache_count) + "/" + str(cache_Num))
  134.  
  135. cache_count+=1
  136. fileOut=open("output.txt","w")
  137.  
  138. print(str(cache_Num),file=fileOut)
  139. for i in range (cache_Num):
  140. print(i,file=fileOut,end=" ")
  141. for j in solution[i]:
  142. print(j,file=fileOut, end= " ")
  143. print(file=fileOut)
  144.  
  145. fileOut.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement