fuzz_ga.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # fuzz_ga.py
  2. import numpy as np
  3. from ga import *
  4. import matplotlib.pyplot as plt
  5. from fit_funcs import *
  6. domain_flags = [True,True,True,True,True]
  7. f_funcs, domain = get_zdt(domain_flags)
  8. pri_queue_size = 50
  9. pop_size = 24
  10. dna_size = len(domain)
  11. print("[" + os.path.basename(__file__) + ", Line " + str(sys._getframe().f_lineno) + ", " + sys._getframe().f_code.co_name + "] ", dna_size)
  12. pm = 0.05
  13. if_sampling = True
  14. is_err_collection = 1
  15. # 下面的文件中6为seed_num
  16. a = np.random.random_integers(0, 0, size=(6, 125))
  17. np.savetxt("diversity.txt", a)
  18. b = np.random.random_integers(1, 1, size=(6, 125))
  19. np.savetxt("diversity_count.txt", b)
  20. c = np.random.random_integers(1, 1, size=(6, 125))
  21. np.savetxt("entropy.txt", c)
  22. ga = ga(pri_queue_size=pri_queue_size, pop_size=pop_size, dna_size=dna_size, pc=1, pm=0.05, f_funcs=f_funcs, domain=domain, domain_flags=domain_flags)
  23. P = ga.ini_pop(if_sampling)
  24. print("[" + os.path.basename(__file__) + ", Line " + str(sys._getframe().f_lineno) + ", " + sys._getframe().f_code.co_name + "] ", len(P))
  25. ga.run_sim(P, [False, is_err_collection, 2]) # data_collection_para : [is_new_seed(for entropy), is_err_collection(collect err or not), err_type(collect normal(1)/sampling(2)/random data(3))]
  26. ga.calculate_pop_fitness(P)
  27. R = P
  28. N = 20
  29. for i in range(N):
  30. print("[" + os.path.basename(__file__) + ", Line " + str(sys._getframe().f_lineno) + ", " + sys._getframe().f_code.co_name + "] ", '# GA Loop: ', i)
  31. # 选择
  32. P, R = ga.select(R) # P:selected_seed, R:all_seed
  33. # 交叉
  34. Q = ga.pop_cross(P, i + 1)
  35. # 变异
  36. Q = ga.pop_mutation(Q)
  37. # 模拟
  38. print("[" + os.path.basename(__file__) + ", Line " + str(sys._getframe().f_lineno) + ", " + sys._getframe().f_code.co_name + "] ", 'All Seeds: ', R)
  39. print("[" + os.path.basename(__file__) + ", Line " + str(sys._getframe().f_lineno) + ", " + sys._getframe().f_code.co_name + "] ", 'New Seeds: ', Q)
  40. ga.run_sim(R, [False, 0, 0])
  41. ga.run_sim(Q, [True, is_err_collection, 1])
  42. # 合并
  43. R = R + Q
  44. # 计算适应度
  45. ga.calculate_pop_fitness(R)
  46. ga.calulate_pop(R, pri_queue_size)
  47. print("[" + os.path.basename(__file__) + ", Line " + str(sys._getframe().f_lineno) + ", " + sys._getframe().f_code.co_name + "] ", "# GA Loop Test Over", '\n')