Appendix
for i=98.5:1.5:142.5
p_mat=get(handles.figure1,'position');p_mat(3)=i;
set(handles.figure1,'position',p_mat);
pause(0.001)
end
set(handles.uipanel2,'visible','on')
set(handles.uipanel3,'visible','on')
set(handles.push_stop,'visible','on')
L_des_var=[.0001 .0001 .0001 .0001 -10 -10 -5 -5 0.0000001 0.0000001 ]; %input('Lower Bound Design Variable:' );
H_des_var=[7 7 7 7 7 7 5 5 2*pi 2*pi]; %input('Higher Bound Design Variable:' );
obj_fun_no=2;
des_var_no=10;set(handles.text_des_var_no_val,'String',des_var_no)
pop_size=str2double(get(handles.edit_pop_size,'String'));set(handles.text_pop_size_val,'String',pop_size)
generation_no=str2double(get(handles.edit_gen,'String'));
crs_prob=str2double(get(handles.edit_cp,'String'));;set(handles.text_cp_val,'String',crs_prob)
mut_prob=str2double(get(handles.edit_mp,'String'));;set(handles.text_mp_val,'String',mut_prob)
elim_crit=str2double(get(handles.edit_el,'String'));; set(handles.text_el_val,'String',elim_crit)
first_seed=str2double(get(handles.edit_fs,'String'));
set(handles.text_fs_val,'String',first_seed)
pause(0.2)
m_obj_fun(1)=0;m_obj_fun(2)=0;
tic
Parents=gofp(first_seed,des_var_no,pop_size,L_des_var,H_des_var);
init_pop=Parents;
pop=init_pop;
seed=ran2(first_seed
*2);
for generation=1:generation_no
set(handles.text_gen_val,'String',generation)
seed=ran2(-1e6*seed*2);
% crossover operator
para1=evaluation1(pop,obj_fun_no);
if mod(generation,50)==0
[h]=sel_ran(para1,des_var_no,obj_fun_no,m_obj_fun,seed);
[hybed_indv]=optim(h,des_var_no,obj_fun_no);
pop=[pop;hybed_indv];
para1=evaluation1(pop,obj_fun_no);
end
front1=fnds(para1,des_var_no,obj_fun_no,m_obj_fun);
distance=select_factor(para1,obj_fun_no,des_var_no);
pop_cross=crossover(pop,para1,des_var_no,crs_prob,seed,front1,distance);
% Mutation operator
pop_mut=mutation(pop,mut_prob,L_des_var,H_des_var,seed);
% Total population ( main + crossover + mutation)
pop_total=[pop;pop_cross;pop_mut];
para=evaluation1(pop_total,obj_fun_no);
% Select new population from parents and offsprings
[pop,para_final,fronts_final]=evolution(pop_total,para,pop_size,des_var_no,...
obj_fun_no,m_obj_fun,elim_crit,seed,L_des_var,H_des_var,crs_prob,mut_prob);
result(fronts_final,para_final,obj_fun_no,des_var_no);
err_min=min(para(:,11));
tr_min=min(para(:,12));
set(handles.text_err_val,'String',err_min)
set(handles.text_tr_val,'String',tr_min)
pause(0.1)
end
num=rand;
order=fix(rand*4+1);
rn=-1*fix(num*10^order);
while rn==0
num=rand;
order=fix(rand*4+1);
rn=-1*fix(num*10^order);
end
set(handles.edit_fs,'string',rn)
function [result]=boundry(X)
c=0;
for i=1:4
if X(i)>8 | X(i)<0
c=1;
result=c;
return
end
end
for i=5:8
if X(i)>8 | X(i)<-8
c=1;
result=c;
return
end
end
for i=9:10
if X(i)>2*pi | X(i)<0
c=1;
result=c;
return
end
end
result=c;
function [err,tr] = cause(individual)
CXD=[2.203802 4.398573 5.268585 4.999546 4.444195 4.02863 3.958154 4.10944 4.03908 3.461855 2.474852 1.61572 ];
CYD=[4.852923 4.038518 2.369554 1.067374 0.121084 -.520956 -.645528 -.005285 1.230817 2.607709 3.727444 4.443476 ];
save points.mat CXD CYD
point_num=size(CXD,2);
r1=individual(1);r2=individual(2);r3=individual(3);
r4=individual(4);rcx=individual(5);rcy=individual(6);
x0=individual(7);y0=individual(8);teta0=individual(9);
teta2(1)=individual(10);
for I=2:point_num
teta2(I)=teta2(1)+(30*pi/180)*(I-1);
end
for I=1:point_num
coupler=mech_builder(r1,r2,r3,r4,rcx,rcy,x0,y0,teta0,teta2(I));
CX(I)=coupler(1);
CY(I)=coupler(2);
end
H1=grashofcon(individual);
H2=boundry(individual);
length=individual(1:4);
length=sort(length);
ratio=length(4)/length(1);
if ratio > 25
H4=1;
else
H4=0;
end
sumation=0;
for I=1:point_num
sumation=sumation+(CXD(I)-CX(I))^2+(CYD(I)-CY(I))^2;
end
res=[CX;CY];
err=sumation+1e08*H1+1e8*H2+1e08*H4;
tr=tran(individual)+1e08*H1+1e8*H2+1e08*H4;
if nargout==0
disp(' ')
msgbox(sprintf(' The error function is : %g and The transmition angle error is : %g ',err,tr),'Results')
clear
end
function [C]=mech_builder(r1,r2,r3,r4,rcx,rcy,Xo,Yo,teta0,teta2)
a=r2; b=r3; c=r4; d=r1;
r=sqrt(rcx^2+rcy^2);
f=atan(rcy/rcx);
s=1;
% The following lines of code determines the starting configuration of the mechanism
% This is based on the standard cosine formulars and other triangle properties used for analyzing four-bar linkages
theta1=teta2;
x2=a*cos(theta1); % x cooridnate of the crank moving point: Point 2
y2=a*sin(theta1); % y cooridnate of the crank moving point: Point 2
e = sqrt((x2-d)^2+y2^2);
phi2=acos((e^2+c^2-b^2)/(2*e*c));
phi1=atan(y2/(x2-d))+(1-sign(x2-d))*pi/2;
theta3=phi1-s*phi2;
x3=c*cos(theta3)+d; % x cooridnate of the follower moving point: Point 3
y3=c*sin(theta3); % y cooridnate of the crank moving point: Point 3
theta2=atan((y3-y2)/(x3-x2))+(1-sign(x3-x2))*pi/2; %Bug in the code: There is an problem with this step if x3-x2 is zero, but that rarely happens
phi3=theta2+f;
gamma=(theta3-theta2);
x5=x2+r*cos(phi3); % x cooridnate of the coupler point: Point 5
y5=y2+r*sin(phi3); % y cooridnate of the coupler point: Point 5
Pn=rotate(x5,y5,Xo,Yo,teta0); x5n=Pn(1); y5n=Pn(2);
C=[x5n y5n gamma];
function [ output ] = rotate( xold, yold ,x0 ,y0 ,teta0 )
output(1)=xold*cos(teta0)-yold*sin(teta0)+x0;
output(2)=xold*sin(teta0)+yold*cos(teta0)+y0;
function er=tran(mech)
r1=mech(1);r2=mech(2);r3=mech(3);r4=mech(4);
theta2=0:0.1:2*pi;
for i=1:63
C=mech_builder(r1,r2,r3,r4,mech(5),mech(6),0,0,0,theta2(i));
gamma(i)=C(3);
end
%plot(theta2,gamma)
maxg=max(gamma)*180/pi;
ming=min(gamma)*180/pi;
er=(90-maxg)^2+(90-ming)^2;
function z=crossover(popmain,para,des_var_no,crs_prob,seed,front1,distance,L_des_var,H_des_var,L_c,n_inp2)
%Dynamic
F=0.4;
pop=popmain;
n=size(pop);
k=0;
[temp1,temp_ind1]=sort(para(:,des_var_no+1));
ind_best=temp_ind1(1);
Xbest=pop(ind_best,:);
for i=1:n(1)
seed=ran2(-1e6*seed);
r(i)=seed;
end
k=0;
for i=1:n(1)
if r(i)<crs_prob
k=k+1;
end
end
for i=1:2:k
jimjim=9;
while jimjim~=10
for sele=1:3
seed=ran2(-1e6*seed);
pop_crs1_no=ceil(size(pop,1)*seed);
seed=ran2(-1e6*seed);
pop_crs2_no=ceil(size(pop,1)*seed);
seed=ran2(-1e6*seed);
pop_crs3_no=ceil(size(pop,1)*seed);
[front_no1,each1]=find(front1==pop_crs1_no);
[front_no2,each2]=find(front1==pop_crs2_no);
[front_no3,each3]=find(front1==pop_crs3_no);
if front_no1==front_no2
if distance(pop_crs1_no)>distance(pop_crs2_no)
pop_main(sele)=pop_crs1_no;
else
pop_main(sele)=pop_crs2_no;
end
end
if front_no1<front_no2
pop_main(sele)=pop_crs1_no;
else
pop_main(sele)=pop_crs2_no;
end
end
if pop_main(1)~=pop_main(2)
pop_main(3)=pop_crs3_no;
jimjim=10;
end
end
seed=ran2(-1e6*seed);
Xr1=pop(pop_main(1),:);
Xr2=pop(pop_main(2),:);
V=Xbest+F*(Xr1-Xr2);
X_crs=pop(pop_main(3),:);
pop_off(1,:)=V;
pop_off(2,:)=X_crs;
pop_off_cut=pop_off;
seed=ran2(-1e6*seed);
cut_point=ceil((size(pop,2)-1)*seed);
pop_off_cut(1,cut_point+1:size(pop,2))=pop_off(2,cut_point+1:size(pop,2));
pop_off_cut(2,cut_point+1:size(pop,2))=pop_off(1,cut_point+1:size(pop,2));
pop_cross(i,:)=pop_off_cut(1,:);
pop_cross(i+1,:)=pop_off_cut(2,:);
end
pop_cross=grashof(pop_cross);
z=pop_cross;
function [avr]=CTA(parent)
s=size(parent); NP=s(1,1);
sumation=0;
for i=1:NP
indivi=parent(i,:);
err(i) = cause(indivi);
sumation=sumation+real(err(i));
end
avr=sumation/NP;
function para=evaluation1(pop,obj_fun_no);
ind_no=size(pop,1);
x=pop;
n=size(pop,1);
for i=1:n
indivi=x(i,:);
[err(i),tr(i)] = cause(indivi);
end
obj_fun(1,:)=err;
obj_fun(2,:)=tr;
%obj_fun(3,:)=mech_ad;
para=[x obj_fun'];
function [new_pop,para_final,fronts_final]=evolution(pop_total,para,pop_size,...
des_var_no,obj_fun_no,m_obj_fun,elim_crit,seed,L_des_var,H_des_var,crs_prob,mut_prob);
para_final=para;
pop_final=pop_total;
fronts_final=fnds(para_final,des_var_no,obj_fun_no,m_obj_fun);
n=size(fronts_final);
k=1;
for i=1:n(1)
for j=1:n(2)
if fronts_final(i,j)~=0
index(k)=fronts_final(i,j);
k=k+1;
end
end
end
if pop_size<=size(index,2)
[II,JJ]=sort(para_final(:,11));
index(pop_size)=JJ(1);
new_pop=pop_final(index(1:pop_size),:);
else
new_pop=pop_final(index,:);
end
function out_fndf=fndf(pop)
pop_size=size(pop);
bank(1:pop_size(1))=1;
for i=2:pop_size(1)
for j=1:i-1
if bank(j)==1 & bank(i)==1
if pop(i,:)>=pop(j,:)
bank(i)=1;
bank(j)=0;
elseif pop(i,:)<=pop(j,:)
bank(i)=0;
bank(j)=1;
else
bank(i)=1;
bank(j)=1;
end
end
end
end
j=1;
for i=1:pop_size(1)
if bank(i)==1
front(j)=i;
j=j+1;
end
end
out_fndf=front;
function fnds=fnds(para,des_var_no,obj_fun_no,m_obj_fun)
func=para(:,des_var_no+1:des_var_no+obj_fun_no);
[mfunc,nfunc]=size(func);
for i=1:obj_fun_no
if m_obj_fun(i)==0
func(:,i)=1./func(:,i);
end
end
i=1;
sumsize=0;
while sumsize~=mfunc
front=fndf(func);
for k=1:size(front,2)
sorted_front(i,k)=front(k);
end
for j=1:size(front,2)
func(front(j),:)=-inf;
end
i=i+1;
[msf,nsf]=size(sorted_front);
sumsize=sumsize+size(front,2);
end
fnds=sorted_front;
function [population]=gofp(first_seed,des_var_no,pop_size,L_des_var,H_des_var)
X=my_ran2(pop_size,des_var_no,first_seed);
for i=1:pop_size
for j=1:des_var_no
X(i,j)=X(i,j)*H_des_var(j)+L_des_var(j)*(1-X(i,j));
end
end
X=grashof(X);
population=X;
function [result]=grashof(parent)
X=parent;
c=size(X);NP=c(1,1);
for I=1:NP;
x=[X(I,1) X(I,2) X(I,3) X(I,4)];
x=sort(x);
X(I,1)=x(4);
X(I,2)=x(1);
X(I,3)=x(2);
X(I,4)=x(3);
ls=x(4)+x(1);
pq=x(2)+x(3);
if ls>=pq
while ls>=pq
X(I,3)=X(I,3)*1.1;
ls=X(I,1)+X(I,2);
pq=X(I,4)+X(I,3);
end
end
end
result=X;
function [result]=grashofcon(X)
x=[X(1) X(2) X(3) X(4)];
x=sort(x);
ls=x(4)+x(1);
pq=x(2)+x(3);
if (ls<pq)
c=0;
else
c=1;
end
result=c;
function [para,pop_total]=modify_pop(pop_total,para,elim_crit)
for i=1:size(para,1)-1
for j=i+1:size(para,1)
if para(i,:)~=0 & ...
para(i,:)>=((1-elim_crit)*para(j,:)) & para(i,:)<=((1+elim_crit)*para(j,:))
para(j,:)=0;
pop_total(j,:)=0;
end
end
end
% Eliminating zeros
j=0;
for i=1:size(para,1)
if min(para(i,:))>0
j=j+1;
temp_para(j,:)=para(i,:);
temp_pop_total(j,:)=pop_total(i,:);
end
end
para=temp_para;
pop_total=temp_pop_total;
function z=mutation(pop,mut_prob,L_des_var,H_des_var,seed)
[n,m]=size(pop);
for i=1:n
seed=ran2(-1e6*seed);
r(i)=seed;
end
%k is the population no. include in mutation
k=0;
for i=1:n
if r(i)<=mut_prob
k=k+1;
mut_no(k)=i;
end
end
if k==0
seed=ran2(-1e6*seed);
mut_no(1)=ceil(n*seed);
k=1;
end
for i=1:k
seed=ran2(-1e6*seed);
place_mut=ceil(m*seed);
mut_pop(i,:)=pop(mut_no(i),:);
seed=ran2(-1e6*seed);
mut_pop(i,place_mut)=seed*H_des_var(place_mut)+L_des_var(place_mut)*(1-seed);
end
mut_pop=grashof(mut_pop);
z=mut_pop;
function [ out ] = my_ran2(r,c,first_seed)
%UNTITLED1 Summary of this function goes here
% Detailed explanation goes here
seed=ran2(first_seed);
for i=1:r
for j=1:c
seed=ran2(-seed*1e6);
out(i,j)=seed;
end
end
function ran2=ran2(idum)
IM1=2147483563;IM2=2147483399;AM=1./IM1;IMM1=IM1-1;
IA1=40014;IA2=40692;IQ1=53668;IQ2=52774;IR1=12211;IR2=3791;
NTAB=32;NDIV=1+fix(IMM1/NTAB);EPS=1.2e-7;RNMX=1.-EPS;
idum2=123456789; iv=NTAB*0; iy=0;
if idum<=0
idum=max(-idum,1);
idum2=idum;
for j=NTAB+8:-1:1
k=fix(idum/IQ1);
idum=IA1*(idum-k*IQ1)-k*IR1;
if idum<0;idum=idum+IM1;end
if j<=NTAB;iv(j)=idum;end
end
iy=iv(1);
end
k=fix(idum/IQ1);
idum=IA1*(idum-k*IQ1)-k*IR1;
if idum<0;idum=idum+IM1;end
k=fix(idum2/IQ2);
idum2=IA2*(idum2-k*IQ2)-k*IR2;
if idum2<0;idum2=idum2+IM2;end
j=1+fix(iy/NDIV);
iy=iv(j)-idum2;
iv(j)=idum;
if iy<1;iy=iy+IMM1;end
ran2=min(AM*iy,RNMX);
function result(fronts,para,obj_fun_no,des_var_no)
%global fronts func para bound
for i=1:size(fronts,2)
if fronts(1,i)~=0
front(i)=fronts(1,i);
else
break
end
end
if obj_fun_no==2
hold on;
cla
para(front,des_var_no+1:des_var_no+2);
plot(para(front,des_var_no+1),para(front,des_var_no+2),'.')
title('Pareto Front');xlabel('Error Function');ylabel('Transmision Angle');
end
para=para(front,:);
para_temp=para;clmn_no=des_var_no+1;
for j=1:size(para_temp,1)
index(j)=1;
for i=2:size(para_temp,1)
if para_temp(index(j),clmn_no)<para_temp(i,clmn_no)
index(j)=i;
end
end
para_temp(index(j),clmn_no)=-inf;
end
para=para(index,:);
fid=fopen('result.txt','w+');
for i=1:size(para,1)
fprintf(fid,'%d',i);
for j=1:des_var_no+obj_fun_no
fprintf(fid,'\t%17.10f',para(i,j));
end
fprintf(fid,'\n');
end
%fprintf(fid,'* * *\n');
fclose(fid);
%fprintf(1,'\n The result of calculation has been saved in
%''result.txt''.\n\n')
function distance=select_factor(para1,obj_fun_no,des_var_no)
pop=para1(:,des_var_no+1:des_var_no+obj_fun_no);
pop(:,1)=pop(:,1);
pop(:,2)=pop(:,2)/1000;
for i=1:size(pop,1)
for j=1:obj_fun_no
ii=1;jj=1;
for k=1:size(pop,1)
if k~=i
dis=(pop(i,j)-pop(k,j));
if dis>0
dis_po(ii)=dis;
ii=ii+1;
else
dis_ne(jj)=dis;
jj=jj+1;
end
end
end
if ii==1
dis_obj_fun(i,j)=abs(max(dis_ne))+abs(min(dis_ne));
elseif jj==1
dis_obj_fun(i,j)=abs(min(dis_po))+abs(max(dis_po));
else
dis_obj_fun(i,j)=abs(min(dis_po))+abs(max(dis_ne));
end
clear dis_po dis_ne
end
dis_total(i)=sum(dis_obj_fun(i,:));
end
distance=dis_total;
function [ indv_opt ] = optim(pop_sel,des_var_no,obj_fun_no)
for i=1:5
indv=fminsearch(@cause1,pop_sel(1,1:des_var_no));
pop_sel(1,1:des_var_no)=indv;
disp(i)
end
indv_opt(1,:)=indv;
[r]=cause1(indv);
disp(r)
for i=1:5
indv=fminsearch(@cause2,pop_sel(2,1:des_var_no));
pop_sel(2,1:des_var_no)=indv;
disp(i)
end
indv_opt(2,:)=indv;
[d]=cause1(indv);
disp(d)
1