Sunteți pe pagina 1din 4

Block world Problem

states=cell(1,1);
states{1,1}='123g54g';
best=-1;
best_state='';
check=1;
while(check~=0)
%assignn queue the string which has highest fitness value among all the
%given states
for p=1:length(states)
queue=states{1,p};
% k=strfind(queue,'3g');
% final='1423g5g';

%compute the fitness value


ground=strfind(queue,'g');
fitness=zeros(1,length(queue));

for i=1:length(queue)
active=i;next=active+1;
status=1;sum=0;
node=queue(active);

while(queue(next-1)~='g'||next==1)
substr=queue(active:next);
disp(substr);
index=strfind(final,substr);
if(length(index)==0)
sum=sum-1;
else
sum=sum+1;
end
next=next+1;

end
fitness(i)=sum;
if(fitness(i)>best)
best_state=queue;
check=1;
end
end
end
disp('best is-----------------------------------------------------------
-----------------------');
disp(best_state);
if(check==0)
break;
end
%generation of next states
%find all the index of g-1
qu=['g' best_state];
displace=strfind(qu,'g')+1;
displace=[displace(1:length(displace)-1)];

states=cell(1,1);count=1;
st='';
for c=1:length(displace)
rem=[qu(1:displace(c)-1) qu(displace(c)+1:length(qu))];
val=qu(displace(c));
disp(rem);
gs=strfind(rem,'g');

for t=1:length(gs)
temporary='';
temporary=[temporary rem(1:gs(t)) val rem(gs(t)+1:length(rem))];
if(t==length(gs))
temporary=[temporary 'g'];
end

temporary=[temporary(2:length(temporary))];
disp('states are');
disp(temporary);
disp('------------------');
states{1,count}=temporary;
count=count+1;

end
end
end % end of while
MiniMax Problem

a=[0,0,0,0,3,12,8,2,4,6,14,5,2];
parent=[-1,1,1,1,2,2,2,3,3,3,4,4,4];
% starting from max;
start=1;
max=0;
min=0;

%count initial number of non zero node

nz=0;zero=[];
while(true)
for i=1:length(a)
if(a(i)==0)
zero=[zero i];
else
nz=nz+1;
end
end

if(length(zero)==0)
break;
end
level=log(nz)/log(3)+1;
level=level-1;
nz1=log(100)/log(10);
count=nz/3;
% starting from max;

if(mod(sym(level),2)==0)
disp('last level is min');
min=1;
max=0;
else
disp('last level is max');
min=0;
max=1;
end

extract=[];

%extract count number of nodes from zeros


for b=length(zero):-1:1
if(count>0)
extract=[zero(b) extract];
count=count-1;
zero=[zero(1:length(zero)-1)];
end
end

if(min==1)
while(length(extract)>0)
child_value=[];
node=extract(1);
for t=1:length(parent)
if(parent(t)==node)
child_value=[child_value a(t)];
end
end

%find minimum among all the values


h=10000;
for v=1:length(child_value)
if(child_value(v)<h)
h=child_value(v);
end
end

a(node)=h;
extract=[extract(2:length(extract))];
disp(child_value);
disp(a);
end
elseif(max==1)
while(length(extract)>0)
child_value=[];
node=extract(1);
for t=1:length(parent)
if(parent(t)==node)
child_value=[child_value a(t)];
end
end

%find minimum among all the values


h=-1;
for v=1:length(child_value)
if(child_value(v)>h)
h=child_value(v);
end
end

a(node)=h;
extract=[extract(2:length(extract))];
disp(child_value);
disp(a);
end
end
end

disp('the finally array is');


disp(a);

S-ar putea să vă placă și