Professional Documents
Culture Documents
4.
a. Max-product composition of two fuzzy relations.
Code:
Output:
code:
w1=input('Weight w1=');
w2=input('Weight w2=');
disp('Enter the threshold value');
theta=input('theta=');
y=[0 0 0 0];
x1=[1 1 0 0];
x2=[1 0 1 0];
z=[1 0 0 0];
con=1;
while con
zin=x1*w1+x2*w2;
for i=1:4
if zin(i)>=theta
y(i)=1;
else y(i)=0;
end
end
disp('Output of net=');
disp(y);
if y==z
con=0;
else
disp('Net is not learning. Enter another set of weights and threshold value');
w1=input('Weight w1=');
w2=input('Weight w2=');
theta=input('theta=');
end
end
disp(w1);
disp(w2);
disp('Threshold value=');
disp(theta);
Output:
Code:
clear;
stars = 30;
iterations = 500;
population = 100;
mutation = 0.7;
mutation_step = 0.8;
grid_size = 10;
for i = 1:stars
for j = 1:2
star(i,j) = rand(1)*grid_size;
end
end
plot(star(:,1),star(:,2),'x')
hold on
for i = 1:population
for j = 1:2
cit(i,j)= rand(1)*grid_size;
end
end
for j = 1:population
for k = 1:stars
d(k) = sqrt((star(k,1)-cit(j,1))^2 + (star(k,2)-cit(j,2))^2);
end
d(stars+1) = cit(j,1);
d(stars+2) = grid_size - cit(j,1);
d(stars+3) = cit(j,2);
d(stars+4) = grid_size - cit(j,2);
d = sort(d);
cit(j,3) = d(1);
end
cit = sortrows(cit,-3);
cit(1,:)
cit(:,3) = cit(:,3)/cit(1,3);
for i = 1:iterations
%--------------- Mating Selection -----------------
cit(:,3) = cit(:,3)/cit(1,3);
pool = [];
for l = 1:population
if rand(1)<cit(l,3),pool=[pool;cit(l,:)];,end
end
%--------------- Crossover ------------------
s = size(pool,1);
if s/2 - round(s/2) ~=0 , pool = pool(1:s-1,:); , s = size(pool,1); , end
for m = 1:2:s
pool = [ pool ; [pool(m,1) , pool(m+1,2) , 0]];
pool = [ pool ; [pool(m+1,1) , pool(m,2) , 0]];
end
%--------------- Mutation -------------------
s = size(pool,1);
for m = 1:s
if ((rand < mutation) & (pool(m,1) < grid_size - mutation_step)) ,
pool(m,1)=pool(m,1)+(2*rand - 1)*mutation_step;,end
if ((rand < mutation) & (pool(m,2) < grid_size - mutation_step)) ,
pool(m,2)=pool(m,2)+(2*rand - 1)*mutation_step;,end
end
%--------------- Invironmental Selection------------
temp = [cit ; pool];
ts = size(temp,1);
for j = 1:ts
for k = 1:stars
d(k) = sqrt((star(k,1)-temp(j,1))^2 + (star(k,2)-temp(j,2))^2);
end
d(stars+1) = temp(j,1);
d(stars+2) = grid_size - temp(j,1);
d(stars+3) = temp(j,2);
d(stars+4) = grid_size - temp(j,2);
d = sort(d);
temp(j,3) = d(1);
end
temp = sortrows(temp,-3);
cit = temp(1:population,:);
end
xc = cit(1,1);
yc = cit(1,2);
r = cit(1,3);
x = (xc-r) : 0.05 : (xc+r);
for i = 1:size(x,2)
y(i) = yc + sqrt(r^2-(x(i)-xc)^2);
end
plot (x,y)
for i = 1:size(x,2)
y(i) = yc - sqrt(r^2-(x(i)-xc)^2);
end
plot (x,y)
hold off
Output: