⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 whopf.c

📁 hopfield 算法的实现与应用。附带demo演示
💻 C
📖 第 1 页 / 共 2 页
字号:
/*--------------------------------------------------------------------*/static void mn_enable (int enable){                               /* --- enable/disable menu items */  HMENU        menu;            /* handle of window menu */  MENUITEMINFO mii;             /* for menu customization */  UINT         flag;            /* enable/disable flag */  menu = GetMenu(hmain);        /* get menu handle */  flag = (enable) ? MF_ENABLED : MF_GRAYED;  EnableMenuItem(menu, MI_LOAD,   flag);  /* enable/disable context */  EnableMenuItem(menu, MI_SAVE,   flag);  /* sensitive menu items */  EnableMenuItem(menu, MI_GRID,   flag);  EnableMenuItem(menu, MI_NEXT,   flag);  EnableMenuItem(menu, MI_STORE,  flag);  EnableMenuItem(menu, MI_DELETE, flag);  mii.cbSize = sizeof(MENUITEMINFO);  /* set either the */  mii.fMask  = MIIM_ID|MIIM_TYPE;     /* 'Start Training' or */  mii.fType  = MFT_STRING;            /* 'Stop Training' item */  mii.wID    = (enable) ? MI_START : MI_STOP;  LoadString(hinst, (enable) ? CS_START : CS_STOP, text, 80);  mii.dwTypeData = text; mii.cch = strlen(text);  SetMenuItemInfo(menu, (enable) ? MI_STOP : MI_START, FALSE, &mii);}  /* mn_enable() *//*----------------------------------------------------------------------  Dialog Box Procedures----------------------------------------------------------------------*/void dbl_set (HWND hwnd, int item, double num){                               /* --- set floating point number */  sprintf(text, "%g", num);     /* format floating point number */  edt_set(hwnd, item, text);    /* set formatted number */}  /* dbl_set() *//*--------------------------------------------------------------------*/double dbl_get (HWND hwnd, int item){                               /* --- get floating point number */  edt_get(hwnd, item, text, 64);    /* get input text and */  return strtod(text, NULL);    /* convert it to a number */}  /* dbl_get() *//*--------------------------------------------------------------------*/BOOL CALLBACK dp_about (HWND hwnd, UINT msg, WPARAM wp, LPARAM lp){                               /* --- 'about' dialog box procedure */  switch (msg) {                /* evaluate message */    case WM_COMMAND:            /* --- if command */      if (wp != IDOK) break;    /* process only 'Ok' button */    case WM_CLOSE:              /* --- if dialog closed */      EndDialog(hwnd, 0);       /* terminate dialog */      return 1;                 /* return 'ok' */  }  return 0;                     /* everything else not handled */}  /* dp_about() *//*--------------------------------------------------------------------*/BOOL CALLBACK dp_grid (HWND hwnd, UINT msg, WPARAM wp, LPARAM lp){                               /* --- grid/shape dialog procedure */  switch (msg) {                /* evaluate message */    case WM_INITDIALOG:         /* dialog initialization */      int_set(hwnd, DI_WIDTH,  nnpar.width);      int_set(hwnd, DI_HEIGHT, nnpar.height);      int_set(hwnd, DI_GRID,   nnpar.grid);      break;    case WM_COMMAND:            /* command */      switch (wp) {             /* evaluate command code */        case IDOK:              /* if 'Ok' button pressed */          nnpar.width  = int_get(hwnd, DI_WIDTH);          if      (nnpar.width  < 2)   nnpar.width  = 2;          else if (nnpar.width  > 100) nnpar.width  = 100;                                /* width  of network (in units) */          nnpar.height = int_get(hwnd, DI_HEIGHT);          if      (nnpar.height < 1)   nnpar.height = 1;          else if (nnpar.height > 100) nnpar.height = 100;                                /* height of network (in units) */          nnpar.grid   = int_get(hwnd, DI_GRID);          if      (nnpar.grid   < 4)   nnpar.grid   = 4;          else if (nnpar.grid   > 64)  nnpar.grid   = 64;                                /* grid width (in pixels) */          EndDialog(hwnd, 0);   /* get inputs from edit controls */          return 1;             /* return 'ok' */        case IDCANCEL:          /* if 'Cancel' button pressed, */          EndDialog(hwnd, -1); return 1;   /* terminate dialog */      } break;    case WM_CLOSE:              /* dialog box has been closed */      EndDialog(hwnd, -1); return 1;      /* terminate dialog */  }  return 0;                     /* everything else not handled */}  /* dp_grid() *//*--------------------------------------------------------------------*/BOOL CALLBACK dp_params (HWND hwnd, UINT msg, WPARAM wp, LPARAM lp){                               /* --- parameters dialog procedure */  switch (msg) {                /* evaluate message */    case WM_INITDIALOG:         /* dialog initialization */      LoadString(hinst, CS_SINGLE, text, 80);      cmb_add(hwnd, DI_MODE,   text);      LoadString(hinst, CS_ALL,    text, 80);      cmb_add(hwnd, DI_MODE,   text);      cmb_set(hwnd, DI_MODE,   nnpar.mode);      dbl_set(hwnd, DI_DELAY,  nnpar.delay);      break;                    /* set learning parameters */    case WM_COMMAND:            /* command */      switch (wp) {             /* evaluate command code */        case IDOK:              /* if 'Ok' button pressed */          nnpar.mode  = cmb_get(hwnd, DI_MODE);  /* update mode */          nnpar.delay = dbl_get(hwnd, DI_DELAY);          if      (nnpar.delay < 0.001) nnpar.delay  = 0.001;          else if (nnpar.delay > 10)    nnpar.delay  = 10;                                /* delay between redraws */          EndDialog(hwnd, 0);   /* terminate dialog and */          return 1;             /* return 'ok' */        case IDCANCEL:          /* if 'Cancel' button pressed, */          EndDialog(hwnd, -1); return 1;   /* terminate dialog */      } break;    case WM_CLOSE:              /* dialog box has been closed */      EndDialog(hwnd, -1); return 1;      /* terminate dialog */  }  return 0;                     /* everything else not handled */}  /* dp_params() *//*----------------------------------------------------------------------  Window Procedure----------------------------------------------------------------------*/LRESULT CALLBACK wp_main (HWND hwnd, UINT msg, WPARAM wp, LPARAM lp){                               /* --- main window procedure */  static PAINTSTRUCT ps;        /* for BeginPaint call */  HDC     hdc;                  /* handle of device context */  int     i;                    /* pattern index */  HOPFNET *tmp;                 /* buffer for new Hopfield network */  switch (msg) {                /* evaluate message */    case WM_PAINT:              /* --- if to paint window */      hdc = BeginPaint(hwnd, &ps);      if (!hdc) return 0;       /* get device context for drawing */      redraw(hdc);              /* redraw the window contents */      EndPaint(hwnd, &ps);      /* and release the device context */      break;    case WM_COMMAND:            /* --- if menu item selected */      switch (wp) {             /* evaluate item */        case MI_LOAD:           /* Menu Item `Load Map...' */          if (load() == 0) resize(hwnd);          return 0;             /* load a map and redraw the window */                case MI_SAVE:           /* Menu Item `Save Map...' */          save(); return 0;     /* save a self-organizing map */        case MI_QUIT:           /* Menu Item 'Quit' */          DestroyWindow(hwnd);  /* stop network training */          return 0;             /* and destroy the window */                case MI_CLEAR:          /* Menu Item `Clear' */          hfn_clear(hfn);       /* clear all activations */          redraw(NULL);         /* and redraw the window */          return 0;        case MI_NEXT:           /* Menu Item `Next Pattern' */          if (running) return 0;/* if update is running, abort */          i = hfn_patcnt(hfn);  /* get and check */          if (i <= 0) return 0; /* the number of patterns */          if (++nnpar.curr >= i) nnpar.curr = 0;          hfn_patget(hfn, nnpar.curr);          redraw(NULL);         /* get the current pattern */          return 0;             /* and redraw the window */        case MI_STORE:          /* Menu Item `Store Pattern' */          if (running) return 0;/* if update is running, abort */          if (hfn_patadd(hfn) != 0)            error(E_PATCNT);    /* store the current pattern */          return 0;             /* and check for an error */        case MI_DELETE:         /* Menu Item `Delete Pattern' */          if (running) return 0;/* if update is running, abort */          i = hfn_patfind(hfn);          if (i >= 0) hfn_patrem(hfn, i);          return 0;             /* find and delete the pattern */        case MI_INIT:           /* Menu Item `Initialize' */          hfn_rand(hfn, drand); /* set random activations */		  redraw(NULL);         /* and redraw the window */          return 0;        case MI_START:          /* Menu Item `Start Training' */          mn_enable(0);         /* disable certain menu items */          running = 1; update(hmain, WM_TIMER, 1, 0);          return 0;             /* start the training */        case MI_STOP:           /* Menu Item `Stop Training' */          KillTimer(hmain, 1);  /* kill the timer */          mn_enable(1);         /* enable all menu items */          return running = 0;   /* clear the running flag */                case MI_REDRAW:         /* Menu Item 'Redraw' */          InvalidateRect(hwnd, NULL, 0);          return 0;             /* invalidate window contents */                case MI_GRID:           /* Menu Item 'Grid/Shape...' */          if (DialogBox(hinst, MKIRSC(DB_GRID), hwnd, dp_grid) == 0) {            tmp = hfn_create(nnpar.width, nnpar.height);            if (!tmp) { error(E_NOMEM); return 0; }            hfn_delete(hfn); hfn = tmp;            resize(hwnd);       /* replace the Hopfield network */          }                     /* and redraw the window contents */          return 0;             /* get pattern region and grid size */        case MI_PARAMS:         /* Menu Item 'Parameters...' */          if (DialogBox(hinst, MKIRSC(DB_PARAMS), hwnd, dp_params) == 0)            InvalidateRect(hwnd, NULL, 0);          return 0;             /* get learning parameters */                case MI_ABOUT:          /* Menu Item 'About' */          DialogBox(hinst, MKIRSC(DB_ABOUT), hwnd, dp_about);          return 0;             /* show dialog box 'About Wsom' */      } break;    case WM_LBUTTONDOWN:        /* left mouse button pressed */      mouse(LOWORD(lp), HIWORD(lp), 1);      return 0;                 /* process the mouse click */    case WM_LBUTTONUP:          /* left mouse button released */      mouse(LOWORD(lp), HIWORD(lp), -1);      return 0;                 /* process the mouse click */    case WM_MOUSEMOVE:          /* mouse moved */      mouse(LOWORD(lp), HIWORD(lp), 0);      return 0;                 /* process the mouse click */    case WM_CLOSE:              /* window closed */    case WM_DESTROY:            /* or destroyed */      PostQuitMessage(0);       /* terminate program */      return 0;  }  return DefWindowProc(hwnd, msg, wp, lp);}  /* wp_main() *//*----------------------------------------------------------------------  Main Program----------------------------------------------------------------------*/int WINAPI WinMain (HINSTANCE hcurr, HINSTANCE hprev,                    LPSTR cmdln, int cmd){                               /* --- main function */  WNDCLASS wc;                  /* window class */  RECT     rc;                  /* window rectangle */  MSG      msg;                 /* message */  /* --- create window class --- */  hinst = hcurr;                /* note instance handle */  if (!hprev) {                 /* if first instance of this program */    wc.style         = CS_HREDRAW | CS_VREDRAW;    wc.lpfnWndProc   = wp_main;    wc.cbClsExtra    = wc.cbWndExtra = 0;    wc.hInstance     = hinst;    wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);    wc.hbrBackground = NULL;    wc.lpszMenuName  = MKIRSC(MN_MAIN);    wc.lpszClassName = "WhopfClass";    if (!RegisterClass(&wc))    /* register class */      return 0;                 /* of main window */  }  srand((unsigned long)time(NULL));  hfn = hfn_create(nnpar.width, nnpar.height);  if (!hfn) error(E_NOMEM);  /* --- get pens and brushes --- */  pen_black = GetStockObject(BLACK_PEN);  br_white  = GetStockObject(WHITE_BRUSH);  br_grey   = CreateSolidBrush(RGB(0x40, 0x40, 0x40));  /* --- create window --- */  rc.left = 0; rc.right  = nnpar.width  *nnpar.grid -2;  rc.top  = 0; rc.bottom = nnpar.height *nnpar.grid -2;  AdjustWindowRect(&rc, WINSTYLE, 1);  hmain = CreateWindow("WhopfClass", PRGNAME, WINSTYLE,                       30, 30, rc.right-rc.left, rc.bottom-rc.top,                       NULL, NULL, hinst, NULL);  if (!hmain) return 0;         /* create main window */  hdc_win = GetDC(hmain);       /* get a device context */  if (!hdc_win) error(E_INIT);  /* for independent drawing */  ShowWindow(hmain, cmd);       /* open and */  UpdateWindow(hmain);          /* update main window */  /* --- dispatch messages --- */  while (GetMessage(&msg, NULL, 0, 0) > 0) {    TranslateMessage(&msg);     /* get messages, translate */    DispatchMessage (&msg);     /* and dispatch them */  }  /* --- clean up --- */  ReleaseDC(hmain, hdc_win);    /* release device context of window */  DeleteObject(br_grey);        /* delete created brush */  return msg.wParam;}  /* WinMain() */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -