Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import matplotlib.pyplot as plt
- np.random.seed(4)
- # def fitness_function(x):
- # sum = 2.0
- # for item in x:
- # sum -= item**2.0
- # return sum
- def fitness_function(z):
- x = z[0]
- y = z[1]
- sum = (
- 1 + (x + y + 1) ** 2 * (19 - 14 * x + 3 * x**2 - 14 * y + 6 * x * y + 3 * y**2)
- ) * (
- 30
- + (2 * x - 3 * y) ** 2
- * (18 - 32 * x + 12 * x**2 + 48 * y - 36 * x * y + 27 * y**2)
- )
- sum = 1000000 - sum
- return sum
- # def fitness_function(x):
- # sum = np.float64(-20.0)
- # for item in x:
- # sum += -(item**2) + 10 * np.cos(2 * np.pi * item)
- # sum = 1 / (np.e ** (-sum / 2))
- # return sum
- def get_zero_population(seed, count_population, demention_population):
- zero_population = np.random.uniform(
- -2.0, 2.0, (count_population, demention_population)
- )
- return zero_population
- def get_psi(g, NP, Lambda):
- psi = ((g) * NP + 1) ** (1 / Lambda)
- return psi
- def select_reference_vertor(clear_generation, NP, g):
- reference_vector = None
- array_fitness_value = 0.0
- sum_arr = 0.0
- arr_ver = 0.0
- psi = get_psi(g, NP, 50)
- for item in clear_generation:
- num = fitness_function(item)
- array_fitness_value = np.append(array_fitness_value, num**psi)
- sum_arr += num**psi
- for i in array_fitness_value:
- arr_ver = np.append(arr_ver, i / sum_arr)
- # print(arr_ver)
- id = np.random.choice(len(arr_ver), p=arr_ver)
- # TODO change np.random
- reference_vector = clear_generation[id - 2]
- return reference_vector
- def calculate_A(x_min, x_max, x_r, e):
- return np.arctan((x_max - x_r) / e) - np.arctan((x_min - x_r) / e)
- def calculate_e(g, NP, D):
- return 1 / ((g) * (NP) + 1) ** (1 / (2 * D))
- def generate_potential_offspring(x_r, e, A):
- return x_r + e * np.tan((np.random.rand() - 0.5) * A)
- def sofa(zero_population, data_cloud, fitness, mod, steps_number, epsilon):
- # TODO add data_cloud,fitness,mod,epsilon,true_answer
- start_population = np.copy(zero_population)
- mutant_populaion = np.copy(zero_population)
- arr_value_best_item = np.array([fitness_function(start_population[0])])
- value_best_item = np.copy(arr_value_best_item[0])
- best_item = None
- for item in range(steps_number):
- reference_vector = select_reference_vertor(
- start_population, len(start_population), item
- )
- e = calculate_e(item, len(start_population), len(start_population[0]))
- for i in range(len(start_population)):
- const_a = calculate_A(-2.0, 2.0, reference_vector, e)
- mutant_populaion[i] = reference_vector + np.tan(
- (np.random.random(2) - 0.5)
- * const_a
- # 2 - D
- )
- for i in range(len(start_population)):
- fit_mutant_popul = fitness_function(mutant_populaion[i])
- fit_start_popul = fitness_function(start_population[i])
- if fit_mutant_popul > fit_start_popul:
- start_population[i] = mutant_populaion[i]
- if fit_mutant_popul > value_best_item:
- value_best_item = fit_mutant_popul
- best_item = np.copy(mutant_populaion[i])
- arr_value_best_item = np.append(arr_value_best_item, value_best_item)
- # print(f"final population: {start_population}")
- print(f"best vector: {best_item}")
- print(f"global maximum: {value_best_item}")
- # print(f"fitness_changing: {arr_value_best_item}")
- index = list(np.arange(1.0, len(arr_value_best_item) + 1, 1))
- # print(f"index = {index}")
- fig, ax = plt.subplots()
- ax = plt.plot(index, arr_value_best_item)
- plt.show()
- return start_population, best_item, value_best_item
- if __name__ == "__main__":
- # TODO optimization
- # TODO graphics
- steps = 10000
- steps = int(input("steps = "))
- # 2 - D
- a = get_zero_population(1, 50, 2)
- # print(f"zero_population {a}")
- # print(fitness_function([2, 2]))
- # print(fitness_function([-0.88270682, -0.70499897]))
- sofa(a, None, None, None, steps, 0.0001)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement