Advertisement
vatman

Untitled

Apr 10th, 2024
626
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.16 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3.  
  4. np.random.seed(4)
  5.  
  6.  
  7. # def fitness_function(x):
  8. #     sum = 2.0
  9. #     for item in x:
  10. #         sum -= item**2.0
  11. #     return sum
  12.  
  13.  
  14. def fitness_function(x):
  15.     sum = np.float64(-20.0)
  16.     for item in x:
  17.         sum += -(item**2) + 10 * np.cos(2 * np.pi * item)
  18.     sum = 1 / (np.e ** (-sum / 2))
  19.     return sum
  20.  
  21.  
  22. def get_zero_population(seed, count_population, demention_population):
  23.     zero_population = np.random.uniform(
  24.         -5.2, 5.2, (count_population, demention_population)
  25.     )
  26.     return zero_population
  27.  
  28.  
  29. def get_psi(g, NP, Lambda):
  30.     psi = ((g) * NP + 1) ** (1 / Lambda)
  31.     return psi
  32.  
  33.  
  34. def select_reference_vertor(clear_generation, NP, g):
  35.     reference_vector = None
  36.     array_fitness_value = 0.0
  37.     sum_arr = 0.0
  38.     arr_ver = 0.0
  39.     psi = get_psi(g, NP, 100)
  40.     for item in clear_generation:
  41.         num = fitness_function(item)
  42.  
  43.         array_fitness_value = np.append(array_fitness_value, num**psi)
  44.         sum_arr += num**psi
  45.     for i in array_fitness_value:
  46.         arr_ver = np.append(arr_ver, i / sum_arr)
  47.  
  48.     id = np.random.choice(len(arr_ver), p=arr_ver)
  49.     # TODO change np.random
  50.     reference_vector = clear_generation[id - 2]
  51.  
  52.     return reference_vector
  53.  
  54.  
  55. def calculate_A(x_min, x_max, x_r, e):
  56.     return np.arctan((x_max - x_r) / e) - np.arctan((x_min - x_r) / e)
  57.  
  58.  
  59. def calculate_e(g, NP, D):
  60.     return 1 / ((g) * (NP) + 1) ** (1 / (2 * D))
  61.  
  62.  
  63. def generate_potential_offspring(x_r, e, A):
  64.     return x_r + e * np.tan((np.random.rand() - 0.5) * A)
  65.  
  66.  
  67. def sofa(zero_population, data_cloud, fitness, mod, steps_number, epsilon):
  68.     # TODO add data_cloud,fitness,mod,epsilon,true_answer
  69.     start_population = np.copy(zero_population)
  70.     mutant_populaion = np.copy(zero_population)
  71.     arr_value_best_item = np.array([fitness_function(start_population[0])])
  72.     value_best_item = arr_value_best_item[0]
  73.     best_item = None
  74.  
  75.     for item in range(steps_number):
  76.         reference_vector = select_reference_vertor(
  77.             start_population, len(start_population), item
  78.         )
  79.         # print(f"reference_vector {reference_vector}")
  80.         e = calculate_e(item, len(start_population), len(start_population[0]))
  81.         for i in range(len(start_population)):
  82.             const_a = calculate_A(-5.2, 5.2, i, e)
  83.             mutant_populaion[i] = reference_vector + np.tan(
  84.                 (np.random.rand() - 0.5) * const_a
  85.             )
  86.  
  87.         # print(f"mutant_populaion {mutant_populaion}")
  88.         for i in range(len(start_population)):
  89.             fit_mutant_popul = fitness_function(mutant_populaion[i])
  90.             fit_start_popul = fitness_function(start_population[i])
  91.             # print(f"test {fit_mutant_popul} start {fit_start_popul}")
  92.             if fit_mutant_popul > fit_start_popul:
  93.                 start_population[i] = mutant_populaion[i]
  94.                 if fit_mutant_popul > value_best_item:
  95.                     value_best_item = fit_mutant_popul
  96.                     best_item = mutant_populaion[i]
  97.                     # print(f"best in moment{best_item}")
  98.                     # print(f"best_value in moment{value_best_item}")
  99.         arr_value_best_item = np.append(arr_value_best_item, value_best_item)
  100.     # TODO move for up 3 lines
  101.     # for item in start_population:
  102.     #     value_item = fitness_function(item)
  103.     #     if value_item > value_best_item:
  104.     #         value_best_item = value_item
  105.     #         best_item = item
  106.  
  107.     print(f"final population: {start_population}")
  108.     print(f"best vector: {best_item}")
  109.     print(f"global maximum: {value_best_item}")
  110.     print(f"fitness_changing: {arr_value_best_item}")
  111.     index = list(np.arange(1.0, len(arr_value_best_item) + 1, 1))
  112.     # print(f"index = {index}")
  113.     fig, ax = plt.subplots()
  114.     ax = plt.plot(index, arr_value_best_item)
  115.  
  116.     plt.show()
  117.     return start_population, best_item, value_best_item
  118.  
  119.  
  120. if __name__ == "__main__":
  121.     # TODO optimization
  122.     # TODO graphics
  123.     steps = 10000
  124.     steps = int(input("steps = "))
  125.     a = get_zero_population(1, 50, 2)
  126.     print(f"zero_population {a}")
  127.     sofa(a, None, None, None, steps, 0.0001)
  128.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement