【优化布局】基于遗传算法求解PCB元器件布局优化问题附matlab代码
1 简介遗传算法GeneticAlgorithmGA是一种受人工生命启发模拟生物进化过程的随机搜索算法。遗传算法的理论及应用的研究受到广大研究者们的重视应用领域也得到了广泛推广。遗传算法在求解函数优化问题时算法中的控制参数交叉概率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 GUIDEs 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 jj1:circuit(ii,4) % for kk1:circuit(ii,3) % board(layout(ii,3,1)kk275,layout(ii,2,1)-jj352)board(layout(ii,3,1)kk275,layout(ii,2,1)-jj352) | 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 jj1:circuit(ii,4) % for kk1:circuit(ii,3) % board(layout(ii,3,1)-jj277,layout(ii,2,1)-kk352)board(layout(ii,3,1)-jj277,layout(ii,2,1)-kk352) | u(kk,jj); % end % end end hold on; line(x,y); hold on; end for jj1:ComponentNumber kkPinNumofSameNet; while NetTree(kk,3,jj,next)~0 line([net(NetTree(kk,1,jj,next),1,jj1,next),net(NetTree(kk,2,jj,next),1,jj1,next)],[net(NetTree(kk,1,jj,next),2,jj1,next),net(NetTree(kk,2,jj,next),2,jj1,next)],Color,r); kkkk-1; end end for kk1:ComponentNumber1 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 nextgroup next1; else nextnext1; 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,请等待...); pExchangestr2double(get(handles.edit4,String)); pChangestr2double(get(handles.edit5,String)); global gene; global group; global ComponentNumber; global generation; global best; % worst1:2; % Gdelay1; % for ii3: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; bestnum0; for ii1:group if (best(1,1)gene(1,1,ii)) (gene(1,3,ii)0) best(1,1)gene(1,1,ii); bestnumii; end end if bestnum~0 bestgene(:,:,bestnum); %global next; axis([-350,350,-275,275]); cla; for ii1:ComponentNumber % clear u; % ufootprint(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 jj1:circuit(ii,4) % for kk1:circuit(ii,3) % board(layout(ii,3,1)-kk277,layout(ii,2,1)jj350)board(layout(ii,3,1)-kk277,layout(ii,2,1)jj350) | 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 jj1:circuit(ii,4) % for kk1:circuit(ii,3) % board(layout(ii,3,1)jj275,layout(ii,2,1)kk350)board(layout(ii,3,1)jj275,layout(ii,2,1)kk350) | 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 jj1:circuit(ii,4) % for kk1:circuit(ii,3) % board(layout(ii,3,1)kk275,layout(ii,2,1)-jj352)board(layout(ii,3,1)kk275,layout(ii,2,1)-jj352) | 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 jj1:circuit(ii,4) % for kk1:circuit(ii,3) % board(layout(ii,3,1)-jj277,layout(ii,2,1)-kk352)board(layout(ii,3,1)-jj277,layout(ii,2,1)-kk352) | u(kk,jj); % end % end end hold on; line(x,y); hold on; end for jj1:ComponentNumber kkPinNumofSameNet; while NetTree(kk,3,jj,bestnum)~0 line([net(NetTree(kk,1,jj,bestnum),1,jj1,bestnum),net(NetTree(kk,2,jj,bestnum),1,jj1,bestnum)],[net(NetTree(kk,1,jj,bestnum),2,jj1,bestnum),net(NetTree(kk,2,jj,bestnum),2,jj1,bestnum)],Color,r); kkkk-1; end end for kk1:ComponentNumber1 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; run0; % 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代码问题可私信交流。部分理论引用网络文献若有侵权联系博主删除。