Вы находитесь на странице: 1из 5

#include<stdio.

h> 

#include<conio.h> 

#include<iostream.h> 

#include<stdlib.h> 

#include<time.h> 

#include<math.h> 

float x1[]={0,0,1,1}; 

float x2[]={0,1,0,1}; 

float y1[]={0,0,0,1}; 

struct input_rule 

  float o1; 

}irule[4]; 

float theta=0.5,n=0.3; 

void main() 

    clrscr(); 

    int ir=0,m=0,p; 

    float a,del,del_w11=0,del_w21=0,r1,r2; 

    float w11,w11p=0; 

    float w21,w21p=0; 
    randomize(); 

    r1=(rand()/((float)(RAND_MAX)+1)); 

    r2=(rand()/((float)(RAND_MAX)+1)); 

    w11=r1; 

    w21=r2; 

    while(ir<4) 

    { 

  for(p=ir;p<4;p++) 

  { 

    m++; 

    l1: 

    a=((w11*x1[p]+w21*x2[p])‐theta); 

    if(a>0) 

      irule[p].o1=1; 

    else 

      irule[p].o1=0; 

    if(irule[p].o1!=y1[p]) 

    { 

      del=y1[p]‐irule[p].o1; 

      del_w11=n*x1[p]*del; 

      del_w21=n*x2[p]*del; 

      w11p=w11; 

      w21p=w21; 

      if(x1[p]*del!=0) 

        w11=w11+del_w11; 
      if(x2[p]*del!=0) 

        w21=w21+del_w21; 

      if(w21>0.5) 

      { 

        ir=0; 

        w21‐=0.1; 

        goto l1; 

      } 

      if(w11>0.5) 

      { 

        ir=0; 

        w11‐=0.1; 

        goto l1; 

      } 

      if(w21<0) 

      { 

        ir=0; 

        w21+=0.1; 

        goto l1; 

      } 

      if(w11<0) 

      { 

        ir=0; 

        w11+=0.1; 

        goto l1; 
      } 

      goto l1; 

    } 

  } 

  ir++; 

    } 

    int j=0; 

    cout<<"\n\t\t\t`Neural Networks Program'\n\nActual AND Table : \nx1 "<<"x2 "<<"y1\n"; 

    for(j=0;j<4;j++) 

  cout<<x1[j]<<"  "<<x2[j]<<"  "<<y1[j]<<endl; 

    cout<<"\nAssumptions are as follows: \nConsidering Learning Rate(n) : 0.3\nThreshHold Limit(theta)      
: 0.5\n"; 

    cout<<"\n\nPress Enter to perform learning through Neural Networks\ni.e. by using Single Layer 
Preceptron Model of the above AND Problem..."; 

    getch(); 

    clrscr(); 

    cout<<"\t\t\t   `AND Problem(Single Layer Preceptron Model)'\nFinal Table : \nx1    x2      w11          
"<<"w21  "<<"    y11(Actual O/P)    "<<"o1(Desired O/P)\n"; 

    cout<<x1[0]<<"     "<<x2[0]<<"    "<<w11<<"     "<<w21<<"        "<<y1[0]<<"           ==        
"<<irule[0].o1<<endl; 

    cout<<x1[1]<<"     "<<x2[1]<<"    "<<w11<<"     "<<w21<<"        "<<y1[1]<<"           ==        
"<<irule[1].o1<<endl; 

    cout<<x1[2]<<"     "<<x2[2]<<"    "<<w11<<"     "<<w21<<"        "<<y1[2]<<"           ==        
"<<irule[2].o1<<endl; 

    cout<<x1[3]<<"     "<<x2[3]<<"    "<<w11<<"     "<<w21<<"        "<<y1[3]<<"           ==        
"<<irule[3].o1<<endl; 
    cout<<endl; 

    cout<<"\nInitial Weights : \n"; 

    cout<<r1<<","<<r2; 

    cout<<"\n\nAdjusted Weights : \n"; 

    cout<<w11<<","<<w21<<endl; 

    int totali=0; 

    totali+=m; 

    cout<<"\nTotal Number of Weight Adjustment Iterations : "<<totali; 

    getch(); 

Вам также может понравиться