📄 xhopf.c
字号:
/*---------------------------------------------------------------------- File : xhopf.c Contents: Hopfield network as associative memory Author : Christian Borgelt History : 2002.12.01 file created from file xsom.c 2003.01.22 mouse event handling improved----------------------------------------------------------------------*/#include <stdio.h>#include <stdlib.h>#include <stdarg.h>#include <string.h>#include <math.h>#include <time.h>#include <assert.h>#include <X11/Intrinsic.h>#include <X11/StringDefs.h>#include <X11/Core.h>#include <X11/Shell.h>#include <X11/Xaw/Form.h>#include <X11/Xaw/Label.h>#include <X11/Xaw/Command.h>#include <X11/Xaw/Viewport.h>#include <X11/Xaw/AsciiText.h>#include "menu.h"#include "dialog.h"#include "fselect.h"#include "hopf.h"/*---------------------------------------------------------------------- Preprocessor Definitions----------------------------------------------------------------------*//* --- error codes --- */#define OK 0 /* no error */#define E_NONE 0 /* no error */#define E_NOMEM (-1) /* not enough memory */#define E_FOPEN (-2) /* cannot open file */#define E_FREAD (-3) /* read error on file */#define E_FWRITE (-4) /* write error on file */#define E_INIT (-5) /* initialization failed */#define E_WIDGET (-6) /* widget creation failed */#define E_DIALOG (-7) /* cannot create dialog box */#define E_COLOR (-8) /* color allocation failed */#define E_GRAPH (-9) /* cannot create graphics context */#define E_PATCNT (-10) /* pattern capacity exceeded */#define E_UNKNOWN (-11) /* unkown error */#define FMT_MAX 256 /* max. len. of error message format *//*---------------------------------------------------------------------- Type Definitions----------------------------------------------------------------------*/typedef struct { /* --- error message data --- */ const char *name, *type; /* name and type of the error message */ const char *class; /* resource class of error message */ const char *dflt; /* default message */ int argc; /* number of arguments */} ERRMSG; /* (error message) *//*---------------------------------------------------------------------- Action Function Prototypes----------------------------------------------------------------------*/static void mouse (Widget w, XEvent *e, String *s, Cardinal *c);static void load (Widget w, XEvent *e, String *s, Cardinal *c);static void save (Widget w, XEvent *e, String *s, Cardinal *c);static void quit (Widget w, XEvent *e, String *s, Cardinal *c);static void clear (Widget w, XEvent *e, String *s, Cardinal *c);static void store (Widget w, XEvent *e, String *s, Cardinal *c);static void delete (Widget w, XEvent *e, String *s, Cardinal *c);static void next (Widget w, XEvent *e, String *s, Cardinal *c);static void init (Widget w, XEvent *e, String *s, Cardinal *c);static void exec (Widget w, XEvent *e, String *s, Cardinal *c);static void redraw (Widget w, XEvent *e, String *s, Cardinal *c);static void grid (Widget w, XEvent *e, String *s, Cardinal *c);static void params (Widget w, XEvent *e, String *s, Cardinal *c);static void about (Widget w, XEvent *e, String *s, Cardinal *c);/*---------------------------------------------------------------------- Constants----------------------------------------------------------------------*/#include "xhopf.rsc" /* fallback resources */static XtActionsRec actions[] = { { "mouse", mouse }, /* mouse movement */ { "load", load }, /* File > Load Map... */ { "save", save }, /* File > Save Map... */ { "quit", quit }, /* File > Quit */ { "clear", clear }, /* Actions > Clear */ { "next", next }, /* Actions > Next Pattern */ { "store", store }, /* Actions > Store Pattern */ { "delete", delete }, /* Actions > Delete Pattern */ { "init", init }, /* Actions > Initialize */ { "redraw", redraw }, /* Actions > Redraw */ { "exec", exec }, /* Actions > Start/Stop Training */ { "grid", grid }, /* Settings > Grid... */ { "params", params }, /* Settings > Parameters... */ { "about", about }, /* Help > About XSOM... */ { "db_focus", db_focus }, /* set focus to input gadget */ { "db_next", db_next }, /* switch to next input gadget */ { "db_close", db_close } }; /* close dialog box *//* --- error messages --- */static const ERRMSG errmsgs[] = { /* error message data */ /* E_NONE 0 */ { "error", "none", "XHopf.Error", "no error", 0 }, /* E_NOMEM -1 */ { "error", "nomem", "XHopf.Error", "not enough memory", 0 }, /* E_FOPEN -2 */ { "warning", "fopen", "XHopf.Warning", "cannot open file:\n%s", 1 }, /* E_FREAD -3 */ { "warning", "fread", "XHopf.Warning", "read error on file:\n%s", 1 }, /* E_FWRITE -4 */ { "warning", "fwrite", "XHopf.Warning", "write error on file:\n%s", 1 }, /* E_INIT -5 */ { "error", "init", "XHopf.Error", "initialization failed", 0 }, /* E_WIDGET -6 */ { "error", "widget", "XHopf.Error", "widget creation failed", 0 }, /* E_DIALOG -7 */ { "warning", "dialog", "XHopf.Warning", "cannot create dialog box", 0 }, /* E_COLOR -8 */ { "warning", "color", "XHopf.Warning", "color allocation failed", 0 }, /* E_GRAPH -9 */ { "error", "gc", "XHopf.Error", "cannot create graphics context", 0 }, /* E_PATCNT -10 */ { "warning", "patcnt", "XHopf.Warning", "pattern capacity exceeded", 0 }, /* E_UNKNOWN -11 */ { "error", "unknown", "XHopf.Error", "unknown error", 0 }};/*---------------------------------------------------------------------- Global Variables----------------------------------------------------------------------*/static XtAppContext appctx; /* X11 application context */static Display *display; /* display connected to */static int screen; /* screen of the display */static Window window; /* main window */static Atom wm_delwin; /* delete window atom of window mgr. */static char fmt[FMT_MAX]; /* format and buffer for messages */static char buf[PATH_MAX +FMT_MAX];/* --- main widgets and menu --- */static Widget w_top; /* top level widget */static Widget w_main; /* main window widget */static Widget w_view; /* viewport widget */static Widget w_net; /* network widget */static MENU *menu; /* pull down menu */static GC gc; /* graphics context for drawing *//* --- property sheets --- */static PSHEET *psh_grid = NULL; /* grid and shape */static PSHEET *psh_params = NULL; /* learning parameters *//* --- color information --- */static unsigned long black; /* black and white */static unsigned long white; /* for normal drawing */static unsigned long grey; /* grey for background shape *//* --- neural network information --- */static struct { /* -- parameters -- */ int width, height; /* width and height of network */ int grid; /* grid width (in pixels) */ int mode; /* update mode */ int curr; /* current pattern */ double delay; /* delay (in seconds) */} nnpar = { 10, 12, 24, 0, -1, 0.01 };static HOPFNET *hfn = NULL; /* Hopfield network */static int running = 0; /* whether computations are running */static XtIntervalId iid; /* id of time interval procedure *//*---------------------------------------------------------------------- Error Reporting Functions----------------------------------------------------------------------*/static void dcb_nop (Widget widget, XtPointer client, XtPointer call){ /* --- close dialog box (callback) */ while (!XtIsTransientShell(widget)) widget = XtParent(widget); /* find the shell widget */ XtPopdown(widget); /* close the dialog box */ mn_enable(menu, MN_MENU, 1); /* and enable the menu bar */} /* dcb_nop() *//*--------------------------------------------------------------------*/static void error (int code, ...){ /* --- print error message */ va_list va; /* list of variable arguments */ const ERRMSG *msg; /* error message */ String argv[8]; /* variable argument vector */ Cardinal argc; /* number of arguments */ if ((code > 0) || (code < E_UNKNOWN)) code = E_UNKNOWN; /* check error code */ msg = errmsgs -code; /* get error message data */ if (!msg->name) msg = errmsgs -E_UNKNOWN; va_start(va, code); /* get list of variable arguments */ for (argc = 0; (int)argc < msg->argc; argc++) argv[argc] = va_arg(va, String); va_end(va); /* get message arguments */ if (strcmp(msg->name, "error") == 0) /* if name is ``error'' */ XtAppErrorMsg(appctx, msg->name, msg->type, msg->class, msg->dflt, argv, &argc); /* print error message */ else { /* if name is ``warning'' */ XtAppGetErrorDatabaseText(appctx, msg->name, msg->type, msg->class, msg->dflt, fmt, FMT_MAX, NULL); va_start(va, code); /* list of variable arguments */ vsprintf(buf, fmt, va); /* get and format message */ va_end(va); /* end variable argument evaluation */ mn_enable(menu, MN_MENU,0); /* disable menu bar and show dialog */ if (db_alert(w_top, dcb_nop, buf) != 0) { mn_enable(menu, MN_MENU, 1); /* enable menu bar */ XtAppWarningMsg(appctx, msg->name, msg->type, msg->class, msg->dflt, argv, &argc);/* if the alert dialog box */ } /* cannot be shown, */ } /* print a warning message */} /* error() *//*--------------------------------------------------------------------*/static void resize (int width, int height){ /* --- resize widgets */ Dimension w, h; /* width and height of top widget */ width *= nnpar.grid; /* compute the extensions */ height *= nnpar.grid; /* in pixels */ w = mn_minwidth(menu)-1; /* compute board size in pixels and */ h = mn_height(menu) +height; /* height of interior of top wiget */ if (width > w) w = width; /* get minimal width of board widget */ XtVaSetValues(w_top, XtNwidth, w+1, XtNheight, h+1, XtNmaxWidth, w+1, XtNmaxHeight, h+1, NULL); XtVaSetValues(w_net, XtNwidth, w-1, XtNheight, height-1, NULL); XtVaSetValues(w_view, XtNwidth, w-1, XtNheight, height-1, NULL); mn_resize(menu, w); /* set new widget extensions */} /* resize() *//*---------------------------------------------------------------------- Random Number Functions----------------------------------------------------------------------*/#ifdef DRAND48 /* if library for drand48() available */extern void srand48 (long seed);extern double drand48 (void); /* use drand48 functions */#define dseed(s) srand48((long)(s))#define drand drand48#else /* if only standard rand() available */#define dseed(s) srand((unsigned)(s))static double drand (void){ return rand()/(RAND_MAX +1.0); }#endif/*---------------------------------------------------------------------- Dialog Box Callback Functions----------------------------------------------------------------------*/static void dcb_load (Widget widget, XtPointer ok, XtPointer fname){ /* --- 'Load Map...' callback */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -