Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- PSO
- import pyswarms as ps
- from pyswarms.single.global_best import GlobalBestPSO
- import matplotlib.pyplot as plt
- from pyswarms.utils.plotters import plot_cost_history
- from pyswarms.utils.plotters.formatters import Mesher, Designer
- import numpy as np
- import math
- options = {'c1': 0.5, 'c2': 0.3, 'w': 0.9}
- bounds = (x_min, x_max)
- optimizer = GlobalBestPSO(n_particles=100, dimensions=2, options=options,bounds=bounds)
- cost, pos = optimizer.optimize(func, iters=200)
- -- GA
- from genetic_algorithm.main import genetic_optimisation
- param_space = {"x1": [-5, 5],"x2": [-5, 5]}
- xopt=genetic_optimisation(input_model=fitness, param_space=param_space, pop_size=100,
- num_parents=2,max_num_generations=500, mutation_prob=0.5, stoping_rounds=50, integer_params=[])
- print(xopt.get('best fitness'))
- print(xopt.get('best params'))
- --draw
- import matplotlib.pyplot as plt
- from mpl_toolkits.mplot3d import Axes3D
- import numpy as np
- x1v = np.arange(-5, 5, 0.01)
- x2v = np.arange(-5, 5, 0.01)
- x1, x2 = np.meshgrid(x1v, x2v)
- f = np.sin(x1) + np.cos(x2)
- fig = plt.figure()
- ax = fig.gca(projection=ā3dā)
- p1 = ax.plot_surface(x1, x2, f)
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement