Advertisement
vatman

Untitled

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