Hello guys,
I'm trying to run the function "WattsStrogatz" 100 times and in each iteration I want to run the function "Test_Healing" 300 times, kindly see the attached screenshots. When I run my script, MATLAB says that variable G is not defined. Could you please teach me how to do it.
Thanks!
function fos=Test_Healing(Nodes,k)% Generating a spanning tree graph from Small World architecture
%G = WattsStrogatz(Nodes,2,.25);
%Nodes=100;
E=G.Edges{:,:};%figure;
%p = plot(G);
[T,Pred]=minspantree(G);Ea=T.Edges{:,:};%highlight(p,T);
%figure;%pt=plot(T);
Ed=E;[ia, ib] = ismember(Ed, Ea, 'rows'); %E-Ea, Ed here is E
Ed(ia, :) = []; %Dormant Edges
edge=randperm(Nodes-1,k); Tf=rmedge(T,edge);%figure;%Pf=plot(Tf);
% Egf(:,3)=[];
Egf=Tf.Edges{:,:};Ef=Ea;E_fail=[]; %Constructing Efail i.e. contains all isolated edges
[iu, iv] = ismember(Ef, Egf, 'rows'); %E-Ea, Ef here is Ea
Ef(iu, :) = []; % first failed Edge
E_fail=Ef;[bins,binsizes]=conncomp(Tf);% no. of nodes in each component
Components=length(binsizes);%no of components in the graph after the failure happens
idx_u=find(bins~=1);%Unserved Nodes (U)
idx=find(bins==1);% V'=V-U
eff=ismember(Ea,idx_u);E_fail=vertcat(E_fail,Ea(eff(:,1),:)); % Now all fail edges are in E_fail (Ef like in paper)
%%%%%%%%%%%%%%%%%% Algorithm %%%%%%%%%%%%%%%%%%
% Ea'=Ea=Ef
Ea_prime=Ea;[ix, iy]=ismember(Ea_prime,E_fail,'rows'); Ea_prime(ix,:)=[]; % Ed'=Ed-Ef
Ed_prime=Ed;[ixd iyd]=ismember(Ed_prime,E_fail,'rows');Ed_prime(ixd,:)=[];U=idx_u;idxx=idx;loopCounter = 1while ~isempty(U)for jj=length(U):-1:1 a=neighbors(G,idx_u(jj)); [nx ny]=ismember( a,neighbors(T,idx_u(jj)),'rows'); a(nx,:)=[]; if numel(a)==1 av=a; elseif isempty(a) break else av=randsample(a,1); end if ~isempty(a) idxx(end+1)=idx_u(jj); %V'=V'+{v}
U(idx_u==idx_u(jj))=[]; %U=U-{v}
Ea_prime=vertcat(Ea_prime,[idx_u(jj) av]); end end if loopCounter >100 break; endloopCounter = loopCounter + 1;endfos=(numel(idxx)/Nodes); %Resiliency Metric
end
Script:
for i=1:100 G= WattsStrogatz(400,2,0.25); k=1:300; fos=zeros(1,length(k)); for jj=1:length(k) fos(jj)=Test_Healing(400,jj); end rs(i,:)=fos;endrs_avg=mean(rs,1);figure;plot(k,rs_avg)
Best Answer