java二进制安全一线开发 [2024]HotSpotVirtualMachine.java self attach check function Vulnerabilitie HotSpotVirtualMachine.java 阅读全文 2024-02-22 huoji 0 条评论
python一线开发 [2021]手撸机器学习入门之jaya算法 总所周知,所谓机器学习就是一个解方程给x求y的过程罢了. 这是一篇入门的算法代码,又叫做jaya算法(不是java!!!),网上没什么资料,以下是自己python写的. ```python import math import random import matplotlib.pyplot as plt import numpy as np g_group_size = 5000 g_var_weight = 2 g_max_iteration = 100 g_array_f = [0 for i in range(0, g_group_size)] g_array_best = [0 for i in range(0, g_group_size)] g_array_worst = [0 for i in range(0, g_group_size)] g_array_pop_new = [[0 for i in range(0, g_var_weight)] for i in range(0, g_group_size)] #g_number_a = -2 * math.pi #g_number_b = 2 * math.pi #g_number_a = -5.12 #g_number_b = 5.12 g_number_a = -100 g_number_b = 100 g_result = [] array_pop = [[0 for i in range(0, g_var_weight)] for i in range(0, g_group_size)] def fn_objective(pArray): result = 0 for i in range(0, g_var_weight): # result += pArray[i] * pArray[i] # result += math.sin(pArray[i]) * math.pow(math.e, math.pow(1 - math.cos(result), 2)) + math.cos(result) * math.pow(math.e, math.pow(1 - math.sin(pArray[i]), 2)) + math.pow(pArray[i] - result, 2) # result += -(1 + math.cos(12 * math.sqrt(math.pow(pArray[i], 2) + math.pow(result, 2)))) / (0.5 * (math.pow(pArray[i], 2) + math.pow(result, 2)) + 2) result += math.pow(pArray[i], 2) + 2 * math.pow(result, 2) - 0.3 * math.cos(3 * math.pi * pArray[i]) - 0.4 * math.cos(4 * math.pi * result) + 0.7 return result def fn_clean_array(pArray): for i in range(0, g_group_size): for z in range(0, g_var_weight): if pArray[i][z] < g_number_a: pArray[i][z] = g_number_a if pArray[i][z] > g_number_b: pArray[i][z] = g_number_b return pArray for i in range(0, len(array_pop)): for z in range(0, len(array_pop[i])): array_pop[i][z] = random.uniform(g_number_a, g_number_b) g_array_f[i] = fn_objective(array_pop[i]) print("array_pop: ") print(array_pop) print("g_array_f: ") print(g_array_f) iter_num = 1 min_index = 0 max_index = 0 while iter_num <= g_max_iteration: min_value = min(g_array_f) min_index = g_array_f.index(min_value) - 1 g_array_best = array_pop[min_index] max_value = max(g_array_f) max_index = g_array_f.index(max_value) - 1 g_array_worst = array_pop[max_index] for i in range(0, g_group_size): for z in range(0, g_var_weight): wrost_number = g_array_worst[z] - math.fabs(array_pop[i][z]) best_number = g_array_best[z] - math.fabs(array_pop[i][z]) g_array_pop_new[i][z] = array_pop[i][z] + \ random.random() * best_number - wrost_number g_array_pop_new = fn_clean_array(g_array_pop_new) f_new = 0 for i in range(0, len(g_array_pop_new)): f_new = fn_objective(g_array_pop_new[i]) if f_new < g_array_f[i]: g_array_f[i] = f_new for z in range(g_var_weight): array_pop[i][z] = g_array_pop_new[i][z] min_value = min(g_array_f) min_index = g_array_f.index(min_value) g_array_best = array_pop[min_index] print("min number {} ".format(g_array_f[min_index])) print("x=") print(g_array_best) #g_result.insert(0, g_array_f[min_index]) g_result.append(g_array_f[min_index]) iter_num += 1 x = np.arange(0, len(g_result)) y = np.array(g_result) plt.title("jaya") plt.xlabel("min number") plt.ylabel("y") plt.plot(x, y) plt.show() ``` 请注意有一些公式需要很大的族群范围才能收敛 论文里面的收敛图: ![](https://key08.com/usr/uploads/2021/05/2290756614.png) 这边的: ![](https://key08.com/usr/uploads/2021/05/3846208643.png) 阅读全文 2021-05-24 huoji 0 条评论