Saturday, January 26, 2019
Soft Computing Practical File
practaSAGAR INSTITUTE OF RESEARCH AND TECHNOLOGY SOFT COMPUTING PRACTICAL charge up (CS-801) Subject Guide Submitted by INDEX S. No. List of experiments sense of touch 1. consume Perceptron network with binary input and outturn. 2. Using Madaline net, generate XOR prevail with bipolar inputs and targets. 3. Calculation of forward-looking weights for a back propagation network, inclined the values of input pattern, output pattern, target output, learning rate and activating exercise. 4. Use of ART algorithm to cluster vectors. 5. Implement traveling salesman job use genetic algorithm. 6. Implement various laws associated with logy sets. 7. Implement hirsute sets. 8. Implement word matching using GA. Experiment 1 Implement Perceptron network with binary input and output. plan /*PERCEPTRON*/ involve<stdio. h> admit<conio. h> main() sign-language(a) int x42,tar4 float w2,wc2,out=0 int i,j,k=0,h=0 float s=0,b=0,bc=0, of import=0 float the ta clrscr() printf( degrade the value of theta & adenosine monophosphate alpha) scanf(%f%f,&theta,&alpha) for(i=0i<=3i++) printf(Enter the value of %d Input track & Target,i) for(j=0j<=1j++) scanf(%d,&xij) scanf(%d,&tari) wi=0 wci=0 printf(Net TargetWeight changesNew weights persuade changesBias ) printf(&8212&8212&8212&8212&8212&8212&8212&8212&8212&8212&8212&8212&8212&8212&8212&8212&8212&8212&8212&8212&8212&8212&8212&8212&8212 ) mew printf(ITERATION %d ,h) printf(&8212&8212&8212&8212&8212&8212&8212&8212&8212&8212&8212&8212&8212&8212&8212&8212&8212&8212&8212&8212&8212&8212&8212&8212&8212- ) for(i=0i<=3i++) for(j=0j<=1j++) s+=(float)xij*wj s+=b printf(%. 2f,s) if(s>theta) out=1 else if(s<-theta) ut=-1 else out=0 printf(%d,tari) s=0 if(out==tari) for(j=0j<=1j++) wcj=0 bc=0 printf(%. 2f,wcj) for(j=0j<=1j++) printf(%. 2f,wj) k+=1 b+=bc printf(%. 2f,bc) printf(%. 2f,b) else for(j=0j<=1j++) wcj=xij*tari*alpha wj+=wcj printf(%. 2f,wcj) wcj=0 for(j=0j&l t=1j++) printf(%. 2f,wj) bc=tari*alpha b+=bc printf(%. 2f,bc) printf(%. 2f,b) printf( ) if(k==4) printf( Final weights ) for(j=0j<=1j++) printf(w%d=%. 2f,j,wj) printf(Bias b=%. 2f,b) else k=0h=h+1 getch() goto mewgetch() issue Experiment 2 Using Madaline net, generate XOR function with bipolar inputs and targets. Program /*MADALINE*/ let in<stdio. h> include<conio. h> main() signed int x44,tar4 float wc4,w4,e=0,er=0,yin=0,alp=0. 5,b=0,bc=0,t=0 int i,j,k,q=1 clrscr() for(i=0i<=3i++) printf( Enter the %d row and target,i) for(j=0j<=3j++) scanf(%d,&xij) scanf(%d,&tari) printf(%d,tari) wi=0. 0 wci=0. 0 mew er=0e=0 yin=0 printf( ITERATION%d,q) printf( &8212&8212&8212&8212&8212&8212) or(i=0i<=3i++) t=tari for(j=0j<=3j++) yin=yin+xij*wj b=b+bc yin=yin+b bc=0. 0 printf( Net=%f,yin) e=(float)tari-yin yin=0. 0 printf(Error=%f,e) printf(Target=%d ,tari) er=er+e*e for(k=0k<=3k++) wck=xik*e*alp wk+=wck wck=0. 0 printf(Weights ) for(k=0k<=3k++) printf(%f,wk) bc= e*alp printf(b=%. 2f,b) getch() printf( Error Square=%f,er) if(er<=1. 000) printf( ) for(k=0k<=1k++) printf(%f,wk) getch() else e=0 er=0 yin=0 q=q+1 goto mew getch() OutputExperiment 3 Calculation of new weights for a back propagation network, given the values of input pattern, output pattern, target output, learning rate and activation function. Program /*BACK extension NETWORK*/ include<stdio. h> include<conio. h> include<math. h> include<stdlib. h> vacancy main() float v24,w41,vc24,wc41,de,del4,bl,bia,bc4,e=0 float x42,t4,zin4,delin4,yin=0,y,dy,dz4,b4,z4,es,alp=0. 02 int i,j,k=0,itr=0 v00=0. 1970 v01=0. 3191 v02=-0. 1448 v03=0. 3594 v10=0. 3099 v11=0. 1904 v12=-0. 0347 13=-0. 4861 w00=0. 4919 w10=-0. 2913 w20=-0. 3979 w30=0. 3581 b0=-0. 3378 b1=0. 2771 b2=0. 2859 b3=-0. 3329 bl=-0. 141 x00=-1 x01=-1 x10=-1 x11=1 x20=1 x21=-1 x30=1 x31=1 t0=0 t1=1 t2=1 t3=0 clrscr() for(itr=0itr<=387itr++) e=0 es=0 for(i=0i<=3i++) do for(j=0j<=1j++) zink+=xij*vjk zink+= bk k+=1 while(k<=4) for(j=0j<=3j++) zj=(1-exp(-zinj))/(1+exp(-zinj)) dzj=((1+zj)*(1-zj))*0. 5 for(j=0j<=3j++) yin+=zj*wj0 yin+=bl y=(1-exp(-yin))/(1+exp(-yin)) y=((1+y)*(1-y))*0. 5 de=(ti-y)*dy e=ti-y es+=0. 5*(e*e) for(j=0j<=3j++) wcj0=alp*de*zj delinj=de*wj0 delj=delinj*dzj bia=alp*de for(k=0k<=1k++) for(j=0j<=3j++) vckj=alp*delj*xik vkj+=vckj for(j=0j<=3j++) bcj=alp*delj wj0+=wcj0 bj+=bcj bl+=bia for(j=0j<=3j++) zinj=0 zj=0 dzj=0 delinj=0 delj=0 bcj=0 k=0yin=0y=0 dy=0bia=0de=0 printf( Epoch %d ,itr) for(k=0k<=1k++) for(j=0j<=3j++) printf(%f,vkj) printf( ) printf( ) for(k=0k<=3k++) printf(%f,wk0) rintf( %f,bl) printf() for(k=0k<=3k++) printf(%f,bk) getch() getch() Output Experiment 4 Use of ART algorithm to cluster vectors. Program /* ART NETWORK TO CLUSTER FOUR VECTORS */ include<stdio. h> include<conio. h> main() float n=4. 0,m=3. 0,o=0. 4,l=2. 0 float b43,t34,s4,x4,sin=0,y3,xin=0 int i,j,k=0,J,c=0 y0=0,y1=0,y2=0 clrscr() for(i=0i<=3i++) for(j =0j<=2j++) bij=0. 2 for(i=0i<=2i++) for(j=0j<=3j++) tij=1. 0 mew printf(Enter the input value ) for(i=0i<=3i++) scanf(%f,&si) xi=si in+=si for(i=0i<=2i++) printf( Y) do yi+=sk*bki k+=1 while(k<=3) if(y0>=y1) if(y0>=y2) J=0 else J=2 else if(y1>=y2) J=1 else J=2 for(i=0i<=3i++) xi=si*tJi xin+=xi if(xin/sin>=0. 4) for(i=0i<=3i++) biJ=(2*xi)/(1+xin) tJi=xi else yJ=-1 printf( ) for(i=0i<=3i++) for(j=0j<=2j++) printf(%f,bij) printf( ) for(i=0i<=2i++) for(j=0j<=3j++) printf(%f,tij) printf( ) getch() y0=y1=y2=0 sin=xin=0 c+=1 k=0 if(c<=3) goto mew getch() OutputExperiment 5 Implement traveling salesman problem using genetic algorithm. Program include<stdio. h> include<conio. h> int tsp1010=999,10,3,2,5,6,7,2,5,4, 20,999,3,5,10,2,8,1,15,6, 10,5,999,7,8,3,11,12,3,2, 3,4,5,999,6,4,10,6,1,8, 1,2,3,4,999,5,10,20,11,2, 8,5,3,10,2,999,6,9,20,1, 3,8,5,2,20,21,999,3,5,6, 5,2,1,25,15,10,6,999,8,1, 10,11,6,8,3,4,2,15,999,1, 5,10,6,4,15,1,3,5,2,999 int pa100 010= 0,1,2,3,4,5,6,7,8,9, 9,8,6,3,2,1,0,4,5,7, 2,3,5,0,1,4,9,8,6,7, 4,8,9,0,1,3,2,5,6,7 int i,j,k,l,m,y,loc, move over,row,col,it,x=3,y=3 int think,row=0,res110,row1,col1,z nt numoff=4 int offspring100010 int mincost=9999,mc main() int gen clrscr() printf(Number of con temporaryoraries ) scanf(%d,&gen) offcal1(pa) offcal2(pa) printf( First Generation ) for(i=0i<counti++) for(j=0j<10j++) printf(%d ,offspringij) printf( ) for(y=1y<=gen-1y++) getch() clrscr() for(i=0i<counti++) for(j=0j<10j++) paij=offspringij numoff=count offcal1(pa) offcal2(pa) printf( %d Generation ,y+1) for(i=0i<counti++) for(j=0j<10j++) printf(%d ,offspringij) printf( ) getch() clrscr() rintf(Minimum Cost Path ) for(z=0z<10z++) printf(%d ,res0z) printf( Minimum Cost %d ,mincost) /* finding the offspring using cyclic crossover */ offcal1(pa) int pa100010 count=0 for(i=0i<1000i++) for(j=0j<10j++) offspringij=-1 for(k=0k<numoffk++) for(l=k+1l<numoffl++) offspringrow0=pak0 loc=pal0 flag= 1 while(flag = 0) for(j=0j<10j++) if(pakj == loc ) if (offspringrowj==-1) offspringrowj=loc loc=palj else flag=0 /* end while*/ for(m=0m<10m++) if(offspringrowm == -1) offspringrowm=palm or(z=0z<10z++) if(z<9) row1=offspringrowz col1=offspringrowz+1 mc=mc+tsprow1col1 else row1=offspringrowz col1=offspringrow0 mc=mc+tsprow1col1 if(mc < mincost) for(z=0z<10z++) res0z=offspringrowz mincost=mc count++ row++ /* end l*/ offcal2(pa) int pa100010 for(k=0k<numoffk++) for(l=k+1l<numoffl++) offspringrow0=pal0 loc=pak0 flag=1 while(flag = 0) for(j=0j<10j++) if(palj == loc ) if (offspringrowj==-1) offspringrowj=loc loc=pakj else flag=0 /* end while*/ for(m=0m<10m++) if(offspringrowm == -1) offspringrowm=pakm for(z=0z<10z++) if(z<9) row1=offspringrowz col1=offspringrowz+1 mc=mc+tsprow1col1 else row1=offspringrowz col1=offspringrow0 mc=mc+tsprow1col1 row++ if(mc < mincost) for(z=0z<10z++) res0z=offspringrowz mincost=mc count++ /* end l*/ Output Experiment 6 Im plement various laws associated with wooly sets. Program include<stdio. h> include<alloc. h> include<conio. h> include<stdlib. h> struct banding float nr5 float dr5 int n typedef struct prepare wooly oid printval(fuzzy *m,char *x) int i printf( %s=,x) for(i=0i<m->ni++) printf( %6. 2f / %6. 2f,m->nri,m->dri) if(i =m->n-1) putch(+) printf( ) fuzzy unionset(fuzzy a,fuzzy b) fuzzy temp char ch int i temp. n=a. n for(i=0i<a. ni++) if(a. dri =b. dri) printf( Denominators not equal) getch() exit(0) if(a. nri<b. nri) temp. nri=b. nri else temp. nri=a. nri temp. dri=a. dri light temp fuzzy intersect(fuzzy a,fuzzy b) fuzzy temp int i temp. n=a. n for(i=0i<a. ni++) if(a. dri =b. dri) printf( Denominators not equal) etch() exit(0) if(a. nri>b. nri) temp. nri=b. nri else temp. nri=a. nri temp. dri=a. dri pay back temp fuzzy complement(fuzzy a) fuzzy temp int i temp. n=a. n for(i=0i<a. ni++) temp. nri=1-a. nri temp. dri=a. dri return temp vacuous main () fuzzy a,b,autonomic nervous system char ch clrscr() a. n=b. n=3 a. nr0=0. 1 a. dr0=1 a. nr1=0. 2 a. dr1=2 a. nr2=0. 3 a. dr2=3 b. nr0=0. 4 b. dr0=1 b. nr1=0. 3 b. dr1=2 b. nr2=0. 2 b. dr2=3 printval(&a,A) printval(&b,B) printf( Menu 1. AUB 2. AB 3. A 4.B 5. Print S,A,B 6. Exit) while(1) switch((ch=getch())) circumstance 1 ans=unionset(a,b) printval(&ans,1. AUB) scatter grapheme 2 ans=intersect(a,b) printval(&ans,2. AB) break scale 3 ans=complement(a) printval(&ans,3. A) break case 4 ans=complement(b) printval(&ans,4. B) break case 5 printval(&a,A) printval(&b,B) break case 6 exit(0) Output Experiment 7 Implement fuzzy sets. Program include<stdio. h> include<alloc. h> include<conio. h> include<stdlib. h> struct SET float nr5 float dr5 int n ypedef struct SET fuzzy void getval(fuzzy *m,char *x) int i float f clrscr() printf( Enter the %s ,x) for(i=0i<m->ni++) printf( Numerator Element %d ,i+1) scanf(%f,&f) m->nri=f fflush(std in) printf( Denominator Element %d,i+1) scanf(%f,&f) m->dri=f void printval(fuzzy *m,char *x) int i printf( %s=,x) for(i=0i<m->ni++) printf( %6. 2f / %6. 2f,m->nri,m->dri) if(i =m->n-1) putch(+) printf( ) fuzzy unionset(fuzzy a,fuzzy b) fuzzy temp char ch int i temp. n=a. n for(i=0i<a. ni++) if(a. dri =b. dri) printf( Denominators not equal) getch() exit(0) if(a. nri<b. nri) temp. nri=b. nri else temp. nri=a. nri temp. dri=a. dri return temp fuzzy intersect(fuzzy a,fuzzy b) fuzzy temp int i temp. n=a. n for(i=0i<a. ni++) if(a. dri =b. dri) printf( Denominators not equal) getch() exit(0) if(a. nri>b. nri) temp. nri=b. nri else temp. nri=a. nri temp. dri=a. dri return temp fuzzy complement(fuzzy a) fuzzy temp int i temp. n=a. n for(i=0i<a. ni++) temp. nri=1-a. nri temp. dri=a. dri return temp void main() fuzzy a,b,ans char ch clrscr() printf( Enter the no of componets) scanf(%d,&a. n) b. n=a. n getval(&a,A) getval(&b,B) clrscr() printval(&a,A) printval(&b,B) getch() while(1) clrscr() printf( Menu 1. AUB 2. AB 3. A 4. B 5. Print S,A,B 6. Exit) switch((ch=getch())) case 1 ans=unionset(a,b) printval(&ans,AUB) getch() break case 2 ans=intersect(a,b) printval(&ans,AB) getch() break case 3 ans=complement(a) printval(&ans,A) getch() break case 4 ans=complement(b) printval(&ans,B) getch() break ase 5 printval(&a,A) printval(&b,B) getch() break case 6 exit(0) Output Experiment 8 Implement word matching using GA. Program include<stdio. h> include<conio. h> include<stdlib. h> include<dos. h> char input15,parent5015,child5015,mating_pool10515,mutant0515 int pfit50,cfit50,fit105,mfit05,gen=0 void get_input() int i clrscr() printf( account book MATCHING caper GENETIC ALGORITHMS ASSIGNMENT) printf( **********************************************************) printf( ENTER THE WORD TO BE MATCHED ) canf(%s,input) printf( THE ASCII EQUIVALENT OF THE LETTERS IN THE ENTERED WORD) printf( &8212&8212&8 212&8212&8212&8212&8212&8212&8212&8212&8212&8212&8212&8212&8212&8212&8212&8212&8212&8212) printf( LETTERS ) for(i=0i<strlen(input)i++) printf( %c ,inputi) printf( ASCII ) for(i=0i<strlen(input)i++) printf( %3d,inputi) getch() void initial_pop() int i,j randomize() for(i=0i<50i++) for(j=0j<strlen(input)j++) parentij=random(26)+97 if(parentij==inputj) pfiti++ void display() int i,j,nexti clrscr() printf( THE CHROMOSOMES OF PARENTS AND CHILDREN) rintf( &8212&8212&8212&8212&8212&8212&8212&8212&8212&8212&8212&8212&8212&8212 ) printf( PREVIOUS generation CHILDREN CHROMOSOMES) for(i=0i<50i++) if(((i)%4)==0) printf( ) for(j=0j<strlen(input)j++) printf(%c,childij) printf(% 2d ,cfiti) printf( MUTANTS OF THIS GENERATION ) for(i=0i<05i++) if (i==3) printf( ) for(j=0j<strlen(input)j++) printf(%c,mutantij) printf(% 2d ,mfiti) getch() clrscr() printf( THE CHROMOSOMES OF PARENTS AND CHILDREN) printf( &8212&8212&8212&8212&8212&8212&8212&8212&8212&8212&8212&8212&8212&8212 ) rintf( NEX T GENERATION PARENTS CHROMOSOMES) for(i=0i<50i++) if(((i)%4)==0) printf( ) for(j=0j<strlen(input)j++) printf(%c,parentij) printf(% 2d ,pfiti) getch() void reproduction() //sorting_based_on_fitness() char tempc int temp int i,j,k for(i=0i<50i++) for(j=0j<strlen(input)j++) mating_poolij=parentij fiti=pfiti for(i=50i<100i++) for(j=0j<strlen(input)j++) mating_poolij=childi-50j fiti=cfiti-50 for(i=100i<105i++) for(j=0j<strlen(input)j++) mating_poolij=mutanti-100j fiti=mfiti-100 //sorting or(i=0i<105i++) for(j=i+1j<105j++) if(fiti<fitj) for(k=0k<strlen(input)k++) tempc=mating_poolik mating_poolik=mating_pooljk mating_pooljk=tempc temp=fiti fiti=fitj fitj=temp for(i=0i<50i++) for(j=0j<strlen(input)j++) parentij=mating_poolij pfiti=fiti for(i=50i<100i++) for(j=0j<strlen(input)j++) childi-50j=mating_poolij cfiti-50=fiti void crossover() int xover_pt int i,j,k for(i=0i<50i++) xover_pt=random(strlen(input)) cfiti=0 cfiti+1=0 for(j=0j<xover_ptj++) childij=par entij f (inputj==childij) cfiti++ childi+1j=parenti+1j if(inputj==childi+1j) cfiti+1++ for(j=xover_ptj<strlen(input)j++) childij=parenti+1j if(inputj==childij) cfiti++ childi+1j=parentij if(inputj==childi+1j) cfiti+1++ i++ void mutation() int i,mut_pt,j char mut_val randomize() for(i=0i<05i++) mut_pt=random(strlen(input)) mut_val=random(26)+97 mfiti=0 for(j=0j<mut_ptj++) mutantij=parent1j if (mutantij==inputj) mfiti++ mut_val=inputj mutantimut_pt=mut_val if (mutantij==inputj) mfiti++ or(j=mut_pt+1j<strlen(input)j++) mutantij=parent1j if (mutantij==inputj) mfiti++ void results() int i clrscr() printf( WORD MATCHING PROBLEM GENETIC ALGORITHM ASSIGNMENT) printf( **********************************************************) printf( THE MATCHING WORD FOR THE GIVEN arousal WORD) printf( OBTAINED USING GENETIC ALGORITHM) printf( ) for(i=0i<strlen(input)i++) printf(%c,parent0i) printf( ) for(i=0i<strlen(input)i++) printf(-) printf( USER INPUT %s,input) rintf( THE FITNESS OF THE GA GENERATED WORD AND THE USERS INPUT) printf( %2d/%d,pfit0,strlen(input)) printf( GENERATIONS take %d,gen) int input_ extract() int choice,i clrscr() printf( GENEREATION NUMBER %d,gen) printf( &8212&8212&8212&8212&8212&8212&8212&8212&8212&8212) printf( THE FITTEST INDIVIDUAL TILL THE PREVIOUS GENERATION ) for(i=0i<strlen(input)i++) printf(%c,parent0i) printf( / ) for(i=0i<strlen(input)i++) printf(%c,inputi) printf( WITH A FITNESS OF %d/%d,pfit0,strlen(input)) rintf( ENTER YOUR pickaxe (TO CONTINUE 1 TO EXIT 0) ) scanf(%d,&choice) return choice void main() int i,choice clrscr() get_input() initial_pop() //display() reproduction()//sorting_based_on_fitness() display() printf( ENTER YOUR CHOICE (TO CONTINUE 1 TO EXIT 0) ) scanf(%d,&choice) while((choice==1)&&(pfit0 =strlen(input))) crossover() gen++ mutation() reproduction()//sorting_based_on_fitness() display() choice=input_choice() sound(1000)delay(200) nosound()delay(200)results() getch() sound(1000)delay(200 )nosound() Output
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment