Advertisement
Guest User

hehh

a guest
Apr 7th, 2020
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.67 KB | None | 0 0
  1. import numpy as np
  2. import pandas as pd
  3. import threading
  4. import os
  5. from threading import Lock
  6. from influxdb import InfluxDBClient
  7. from optimal_downsampling_manager.decision_type import Decision
  8. from optimal_downsampling_manager.resource_predictor.table_estimator import IATable,AnalyTimeTable
  9. from optimal_downsampling_manager.resource_predictor.table_estimator import get_context
  10.  
  11. import time
  12. import sys
  13. import random
  14.  
  15. ANALY_LIST=["illegal_parking0","people_counting"]
  16.  
  17. delta_i= 900# seconds
  18.  
  19. # pre_a_selected=[4000.0,2000.0,1000.0,500.0,100.0]
  20.  
  21. pre_a_selected=[5.0, 10.0, 25.0, 50.0, 100.0]
  22.  
  23. valid_list=[]
  24. f_c = 24 * 60 # fps * second
  25. analyTimeTable = AnalyTimeTable(False)
  26. iATable = IATable(False)
  27. debug = False
  28. trigger_interval=6
  29.  
  30. clock = {
  31.             "month":11,
  32.             "day":8,
  33.             "hour":6,
  34.             "min_":00
  35.         }
  36.  
  37. if __name__=='__main__':
  38.     start=time.time()
  39.     DBclient = InfluxDBClient('localhost', 8086, 'root', 'root', 'nmsl')
  40.    
  41.     # result = DBclient.query('SELECT * FROM raw_11_8 limit 360')
  42.     # result_point = list(result.get_points(measurement='raw_11_8'))
  43.  
  44.     result = DBclient.query("SELECT * FROM raw_11_8")
  45.     result_list = list(result.get_points(measurement='raw_11_8'))
  46.  
  47.  
  48.     clip_list = []
  49.     for c in result_list:
  50.         v = c['name'].split("/")[-1]
  51.         info_v = v.split("_")
  52.         date = info_v[2].split("-")
  53.         month = int(date[1])
  54.         day = int(date[2])
  55.         time_ = os.path.splitext(info_v[3])[0].split(":")
  56.         hour = int(time_[0])
  57.         min_ = int(time_[1])
  58.         if month == clock["month"] and (clock["day"]-day+30)%30<2:
  59.             diff = ((clock["hour"] + clock["min_"]/60)  - (hour + (min_/60))+24)%24
  60.             if diff < trigger_interval:
  61.                 clip_list.append(v)
  62.  
  63.     clip_array = np.zeros((len(clip_list),2),dtype=np.uint8)
  64.     for id_r, r in enumerate(clip_list):
  65.         clip_array[id_r][0], clip_array[id_r][1] = get_context(r)
  66.  
  67.     if debug:
  68.         print("Time")
  69.         for k,r in enumerate(clip_array):
  70.             time_dataframe = pd.DataFrame(columns=['illegal_parking0','people_counting'])
  71.             print(clip_list[k])
  72.  
  73.             for l in range(len(pre_a_selected)):
  74.                 time_list = []
  75.                 time_list.append(analyTimeTable.get_estimation(day_of_week =r[0], time_of_day = r[1], a_type=0, a_parameter=l))
  76.                 time_list.append(analyTimeTable.get_estimation(day_of_week=r[0],time_of_day=r[1], a_type=1, a_parameter=l))
  77.                 time_dataframe.loc[l] = time_list
  78.             print(time_dataframe)
  79.         print("---------------------------------------")
  80.         print("IA")
  81.         for k,r in enumerate(clip_array):
  82.             value_dataframe = pd.DataFrame(columns=['illegal_parking0','people_counting'])
  83.             print(clip_list[k])
  84.             for l in range(len(pre_a_selected)):
  85.                 value_list = []
  86.                 value_list.append(iATable.get_estimation(day_of_week=r[0],time_of_day=r[1], a_type=0, a_parameter=l))
  87.                 value_list.append(iATable.get_estimation(day_of_week=r[0],time_of_day=r[1], a_type=1, a_parameter=l))
  88.                 value_dataframe.loc[l] = value_list
  89.             print(value_dataframe)
  90.  
  91.  
  92.     # sys.exit()
  93.  
  94.  
  95.     target_clip_num = clip_array.shape[0]
  96.     target_deadline = delta_i
  97.  
  98.     Estate = np.zeros((target_clip_num,len(ANALY_LIST),target_deadline),dtype=np.float32)
  99.     L_time = np.zeros((target_clip_num,len(ANALY_LIST),target_deadline),dtype=np.float32)
  100.     r = np.zeros((target_clip_num,len(ANALY_LIST)),dtype=np.int8)
  101.     r.fill(-1)
  102.     L =  np.array([
  103.             [
  104.                 [
  105.                     r for q in range(delta_i)
  106.                 ] for j in range(len(ANALY_LIST))
  107.             ] for i in range(target_clip_num)
  108.         ])
  109.     print()
  110.  
  111.  
  112.     W = np.ones((target_clip_num,len(ANALY_LIST)),dtype=float)
  113.    
  114.     print("Consider "+str(target_clip_num)+" clips")
  115.     s = time.time()
  116.  
  117.     for c in range(target_clip_num):
  118.         print(clip_list[c])
  119.         for a in range(len(ANALY_LIST)):
  120.             if a==0:
  121.                 if c==0:
  122.                     last_a = 0
  123.                     last_c = 0
  124.                 else:
  125.                     last_a = -1
  126.                     last_c = c-1
  127.             else:
  128.                 last_c = c
  129.                 last_a = a-1
  130.            
  131.             for t in range(target_deadline):
  132.                 diff_p_comp = list()
  133.                 for l in range(len(pre_a_selected)):
  134.                     cost_time = analyTimeTable.get_estimation(day_of_week=clip_array[c][0],time_of_day=clip_array[c][1], a_type=a, a_parameter=l)
  135.  
  136.                     reduced_time = int(t-cost_time)
  137.  
  138.                    
  139.                     left_time = t - L_time[last_c][last_a][reduced_time] - cost_time
  140.  
  141.                     if left_time<0:
  142.                         continue  
  143.                    
  144.                     # get estimated info amount
  145.                     if pre_a_selected[l]!=-1:
  146.                         esti_info_amount = W[c][a] * iATable.get_estimation(day_of_week=clip_array[c][0],time_of_day=clip_array[c][1], a_type=a, a_parameter=l) * f_c
  147.                     else:
  148.                         esti_info_amount = 0
  149.                     ## weight normalized
  150.                     # esti_info_amount /= W[c].sum()
  151.                    
  152.                     diff_p_comp.append((esti_info_amount,l,reduced_time,cost_time))
  153.  
  154.                 if len(diff_p_comp) > 0:
  155.                     esti_info_amount, l, reduced_time, cost_time = max(diff_p_comp)
  156.                 else:
  157.                     continue
  158.  
  159.                 if esti_info_amount + Estate[last_c][last_a][reduced_time] > Estate[last_c][last_a][t]:
  160.                     Estate[c][a][t] = esti_info_amount + Estate[last_c][last_a][reduced_time]
  161.                     L[c][a][t] = L[last_c][last_a][reduced_time]
  162.                     L[c][a][t][c][a] = l
  163.                     L_time[c][a][t] = L_time[last_c][last_a][reduced_time] + cost_time
  164.                 else:
  165.                     Estate[c][a][t] = Estate[last_c][last_a][t]
  166.                     L[c][a][t] = L[last_c][last_a][t]
  167.                     L_time[c][a][t] = L_time[last_c][last_a][t]
  168.                    
  169.  
  170.     count=0
  171.     for k,r in enumerate(L[-1][-1][-1]):
  172.         for c in r:
  173.             if c!=-1:
  174.                 count+=1
  175.         print(clip_list[k],r)
  176.     print(count)
  177.     print("Estimated execution time:",L_time[-1][-1][-1])
  178.     print("Estimated info:",Estate[-1][-1][-1])
  179.     print("Algo running time:",time.time()-s)
  180.  
  181.  
  182.    
  183.     # max_info = 0
  184.     # max_idx = (-1,-1,-1,-1,-1,-1)
  185.     # cost_time = 0
  186.     # pre_a_selected.append(-1)
  187.     # for i in range(len(pre_a_selected)):
  188.     #     if i == len(pre_a_selected)-1:
  189.     #         ia11=0
  190.     #     else:
  191.     #         ia11 = W[c][0] * iATable.get_estimation(day_of_week=clip_array[0][0],time_of_day=clip_array[0][1], a_type=0, a_parameter=i)* pre_a_selected[i]
  192.     #     for j in range(len(pre_a_selected)):
  193.  
  194.     #         if j == len(pre_a_selected)-1:
  195.     #             ia12=0
  196.     #         else:
  197.     #             ia12 = W[c][1] * iATable.get_estimation(day_of_week=clip_array[0][0],time_of_day=clip_array[0][1], a_type=1, a_parameter=j)* pre_a_selected[j]
  198.  
  199.     #         ia1 = ia11+ia12
  200.  
  201.     #         for k in range(len(pre_a_selected)):
  202.     #             if k == len(pre_a_selected)-1:
  203.     #                 ia21=0
  204.     #             else:
  205.     #                 ia21 = W[c][0] * iATable.get_estimation(day_of_week=clip_array[1][0],time_of_day=clip_array[1][1], a_type=0, a_parameter=k)* pre_a_selected[k]
  206.     #             for l in range(len(pre_a_selected)):
  207.     #                 if l == len(pre_a_selected)-1:
  208.     #                     ia22=0
  209.     #                 else:
  210.     #                     ia22 = W[c][1] * iATable.get_estimation(day_of_week=clip_array[1][0],time_of_day=clip_array[1][1], a_type=1, a_parameter=l)* pre_a_selected[l]
  211.  
  212.     #                 ia2 = ia21+ia22
  213.  
  214.     #                 for r in range(len(pre_a_selected)):
  215.     #                     if r == len(pre_a_selected)-1:
  216.     #                         ia31=0
  217.     #                     else:
  218.     #                         ia31 = W[c][0] * iATable.get_estimation(day_of_week=clip_array[2][0],time_of_day=clip_array[2][1], a_type=0, a_parameter=r)* pre_a_selected[r]
  219.     #                     for s in range(len(pre_a_selected)):
  220.     #                         if s == len(pre_a_selected)-1:
  221.     #                             ia32=0
  222.     #                         else:
  223.     #                             ia32 = W[c][1] * iATable.get_estimation(day_of_week=clip_array[2][0],time_of_day=clip_array[2][1], a_type=1, a_parameter=s)* pre_a_selected[s]
  224.     #                         ia3 = ia31+ia32
  225.     #                         if ia1+ia2+ia3 > max_info:
  226.     #                             time=0
  227.     #                             tmp = (i,j,k,l,r,s)
  228.     #                             for id_a, p in enumerate(tmp):
  229.     #                                 if p==len(pre_a_selected)-1:
  230.     #                                     continue
  231.     #                                 time += analyTimeTable.get_estimation(day_of_week=clip_array[int(id_a/2)][0],time_of_day=clip_array[int(id_a/2)][1], a_type = id_a%2, a_parameter = p)
  232.     #                             if time < delta_i:
  233.     #                                 max_info = ia1+ia2+ia3
  234.     #                                 max_idx = tmp
  235.     #                                 cost_time = time
  236.     #                             else:
  237.     #                                 continue
  238.     # print("Brute force:")
  239.     # print("Info:", max_info)
  240.     # print("lengths:", max_idx)
  241.     # print("Time:", cost_time)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement