⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 simulatedannealing1.m

📁 运用模拟退火算法对战区通信网频率进行了规划
💻 M
字号:
%模拟退火主函数,返回每个移动站的分配频率矩阵
function frequency_planning=simulatedannealing1()

% global p_same;
% global p_near;
% global p_three;
% global p_wav;
% global p_address;

N=input('移动站个数=');
                                                          
frequency=input('待分配的频率=(单位GHz)');       %[4.40--5.00GHz]

initial_temperature=input('初始温度=');

cooling_rate=input('退火率=');

tmin=input('终止退火门限=');

d0=input('同频干扰最小间隔距离=') ;               %40km                               

d1=input('临频干扰最小间隔距离=') ;                %20km                                 

d2=input('同址间隔=') ;                    %1                         

m=input('同址间隔频率=');                   %0.12GHz     

dir=input('最小方向角度间隔=');

%global N 



%随机产生N个点的坐标
coordinate=rand(2,N)*200                                            %范围200×200km
%coordinate=[4.2380 4.1110 3.4424 3.4360 5.7528 7.4141 1.1123 0.4739 3.8062 1.7595;...
  %  6.4383 6.9217 4.5624 3.1762 5.3925 3.3502 8.6671 9.3412 0.8549 1.3039]

frequency_planning=zeros(3,N);

frequency_planning(1,:)=coordinate(1,:);

frequency_planning(2,:)=coordinate(2,:);

EE=zeros(1,2000);



%[4.7981 4.4276 5.6395 9.8541 5.5078 2.6521 0.7307 6.4281 ... 
           % 8.4570 2.2074 0.0923 8.3862 5.8552 8.3150 2.0552 3.1368 ...
           % 7.0747 4.1244 0.7868 1.0697;9.9396 5.2813 0.4833 0.6265 ...
            %4.3875 3.4448 4.3542 1.1874 1.5556 1.8544 5.0770 9.1364 ...
           % 7.2634 5.2048 9.9263 3.7561 7.1278 8.2866 3.8678 8.8534] ;           %随机产生一个2×N的矩阵,作为每个移动站的坐标,
                                                                                   %其第一行为每个站的横坐标,第二行为每个站的纵坐标
%for j=1:2  
    
    st=cputime;
                                                                                   
Location_frequency=mobilestation(coordinate,N,frequency);                          %首先随机进行频率分配,作为分配的初值

global iterations ;                                                                %iterations为每次退火的迭代次数

temperature=initial_temperature;                                                 %初始温度赋给变量temperature

iterations=0;

complete_temperature_iterations=0;                                                  %记录达到冷凝温度的次数

Nk=N;



n=N;

Eold=objective_function(Location_frequency,frequency,N,coordinate,d0,d1,d2,m,dir);   %产生一个目标函数
      
EEold=Eold;

while temperature>tmin

       for k=1:Nk
                 
          Location_frequency=swapmobilestation(n,Location_frequency)  ;                  %调用swapmobilestation函数产生新的频率配置
    
          Enew=objective_function(Location_frequency,frequency,N,coordinate,d0,d1,d2,m,dir); %再次计算目标函数
          
     
          
        
    
          diff=abs(Enew-Eold);                                                          %目标函数的差值保存给变量diff
          
          
    
          if Enew<Eold                                                                   %如果新的目标函数小于旧的目标函数
                                                                                         %证明新的频率分配比旧的频率分配所产生的干扰要小
             %if j==1                                                
                                         
             frequency_planning(3,:)=Location_frequency(3,:);                                    %新的频率分配赋给frequency_planing返回
             
             %else frequency_planning(4,:)=Location_frequency(3,:);
                 
             %end
        
             Eold=Enew;                                                                  %把新的目标函数值赋给旧的,以便下次比较
          
             n=round(n*exp(-diff/(k*temperature)));                                      %首先指定最大数目的交换
                                                                                         %交换的次数随温度的降低而减少,swapmobilestation函数
             
                                                                                         %中交换的频率减少,最后达到只有一对频率进行交换
                                                                                         %n不能等于0
          
             if n==0                                                                     %如果n等于0,让n等于1
              
                n=1;
              
             end
        
          else
             if rand(1)<exp(-diff/(temperature))                                  %如果不满足条件,则以小于exp(-diff/(temperature))的概率接受
                                                                                  %Metropolis接受
                 %if j==1                                                                 
                 
                 frequency_planning(3,:)=Location_frequency(3,:);
                 
                 %else frequency_planning(4,:)=Location_frequency(3,:);
           
                 %end
                     
                 Eold=Enew;
                
                n=round(n*exp(-diff/(k*temperature)));
          
                if n==0
              
                   n=1;
              
                end
           
             end
         
          end
      end
          if Eold>=EEold                              %达到冷凝的条件
              
              complete_temperature_iterations=complete_temperature_iterations+1;         %冷凝的次数加1
              
          else EEold=Eold;
              
              complete_temperature_iterations=0;
              
          end 
          
      if complete_temperature_iterations>=30 ;                                   %冷凝温度超过50次终止退火,连续若干个新解都没有被接受时终止算法
        
         break;
         
      end
        
      temperature=cooling_rate*temperature;                                      %进行退火T=aT,采用指数退温
      
      Nk=round(Nk/cooling_rate);                                                %随着温度的降低增加迭代的次数
        
      iterations=iterations+1;                                                 %记录退火的次数
      
     EE(1,iterations)=EEold;
     
 
      
end

EEE=zeros(1,iterations);

for i=1:2000
    
    if EE(1,i)~=0
        
        EEE(1,i)=EE(1,i);
        
    end
    
end


%if j==1

x=1:1:iterations;



plot(x,EEE);

xlabel('迭代次数(降温次数)');

ylabel('目标函数值');



et=cputime-st;


fprintf('\n\n\n\t\t\t降温次数=%d\n',iterations);
    
fprintf('\t\t\t目前温度=%3.3f\n',temperature);

fprintf('\t\t\t最终达到冷凝温度%d次\n',complete_temperature_iterations);

fprintf('\t\t\t最终目标函数值=%d\n',EEold);

fprintf('\t\t\t运行时间=%3.4f\n',et);

end

%end

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -