【优化布局】基于遗传算法求解PCB元器件布局优化问题附matlab代码

178 阅读6分钟

1 简介

遗传算法(GeneticAlgorithm,GA)是一种受人工生命启发,模拟生物进化过程的随机搜索算法。遗传算法的理论及应用的研究受到广大研究者们的重视,应用领域也得到了广泛推广。遗传算法在求解函数优化问题时,算法中的控制参数交叉概率Pc和变异概率Pm取值的选择对遗传算法的性能影响很大,目前,普遍接受的Pc和Pm取值范围分别是0.4~0.99和0.0001~0.1,这两个取值范围的合理性以及科学性缺乏有效研究。为此,本文针对一类能够展开成幂级数的函数,通过大量实验,以遗传算法在求得全局最优解时所需的迭代次数最少为目标,用实验验证和数据分析的方法对Pc和Pm的取值范围进行了系统的研究,得出结论如下:(1)通过对大量实验数据的分析,以遗传算法在求得全局最优解时所需的迭代次数最少为目标,本文得出Pc的建议取值区间为[0.6,0.99], Pm的建议取值区间为[0.009,0.03];(2)通过实验数据,对Pc和Pm的交叉影响进行了分析,当Pc在本文建议的区间中取值时,Pm对遗传算法在求得全局最优解时所需的迭代次数有显著影响,然而当Pm在本文建议的区间中取值时,Pc对遗传算法在求得全局最优解时所需的迭代次数的影响不显著;(3)当变异概率Pm在本文建议的区间内取值时,GA取得全局最优解所需的计算量比在普遍接受的区间内取值时所需的计算量节约了3倍以上;比在本文建议区间之外但又在普遍接受的区间内取值时所需的计算量节约了4倍以上。​

2 部分代码

function varargout = IOA(varargin)
% IOA M-file for IOA.fig
%     IOA, by itself, creates a new IOA or raises the existing
%     singleton*.
%
%     H = IOA returns the handle to a new IOA or the handle to
%     the existing singleton*.
%
%     IOA('CALLBACK',hObject,eventData,handles,...) calls the local
%     function named CALLBACK in IOA.M with the given input arguments.
%
%     IOA('Property','Value',...) creates a new IOA or raises the
%     existing singleton*. Starting from the left, property value pairs are
%     applied to the GUI before IOA_OpeningFcn gets called. An
%     unrecognized property name or invalid value makes property application
%     stop. All inputs are passed to IOA_OpeningFcn via varargin.
%
%     *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
%     instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help IOA

% Last Modified by GUIDE v2.5 03-Dec-2009 19:33:29
2,next),layout(ii,2,next)];
           y=[layout(ii,3,next),layout(ii,3,next),layout(ii,3,next)+circuit(ii,3)-1,layout(ii,3,next)+circuit(ii,3)-1,layout(ii,3,next)];
           line([layout(ii,2,next),layout(ii,2,next)-4],[layout(ii,3,next)+4,layout(ii,3,next)]);
%             for jj=1:circuit(ii,4)
%                 for kk=1:circuit(ii,3)
%                     board(layout(ii,3,1)+kk+275,layout(ii,2,1)-jj+352)=board(layout(ii,3,1)+kk+275,layout(ii,2,1)-jj+352) | u(kk,jj);
%                 end
%             end
       case 3
           x=[layout(ii,2,next),layout(ii,2,next),layout(ii,2,next)-circuit(ii,3)+1,layout(ii,2,next)-circuit(ii,3)+1,layout(ii,2,next)];
           y=[layout(ii,3,next),layout(ii,3,next)-circuit(ii,4)+1,layout(ii,3,next)-circuit(ii,4)+1,layout(ii,3,next),layout(ii,3,next)];
           line([layout(ii,2,next),layout(ii,2,next)-4],[layout(ii,3,next)-4,layout(ii,3,next)]);
%             for jj=1:circuit(ii,4)
%                 for kk=1:circuit(ii,3)
%                     board(layout(ii,3,1)-jj+277,layout(ii,2,1)-kk+352)=board(layout(ii,3,1)-jj+277,layout(ii,2,1)-kk+352) | u(kk,jj);
%                 end
%             end
   end
   hold on;
   line(x,y);
   hold on;
end
for jj=1:ComponentNumber
   kk=PinNumofSameNet;
   while NetTree(kk,3,jj,next)~=0
       line([net(NetTree(kk,1,jj,next),1,jj+1,next),net(NetTree(kk,2,jj,next),1,jj+1,next)],[net(NetTree(kk,1,jj,next),2,jj+1,next),net(NetTree(kk,2,jj,next),2,jj+1,next)],'Color','r');
       kk=kk-1;
   end
end
for kk=1:ComponentNumber+1
   if net(1,1,kk,next)>1
       plot(net(2:net(1,1,kk,next),1,kk,next),net(2:net(1,1,kk,next),2,kk,next),'.');
   end
   hold on;
end
str=['p=',num2str(gene(1,1,next)),' L=',num2str(gene(1,2,next)),' S=',num2str(gene(1,3,next))];
text(-340,250,str);
hold off;
if next==group
   next=1;
else
   next=next+1;
end
% hObject   handle to pushbutton3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles   structure with handles and user data (see GUIDATA)



function edit_beta_Callback(hObject, eventdata, handles)
% hObject   handle to edit_beta (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles   structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit_beta as text
%       str2double(get(hObject,'String')) returns contents of edit_beta as a double


% --- Executes during object creation, after setting all properties.
function edit_beta_CreateFcn(hObject, eventdata, handles)
% hObject   handle to edit_beta (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles   empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
   set(hObject,'BackgroundColor','white');
end



function edit4_Callback(hObject, eventdata, handles)
% hObject   handle to edit4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles   structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit4 as text
%       str2double(get(hObject,'String')) returns contents of edit4 as a double


% --- Executes during object creation, after setting all properties.
function edit4_CreateFcn(hObject, eventdata, handles)
% hObject   handle to edit4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles   empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
   set(hObject,'BackgroundColor','white');
end



function edit5_Callback(hObject, eventdata, handles)
% hObject   handle to edit5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles   structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit5 as text
%       str2double(get(hObject,'String')) returns contents of edit5 as a double


% --- Executes during object creation, after setting all properties.
function edit5_CreateFcn(hObject, eventdata, handles)
% hObject   handle to edit5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles   empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
   set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
set(handles.text7,'String','请等待...');
pExchange=str2double(get(handles.edit4,'String'));
pChange=str2double(get(handles.edit5,'String'));
global gene;
global group;
global ComponentNumber;
global generation;
global best;
% worst=1:2;
% Gdelay=1;
% for ii=3:group
%     if gene(1,1,ii)<gene(1,1,worst(1))
%         worst(1)=ii;
%     else if gene(1,1,ii)<gene(1,1,worst(2))
%             worst(2)=ii;
%         end
%     end
% end
   best(1,1)=(max(gene(1,2,:))+max(max(gene(1,3,:)),best(1,3)))/(best(1,2)+best(1,3))-1;
   bestnum=0;
   for ii=1:group
       if (best(1,1)<gene(1,1,ii)) && (gene(1,3,ii)==0)
           best(1,1)=gene(1,1,ii);
           bestnum=ii;
       end
   end
   if bestnum~=0
       best=gene(:,:,bestnum);
       %global next;
       axis([-350,350,-275,275]);
       cla;
       for ii=1:ComponentNumber
       %     clear u;
       %     u=footprint(circuit(ii,2),circuit(ii,5:20));
           switch layout(ii,4,bestnum)
               case 0
                   x=[layout(ii,2,bestnum),layout(ii,2,bestnum)+circuit(ii,4)-1,layout(ii,2,bestnum)+circuit(ii,4)-1,layout(ii,2,bestnum),layout(ii,2,bestnum)];
                   y=[layout(ii,3,bestnum),layout(ii,3,bestnum),layout(ii,3,bestnum)-circuit(ii,3)+1,layout(ii,3,bestnum)-circuit(ii,3)+1,layout(ii,3,bestnum)];
                   line([layout(ii,2,bestnum),layout(ii,2,bestnum)+4],[layout(ii,3,bestnum)-4,layout(ii,3,bestnum)]);
       %             for jj=1:circuit(ii,4)
       %                 for kk=1:circuit(ii,3)
       %                     board(layout(ii,3,1)-kk+277,layout(ii,2,1)+jj+350)=board(layout(ii,3,1)-kk+277,layout(ii,2,1)+jj+350) | u(kk,jj);
       %                 end
       %             end
               case 1
                   x=[layout(ii,2,bestnum),layout(ii,2,bestnum),layout(ii,2,bestnum)+circuit(ii,3)-1,layout(ii,2,bestnum)+circuit(ii,3)-1,layout(ii,2,bestnum)];
                   y=[layout(ii,3,bestnum),layout(ii,3,bestnum)+circuit(ii,4)-1,layout(ii,3,bestnum)+circuit(ii,4)-1,layout(ii,3,bestnum),layout(ii,3,bestnum)];
                   line([layout(ii,2,bestnum),layout(ii,2,bestnum)+4],[layout(ii,3,bestnum)+4,layout(ii,3,bestnum)]);
       %             for jj=1:circuit(ii,4)
       %                 for kk=1:circuit(ii,3)
       %                     board(layout(ii,3,1)+jj+275,layout(ii,2,1)+kk+350)=board(layout(ii,3,1)+jj+275,layout(ii,2,1)+kk+350) | u(kk,jj);
       %                 end
       %             end
               case 2
                   x=[layout(ii,2,bestnum),layout(ii,2,bestnum)-circuit(ii,4)+1,layout(ii,2,bestnum)-circuit(ii,4)+1,layout(ii,2,bestnum),layout(ii,2,bestnum)];
                   y=[layout(ii,3,bestnum),layout(ii,3,bestnum),layout(ii,3,bestnum)+circuit(ii,3)-1,layout(ii,3,bestnum)+circuit(ii,3)-1,layout(ii,3,bestnum)];
                   line([layout(ii,2,bestnum),layout(ii,2,bestnum)-4],[layout(ii,3,bestnum)+4,layout(ii,3,bestnum)]);
       %             for jj=1:circuit(ii,4)
       %                 for kk=1:circuit(ii,3)
       %                     board(layout(ii,3,1)+kk+275,layout(ii,2,1)-jj+352)=board(layout(ii,3,1)+kk+275,layout(ii,2,1)-jj+352) | u(kk,jj);
       %                 end
       %             end
               case 3
                   x=[layout(ii,2,bestnum),layout(ii,2,bestnum),layout(ii,2,bestnum)-circuit(ii,3)+1,layout(ii,2,bestnum)-circuit(ii,3)+1,layout(ii,2,bestnum)];
                   y=[layout(ii,3,bestnum),layout(ii,3,bestnum)-circuit(ii,4)+1,layout(ii,3,bestnum)-circuit(ii,4)+1,layout(ii,3,bestnum),layout(ii,3,bestnum)];
                   line([layout(ii,2,bestnum),layout(ii,2,bestnum)-4],[layout(ii,3,bestnum)-4,layout(ii,3,bestnum)]);
       %             for jj=1:circuit(ii,4)
       %                 for kk=1:circuit(ii,3)
       %                     board(layout(ii,3,1)-jj+277,layout(ii,2,1)-kk+352)=board(layout(ii,3,1)-jj+277,layout(ii,2,1)-kk+352) | u(kk,jj);
       %                 end
       %             end
           end
           hold on;
           line(x,y);
           hold on;
       end
       for jj=1:ComponentNumber
           kk=PinNumofSameNet;
           while NetTree(kk,3,jj,bestnum)~=0
               line([net(NetTree(kk,1,jj,bestnum),1,jj+1,bestnum),net(NetTree(kk,2,jj,bestnum),1,jj+1,bestnum)],[net(NetTree(kk,1,jj,bestnum),2,jj+1,bestnum),net(NetTree(kk,2,jj,bestnum),2,jj+1,bestnum)],'Color','r');
               kk=kk-1;
           end
       end
       for kk=1:ComponentNumber+1
           if net(1,1,kk,bestnum)>1
               plot(net(2:net(1,1,kk,bestnum),1,kk,bestnum),net(2:net(1,1,kk,bestnum),2,kk,bestnum),'.');
           end
           hold on;
       end
       str=['p=',num2str(best(1,1)),' L=',num2str(best(1,2)),' S=',num2str(best(1,3))];
       text(-340,250,str);
       hold off;
   end
   str1=['当前:第',num2str(generation),'代'];
   set(handles.text7,'String',str1);
   pause(1);
end
% hObject   handle to pushbutton5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles   structure with handles and user data (see GUIDATA)


% --- Executes on button press in pushbutton6.
function pushbutton6_Callback(hObject, eventdata, handles)
global run;
run=0;
% hObject   handle to pushbutton6 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles   structure with handles and user data (see GUIDATA)

3 仿真结果

4 参考文献

[1]黄艳飞. 基于热可靠性的PCB板电子元件优化布局方法研究. Diss. 江苏大学.

博主简介:擅长智能优化算法、神经网络预测、信号处理、元胞自动机、图像处理、路径规划、无人机等多种领域的Matlab仿真,相关matlab代码问题可私信交流。

部分理论引用网络文献,若有侵权联系博主删除。