├── Bayesian modelling and updating algorithms ├── Bayesian_modelling .m ├── DEMCMC_algorithm.m ├── EMCMC_algorithm.m ├── MH_algorithm.m ├── PopMCMC_algorithm.m └── error ellipse.txt /Bayesian modelling and updating algorithms: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Bayesian_modelling .m: -------------------------------------------------------------------------------- 1 | %% Information: 2 | % Paper Source: Evolutionary Markov Chain Monte Carlo Algorithm for Bayesian 3 | % model Updating. 4 | % Code: Bayesian and updating parameters modelling used for updating algorithm 5 | % Updating five parameters of the FE model. 6 | %-------------------------------------------------------------------------- 7 | %% Bayesian and updating parameters modelling 8 | %-------------------------------------------------------------------------- 9 | % Posterior function: 10 | %Wm: measured natural frequencies. 11 | %Wc: calculated natural frequencies. 12 | %sup: sampled updating parameter value. 13 | %mup: mean updating parameter value. 14 | %var: varince vector. 15 | %Cov: covaiance matrxi. 16 | Posterior = @(Wm,Wc,Cov,var,mu,sup) (Wc - Wm)*(inv(var*var))*(Wc - Wm)'... 17 | +(sup-mup)*(inv(Cov*Cov))*(sup-mup)'; 18 | %-------------------------------------------------------------------------- 19 | % Prior function: 20 | %x_par: updating parameters. 21 | %sig: step control for single-chain method. 22 | %d: dimensional vector of the updating parameters. 23 | %N = ; number of chains for multichain algorithms. 24 | Pior = @(x_par,mu,sig,d,N) (1/(2*pi)^(d/2))*(1/det(sig*sig)^0.5)... 25 | *exp(-0.5*(x_par-mu)*(inv(sig*sig))*(x_par-mu)'); 26 | %-------------------------------------------------------------------------- 27 | %The initial 28 | %nsamples = ; %number of required samples. 29 | %uE_m = ; %updating element mean value. 30 | %uE_c = ; %updating element calculated value. 31 | Par1 = uE_c; %updating parameter number 1. 32 | Par2 = uE_c; %updating parameter number 2. 33 | Par3 = uE_c; %updating parameter number 3. 34 | Par4 = uE_c; %updating parameter number 4. 35 | Par5 = uE_c; %updating parameter number 5. 36 | x_par = [Par1, Par2, Par3, Par4, Par5]; 37 | d = length(x_par); 38 | %Wm = [] ; 39 | %-------------------------------------------------------------------------- 40 | %Call FEA simulator to calculate the natural frequencies 41 | Wc = FEA_simulator(Par1, Par2, Par3, Par4, Par5)'; 42 | W_init = Wc; %Initial calculated natural frequencies. 43 | %-------------------------------------------------------------------------- 44 | %% Call the updating algorithm 45 | Errorcv = []; %Matrix of total evaluation error. 46 | samples = zeros(nsamples, d); %Matrix of returned samples. 47 | % xmax = []; %Maximum bound vector of d_parameters 48 | % xmin = []; %Minimum bound vector of d_parameters 49 | blength = xmax-xmin; %Bound lenght 50 | %-------------------------------------------------------------------------- 51 | %% Results and Figures 52 | Par_f = (1/nsamples)*sum(samples,1); %mean values of the updated parameters 53 | Wc_f = FEA_simulator(Par_f(1),Par_f(2), Par_f(3),Par_f(4),Par_f(5))'; %Final Wc. 54 | StandardD = std(samples,0,1); %The Standard Deviation of the samples 55 | corr = cov(samples)./(StandardD'*StandardD);%The correlation between the updated parameters 56 | figue(); plot(Errorcv); %Plot error curve 57 | figure(); bar3(corr); %Plot the corr. 58 | figure(); boxplot(samples); %box-plot of the generated samples 59 | %-------------------------------------------------------------------------- 60 | %-------------------------------------------------------------------------- 61 | 62 | 63 | -------------------------------------------------------------------------------- /DEMCMC_algorithm.m: -------------------------------------------------------------------------------- 1 | %% Information: 2 | % Paper Source: Evolutionary Markov Chain Monte Carlo Algorithm for Bayesian 3 | % model Updating. 4 | % Code: The Differential Evolution Markov Chain Monte Carlo algorithm. 5 | % Updating five parameters of the FE model. 6 | %-------------------------------------------------------------------------- 7 | %% The Differential Evolution Markov Chain Monte Carlo algorithm. 8 | %-------------------------------------------------------------------------- 9 | nw = length(Wc); %Number of updated natural frequencies 10 | gamma_RWM = 2.38/sqrt(2*d); %dEFULT jump rate 11 | %N = ; %Number of chains 12 | Samples = nan(nsamples,d,N); %Generated samples matrix. 13 | Samples(1,1:d,1:N) = reshape(sample',1,d,N); 14 | 15 | %Initial samples 16 | for i=1:d 17 | sample(:,i)= prior(N,xmin(i),xmax(i)); 18 | end 19 | 20 | %Initial calculated natural frequencies 21 | for j=1:N 22 | Wc = FEA_simulator(sample(j,1), sample(j,2), sample(j,3), sample(j,4), sample(j,5))'; 23 | I_p(j,1) = Posterior(sample(j,:),Wm,Wc,Cov,var,mu); % initial population 24 | wc_x(j,1:nw)= Wc; 25 | end 26 | 27 | %the index of chains for DE 28 | for i = 1:N 29 | R(i,1:N-1) = setdiff(1:N,i); 30 | end 31 | 32 | % Dynamic part 33 | for t = 2:nsamples 34 | [~,draw] = sort(rand(N-1,N)); 35 | g = randsample([gamma_RWM 1],1,true,[0.9 0.1]); 36 | for i = 1:N 37 | r1 = R(i,draw(1,i)); 38 | r2 = R(i,draw(2,i)); 39 | sample_P(i,1:d) = sample(i,1:d) + g*(sample(r1,1:d)-sample(r2,1:d))... 40 | + (1e-6)*randn(1,d); 41 | 42 | for j = 1:d 43 | overlimit_x(1,j)=sample_P(i,j)< xmax(1,j); 44 | underlimit_x(1,j)=sample_P(i,j)> xmin(1,j); 45 | end 46 | 47 | sample_P(i,1:d)=sample_P(i,1:d).*overlimit_x(1,:)+Max(1,:).*not(overlimit_x(1,:)); 48 | sample_P(i,1:d)=sample_P(i,1:d).*underlimit_x(1,:) + Min(1,:).*not(underlimit_x(1,:)); 49 | 50 | 51 | Wc = FEA_simulator(sample_P(i,1), sample_P(i,2), sample_P(i,3), sample_P(i,4), sample_P(i,5))'; 52 | p_px(i,1) = Posterior(sample_P(i,:),Wm,Wc,Cov,var,mu); 53 | wc_px(i,1:nw)=Wc; 54 | 55 | alpha = min(p_px(i,1)/p_p(i,1),1); 56 | 57 | idx = alpha > rand; 58 | if idx, 59 | sample(i,1:d) = sample_P(i,1:d); 60 | p_p(i,1) = p_px(i,1); 61 | wc_x(i,1:nw)=wc_px(i,1:nw); 62 | end 63 | end 64 | 65 | Samples(t,1:d,1:N) = reshape(sample',1,d,N); 66 | Wc_x(t,1:nw,1:N) = reshape(wc_x',1,nw,N); 67 | P_x(t,1:N) = p_p'; 68 | 69 | samples_1 = Samples(:,:,1); 70 | samples_1(isnan(samples_1)) = 0.0; 71 | 72 | Ehcv = (1/t)*sum(samples_1,1); 73 | Wccv = FEA_simulator(Ehcv(1),Ehcv(2), Ehcv(3), Ehcv(4), Ehcv(5))'; 74 | 75 | Percentagecv = zeros(1,nw); 76 | 77 | for j = 1:nw 78 | Percentagecv(1,j) = 100*(abs(Wccv(j) - Wm(j))/Wm(j)); 79 | end 80 | 81 | Errorcv = [Errorcv, (sum(Percentagecv))/nw]; 82 | 83 | end -------------------------------------------------------------------------------- /EMCMC_algorithm.m: -------------------------------------------------------------------------------- 1 | %% Information: 2 | % Paper Source: Evolutionary Markov Chain Monte Carlo Algorithm for Bayesian 3 | % model Updating. 4 | % Code: The Evolutionry Markov Chain Monte Carlo algorithm. 5 | % Updating five parameters of the FE model. 6 | %-------------------------------------------------------------------------- 7 | %% The Evolutionry Markov Chain Monte Carlo algorithm. 8 | nw = length(Wc); % Number of updated natural frequencies 9 | %MaxSubIt = ; % Maximum Number of Sub-iterations 10 | %T0= ; % Initial Temp. 11 | %alpha= ; % Parallel temp. reduction rate 12 | %nPop = ; % Population Size 13 | %nMove = ; % Number of Moves 14 | 15 | % Create Empty Structure for Individuals 16 | empty_individual.Position=[]; 17 | empty_individual.Cost=[]; 18 | 19 | % Create Population Array 20 | pop=repmat(empty_individual,nPop,1); 21 | 22 | % Initialize Best Solution 23 | BestSol.Cost=inf; 24 | 25 | 26 | % Initialize Population 27 | for i=1:nPop 28 | 29 | % Initialize Position 30 | mx=xmax(1); %Max bound 31 | mn=xmin(1); %Max bound 32 | 33 | Max = [mx,mx,mx,mx,mx]; 34 | Min = [mn,mn,mn,mn,mn]; 35 | 36 | n=model.n; 37 | 38 | %Initial random positions 39 | sol=Min+rand(1,n).*(Max-Min); 40 | pop(i).Position=sol; 41 | 42 | % Evaluation 43 | Wc= FEA_simulator(pop(i).Position(:,1), pop(i).Position(:,2), pop(i).Position(:,3),pop(i).Position(:,4), pop(i).Position(:,5))'; 44 | pop(i).Cost=Posterior(pop(i).Position,Wm,Wc,Cov,var,mu); 45 | 46 | 47 | % Update Best Solution 48 | if pop(i).Cost<=BestSol.Cost 49 | BestSol=pop(i); 50 | end 51 | 52 | end 53 | 54 | % Array to Hold Best Cost Values 55 | BestCost=zeros(nsamples,1); 56 | 57 | % Intialize Temp. 58 | T=T0; 59 | 60 | %Dynamic Part 61 | for it=1:nsamples 62 | 63 | for subit=1:MaxSubIt 64 | 65 | % Create and Evaluate New Solutions 66 | newpop=repmat(empty_individual,nPop,nMove); 67 | for i=1:nPop 68 | for j=1:nMove 69 | 70 | % Create Neighbor 71 | pSwap=0.2; %Defult set 72 | pReversion=0.5; %Defult set 73 | pInsertion=1-pSwap-pReversion; 74 | 75 | p=[pSwap pReversion pInsertion]; 76 | 77 | %selection 78 | r=rand; 79 | c=cumsum(p); 80 | i=find(r<=c,1,'first'); 81 | 82 | switch 83 | case 1 84 | % Swap 85 | n=numel(Position1); 86 | I=randsample(n,2); 87 | i1=I(1); 88 | i2=I(2); 89 | Position2([i1 i2])=Position1([i2 i1]); 90 | 91 | case 2 92 | % Reversion 93 | n=numel(Position1); 94 | I=randsample(n,2); 95 | i1=min(I); 96 | i2=max(I); 97 | Position2=Position1; 98 | Position2(i1:i2)=Position1(i2:-1:i1); 99 | 100 | case 3 101 | % Insertion 102 | n=numel(Psition1); 103 | I=randsample(n,2); 104 | i1=I(1); 105 | i2=I(2); 106 | if i1=xmin(1,d); 118 | end 119 | 120 | RV_interval = rand(1,nparams).*blength + Min; 121 | newpop(i,j).Position=newpop(i,j).Position(1,:).*overlimit_x(1,:)+RV_interval.*not(overlimit_x(1,:)); 122 | newpop(i,j).Position=newpop(i,j).Position(1,:).*underlimit_x(1,:) + RV_interval.*not(underlimit_x(1,:)); 123 | 124 | % Evaluation 125 | Wc= FEA_simulator(newpop(i,j).Position(:,1), newpop(i,j).Position(:,2), newpop(i,j).Position(:,3),newpop(i,j).Position(:,4), newpop(i,j).Position(:,5))'; 126 | newpop(i,j).Cost=Posterior(newpop(i,j).Position,Wm,Wc,Cov,Cov1,mu1); 127 | 128 | end 129 | end 130 | newpop=newpop(:); 131 | 132 | % Sort Neighbors 133 | [~, SortOrder]=sort([newpop.Cost]); 134 | newpop=newpop(SortOrder); 135 | 136 | for i=1:nPop 137 | 138 | if newpop(i).Cost<=pop(i).Cost 139 | pop(i)=newpop(i); 140 | 141 | else 142 | DELTA=(newpop(i).Cost-pop(i).Cost)/pop(i).Cost; 143 | P=exp(-DELTA/T); 144 | if rand<=P 145 | pop(i)=newpop(i); 146 | 147 | end 148 | end 149 | 150 | % Update Best Solution Ever Found 151 | if pop(i).Cost<=BestSol.Cost 152 | BestSol=pop(i); 153 | 154 | else 155 | %samples(end+1,:)= BestSol.Position+rand(1,nparams); 156 | end 157 | 158 | end 159 | 160 | end 161 | 162 | 163 | % Store Best Cost Ever Found 164 | BestCost(it)=BestSol.Cost; 165 | samples(end+1,:)=BestSol.Position; 166 | 167 | 168 | % Display Iteration Information 169 | disp(['Iteration ' num2str(it) ': Best Cost = ' num2str(BestCost(it))]); 170 | 171 | % Update Temp. 172 | T=alpha*T; 173 | 174 | 175 | 176 | Ehcv = (1/it)*sum(samples,1); 177 | 178 | Wccv = FEA_simulator(Ehcv(1),Ehcv(2), Ehcv(3), Ehcv(4), Ehcv(5))'; 179 | 180 | Percentagecv = zeros(1,nw); 181 | 182 | for p = 1:nw 183 | Percentagecv(1,p) = 100*(abs(Wccv(p) - Wm(p))/Wm(p)); 184 | end 185 | 186 | Errorcv = [Errorcv, (sum(Percentagecv))/nw] 187 | 188 | 189 | end -------------------------------------------------------------------------------- /MH_algorithm.m: -------------------------------------------------------------------------------- 1 | %% Information: 2 | % Paper Source: Evolutionary Markov Chain Monte Carlo Algorithm for Bayesian 3 | % model Updating. 4 | % Code: The Metropolis-Hastings algorithm. 5 | % Updating five parameters of the FE model. 6 | %-------------------------------------------------------------------------- 7 | %% The Metropolis-Hastings algorithm. 8 | %-------------------------------------------------------------------------- 9 | logpOld = Posterior(x_par,Wm,Wc,Cov,var,mu); %Posterior of Initial Paramters 10 | covsamples = []; %Generated samples 11 | %The dynamic part 12 | for t=1:nsamples 13 | 14 | xprime = x_par + randn(1,d)*sig; 15 | 16 | for i = 1:d 17 | overlimit_x(1,i)=xprime(1,i)<=xmax(1,i); 18 | underlimit_x(1,i)=xprime(1,i)>=xmin(1,i); 19 | end 20 | 21 | RV_interval = rand(1,d).*blength + xmin; 22 | xprime=xprime(1,:).*overlimit_x(1,:)+xmax(1,:).*not(overlimit_x(1,:)); 23 | xprime=xprime(1,:).*underlimit_x(1,:) + xmin(1,:).*not(underlimit_x(1,:)); 24 | Wc = FEA_simulator(xprime(1,1), xprime(1,2),xprime(1,3), xprime(1,4), xprime(1,5))'; 25 | logpNew = Posterior(xprime,Wm,Wc,Cov,var,mu); 26 | alpha = exp(logpOld-logpNew); 27 | qnumer = proposalProb(x_par,xprime,sig,d); %proposalProb; % q(x|x') 28 | qdenom = proposalProb(xprime,x_par,sig,d); %proposalProb; % q(x'|x) 29 | alpha = alpha * (qnumer/qdenom); 30 | r = min(1, alpha); 31 | u = rand(1,1); 32 | if u < r 33 | x_par = xprime; 34 | naccept = naccept + 1; 35 | logpOld = logpNew; 36 | Prob1 = r; 37 | end 38 | Prob = [Prob; Prob1]; 39 | samples(t,:) = x_par; 40 | Wc = FEA_simulator(x_par(1,1), x_par(1,2),x(1,3), x_par(1,4),x_par(1,5))'; 41 | WWc(t,:) = Wc; 42 | covsamples = [covsamples; x]; 43 | Ehcv = (1/t)*sum(covsamples,1); %Current updated parameter values. 44 | Wccv = FEA_simulator(Ehcv(1),Ehcv(2), Ehcv(3),Ehcv(4), Ehcv(5))'; %Wc current value. 45 | Percentagecv = zeros(1,length(Wc)); %Percentage error. 46 | for j = 1:nparams 47 | Percentagecv(1,j) = 100*(abs(Wccv(j) - Wm(j))/Wm(j)); 48 | end 49 | Errorcv = [Errorcv, (sum(Percentagecv))/length(Wc)] 50 | end 51 | -------------------------------------------------------------------------------- /PopMCMC_algorithm.m: -------------------------------------------------------------------------------- 1 | %% Information: 2 | % Paper Source: Evolutionary Markov Chain Monte Carlo Algorithm for Bayesian 3 | % model Updating. 4 | % Code: The Population Markov Chain Monte Carlo algorithm. 5 | % Updating five parameters of the FE model. 6 | %-------------------------------------------------------------------------- 7 | %% The Population Markov Chain Monte Carlo algorithm. 8 | %-------------------------------------------------------------------------- 9 | nw = length(Wc); %Number of updated natural frequencies 10 | %N = ; %Number of chains 11 | Samples = nan(nsamples,d,N); %Generated samples matrix. 12 | P_x = nan(T,N); %Matrix of chains and density 13 | n_cr =3; %Defult initial value of crossover 14 | delta =3; %Defult parameters 15 | c=0.1; %Defult parameters 16 | c_ctar=1e-12; %Defult parameters 17 | p_g=0.2; %Defult parameters 18 | 19 | [J,n_id]=deal(zeros(1,n_CR)); 20 | CR=[1:n_CR]/n_CR; 21 | pCR=ones(1,n_CR)/n_CR; %Crossover values and select prob. 22 | 23 | %Initial samples 24 | for i=1:d 25 | sample(:,i)= prior(N,xmin(i),xmax(i)); 26 | end 27 | 28 | for j=1:N 29 | Wc = FixedBeam_femesh(sample(j,1), sample(j,2), sample(j,3), sample(j,4), sample(j,5))'; 30 | p_p(j,1) = Posterior(sample(j,:),Wm,Wc,Cov,Cov1,mu1); % Create initial population and compute density 31 | wc_x(j,1:nw)= Wc; 32 | end 33 | 34 | Samples(1,1:d,1:N) = reshape(sample',1,d,N); 35 | Wc_x(1,1:nw,1:N) = reshape(wc_x',1,nw,N); 36 | P_x(1,1:N) = p_p'; % Store initial position of chain and density 37 | 38 | %index of ith chain 39 | for i = 1:N 40 | 41 | R(i,1:N-1) = setdiff(1:N,i); 42 | 43 | end 44 | 45 | % Dynamic part 46 | for t = 2:nsamples 47 | 48 | [~,draw] = sort(rand(N-1,N)); % Randomly permute [1,...,N?1] N times 49 | dsample = zeros(N,d); %set N Jump vector to zero 50 | lambda = unifrnd(-c,c,N,1); %Draw N lambda values 51 | 52 | 53 | std_sample=std(sample); %calculate the std for each dimension 54 | 55 | 56 | for i = 1:N 57 | 58 | D=randsample([1:delta],1,'true'); 59 | 60 | r1 = R(i,draw(1:D,i)); % Derive r1 61 | 62 | r2 = R(i,draw(D+1:2*D,i)); % Derive r2; r1 not equal r2 not equal i 63 | 64 | 65 | id= randsample(1:n_CR,1,'true',pCR); %select index of crossover value 66 | 67 | 68 | z=rand(1,d); %Draw d values from U[0,1] 69 | 70 | A = find(z xmin(1,j); 93 | end 94 | 95 | sample_P(i,1:d)=sample_P(i,1:d).*overlimit_x(1,:)+Max(1,:).*not(overlimit_x(1,:)); 96 | sample_P(i,1:d)=sample_P(i,1:d).*underlimit_x(1,:) + Min(1,:).*not(underlimit_x(1,:)); 97 | 98 | 99 | Wc = FixedBeam_femesh(sample_P(i,1), sample_P(i,2), sample_P(i,3), sample_P(i,4), sample_P(i,5))'; 100 | p_px(i,1) = Posterior(sample_P(i,:),Wm,Wc,Cov,var,mu); 101 | wc_px(i,1:nw)=Wc; 102 | 103 | alpha = min(p_px(i,1)/p_p(i,1),1); % Compute Metropolis ratio 104 | 105 | idx = alpha > rand; % Alpha larger than U[0,1] or not? 106 | if idx, 107 | sample(i,1:d) = sample_P(i,1:d); 108 | p_p(i,1) = p_px(i,1); % True: Accept proposal 109 | wc_x(i,1:nw)=wc_px(i,1:nw); 110 | else 111 | dsample(i,1:d)=0; 112 | end 113 | J(id) = J(id)+sum((dsample(i,1:d)./std_sample).^2);%Update Jump Distance Crossover 114 | n_id(id)=n_id(id)+1; %Corossover counter 115 | 116 | end 117 | 118 | Samples(t,1:d,1:N) = reshape(sample',1,d,N); 119 | Wc_x(t,1:nw,1:N) = reshape(wc_x',1,nw,N); 120 | P_x(t,1:N) = p_p'; % Add current position and density to chain 121 | 122 | if t