Advertisement
Guest User

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

a guest
Dec 12th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. -- PSO
  2. import pyswarms as ps
  3. from pyswarms.single.global_best import GlobalBestPSO
  4. import matplotlib.pyplot as plt
  5. from pyswarms.utils.plotters import plot_cost_history
  6. from pyswarms.utils.plotters.formatters import Mesher, Designer
  7. import numpy as np
  8. import math
  9. options = {'c1': 0.5, 'c2': 0.3, 'w': 0.9}
  10. bounds = (x_min, x_max)
  11. optimizer = GlobalBestPSO(n_particles=100, dimensions=2, options=options,bounds=bounds)
  12. cost, pos = optimizer.optimize(func, iters=200)
  13. -- GA
  14.  
  15. from genetic_algorithm.main import genetic_optimisation
  16. param_space = {"x1": [-5, 5],"x2": [-5, 5]}
  17. xopt=genetic_optimisation(input_model=fitness, param_space=param_space, pop_size=100,
  18. num_parents=2,max_num_generations=500, mutation_prob=0.5, stoping_rounds=50, integer_params=[])
  19. print(xopt.get('best fitness'))
  20. print(xopt.get('best params'))
  21.  
  22. --draw
  23. import matplotlib.pyplot as plt
  24. from mpl_toolkits.mplot3d import Axes3D
  25. import numpy as np
  26. x1v = np.arange(-5, 5, 0.01)
  27. x2v = np.arange(-5, 5, 0.01)
  28. x1, x2 = np.meshgrid(x1v, x2v)
  29. f = np.sin(x1) + np.cos(x2)
  30. fig = plt.figure()
  31. ax = fig.gca(projection=ā€™3dā€™)
  32. p1 = ax.plot_surface(x1, x2, f)
  33. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement