《wsn定位蒙特卡洛盒mcb算法matlab源碼》由會員分享,可在線閱讀,更多相關(guān)《wsn定位蒙特卡洛盒mcb算法matlab源碼(21頁珍藏版)》請在裝配圖網(wǎng)上搜索。
1、clear;clc;%初始化工作Ns = 40;Nn = 400;Vmax = 50;Xrange = 500;Yrange = 500;tr = 100;Rg = 0.5*0;step = 6;N = 50;ns_range = 200; %每個采樣盒子的最大采樣次數(shù)Nf = 1.5; %采樣盒子確定時,估計位置要擴(kuò)大圓面積for i = 1:Ns Xseed(1,i) = unifrnd(Rg,(Xrange-Rg),1,1); Yseed(1,i) = unifrnd(Rg,(Yrange-Rg),1,1);endfor i = 1:Nn Xnode(1,i)=rand(1,1)*Xra
2、nge; Ynode(1,i)=rand(1,1)*Yrange; Xnode_g(1,i)=Xnode(1,i); %MCL估計位置,初始值設(shè)置為真實位置 Ynode_g(1,i)=Ynode(1,i);end%初始時刻的粒子群,for every nodefor i = 1:Nn for j = 1:N lx(i,j,1) = 0; ly(i,j,1) = 0; endendfor i = 1:Nn error(1,i) = 0;end%figure(1);%plot(Xseed,Yseed,bo,Xnode,Ynode,k*);%節(jié)點們開始運動,每次定位完成才開始下一次運動,這里假設(shè)這個
3、定位過程耗時非常短%仿真步數(shù)for k=2:step %新的時刻,節(jié)點們先運動一下,RWP模型 if Ynode(k,i) Yrange | Ynode(k,i) 0 Xnode(k,i) = Xnode(k-1,i) + r*cos(thita); Ynode(k,i) = Ynode(k-1,i) - r*sin(thita); end end %對每一個node逐個進(jìn)行定位 for i = 1:Nn %測試每個seed是否可以用來定位 A1 = ; %存儲1跳錨節(jié)點序號 A2 = ; %存儲2跳錨節(jié)點序號 for j = 1:Ns d1 = sqrt(Xnode(k,i)-Xseed(k
4、,j)2+(Ynode(k,i)-Yseed(k,j)2); if d1=Xsambox_max(i,j,k) | Ysambox_min(i,j,k)=Ysambox_max(i,j,k) Xsambox_min(i,j,k) = Xanbox_min(k,i); Xsambox_max(i,j,k) = Xanbox_max(k,i); Ysambox_min(i,j,k) = Yanbox_min(k,i); Ysambox_max(i,j,k) = Yanbox_max(k,i); end %從采樣盒子里采樣 lx(i,j,k) = unifrnd(Xsambox_min(i,j,k)
5、,Xsambox_max(i,j,k),1,1); ly(i,j,k) = unifrnd(Ysambox_min(i,j,k),Ysambox_max(i,j,k),1,1); ns = 1; %濾波 and = ; for m = 1:temp1 d1(m) = sqrt(lx(i,j,k)-Xseed(k,A1(m)2+(ly(i,j,k)-Yseed(k,A1(m)2); if d1(m)=tr and = and 0; end %進(jìn)行最終的判斷 and = ; for m = 1:temp1 d1(m) = sqrt(lx(i,j,k)-Xseed(k,A1(m)2+(ly(i,j,
6、k)-Yseed(k,A1(m)2); if d1(m)tr and = and 1; end end if sum(and)=0 lx(i,j,k) = 0; ly(i,j,k) = 0; end end %至此 節(jié)點 i 的粒子群已經(jīng)得到 %下面是進(jìn)行定位,粒子群的優(yōu)化 num_zero = 0; for j = 1:N if lx(i,j,k) = 0 num_zero = num_zero+1; end end %k時刻的定位位置 end %第二種情況 的 上一時間沒有錨節(jié)點,沒有采樣粒子的更新,那么這種情況下 采樣盒子就是錨盒子 if temp1=0 & temp2=0 & sum(
7、lx(i,:,k-1)=0 for j = 1:N sambox1 = Xanbox_min(k,i) ; sambox2 = Xanbox_max(k,i) ; sambox3 = Yanbox_min(k,i) ; sambox4 = Yanbox_max(k,i) ; Xsambox_min(i,j,k) = max(sambox1); Xsambox_max(i,j,k) = min(sambox2); Ysambox_min(i,j,k) = max(sambox3); Ysambox_max(i,j,k) = min(sambox4); %從采樣盒子里采樣 lx(i,j,k) =
8、unifrnd(Xsambox_min(i,j,k),Xsambox_max(i,j,k),1,1); ly(i,j,k) = unifrnd(Ysambox_min(i,j,k),Ysambox_max(i,j,k),1,1); ns = 1; %濾波 and = ; for m = 1:temp1 d1(m) = sqrt(lx(i,j,k)-Xseed(k,A1(m)2+(ly(i,j,k)-Yseed(k,A1(m)2); if d1(m)tr and = and 1; end end while(sum(and)=0 & ns = ns_range) %從采樣盒子里采樣 lx(i,j
9、,k) = unifrnd(Xsambox_min(i,j,k),Xsambox_max(i,j,k),1,1); ly(i,j,k) = unifrnd(Ysambox_min(i,j,k),Ysambox_max(i,j,k),1,1); %濾波 and = ; for m = 1:temp1 d1(m) = sqrt(lx(i,j,k)-Xseed(k,A1(m)2+(ly(i,j,k)-Yseed(k,A1(m)2); if d1(m)tr and = and 1; end end ns = ns+1; end %進(jìn)行最終的判斷 and = ; for m = 1:temp1 d1(m
10、) = sqrt(lx(i,j,k)-Xseed(k,A1(m)2+(ly(i,j,k)-Yseed(k,A1(m)2); if d1(m)tr and = and 1; end end if sum(and)=0 lx(i,j,k) = 0; ly(i,j,k) = 0; end end %至此 節(jié)點 i 的粒子群已經(jīng)得到 %下面是進(jìn)行定位,粒子群的優(yōu)化 num_zero = 0; for j = 1:N if lx(i,j,k) = 0 num_zero = num_zero+1; end end %k時刻的定位位置 if num_zero = N Xnode_g(k,i) = Xnode
11、_g(k-1,i); Ynode_g(k,i) = Ynode_g(k-1,i); end if num_zero = N Xnode_g(k,i) = sum(lx(i,:,k)/(N-num_zero); Ynode_g(k,i) = sum(ly(i,:,k)/(N-num_zero); end %粒子群補(bǔ)全N個 if num_zero = N for j = 1:N lx(i,j,k) = 0; ly(i,j,k) = 0; end end if num_zero = N for j = 1:N if lx(i,j,k) = 0 lx(i,j,k) = Xnode_g(k,i); ly
12、(i,j,k) = Ynode_g(k,i); end end end end %第三種情況 的 上一時間有錨節(jié)點,也就是有采樣粒子的更新 if temp1=0 & temp2=0 & sum(lx(i,:,k-1)=0 for j = 1:N sambox1 = Xanbox_min(k,i) Xnode_g(k-1,i)-Nf*Vmax; sambox2 = Xanbox_max(k,i) Xnode_g(k-1,i)+Nf*Vmax; sambox3 = Yanbox_min(k,i) Ynode_g(k-1,i)-Nf*Vmax; sambox4 = Yanbox_max(k,i) Y
13、node_g(k-1,i)+Nf*Vmax; Xsambox_min(i,j,k) = max(sambox1); Xsambox_max(i,j,k) = min(sambox2); Ysambox_min(i,j,k) = max(sambox3); Ysambox_max(i,j,k) = min(sambox4); %增加一步判斷項,若是錨盒子和運動圓能夠有交集,就使用交集,如果沒有交集,則使用錨盒子 if Xsambox_min(i,j,k)=Xsambox_max(i,j,k) | Ysambox_min(i,j,k)=Ysambox_max(i,j,k) Xsambox_min(
14、i,j,k) = Xanbox_min(k,i); Xsambox_max(i,j,k) = Xanbox_max(k,i); Ysambox_min(i,j,k) = Yanbox_min(k,i); Ysambox_max(i,j,k) = Yanbox_max(k,i); end %從采樣盒子里采樣 lx(i,j,k) = unifrnd(Xsambox_min(i,j,k),Xsambox_max(i,j,k),1,1); ly(i,j,k) = unifrnd(Ysambox_min(i,j,k),Ysambox_max(i,j,k),1,1); ns = 1; %濾波 and =
15、; for m = 1:temp2 d1(m) = sqrt(lx(i,j,k)-Xseed(k,A2(m)2+(ly(i,j,k)-Yseed(k,A2(m)2); if d1(m)tr & d1(m)tr & d1(m)tr & d1(m)tr & d1(m)tr & d1(m)=Xsambox_max(i,j,k) | Ysambox_min(i,j,k)=Ysambox_max(i,j,k) Xsambox_min(i,j,k) = Xanbox_min(k,i); Xsambox_max(i,j,k) = Xanbox_max(k,i); Ysambox_min(i,j,k) = Y
16、anbox_min(k,i); Ysambox_max(i,j,k) = Yanbox_max(k,i); end %從采樣盒子里采樣 %從采樣盒子里采樣 lx(i,j,k) = unifrnd(Xsambox_min(i,j,k),Xsambox_max(i,j,k),1,1); ly(i,j,k) = unifrnd(Ysambox_min(i,j,k),Ysambox_max(i,j,k),1,1); %濾波 and = ; for m = 1:temp1 d1(m) = sqrt(lx(i,j,k)-Xseed(k,A1(m)2+(ly(i,j,k)-Yseed(k,A1(m)2);
17、if d1(m)tr and = and 1; end end for m = 1:temp2 d1(m) = sqrt(lx(i,j,k)-Xseed(k,A2(m)2+(ly(i,j,k)-Yseed(k,A2(m)2); if d1(m)tr & d1(m)=2*tr and = and 0; else and = and 1; end end ns = ns+1; end %進(jìn)行最終的判斷 and = ; for m = 1:temp1 d1(m) = sqrt(lx(i,j,k)-Xseed(k,A1(m)2+(ly(i,j,k)-Yseed(k,A1(m)2); if d1(m)t
18、r and = and 1; end end for m = 1:temp2 d1(m) = sqrt(lx(i,j,k)-Xseed(k,A2(m)2+(ly(i,j,k)-Yseed(k,A2(m)2); if d1(m)tr & d1(m)=2*tr and = and 0; else and = and 1; end end if sum(and)=0 lx(i,j,k) = 0; ly(i,j,k) = 0; end end %至此 節(jié)點 i 的粒子群已經(jīng)得到 %下面是進(jìn)行定位,粒子群的優(yōu)化 num_zero = 0; for j = 1:N if lx(i,j,k) = 0 num_zero = num_zero+1; end end %k時刻的定位位置 if num_zero = N Xnode_g(k,i) = Xnode_g(k-1,i); Ynode_g(k,i) = Ynode_g(k-1,i);