声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 1763|回复: 1

[人工智能] GA与神经网络的结合的程序

[复制链接]
发表于 2007-6-25 03:32 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
GA与神经网络的结合的程序
  1. clear;
  2. % Generate a set of points in the space
  3. source_point = -10+20*rand(2,200);

  4. % Classified boundary is y = sin(x)+2*cos^2(x)
  5. points_c = zeros(1,200)
  6. for i = 1:200
  7.   point_c(i) = source_point(2,i)>(sin(source_point(1,i))+2*(cos(source_point(1,i))^2));
  8. end

  9. save training_data
  10. % Plot the result
  11. figure();
  12. hold on;
  13. for i = 1:200
  14.   if point_c(i) == 1
  15.       plot(source_point(1,i),source_point(2,i),'*');
  16.   else
  17.       plot(source_point(1,i),source_point(2,i),'+');
  18.   end
  19. end
  20. %plot the boundary
  21. x = -10:0.01:10;
  22. y = sin(x)+2.*cos(x).^2;
  23. plot(x,y,'r');
  24. hold off;

  25. % The training progress
  26. % Defined the lower boundary
  27. LB = zeros(1,25);
  28. LB(1:18) = -1*ones(1,18);
  29. LB(19:25) = -10*ones(1,7);

  30. % Defined the upper boundary
  31. UB = zeros(1,25);
  32. UB(1:18) = 1*ones(1,18);
  33. UB(19:25) = 10*ones(1,7);

  34. % Set the training option
  35. options=gaoptimset;
  36. options.PopulationSize=20;
  37. options.Generations=100;
  38. options.StallGenLimit=Inf;
  39. options.StallTimeLimit=Inf;
  40. options = gaoptimset(options,'PlotFcns',{@gaplotbestf}')
  41. [par,fval] = ga(@fitness_fuction,25,[],[],[],[],LB,UB,[],options);

  42. % Generate 50 points to test the parameter
  43. exam_point = -10+20*rand(2,50);
  44. output = exam_function(par,exam_point);

  45. % Plot the result
  46. figure();
  47. hold on;
  48. for i = 1:50
  49.   if output(i) == 1
  50.       plot(exam_point(1,i),exam_point(2,i),'*');
  51.   else
  52.       plot(exam_point(1,i),exam_point(2,i),'+');
  53.   end
  54. end
  55. %plot the boundary
  56. x = -10:0.01:10;
  57. y = sin(x)+2.*cos(x).^2;
  58. plot(x,y,'r');
  59. hold off;
复制代码


fitness function的源代码:

  1. function fval = fitness_function(x);

  2. load training_data;

  3. h = zeros(6,200);
  4. % Hidden layer output
  5. h_out(1,:)= tanh(source_point(1,:)*x(1)+source_point(2,:)*x(2)+x(19));
  6. h_out(2, :) = tanh(source_point(1,:)*x(3)+source_point(2,:)*x(4)+x(20));
  7. h_out(3, :)= tanh(source_point(1,:)*x(5)+source_point(2,:)*x(6)+x(21));
  8. h_out(4, :)= tanh(source_point(1,:)*x(7)+source_point(2,:)*x(8)+x(22));
  9. h_out(5, :)= tanh(source_point(1,:)*x(9)+source_point(2,:)*x(10)+x(23));
  10. h_out(6, :)= tanh(source_point(1,:)*x(11)+source_point(2,:)*x(12)+x(24));

  11. % Output layer output
  12. res = x(13:18)*h_out+x(25);
  13. zm = zeros(1,200);
  14. om = ones(1,200);
  15. res(reszm) = om(res>zm);
  16. fval = sum(abs(point_c-res));
复制代码


检测程序的源代码:
  1. function res = exam_function(x,data);
  2. h = zeros(6,50);
  3. % Hidden layer output
  4. h_out(1, :)= tanh(data(1,:)*x(1)+data(2,:)*x(2)+x(19));
  5. h_out(2, :)= tanh(data(1,:)*x(3)+data(2,:)*x(4)+x(20));
  6. h_out(3, :)= tanh(data(1,:)*x(5)+data(2,:)*x(6)+x(21));
  7. h_out(4, :)= tanh(data(1,:)*x(7)+data(2,:)*x(8)+x(22));
  8. h_out(5, :)= tanh(data(1,:)*x(9)+data(2,:)*x(10)+x(23));
  9. h_out(6, :)= tanh(data(1,:)*x(11)+data(2,:)*x(12)+x(24));

  10. % Output layer output
  11. res = x(13:18)*h_out+x(25);
  12. zm = zeros(1,50);
  13. om = ones(1,50);
  14. res(reszm) = om(res>zm);
复制代码
回复
分享到:

使用道具 举报

发表于 2008-3-4 22:46 | 显示全部楼层
有点意思哦
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-11-17 17:42 , Processed in 0.077586 second(s), 17 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表