def blind_search(name_function, size_random_search): point_list = [] generated_data_x = [ random.uniform(static_data.get_min_range(name_function), static_data.get_max_range(name_function)) for _ in range(size_random_search) ] generated_data_y = [ random.uniform(static_data.get_min_range(name_function), static_data.get_max_range(name_function)) for _ in range(size_random_search) ] tmp = func_file.return_value_function( [generated_data_x[0], generated_data_y[0]], name_function) cord_X = generated_data_x[0] cord_Y = generated_data_y[0] for i, j in zip(generated_data_x, generated_data_y): min = func_file.return_value_function([i, j], name_function) point_list.append([i, j, min]) if min < tmp: tmp = min cord_X = i cord_Y = j return [cord_X, cord_Y, tmp]
def blind_search_points(name_function, size_random_search): point_list = [] generated_data_x = [ random.uniform(static_data.get_min_range(name_function), static_data.get_max_range(name_function)) for _ in range(size_random_search) ] generated_data_y = [ random.uniform(static_data.get_min_range(name_function), static_data.get_max_range(name_function)) for _ in range(size_random_search) ] tmp = func_file.return_value_function( [generated_data_x[0], generated_data_y[0]], name_function) cord_X = generated_data_x[0] cord_Y = generated_data_y[0] for i, j in zip(generated_data_x, generated_data_y): min = func_file.return_value_function([i, j], name_function) point_list.append([i, j, min]) if min < tmp: tmp = min cord_X = i cord_Y = j print("X: " + str(cord_X)) print("Y: " + str(cord_Y)) print("VALUE: " + str(tmp)) print("") return point_list
def PSO(func, dim, popSize, iteration, vmax): population = generate_first_population(func, popSize, dim) gBest = find_best_in_list(population, func) new_population = [] all_points = [] points = [point[0] for point in population] all_points.append(points) for i in range(iteration): for j in population: new_velocity = [] new_point = [] for x in range(dim): # point j = [[x,y], [velocity_x, velocity_y], [pBest_x,pBest_y]] new_velocity.append(j[1][x] + (C1 * random.uniform(0, 1) * (j[2][x] - j[0][x])) + (C2 * random.uniform(0, 1) * (gBest[x] - j[0][x]))) new_point.append(j[0][x] + new_velocity[x]) for m in range(dim): if new_velocity[m] > vmax or new_velocity[m] < -vmax: new_velocity[m] = new_velocity[m] / 20 for i in range(dim): if new_point[i] > static_data.get_max_range(func): new_point[i] = random.uniform( static_data.get_min_range(func), static_data.get_max_range(func)) if new_point[i] < static_data.get_min_range(func): new_point[i] = random.uniform( static_data.get_min_range(func), static_data.get_max_range(func)) if func_file.return_value_function( new_point, func) < func_file.return_value_function( j[2], func): pBest = new_point else: pBest = j[2] if func_file.return_value_function( pBest, func) < func_file.return_value_function( gBest, func): gBest = pBest print([new_point, new_velocity, pBest]) new_population.append([new_point, new_velocity, pBest]) # for graph points = [point[2] for point in new_population] all_points.append(points) population = new_population new_population = [] print(f'gBest = {gBest}') show_3D_graph.animate_in_graph_3D(func, all_points)
def generate_first_population(func, popsize, dimension): population = [] for i in range(0, popsize): gen_point = list(np.random.uniform(static_data.get_min_range(func), static_data.get_max_range(func), dimension)) evaluate = func_file.return_value_function(gen_point, func) population.append([gen_point, evaluate]) return population
def generate_first_population(func, popSize, dimension): population = [] for i in range(0, popSize): gen_point = list( np.random.uniform(static_data.get_min_range(func), static_data.get_max_range(func), dimension)) population.append(gen_point) return population
def animate_in_graph_3D(name_function, points): # MY IMPORT import static_data, func_file range_list = np.arange(static_data.get_min_range(name_function), static_data.get_max_range(name_function), static_data.get_range(name_function)) X = [i for i in range_list for _ in range_list] Y = list(range_list) * len(range_list) if name_function == "ackley": Z = [func_file.ackley([x, y]) for x, y in zip(X, Y)] elif name_function == "levy": Z = [func_file.levy([x, y]) for x, y in zip(X, Y)] elif name_function == "rastrigin": Z = [func_file.rastrigin([x, y]) for x, y in zip(X, Y)] elif name_function == "griewank": Z = [func_file.griewank([x, y]) for x, y in zip(X, Y)] elif name_function == "schwefel": Z = [func_file.schwefel([x, y]) for x, y in zip(X, Y)] elif name_function == "zakharov": Z = [func_file.zakharov([x, y]) for x, y in zip(X, Y)] elif name_function == "sphere": Z = [func_file.sphere([x, y]) for x, y in zip(X, Y)] elif name_function == "michalewicz": Z = [func_file.michalewicz([x, y]) for x, y in zip(X, Y)] fig = plt.figure() ax = fig.gca(projection='3d') ax.plot_trisurf(X, Y, Z, cmap='inferno', edgecolor='none') ax.set_xlabel('X') ax.set_ylabel('Y') ax.set_zlabel('Z') def animate(i): ax.clear() ax.plot_trisurf(X, Y, Z, cmap='inferno', edgecolor='none') ax.set_xlabel('X') ax.set_ylabel('Y') ax.set_zlabel('Z') for j in range(len(points[0])): ax.scatter(points[i][j][0], points[i][j][1]) return ax ani = FuncAnimation(fig, animate, frames=len(points), interval=400, repeat=False) plt.show()
def show_graph_with_searched_point_3D(name_function, point): # MY IMPORT import static_data, func_file range_list = np.arange(static_data.get_min_range(name_function), static_data.get_max_range(name_function), static_data.get_range(name_function)) X = [i for i in range_list for _ in range_list] Y = list(range_list) * len(range_list) if name_function == "ackley": Z = [func_file.ackley([x, y]) for x, y in zip(X, Y)] elif name_function == "levy": Z = [func_file.levy([x, y]) for x, y in zip(X, Y)] elif name_function == "rastrigin": Z = [func_file.rastrigin([x, y]) for x, y in zip(X, Y)] elif name_function == "griewank": Z = [func_file.griewank([x, y]) for x, y in zip(X, Y)] elif name_function == "schwefel": Z = [func_file.schwefel([x, y]) for x, y in zip(X, Y)] elif name_function == "zakharov": Z = [func_file.zakharov([x, y]) for x, y in zip(X, Y)] elif name_function == "sphere": Z = [func_file.sphere([x, y]) for x, y in zip(X, Y)] elif name_function == "michalewicz": Z = [func_file.michalewicz([x, y]) for x, y in zip(X, Y)] fig = plt.figure() ax = fig.gca(projection='3d') ax.plot_trisurf(X, Y, Z, cmap='inferno', edgecolor='none'); ax.scatter(point[0],point[1],point[2]) ax.set_xlabel('X') ax.set_ylabel('Y') ax.set_zlabel('Z') plt.show()