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

📄 xhopf.c

📁 hopfield 算法的实现与应用。附带demo演示
💻 C
📖 第 1 页 / 共 3 页
字号:
/*--------------------------------------------------------------------*/static void exec (Widget w, XEvent *e, String *s, Cardinal *c){                               /* --- Actions > Start/Stop Training */  if (running) {                /* if running, remove procedure */    mn_visible(menu, MN_ITEMS, 3);    mn_enable (menu, MN_ITEMS, 3);    running = 0; XtRemoveTimeOut(iid); }  else {                        /* otherwise start training */    mn_visible(menu, MN_ITEMS, 5);    mn_enable (menu, MN_ITEMS, 4);    hfn_shuffle(hfn, drand);    /* shuffle the neurons */    running = 1; update(NULL, NULL);  }}  /* exec() *//*--------------------------------------------------------------------*/static void redraw (Widget w, XEvent *e, String *s, Cardinal *c){                               /* --- Actions > Redraw */  static Dimension owd = 0;     /* old window width */  static Dimension oht = 0;     /* old window height */  XEvent     de;                /* dummy event */  XRectangle rc;                /* clipping rectangle */  Dimension  wd, ht;            /* window extensions */  int        x, y;              /* loop variables */  int        g, a;              /* grid width, activation */  /* --- initialize graphics --- */  if (!gc) {                    /* if there is no graphics context */    gc = XCreateGC(display, window, 0, NULL);    if (!gc) { error(E_GRAPH); return; }  }                             /* create a graphics context */  XtVaGetValues(w_net, XtNwidth, &wd, XtNheight, &ht, NULL);  if (e && (e->type == Expose)  /* if the window was exposed, */  &&  (wd == owd) && (ht == oht)) {        /* but not resized */    rc.x      = e->xexpose.x;   /* build and set a */    rc.y      = e->xexpose.y;   /* clipping rectangle */    rc.width  = e->xexpose.width;    rc.height = e->xexpose.height;    XSetClipRectangles(display, gc, 0, 0, &rc, 1, Unsorted); }  else {                        /* if the window was resized */    rc.x = rc.y = 0;            /* or this is a forced redraw, */    rc.width  = wd;             /* redraw all of the window */    rc.height = ht;             /* (set full window rectangle) */    while (XCheckTypedWindowEvent(display, window, Expose, &de));  }                             /* remove all exposure events */  /* --- draw Hopfield network --- */  g = nnpar.grid;               /* get the grid width */  for (y = hfn_height(hfn); --y >= 0; ) {    for (x = hfn_width(hfn); --x >= 0; ) {      a = hfn_actget(hfn, x,y); /* traverse the neurons */      XSetForeground(display, gc, (a > 0) ? grey : white);      XFillRectangle(display, window, gc, x*g, y*g, g-1, g-1);    }                           /* color each field according to */  }                             /* the activation of the neuron */  XSetForeground(display, gc, black);  y = hfn_height(hfn) *g -1;    /* draw horizontal grid line */  for (a = -1, x = hfn_width(hfn);  --x > 0; ) {    a += g; XDrawLine(display, window, gc, a, 0, a, y); }  x = hfn_width(hfn) *g -1;     /* draw vertical   grid line */  for (a = -1, y = hfn_height(hfn); --y > 0; ) {    a += g; XDrawLine(display, window, gc, 0, a, x, a); }  XFlush(display);              /* flush buffered drawing commands */  XSetClipMask(display, gc, None);  /* and clear the clipping mask */}  /* redraw() *//*--------------------------------------------------------------------*/static void grid (Widget w, XEvent *e, String *s, Cardinal *c){                               /* --- Settings > Grid/Shape... */  char buf[32];                 /* temporary buffer */  if (!psh_grid) {              /* if property sheet not created yet */    psh_grid = psh_create("gridDlg", w_top, 1);    if (!psh_grid) return;      /* create a property sheet */    if ((psh_addline(psh_grid, "lwidth")              != 0)    ||  (psh_additem(psh_grid, "width",  PSH_EDIT, 0) != 0)    ||  (psh_addline(psh_grid, "lheight")             != 0)    ||  (psh_additem(psh_grid, "height", PSH_EDIT, 0) != 0)    ||  (psh_addline(psh_grid, "lgrid")               != 0)    ||  (psh_additem(psh_grid, "grid",   PSH_EDIT, 0) != 0)) {      psh_delete(psh_grid); error(E_DIALOG); return; }  }                             /* add dialog items */  mn_enable(menu, MN_MENU, 0);  /* disable menu bar */  sprintf(buf, "%d", nnpar.width);   /* network width  (in units) */  psh_setval(psh_grid, "width",  buf);  sprintf(buf, "%d", nnpar.height);  /* network height (in units) */  psh_setval(psh_grid, "height", buf);  sprintf(buf, "%d", nnpar.grid);    /* grid width (in pixels) */  psh_setval(psh_grid, "grid", buf);  psh_handle(psh_grid, dcb_grid);    /* handle the dialog */}  /* grid() *//*--------------------------------------------------------------------*/static void params (Widget w, XEvent *e, String *s, Cardinal *c){                               /* --- Settings > Parameters... */  char buf[32];                 /* buffer for numbers */  if (!psh_params) {            /* if property sheet not created yet */    psh_params = psh_create("paramDlg", w_top, 1);    if (!psh_params) return;     /* create a property sheet */    if ((psh_addline(psh_params, "lmode")               != 0)    ||  (psh_additem(psh_params, "mode",   PSH_MENU, 0) != 0)    ||  (psh_menuadd(psh_params, "mode", "single")      != 0)    ||  (psh_menuadd(psh_params, "mode", "all")         != 0)    ||  (psh_addline(psh_params, "ldelay")              != 0)    ||  (psh_additem(psh_params, "delay",  PSH_EDIT, 0) != 0)) {      psh_delete(psh_params); error(E_DIALOG); return; }  }                             /* set browse button callback */  mn_enable(menu, MN_MENU, 0);  /* disable menu bar */  psh_setval(psh_params, "mode", (char*)nnpar.mode);  sprintf(buf, "%g", nnpar.delay);      /* activation update mode */  psh_setval(psh_params, "delay", buf); /* delay between redraws */  psh_handle(psh_params, dcb_params);   /* handle the dialog */}  /* params() *//*--------------------------------------------------------------------*/static void about (Widget w, XEvent *e, String *s, Cardinal *c){                               /* --- `About' dialog box */  mn_enable(menu, MN_MENU, 0);  /* disable menu bar */  if (db_about(w_top, 0, dcb_nop) != 0)    error(E_DIALOG);            /* show `about' dialog box */}  /* about() *//*----------------------------------------------------------------------  Main Function----------------------------------------------------------------------*/int main (int argc, char *argv[]){                               /* --- main program */  Arg      args[8];             /* widget arguments */  int      n;                   /* argument counter */  Colormap colmap;              /* color map of display */  XColor   xc[9];               /* buffer for colors */  XtSetLanguageProc(NULL, (XtLanguageProc)NULL, NULL);  /* --- create main window --- */  w_top = XtAppInitialize(&appctx, "XHopf",            NULL, 0, &argc, argv, fallback, NULL, 0);  if (!w_top) error(E_INIT);    /* create toplevel widget */  n = 0;                        /* build argument list */  XtSetArg(args[n], XtNdefaultDistance, 0);    n++;  XtSetArg(args[n], XtNresizable,       True); n++;  w_main = XtCreateManagedWidget("main", formWidgetClass,             w_top, args, n);   /* create main widget */  if (!w_main) error(E_WIDGET); /* (form for window layout) */  menu = mn_create(w_main);     /* create a menu bar and */  if (!menu) error(E_NOMEM);    /* add titles and items */  if ((mn_addtitle(menu, "file",     0, 0,          0) != 0)  ||  (mn_additem (menu, "load",     1, mcb_load,   0) != 0)  ||  (mn_additem (menu, "save",     0, mcb_save,   0) != 0)  ||  (mn_additem (menu, NULL,       0, 0,          0) != 0)  ||  (mn_additem (menu, "quit",     0, mcb_quit,   0) != 0)  ||  (mn_addtitle(menu, "actions",  0, 0,          0) != 0)  ||  (mn_additem (menu, "clear",    0, mcb_clear,  0) != 0)  ||  (mn_additem (menu, NULL,       0, 0,          0) != 0)  ||  (mn_additem (menu, "next",     1, mcb_next,   0) != 0)  ||  (mn_additem (menu, "store",    1, mcb_store,  0) != 0)  ||  (mn_additem (menu, "delete",   1, mcb_delete, 0) != 0)  ||  (mn_additem (menu, NULL,       0, 0,          0) != 0)  ||  (mn_additem (menu, "init",     0, mcb_init,   0) != 0)  ||  (mn_additem (menu, "start",    2, mcb_start,  0) != 0)  ||  (mn_additem (menu, "stop",     4, mcb_start,  0) != 0)  ||  (mn_additem (menu, NULL,       0, 0,          0) != 0)  ||  (mn_additem (menu, "redraw",   0, mcb_redraw, 0) != 0)  ||  (mn_addtitle(menu, "settings", 0, 0,          0) != 0)  ||  (mn_additem (menu, "grid",     1, mcb_grid,   0) != 0)  ||  (mn_additem (menu, "params",   0, mcb_params, 0) != 0)  ||  (mn_addtitle(menu, "help",     0, 0,          0) != 0)  ||  (mn_additem (menu, "about",    0, mcb_about,  0) != 0)  ||  (mn_addtitle(menu, NULL,       0, 0,          0) != 0))    error(E_WIDGET);  mn_visible(menu, MN_ITEMS, 3);/* show/hide      menu items */  mn_enable (menu, MN_ITEMS, 3);/* enable/disable menu items */  /* --- create board viewport --- */  n = 0;                        /* build argument list */  XtSetArg(args[n], XtNfromVert,  mn_menubar(menu)); n++;  XtSetArg(args[n], XtNborderWidth, 1);           n++;  XtSetArg(args[n], XtNleft,      XtChainLeft);   n++;  XtSetArg(args[n], XtNtop,       XtChainTop);    n++;  XtSetArg(args[n], XtNright,     XtChainRight);  n++;  XtSetArg(args[n], XtNbottom,    XtChainBottom); n++;  XtSetArg(args[n], XtNresizable, True);          n++;  w_view = XtCreateManagedWidget("view",             viewportWidgetClass, w_main, args, n);  if (!w_view) error(E_WIDGET); /* create viewport */  /* --- create network widget --- */  n = 0;                        /* build argument list */  XtSetArg(args[n], XtNborderWidth, 0);             n++;  XtSetArg(args[n], XtNleft,        XtChainLeft);   n++;  XtSetArg(args[n], XtNtop,         XtChainTop);    n++;  XtSetArg(args[n], XtNright,       XtChainRight);  n++;  XtSetArg(args[n], XtNbottom,      XtChainBottom); n++;  XtSetArg(args[n], XtNresizable,   True);          n++;  w_net = XtCreateManagedWidget("network",             coreWidgetClass, w_view, args, n);  if (!w_net) error(E_WIDGET);  /* create network */  /* --- set widget extensions --- */  n = mn_minwidth(menu);        /* set minimal width and height */  XtVaSetValues(w_top, XtNminWidth,  (n > 8) ? n : 8,                       XtNminHeight, mn_height(menu) +4, NULL);  resize(nnpar.width, nnpar.height); /* set the window sizes */  XtOverrideTranslations(w_net,      /* install redraw function */    XtParseTranslationTable("<Expose>: redraw()"));  /* --- install actions, accelerators and translations --- */  XtAppAddActions(appctx, actions, XtNumber(actions));  XtInstallAllAccelerators(w_main, w_main);  /* --- create a Hopfield network --- */  hfn = hfn_create(nnpar.width, nnpar.height);  if (!hfn) error(E_NOMEM);     /* create a Hopfield network */  /* --- show window --- */  XtRealizeWidget(w_top);       /* show window on display */  display = XtDisplay(w_top);   /* get display of top window */  window  = XtWindow(w_net);    /* and drawable for window */  if (db_alert(w_top, dcb_nop, NULL) != 0)    error(E_WIDGET);            /* create alert dialog box */  /* --- override window manager delete --- */  XtOverrideTranslations(w_top, /* override window manager delete */    XtParseTranslationTable("<Message>WM_PROTOCOLS: quit()"));  wm_delwin = XInternAtom(XtDisplay(w_top), "WM_DELETE_WINDOW", False);  XSetWMProtocols(XtDisplay(w_top), XtWindow(w_top), &wm_delwin, 1);  /* --- allocate colors --- */  screen = DefaultScreen(display);  colmap = DefaultColormap(display, screen);  black  = BlackPixel(display, screen);  white  = WhitePixel(display, screen);  if (XAllocNamedColor(display, colmap, "grey48", xc+1, xc) == 0)    error(E_COLOR);             /* allocate colors and */  grey   = xc[1].pixel;         /* note color pixel values */  dseed((unsigned)time(NULL));  /* init. random number generator */  /* --- evaluate command line arguments --- */  if (argc > 1) dcb_load(NULL, (XtPointer)1, (XtPointer)argv[1]);  /* --- start event loop --- */  XtAppMainLoop(appctx);        /* enter main event loop */  return 0;                     /* return `ok' */}  /* main() */

⌨️ 快捷键说明

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