Advertisement
Graf_Rav

GA Using

Mar 18th, 2020
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.76 KB | None | 0 0
  1. import neuro_library as nl
  2. import matplotlib.pyplot as plt
  3.  
  4. #Создадим генетический алгоритм с обучающей выборкой
  5. ga=nl.GeneticAlgorith()
  6. ga.selection.append(nl.StudyMatrixItem([0,0],[0]))
  7. ga.selection.append(nl.StudyMatrixItem([0,1],[1]))
  8. ga.selection.append(nl.StudyMatrixItem([1,0],[1]))
  9. ga.selection.append(nl.StudyMatrixItem([1,1],[0]))
  10.  
  11.  
  12. #Создаем нейросеть
  13. net=nl.NeuralNet(2,1)
  14. net.create_layer(2,2,100)
  15. net.create_layer(1,2,100)
  16.  
  17.  
  18. #Выведем результат теста
  19. for item in ga.selection:
  20.     net.set_incomes(item.incomes)
  21.     net.compute()
  22.     print("Вход: ",item.incomes,"; выход желаемый: ",item.outcomes," выход реальный: ", net.outputs)
  23.  
  24. #Инициализиурем генетический алгоритм
  25. ga.init_population_from_net(net,3)
  26.  
  27. print("-------------")
  28.  
  29. #задаем начальные массивы
  30. x=[]
  31. y=[]
  32.  
  33. #Проводим обучение
  34. i=1
  35. k=1
  36. x.append(0)
  37. y.append(ga.population[0].target_function)
  38. while i<=150:
  39.     ga.next_age()
  40.     x.append(i)
  41.     y.append(ga.population[0].target_function)
  42.     i=i+1
  43.     k=k+1
  44.  
  45. #Выведем результат теста
  46. print("После оптимизации")
  47. net=ga.population[0]
  48. for item in ga.selection:
  49.     net.set_incomes(item.incomes)
  50.     net.compute()
  51.     print("Вход: ",item.incomes,"; выход желаемый: ",item.outcomes," выход реальный: ", net.outputs)
  52.        
  53.  
  54. #Строим график
  55. fig = plt.figure()
  56. plt.plot(x, y)
  57.      
  58. #Отображаем заголовки и подписи осей
  59. plt.title('График функции')
  60. plt.ylabel('Ось Y')
  61. plt.xlabel('Ось X')
  62. plt.grid(True)
  63. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement