fit_funcs.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # fit_funcs.py
  2. import numpy as np
  3. import math
  4. from generate_carla import *
  5. def fuzzscene_f1(x, pop_seed_name, error_number, choose_time, list_er, domain_flags, data_collection_para):
  6. d = np.loadtxt("diversity_count.txt") # load diversity and entropy count metrix
  7. e = np.loadtxt("entropy.txt")
  8. diversity_sum = 0.0
  9. seed_str = pop_seed_name.split("_")[3]
  10. seed_number = int(seed_str[0])
  11. iterate = int(pop_seed_name.split("_")[1])
  12. if error_number is None: # exclude seed already after ga_err_test
  13. error_number, div, list_er = ga_sim(x[0], x[1], x[2], x[3], x[4], x[5], x[6], x[7], pop_seed_name, domain_flags,
  14. data_collection_para)
  15. print("[" + os.path.basename(__file__) + ", Line " + str(sys._getframe().f_lineno) + ", " + sys._getframe().f_code.co_name + "] ", error_number, div)
  16. for i in range(len(list_er)): # cal diversity metrix
  17. if iterate != 0:
  18. d[seed_number - 1, int(list_er[i])] = d[seed_number - 1, int(list_er[i])] * 0.5
  19. if data_collection_para[0]: # update err metrix for entropy
  20. for i in range(len(list_er)):
  21. e[seed_number - 1, int(list_er[i])] += 1
  22. np.savetxt("entropy.txt", e)
  23. for i in range(len(list_er)):
  24. diversity_sum = d[seed_number - 1, int(list_er[i])] + diversity_sum
  25. np.savetxt("diversity_count.txt", d)
  26. # diversity_total = diversity_sum / 125.0 + math.exp(-choose_time)
  27. diversity_total = math.exp(-choose_time)
  28. err = error_number / 125.0
  29. return [err, diversity_total], error_number, list_er
  30. def get_zdt(domain_flags):
  31. f_funcs = fuzzscene_f1
  32. ori_domain = [[0, 255], [0, 255], [0, 255], [1, 12], [8, 16], [0, 200], [0, 100], [30, 100]]
  33. #
  34. domain = ori_domain
  35. for i in range(len(domain_flags)):
  36. if not domain_flags[i]:
  37. if i == 0:
  38. domain[0] = [0, 0]
  39. domain[1] = [0, 0]
  40. domain[2] = [255, 255]
  41. elif i == 1:
  42. domain[3] = [3, 3]
  43. domain[4] = [12, 12]
  44. else:
  45. domain[i] = domain[i]
  46. return f_funcs, domain
  47. def cal_entropy(metrix):
  48. sum_err = metrix.sum()
  49. entropy = 0.0
  50. if sum_err == 0:
  51. return entropy
  52. for i in np.nditer(metrix):
  53. if i != 0:
  54. p = i / sum_err
  55. entropy += -1 * (p * math.log2(p))
  56. return entropy
  57. def cal_fitness(R):
  58. b = np.loadtxt("entropy.txt")
  59. ori_entropy = cal_entropy(b)
  60. # 6为seed_num
  61. temp_metrix = np.random.random_integers(1, 1, size=(6, 125))
  62. flag = (b == temp_metrix).all() # is origin 24 seeds or not
  63. for r in R:
  64. b = np.loadtxt("entropy.txt")
  65. ori_entropy = cal_entropy(b)
  66. pop_seed_name = r.pop_seed_name
  67. seed_str = pop_seed_name.split("_")[3]
  68. seed_number = int(seed_str[0])
  69. list_er = r.list_er
  70. entropy = ori_entropy
  71. if not flag: # exclude first 24 seeds which entropy
  72. for i in range(len(list_er)):
  73. b[seed_number - 1, int(list_er[i])] += 1
  74. entropy = cal_entropy(b)
  75. print("[" + os.path.basename(__file__) + ", Line " + str(sys._getframe().f_lineno) + ", " + sys._getframe().f_code.co_name + "] ", r.f)
  76. if len(r.f) < 3:
  77. r.f.append(entropy - ori_entropy)
  78. else:
  79. r.f[2] = entropy - ori_entropy
  80. with open('r_list.csv', 'a+', encoding='utf-8') as el: # save entropy
  81. cw = csv.writer(el)
  82. cw.writerow([r.pop_seed_name, r.dna, r.f, entropy - ori_entropy])