generate_radar.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. from re import A
  2. from turtle import color
  3. from xml.dom.minidom import parse
  4. import xml.dom.minidom
  5. import random
  6. import json
  7. import os
  8. import sys
  9. from numpy import var
  10. from pyrsistent import v
  11. from regex import F
  12. from scipy import rand
  13. # from radar_error_test import *
  14. # day_of_month = [0,31,29,31,30,31,30,31,31,30,31,30,31]
  15. model_name = "4"
  16. # Function vehicleRandomization
  17. #
  18. # Parameters:
  19. # name: 指定的ScenarioObject名称
  20. # car_type: boolean值,true时随机car类型,false时不发生随机
  21. # car_color: list[boolean],决定三种颜色RGB是否发生随机
  22. # var_map: dict 存储变量值
  23. def vehicleRandomization(name, red, green, blue, var_map, e):
  24. scenario_obj = e.getElementsByTagName("ScenarioObject")
  25. for obj in scenario_obj:
  26. if obj.getAttribute("name") == name:
  27. v = obj.getElementsByTagName('Vehicle')[0] # 获取带随机ScenarioObject代码块
  28. org_car_name = v.getAttribute("name")
  29. # variable["org_car_name"] = org_car_name
  30. p_1 = v.getElementsByTagName('Properties')[0] # Properties层
  31. p_2 = p_1.getElementsByTagName('Property')
  32. for p in p_2:
  33. if p.getAttribute("name") == "color":
  34. org_color = p.getAttribute("value")
  35. # print("车辆原颜色为", org_color, '\n')
  36. color_list = org_color.split(",")
  37. var_map["org_R"] = color_list[0]
  38. var_map["org_G"] = color_list[1]
  39. var_map["org_B"] = color_list[2]
  40. final_color = ""
  41. c1 = str(red)
  42. var_map["rand_R"] = c1
  43. final_color += c1
  44. final_color += ","
  45. c2 = str(green)
  46. var_map["rand_G"] = c2
  47. final_color += c2
  48. final_color += ","
  49. c3 = str(blue)
  50. var_map["rand_B"] = c3
  51. final_color += c3
  52. p.setAttribute("value", final_color)
  53. # print("车辆颜色随机为", final_color, '\n')
  54. # Function envRandomization
  55. #
  56. # Parameters:
  57. # is_time: boolean值,true时随机日期类型,false时不发生随机
  58. # is_sun: boolean值,true时随机intensity,false时不发生随机
  59. # is_precipitation: boolean值,true时随机天气类型,在rain和dry中间取值
  60. # is_fog: boolean值,true时随机fog的visual_range以及intensity
  61. # var_map: dict 存储变量值
  62. def envRandomization(mut_date, mut_time, mut_fog, mut_precipitation, mut_sun_intensity, var_map, s): # 环境随机代码
  63. env = s.getElementsByTagName('Init')[0].getElementsByTagName('Actions')[0].getElementsByTagName('GlobalAction')[
  64. 0].getElementsByTagName('EnvironmentAction')[0].getElementsByTagName('Environment')[0]
  65. time_of_day = env.getElementsByTagName("TimeOfDay")[0]
  66. weather = env.getElementsByTagName("Weather")[0]
  67. sun = weather.getElementsByTagName("Sun")[0]
  68. fog = weather.getElementsByTagName("Fog")[0]
  69. p = weather.getElementsByTagName("Precipitation")[0]
  70. # 原始时间数据读取
  71. datetime = time_of_day.getAttribute("dateTime")
  72. var_map["org_time"] = datetime
  73. # 原始太阳照度读取
  74. org_s_intensity = sun.getAttribute("intensity")
  75. var_map["org_sun_intensity"] = org_s_intensity
  76. # 原始雾天数据读取
  77. fog_para = fog.getAttribute("visualRange").split("\\")
  78. if (len(fog_para) == 1):
  79. org_visual_range = fog_para[0]
  80. # org_f_intensity = 100
  81. # else:
  82. # org_visual_range = fog_para[0]
  83. # org_f_intensity = fog_para[1]
  84. var_map["org_visual_range"] = org_visual_range
  85. # var_map["org_fog_intensity"] = org_f_intensity
  86. # 原始降水数据读取
  87. org_precipitation = p.getAttribute("precipitationType")
  88. org_p_intensity = p.getAttribute("intensity")
  89. var_map["org_precipitation"] = org_precipitation
  90. var_map["org_precipitation_intensity"] = org_p_intensity
  91. # 日期
  92. time_of_day.setAttribute("animation", "true")
  93. date = str(datetime.split("T")[0])
  94. rand_month = mut_date
  95. ori_day = date[8:]
  96. if (rand_month < 10): # format
  97. rand_month = "0" + str(rand_month)
  98. # if(rand_day<10): #format
  99. # rand_day = "0" + str(rand_day)
  100. rand_date = "2020-" + str(rand_month) + "-" + str(ori_day)
  101. var_map["rand_date"] = rand_date
  102. # 时间
  103. time_of_day.setAttribute("animation", "true")
  104. date = rand_date
  105. rand_hour = mut_time
  106. if (rand_hour < 10): # format
  107. rand_hour = "0" + str(rand_hour)
  108. rand_time = date + "T" + str(rand_hour) + ":00:00"
  109. time_of_day.setAttribute("dateTime", rand_time)
  110. var_map["rand_time"] = rand_time
  111. # 照度
  112. rand_s_intensity = round((mut_sun_intensity / 100.0), 2)
  113. sun.setAttribute("intensity", str(rand_s_intensity))
  114. var_map["rand_sun_intensity"] = rand_s_intensity
  115. # 降水
  116. rand_p_intensity = round((mut_precipitation / 100.0), 2)
  117. p.setAttribute("intensity", str(rand_p_intensity))
  118. var_map["rand_precipitation_intensity"] = rand_p_intensity
  119. # 雾天及雾浓度
  120. rand_fog = str(mut_fog) # 雾天
  121. var_map["rand_fog"] = rand_fog
  122. fog_para = rand_fog
  123. fog.setAttribute("visualRange", fog_para)
  124. var_map["visualRange"] = fog_para
  125. def Simulation(rand_para, var_map, e, s):
  126. print(rand_para)
  127. vehicleRandomization("adversary", rand_para[0], rand_para[1], rand_para[2], var_map, e)
  128. envRandomization(rand_para[3], rand_para[4], rand_para[5], rand_para[6], rand_para[7], var_map, s)
  129. def writeBack(xml_path, DOMTree):
  130. # 写回源文件使属性修改生效
  131. fp = open(xml_path, 'w+', encoding='utf-8')
  132. DOMTree.writexml(fp, indent='', addindent='', newl='', encoding='utf-8')
  133. fp.close()
  134. def ga_sim(color1, color2, color3, time1, time2, fog, rain, sun, seed_name, data_collection_para):
  135. mutation_name = seed_name.split("_")
  136. complete_mutation_name = "seed_0_0_" + mutation_name[3]
  137. parse_path = "../seed_pool/" + complete_mutation_name
  138. # 使用minidom解析器打开 XML 文档
  139. DOMTree = xml.dom.minidom.parse(parse_path)
  140. # Linux下改一下路径
  141. ele = DOMTree.documentElement
  142. e = ele.getElementsByTagName("Entities")[0]
  143. s = ele.getElementsByTagName("Storyboard")[0]
  144. xml_path = '../seed_pool/' + seed_name
  145. variable = {"name": "origin & random parameters"}
  146. rand_para = [color1, color2, color3, time1, time2, fog, rain, sun]
  147. Simulation(rand_para, variable, e, s)
  148. # print(variable)
  149. writeBack(xml_path, DOMTree)
  150. os.system("bash ./ga_sim.sh " + seed_name)
  151. data_collection_para = str(data_collection_para[0])+','+str(data_collection_para[1])+','+str(data_collection_para[2])
  152. os.system("python ga_error_test.py " + model_name + " 1 " + seed_name + " " + data_collection_para)
  153. error = 0
  154. div = 0
  155. list_er = []
  156. with open('list.txt', 'r') as p:
  157. for line in p:
  158. list_er.append(eval(line))
  159. # print(type(eval(line)))
  160. with open('./error_count.csv', 'r') as f:
  161. rows = len(f.readlines()) - 1
  162. f.seek(0)
  163. for i, line in enumerate(f):
  164. if i == 0:
  165. continue
  166. if line.split(',')[0] == seed_name:
  167. error = int(line.split(',')[1])
  168. div = int(line.split(',')[2])
  169. return error, div, list_er