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

📄 xhopf.c

📁 hopfield 算法的实现与应用。附带demo演示
💻 C
📖 第 1 页 / 共 3 页
字号:
  FILE    *file;                /* output file */  HOPFNET *tmp;                 /* temp. buffer for Hopfield network */  mn_enable(menu, MN_MENU, 1);  /* enable the menu bar */  if (!ok) return;              /* if not ok pressed, abort */  file = fopen(fname, "r");     /* open the input file */  if (!file) { error(E_FOPEN, fname); return; }  tmp = hfn_load(file);         /* load the Hopfield network */  fclose(file);                 /* close the input file */  if (!tmp) { error(E_FREAD, fname); return; }  hfn_delete(hfn); hfn = tmp;   /* replace the existing network */  nnpar.width  = hfn_width(hfn);  nnpar.height = hfn_height(hfn);  resize(nnpar.width, nnpar.height);  redraw(NULL,NULL,NULL,NULL);  /* redraw the window contents */}  /* dcb_load() *//*--------------------------------------------------------------------*/static void dcb_save (Widget widget, XtPointer ok, XtPointer fname){                               /* --- 'Save Map...' callback */  FILE *file;                   /* output file */  mn_enable(menu, MN_MENU, 1);  /* enable the menu bar */  if (!ok) return;              /* if not ok pressed, abort */  file = fopen(fname, "w");     /* open the output file */  if (!file) { error(E_FOPEN, fname); return; }  hfn_save(hfn, file);          /* save the Hopfield network */  if (fclose(file) != 0)        /* (list of stored patterns), */    error(E_FWRITE);            /* then close the output file */}  /* dcb_save() *//*--------------------------------------------------------------------*/static void dcb_grid (Widget widget, XtPointer ok, XtPointer call){                               /* --- 'Grid...' callback */  HOPFNET *tmp;                 /* temp. buffer for Hopfield network */  int     w;                    /* buffer for widths */  mn_enable(menu, MN_MENU, 1);  /* enable the menu bar */  if (!ok) return;              /* if not ok pressed, abort */  nnpar.width  = atoi(psh_getval(psh_grid, "width"));  if      (nnpar.width  < 2)   nnpar.width  = 2;  else if (nnpar.width  > 100) nnpar.width  = 100;                                /* width  of network (in units) */  nnpar.height = atoi(psh_getval(psh_grid, "height"));  if      (nnpar.height < 1)   nnpar.height = 1;  else if (nnpar.height > 100) nnpar.height = 100;                                /* height of network (in units) */  nnpar.grid   = atoi(psh_getval(psh_grid, "grid"));  if      (nnpar.grid   < 4)   nnpar.grid   = 4;  else if (nnpar.grid   > 64)  nnpar.grid   = 64;                                /* grid width (in pixels) */  tmp = hfn_create(nnpar.width, nnpar.height);  if (!tmp) { error(E_NOMEM); return; }  hfn_delete(hfn); hfn = tmp;   /* replace the Hopfield network */  w = mn_minwidth(menu);        /* and check the grid width */  if (w > nnpar.width *nnpar.grid)    nnpar.grid = (w +nnpar.width -1) /nnpar.width;  resize(nnpar.width, nnpar.height); /* resize the window and */  redraw(NULL,NULL,NULL,NULL);  /* redraw the window contents */}  /* dcb_grid() *//*--------------------------------------------------------------------*/static void dcb_params (Widget widget, XtPointer ok, XtPointer call){                               /* --- 'Parameters...' callback */  mn_enable(menu, MN_MENU, 1);  /* enable menu bar */  if (!ok) return;              /* if not ok pressed, abort */  nnpar.mode  = (int)psh_getval(psh_params, "mode");                                /* neuron update mode */  nnpar.delay = atof(psh_getval(psh_params, "delay"));  if      (nnpar.delay < 0.001) nnpar.delay  = 0.001;  else if (nnpar.delay > 10)    nnpar.delay  = 10;                                /* delay between redraws */  redraw(NULL,NULL,NULL,NULL);  /* redraw the window contents */}  /* dcb_params() *//*----------------------------------------------------------------------  Menu Callback Functions----------------------------------------------------------------------*/static void mcb_load   (Widget widget, XtPointer client, XtPointer call){ load(NULL, NULL, NULL, NULL); }static void mcb_save   (Widget widget, XtPointer client, XtPointer call){ save(NULL, NULL, NULL, NULL); }static void mcb_quit   (Widget widget, XtPointer client, XtPointer call){ quit(NULL, NULL, NULL, NULL); }static void mcb_clear  (Widget widget, XtPointer client, XtPointer call){ clear(NULL, NULL, NULL, NULL); }static void mcb_next   (Widget widget, XtPointer client, XtPointer call){ next(NULL, NULL, NULL, NULL); }static void mcb_store  (Widget widget, XtPointer client, XtPointer call){ store(NULL, NULL, NULL, NULL); }static void mcb_delete (Widget widget, XtPointer client, XtPointer call){ delete(NULL, NULL, NULL, NULL); }static void mcb_init   (Widget widget, XtPointer client, XtPointer call){ init(NULL, NULL, NULL, NULL); }static void mcb_start  (Widget widget, XtPointer client, XtPointer call){ exec(NULL, NULL, NULL, NULL); }static void mcb_redraw (Widget widget, XtPointer client, XtPointer call){ redraw(NULL, NULL, NULL, NULL); }static void mcb_grid   (Widget widget, XtPointer client, XtPointer call){ grid(NULL, NULL, NULL, NULL); }static void mcb_params (Widget widget, XtPointer client, XtPointer call){ params(NULL, NULL, NULL, NULL); }static void mcb_about  (Widget widget, XtPointer client, XtPointer call){ about(NULL, NULL, NULL, NULL); }/*----------------------------------------------------------------------  Action Functions----------------------------------------------------------------------*/static void mouse (Widget w, XEvent *e, String *s, Cardinal *c){                               /* --- evaluate mouse button */  int x, y;                     /* mouse/neuron coordinates */  int g;                        /* grid width */  static int a = 1;             /* activation to set */  if (running) return;          /* if update is running, abort */  if (*c <= 0) return;          /* if no argument is given, abort */  x = e->xbutton.x;             /* get the position at which */  y = e->xbutton.y;             /* the mouse button was pressed */  g = nnpar.grid;               /* compute the neuron coordinates */  x /= g; if ((x < 0) || (x >= hfn_width(hfn)))  return;  y /= g; if ((y < 0) || (y >= hfn_height(hfn))) return;  if (s[0][0] == '1') {         /* on mouse down, get activation */    a = (hfn_actget(hfn, x, y) > 0) ? -1 : 1; }  hfn_actset(hfn, x, y, a);     /* toggle the activation and redraw */  XSetForeground(display, gc, (a > 0) ? grey : white);  XFillRectangle(display, window, gc, x*g, y*g, g-1, g-1);  XFlush(display);              /* flush buffered drawing commands */}  /* mouse() *//*--------------------------------------------------------------------*/static void load (Widget w, XEvent *e, String *s, Cardinal *c){                               /* --- File > Load Map... */  const char *t;                /* buffer for dialog box title */  if (running) return;          /* if update is running, abort */  mn_enable(menu, MN_MENU, 0);  /* disable the menu bar */  XtVaGetValues(XtNameToWidget(mn_menubar(menu), "*load"),    XtNlabel, &t, NULL);        /* get the name of the menu entry */  if (fselect(w_top, t, NULL, NULL, dcb_load) != 0) {    error(E_DIALOG); mn_enable(menu, MN_MENU, 1); }}  /* load() */                 /* open a file select dialog *//*--------------------------------------------------------------------*/static void save (Widget w, XEvent *e, String *s, Cardinal *c){                               /* --- File > Save Map... */  const char *t;                /* buffer for dialog box title */  if (running) return;          /* if update is running, abort */  mn_enable(menu, MN_MENU, 0);  /* disable the menu bar */  XtVaGetValues(XtNameToWidget(mn_menubar(menu), "*save"),    XtNlabel, &t, NULL);        /* get the name of the menu entry */  if (fselect(w_top, t, NULL, NULL, dcb_save) != 0) {    error(E_DIALOG); mn_enable(menu, MN_MENU, 1); }}  /* save() */                 /* open a file select dialog *//*--------------------------------------------------------------------*/static void quit (Widget w, XEvent *e, String *s, Cardinal *c){                               /* --- File > Quit */  if (hfn) hfn_delete(hfn);     /* delete a Hopfield network */  if (w_top) XtDestroyApplicationContext(appctx);  exit(0);                      /* terminate the program */}  /* quit() *//*--------------------------------------------------------------------*/static void clear (Widget w, XEvent *e, String *s, Cardinal *c){                               /* --- Actions > Clear */  hfn_clear(hfn);               /* clear all activations and */  redraw(NULL,NULL,NULL,NULL);  /* redraw the window contents */}  /* clear() *//*--------------------------------------------------------------------*/static void next (Widget w, XEvent *e, String *s, Cardinal *c){                               /* --- Actions > Next Pattern */  int n;                        /* number of patterns */  if (running) return;          /* if update is running, abort */  n = hfn_patcnt(hfn);          /* get and check */  if (n <= 0) return;           /* the number of patterns */  if (++nnpar.curr >= n) nnpar.curr = 0;  hfn_patget(hfn, nnpar.curr);  /* get the current pattern */  redraw(NULL,NULL,NULL,NULL);  /* redraw the window contents */}  /* next() *//*--------------------------------------------------------------------*/static void store (Widget w, XEvent *e, String *s, Cardinal *c){                               /* --- Actions > Store Pattern */  if (running) return;          /* if update is running, abort */  if (hfn_patadd(hfn) != 0)     /* store the current pattern */    error(E_PATCNT);            /* and check for an error */}  /* store() *//*--------------------------------------------------------------------*/static void delete (Widget w, XEvent *e, String *s, Cardinal *c){                               /* --- Actions > Delete Pattern */  int i;                        /* pattern index */  if (running) return;          /* if update is running, abort */  i = hfn_patfind(hfn);         /* find and delete the pattern */  if (i >= 0) hfn_patrem(hfn, i);}  /* delete() *//*--------------------------------------------------------------------*/static void init (Widget w, XEvent *e, String *s, Cardinal *c){                               /* --- Actions > Initialize */  hfn_rand(hfn, drand);         /* init. the activations randomly */  redraw(NULL,NULL,NULL,NULL);  /* redraw the window contents */}  /* init() *//*--------------------------------------------------------------------*/static void update (XtPointer data, XtIntervalId *id){                               /* --- do one training step */  int x, y, a;                  /* neuron coordinates, activation */  int g;                        /* grid width for display */  if (!running) return;         /* check the update flag */  if (nnpar.mode == 0) {        /* if in single neuron mode */    hfn_curr(hfn, &x, &y);      /* get the neuron coordinates */    a = hfn_update(hfn, -1, 0); /* and update the neuron */    g = nnpar.grid;             /* redraw the corresp. field */    XSetForeground(display, gc, (a > 0) ? grey : white);    XFillRectangle(display, window, gc, x*g, y*g, g-1, g-1);    XFlush(display); }          /* flush buffered drawing commands */  else {                        /* if in epoch mode */    hfn_update(hfn, -1, -1);    /* update all neurons */    redraw(NULL, NULL, NULL, NULL);  }                             /* redraw the window contents */  iid = XtAppAddTimeOut(appctx, (unsigned long)(nnpar.delay *1000),          update, NULL);        /* reregister the interval procedure */}  /* update() */

⌨️ 快捷键说明

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