home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: OtherApp / OtherApp.zip / mousepen.zip / HRNNUI.C < prev    next >
Text File  |  1994-11-18  |  11KB  |  388 lines

  1. /* PM User Interface for Back propogation Neural Network */
  2. /* for Numerical Character Recognition */
  3. /* By ROBERT ALDRIDGE - 10/10/94 */
  4.  
  5. #define INCL_WIN
  6. #define INCL_GPI
  7. #define INCL_DOS
  8.  
  9. #define ID_BUTTON1 1
  10. #define ID_BUTTON2 2
  11. #define ID_BUTTON3 3
  12.  
  13. #define HI 0.4
  14. #define LOW 0.1
  15. #define nn 64
  16.  
  17.  
  18. #include <os2.h>
  19. #include <stdio.h>
  20. #include <math.h>
  21. #include <string.h>
  22.  
  23. typedef struct Neuron {
  24.    float output;
  25.    float weight[nn];
  26.    float s;
  27. } neuron;
  28.  
  29. neuron layer1[nn],layer2[nn];              /* 2 Layers of Neurons */
  30.  
  31. short ncnt, wcnt;                          /* A couple of counters */
  32.  
  33. float temp, t=0;
  34.  
  35. static LONG charcell[nn];
  36. static float chararray[nn];
  37. int result=0,cnt,oldresult=0;
  38. char commentarr[nn]="0123456789KLMNOPQRSTUVWXYZ";
  39.  
  40. FILE *infile,*outfile,*outfile2;
  41.  
  42. MRESULT EXPENTRY WindowProc(HWND, ULONG, MPARAM, MPARAM);
  43.  
  44. void calcoutput();
  45. void roundup(float *dnum);
  46. void loadweights();
  47.  
  48. int main(int argc,char *argv[])
  49. {
  50.    char fname[12];
  51.    static CHAR title[] = "Mouse Pen ";
  52.    static CHAR ClientClass[] = "GridWindow";
  53.    static ULONG flFrameFlags =   FCF_TITLEBAR | FCF_SYSMENU | FCF_BORDER |
  54.                                  FCF_TASKLIST | FCF_MINBUTTON;
  55.    HAB   hab;
  56.    HMQ   hmq;
  57.    HWND  hwndFrame, hwndClient;
  58.    QMSG  qmsg;
  59.  
  60.    hab = WinInitialize(0);
  61.    hmq = WinCreateMsgQueue(hab, 0);
  62.  
  63.    WinRegisterClass (
  64.                   hab,
  65.                   ClientClass,
  66.                   WindowProc,
  67.                   CS_SYNCPAINT,
  68.                   0);
  69.  
  70.    hwndFrame = WinCreateStdWindow(
  71.                                  HWND_DESKTOP,
  72.                                  0,
  73.                                  &flFrameFlags,
  74.                                  ClientClass,
  75.                                  title,
  76.                                  0L,
  77.                                  0,
  78.                                  0,
  79.                                  &hwndClient);
  80.  
  81.   
  82.  
  83.    WinSetWindowPos(hwndFrame,HWND_TOP,200,200,160,200,SWP_SIZE | SWP_MOVE );
  84.    WinShowWindow(hwndFrame,TRUE);
  85.  
  86.    WinCreateWindow(
  87.                   hwndClient,
  88.                   WC_BUTTON,
  89.                   "Clear",
  90.                   WS_VISIBLE | BS_PUSHBUTTON,
  91.                   100,
  92.                   10,
  93.                   50,
  94.                   25,
  95.                   hwndClient,
  96.                   HWND_BOTTOM,
  97.                   ID_BUTTON1,
  98.                   NULL,
  99.                   NULL);
  100.  
  101.    WinCreateWindow(
  102.                   hwndClient,
  103.                   WC_BUTTON,
  104.                   "Save",
  105.                   WS_VISIBLE | BS_PUSHBUTTON,
  106.                   100,
  107.                   45,
  108.                   50,
  109.                   25,
  110.                   hwndClient,
  111.                   HWND_BOTTOM,
  112.                   ID_BUTTON2,
  113.                   NULL,
  114.                   NULL);
  115.  
  116.    WinCreateWindow(
  117.                   hwndClient,
  118.                   WC_BUTTON,
  119.                   "Start",
  120.                   WS_VISIBLE | BS_PUSHBUTTON,
  121.                   100,
  122.                   80,
  123.                   50,
  124.                   25,
  125.                   hwndClient,
  126.                   HWND_BOTTOM,
  127.                   ID_BUTTON3,
  128.                   NULL,
  129.                   NULL);
  130.  
  131.     strcpy(fname,argv[1]);
  132.     if( (infile=fopen(fname,"rb"))==NULL )
  133.       exit(0);
  134.     loadweights();
  135.     outfile=fopen("inputs.dat","w");
  136.     outfile2=fopen("outputs.dat","w");
  137.  
  138.    while(WinGetMsg(hab, &qmsg, NULLHANDLE, 0, 0))
  139.       WinDispatchMsg(hab,&qmsg);
  140.  
  141.    WinDestroyWindow(hwndFrame);
  142.    WinDestroyMsgQueue(hmq);
  143.    WinTerminate(hab);
  144.    return 0;
  145. }
  146.  
  147. MRESULT EXPENTRY WindowProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  148. {
  149.    int xpos,ypos,row=0;
  150.    static POINTL points,mpos,textpos;
  151.    HPS pspace;
  152.    static BOOL button1;
  153.    static SHORT charcnt;
  154.    static char resultmsg[]="Char ?";
  155.    
  156.  
  157.    switch(msg) {
  158.  
  159.       case WM_CREATE :
  160.          for(cnt=1; cnt<=nn; cnt++)
  161.          charcell[cnt]=DRO_OUTLINE;
  162.          charcnt=0;
  163.          return 0;
  164.  
  165.  
  166.       case WM_COMMAND :
  167.          switch(COMMANDMSG(&msg)->cmd) {
  168.             case ID_BUTTON1 :
  169.                 for(cnt=1; cnt<=nn; cnt++)
  170.                    charcell[cnt]=DRO_OUTLINE;
  171.                 result=oldresult=0;
  172.                 sprintf(resultmsg,"Char ?");
  173.                 WinInvalidateRect(hwnd,NULL,TRUE);
  174.                 return 0;
  175.              case ID_BUTTON2 :
  176.                  for(cnt=0; cnt<=64; cnt++) {
  177.                   if(charcell[cnt]==DRO_FILL)
  178.                      chararray[cnt]=1;
  179.                   else
  180.                      chararray[cnt]=0;
  181.                }
  182.                for(cnt=1; cnt<=64; cnt++)
  183.                   fprintf(outfile,"%d,",(int)chararray[cnt]);
  184.                fprintf(outfile,"/* Char %c */\n",commentarr[charcnt++]);
  185.                return 0;
  186.             case ID_BUTTON3 :
  187.                for(cnt=1; cnt<=nn; cnt++) {
  188.                   if(charcell[cnt]==DRO_FILL)
  189.                      chararray[cnt]=1;
  190.                   else
  191.                      chararray[cnt]=0;
  192.                }
  193.                calcoutput();
  194.                for(ncnt=0; ncnt<nn; ncnt++) {
  195.                   fprintf(outfile2,"%f \n",layer2[ncnt].output);
  196.                   roundup(&layer2[ncnt].output);
  197.                }
  198.                oldresult=result;
  199.                for(ncnt=0; ncnt<nn; ncnt++) {
  200.                   if(layer2[ncnt].output==1) {
  201.                      result=ncnt;
  202.                      break;
  203.                   }
  204.                } 
  205.                if(oldresult==result)
  206.                   DosBeep(1000,100);
  207.                for(ncnt=1; ncnt<=nn; ncnt++)
  208.                   fprintf(outfile2,"%d, ",(int)chararray[ncnt]);
  209.                fprintf(outfile2," - %c - \n\n",commentarr[result]);
  210.                sprintf(resultmsg,"Char %c",commentarr[result]); 
  211.                textpos.x=10;
  212.                textpos.y=100;
  213.                pspace=WinGetPS(hwnd);
  214.                GpiSetColor(pspace,CLR_BLUE);
  215.                GpiSetBackMix(pspace,BM_OVERPAINT);
  216.                GpiCharStringAt(pspace,&textpos,8,(PCH)"         ");
  217.                textpos.x=10;
  218.                textpos.y=100;
  219.                GpiCharStringAt(pspace,&textpos,8,(PCH)resultmsg);
  220.                WinReleasePS(pspace);
  221.                return 0;
  222.  
  223.          }
  224.          break;
  225.  
  226.  
  227.  
  228.       case WM_BUTTON1DOWN :
  229.          pspace=WinGetPS(hwnd);
  230.          GpiSetColor(pspace,CLR_BLACK);
  231.          mpos.x=MOUSEMSG(&msg)->x;
  232.          mpos.y=MOUSEMSG(&msg)->y;
  233.          if(mpos.x<80 && mpos.y<80) {
  234.             xpos=mpos.x/10;
  235.             ypos=mpos.y/10;
  236.             row=7-ypos;
  237.             charcell[xpos+(row*8)+1]=DRO_FILL;
  238.             button1=TRUE;
  239.             WinSetCapture(HWND_DESKTOP,hwnd);
  240.             points.x=(xpos*10)+1;
  241.             points.y=((ypos*10)+10)-1;
  242.             GpiMove(pspace,&points);
  243.             points.x+=8;
  244.             points.y-=8;
  245.             GpiBox(pspace,charcell[xpos+(row*8)+1],&points,0,0);
  246.          }
  247.          WinReleasePS(pspace);
  248.          break;
  249.       case WM_BUTTON1DBLCLK :
  250.          pspace=WinGetPS(hwnd);
  251.          GpiSetBackMix(pspace,BM_OVERPAINT);
  252.          GpiSetColor(pspace,CLR_WHITE);
  253.          mpos.x=MOUSEMSG(&msg)->x;
  254.          mpos.y=MOUSEMSG(&msg)->y;
  255.          if(mpos.x<80 && mpos.y<80) {
  256.             xpos=mpos.x/10;
  257.             ypos=mpos.y/10;
  258.             row=7-ypos;
  259.             charcell[xpos+(row*8)+1]=DRO_OUTLINE;
  260.             points.x=(xpos*10)+1;
  261.             points.y=((ypos*10)+10)-1;
  262.             GpiMove(pspace,&points);
  263.             points.x+=8;
  264.             points.y-=8;
  265.             GpiBox(pspace,DRO_FILL,&points,0,0);
  266.             GpiSetColor(pspace,CLR_BLACK);
  267.             GpiBox(pspace,charcell[xpos+(row*8)+1],&points,0,0);
  268.          }
  269.          WinReleasePS(pspace);
  270.          break;
  271.  
  272.       case WM_MOUSEMOVE :
  273.          pspace=WinGetPS(hwnd);
  274.          GpiSetColor(pspace,CLR_BLACK);
  275.          if(button1==TRUE) {
  276.             mpos.x=MOUSEMSG(&msg)->x;
  277.             mpos.y=MOUSEMSG(&msg)->y;
  278.             if(mpos.x<80 && mpos.y<80) {
  279.                xpos=mpos.x/10;
  280.                ypos=mpos.y/10;
  281.                row=7-ypos;
  282.                charcell[xpos+(row*8)+1]=DRO_FILL;
  283.                points.x=(xpos*10)+1;
  284.                points.y=((ypos*10)+10)-1;
  285.                GpiMove(pspace,&points);
  286.                points.x+=8;
  287.                points.y-=8;
  288.                GpiBox(pspace,charcell[xpos+(row*8)+1],&points,0,0);
  289.             }
  290.          }
  291.          WinReleasePS(pspace);
  292.          break;
  293.  
  294.       case WM_BUTTON1UP :
  295.          button1=FALSE;
  296.          WinSetCapture(HWND_DESKTOP,NULLHANDLE);
  297.          return 0;
  298.  
  299.  
  300.       case WM_PAINT :
  301.          pspace = WinBeginPaint(hwnd, NULLHANDLE, NULL);
  302.          GpiErase(pspace);
  303.          GpiSetColor(pspace,CLR_RED);
  304.          for(cnt=0; cnt<=80; cnt+=10) {
  305.          points.x=cnt;
  306.          points.y=0;
  307.          GpiMove(pspace,&points);
  308.          points.x=cnt;
  309.          points.y=80;
  310.          GpiLine(pspace,&points);
  311.          points.x=0;
  312.          points.y=cnt;
  313.          GpiMove(pspace,&points);
  314.          points.x=80;
  315.          GpiLine(pspace,&points);
  316.          }
  317.          GpiSetColor(pspace,CLR_BLACK);
  318.          for(ypos=0; ypos<8; ypos++) {
  319.             for(xpos=0; xpos<8; xpos++) {
  320.                points.x=(xpos*10)+1;
  321.                points.y=(80-(ypos*10))-1;
  322.                GpiMove(pspace,&points);
  323.                points.x+=8;
  324.                points.y-=8;
  325.                GpiBox(pspace,charcell[xpos+(row*8)+1],&points,0,0);
  326.             }
  327.             row++;
  328.          }
  329.          textpos.x=10;
  330.          textpos.y=100;
  331.          GpiSetColor(pspace,CLR_PALEGRAY);
  332.          GpiCharStringAt(pspace,&textpos,7,(PCH)"       ");
  333.          textpos.x=10;
  334.          textpos.y=100;
  335.          GpiCharStringAt(pspace,&textpos,7,(PCH)resultmsg);
  336.          WinEndPaint(pspace);
  337.          return 0;
  338.  
  339.     case WM_DESTROY :
  340.          fclose(outfile);
  341.          fclose(outfile2);
  342.          return 0;
  343.    }
  344.    return WinDefWindowProc(hwnd,msg,mp1,mp2);
  345. }
  346.  
  347. void calcoutput()
  348. {
  349.    /* Input Layer - layer1 */
  350.    for(ncnt=0; ncnt<nn; ncnt++) {                              /* Neuron Loop */
  351.       temp=0;
  352.       for(wcnt=0; wcnt<nn; wcnt++)                             /* Weight Loop */
  353.          temp+=(chararray[wcnt+1]*layer1[ncnt].weight[wcnt])-t;
  354.       layer1[ncnt].s=temp;
  355.       layer1[ncnt].output=(1/(1+exp(-layer1[ncnt].s)));
  356.    }
  357.  
  358.    /* First hidden layer - layer2 */
  359.    for(ncnt=0; ncnt<nn; ncnt++) {                              /* Neuron Loop */
  360.       temp=0;
  361.       for(wcnt=0; wcnt<nn; wcnt++)                             /* Weight Loop */
  362.          temp+=(layer1[wcnt].output*layer2[ncnt].weight[wcnt])-t;
  363.       layer2[ncnt].s=temp;
  364.       layer2[ncnt].output=(1/(1+exp(-layer2[ncnt].s)));
  365.    }
  366.    DosBeep(500,100);
  367. }
  368.  
  369.  
  370. void roundup(float* dnum)
  371. {
  372.    if(*dnum<LOW)
  373.       *dnum=0;
  374.    else if(*dnum>HI)
  375.       *dnum=1;
  376. }
  377.  
  378.  
  379. void loadweights()
  380. {
  381.    for(ncnt=0; ncnt<nn; ncnt++) {
  382.       fread(layer1[ncnt].weight,sizeof(layer1[ncnt].weight),1,infile);
  383.       fread(layer2[ncnt].weight,sizeof(layer2[ncnt].weight),1,infile);
  384.    }
  385.    fclose(infile);
  386. }
  387.  
  388.