|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?我要加入
x
GA与神经网络的结合的程序
- clear;
- % Generate a set of points in the space
- source_point = -10+20*rand(2,200);
- % Classified boundary is y = sin(x)+2*cos^2(x)
- points_c = zeros(1,200)
- for i = 1:200
- point_c(i) = source_point(2,i)>(sin(source_point(1,i))+2*(cos(source_point(1,i))^2));
- end
- save training_data
- % Plot the result
- figure();
- hold on;
- for i = 1:200
- if point_c(i) == 1
- plot(source_point(1,i),source_point(2,i),'*');
- else
- plot(source_point(1,i),source_point(2,i),'+');
- end
- end
- %plot the boundary
- x = -10:0.01:10;
- y = sin(x)+2.*cos(x).^2;
- plot(x,y,'r');
- hold off;
- % The training progress
- % Defined the lower boundary
- LB = zeros(1,25);
- LB(1:18) = -1*ones(1,18);
- LB(19:25) = -10*ones(1,7);
- % Defined the upper boundary
- UB = zeros(1,25);
- UB(1:18) = 1*ones(1,18);
- UB(19:25) = 10*ones(1,7);
- % Set the training option
- options=gaoptimset;
- options.PopulationSize=20;
- options.Generations=100;
- options.StallGenLimit=Inf;
- options.StallTimeLimit=Inf;
- options = gaoptimset(options,'PlotFcns',{@gaplotbestf}')
- [par,fval] = ga(@fitness_fuction,25,[],[],[],[],LB,UB,[],options);
- % Generate 50 points to test the parameter
- exam_point = -10+20*rand(2,50);
- output = exam_function(par,exam_point);
- % Plot the result
- figure();
- hold on;
- for i = 1:50
- if output(i) == 1
- plot(exam_point(1,i),exam_point(2,i),'*');
- else
- plot(exam_point(1,i),exam_point(2,i),'+');
- end
- end
- %plot the boundary
- x = -10:0.01:10;
- y = sin(x)+2.*cos(x).^2;
- plot(x,y,'r');
- hold off;
复制代码
fitness function的源代码:
- function fval = fitness_function(x);
- load training_data;
- h = zeros(6,200);
- % Hidden layer output
- h_out(1,:)= tanh(source_point(1,:)*x(1)+source_point(2,:)*x(2)+x(19));
- h_out(2, :) = tanh(source_point(1,:)*x(3)+source_point(2,:)*x(4)+x(20));
- h_out(3, :)= tanh(source_point(1,:)*x(5)+source_point(2,:)*x(6)+x(21));
- h_out(4, :)= tanh(source_point(1,:)*x(7)+source_point(2,:)*x(8)+x(22));
- h_out(5, :)= tanh(source_point(1,:)*x(9)+source_point(2,:)*x(10)+x(23));
- h_out(6, :)= tanh(source_point(1,:)*x(11)+source_point(2,:)*x(12)+x(24));
- % Output layer output
- res = x(13:18)*h_out+x(25);
- zm = zeros(1,200);
- om = ones(1,200);
- res(reszm) = om(res>zm);
- fval = sum(abs(point_c-res));
复制代码
检测程序的源代码:
- function res = exam_function(x,data);
- h = zeros(6,50);
- % Hidden layer output
- h_out(1, :)= tanh(data(1,:)*x(1)+data(2,:)*x(2)+x(19));
- h_out(2, :)= tanh(data(1,:)*x(3)+data(2,:)*x(4)+x(20));
- h_out(3, :)= tanh(data(1,:)*x(5)+data(2,:)*x(6)+x(21));
- h_out(4, :)= tanh(data(1,:)*x(7)+data(2,:)*x(8)+x(22));
- h_out(5, :)= tanh(data(1,:)*x(9)+data(2,:)*x(10)+x(23));
- h_out(6, :)= tanh(data(1,:)*x(11)+data(2,:)*x(12)+x(24));
- % Output layer output
- res = x(13:18)*h_out+x(25);
- zm = zeros(1,50);
- om = ones(1,50);
- res(reszm) = om(res>zm);
复制代码 |
|