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

📄 gaview.c

📁 麻省理工开发的免费遗传算法类库GAlib,很好用
💻 C
📖 第 1 页 / 共 2 页
字号:
// on the value of the single flag.  It needs to know how much of a// buffer to use for spacing between individuals.  We assume that each // individual draws from its centroid.// This is much more nicely done when you derive your own genome that includes// a draw routine and all the graphics info in it, but for the purpose of this// example we'll just make a separate function that draws the genome and others// that return the graphics info about the genomes.#define BUF 10voidDrawCB(Widget w, XtPointer cd, XtPointer){  AppDataPtr data = (AppDataPtr)cd;  XClearWindow(XtDisplay(w), XtWindow(w));  if(data->whichGA == 3) {    GADemeGA* ga = (GADemeGA*)data->ga;    for(int i=0; i<ga->nPopulations(); i++)      DrawPopulation(w, ga->population(i), data->dotgc[i], data->dotgc[i]);  }  else {    DrawPopulation(w, data->ga->population(), data->dotgc[0], data->bestgc);  }}voidDrawPopulation(Widget widget, const GAPopulation& pop, GC dotgc, GC bestgc) {  static int npts = 0;  static XPoint* pts = 0;  if(npts != pop.size()) {    npts = pop.size();    delete [] pts;    pts = new XPoint [npts];  }  Dimension width = 0, height = 0;  XtVaGetValues(widget, XtNwidth, &width, XtNheight, &height, NULL);  Dimension w = width - 2 * BUF;  Dimension h = height - 2 * BUF;  Dimension d = (w < h ? w : h);  w -= d;  h -= d;  Dimension originx = BUF + w/2;  Dimension originy = BUF + h/2;  float factor = (float)d;  factor /= (maxx[theAppData.whichFunction] - minx[theAppData.whichFunction]);  int xbest = 0, ybest = 0;  if(theAppData.whichGenome == 1) {    for(int i=0; i<pop.size(); i++) {      pts[i].x = originx + d/2 + factor *	((GABin2DecGenome&)(pop.individual(i))).phenotype(0);      pts[i].y = originy + d/2 - factor * 	((GABin2DecGenome&)(pop.individual(i))).phenotype(1);    }    xbest = originx + d/2 + factor *      ((GABin2DecGenome&)(pop.best())).phenotype(0);    ybest = originy + d/2 - factor *      ((GABin2DecGenome&)(pop.best())).phenotype(1);  }  else {    for(int i=0; i<pop.size(); i++) {      pts[i].x = originx + d/2 + factor *	((GARealGenome&)(pop.individual(i))).gene(0);      pts[i].y = originy + d/2 - factor * 	((GARealGenome&)(pop.individual(i))).gene(1);    }    xbest = originx + d/2 + factor *      ((GARealGenome&)(pop.best())).gene(0);    ybest = originy + d/2 - factor *      ((GARealGenome&)(pop.best())).gene(1);  }  XDrawPoints(XtDisplay(widget), XtWindow(widget),	      dotgc, pts, npts, CoordModeOrigin);  XDrawPoint(XtDisplay(widget), XtWindow(widget), bestgc, xbest,   ybest);  XDrawPoint(XtDisplay(widget), XtWindow(widget), bestgc, xbest-1, ybest);  XDrawPoint(XtDisplay(widget), XtWindow(widget), bestgc, xbest,   ybest-1);  XDrawPoint(XtDisplay(widget), XtWindow(widget), bestgc, xbest+1, ybest);  XDrawPoint(XtDisplay(widget), XtWindow(widget), bestgc, xbest,   ybest+1);  XDrawPoint(XtDisplay(widget), XtWindow(widget), bestgc, xbest+1, ybest+1);  XDrawPoint(XtDisplay(widget), XtWindow(widget), bestgc, xbest+1, ybest-1);  XDrawPoint(XtDisplay(widget), XtWindow(widget), bestgc, xbest-1, ybest+1);  XDrawPoint(XtDisplay(widget), XtWindow(widget), bestgc, xbest-1, ybest-1);}#undef BUF// These are the objective functions for the genomes.  They simply call the// appropriate function.floatBin2DecObjective(GAGenome& g) {  GABin2DecGenome& genome = (GABin2DecGenome&)g;  return (obj[theAppData.whichFunction])(genome.phenotype(0),					 genome.phenotype(1));}floatRealObjective(GAGenome& g) {  GARealGenome& genome = (GARealGenome&)g;  return (obj[theAppData.whichFunction])(genome.gene(0), genome.gene(1));}/*****************************************************************************//* Type:        2D FUNCTION                                                  *//* Name:        Objective2D_1                                                *//* Description: 2D tooth                                                     *//* Boundaries:  -6 < x < 6                                                   *//*              -6 < y < 6                                                   *//* Source:      modified Himmelblau's function from Deb, K.                  *//*              'GA in multimodal function optimazation' Masters thesis      *//*		TCGA Rep. 89002 / U. of Alabama                              *//*****************************************************************************/floatFunction1(float x, float y) {  float z = -((x*x+y-11)*(x*x+y-11)+(x+y*y-7)*(x+y*y-7))/200 + 10;  return z;}/*****************************************************************************//* Type:        2D FUNCTION                                                  *//* Name:        Objective2D_2                                                *//* Description: Foxholes (25)                                                *//* Boundaries:  -60 < x < 60                                                 *//*              -60 < y < 60                                                 *//* Source:      Shekel's Foxholes problem from De Jong's Diss.(1975)         *//*              'GA in multimodal function optimazation' Masters thesis      *//*		TCGA Rep. 89002 / U. of Alabama                              *//*****************************************************************************/floatFunction2(float x, float y) {  int i;  float sum = 0;  for (i=0; i<25; i++) {    sum += (1 / (1 + i + pow((x-ai[i]),6) + pow((y-bi[i]),6)));  }  float z = 500.0 - (1 / (0.002 + sum));  return z;}/*****************************************************************************//* Type:        2D FUNCTION                                                  *//* Name:        Objective2D_3                                                *//* Description: Schwefel's nasty (4 glob. Max bei (+-420.96/+-420.96)        *//* Boundaries:  -500 < x < 500                                               *//*              -500 < y < 500                                               *//* Source:      Schwefel's function in Schoeneburg                           *//*****************************************************************************/floatFunction3(float x, float y) {  float z = fabs(x) * sin(sqrt(fabs(x))) + fabs(y) * sin(sqrt(fabs(y)));  return 500 + z;}/*****************************************************************************//* Type:        2D FUNCTION                                                  *//* Name:        Objective2D_4                                                *//* Description: Mexican Hat                                                  *//* Boundaries:  -10 < x < 10                                                 *//*              -10 < y < 10                                                 *//* Source:                                                                   *//*****************************************************************************/floatFunction4(float x, float y) {  float z = sin(sqrt(x*x + y*y))*sin(sqrt(x*x + y*y)) - 0.5;  z /= ((1.0 + 0.001*(x*x + y*y))*(1.0 + 0.001*(x*x + y*y)));  z = (0.5 - z);  return (z);}// Here are two versions of the graphic interface.  One version for those of// you with MOTIF on your systems, and one version for those of you with only// the athena widget set.  Sorry, no Windoze version yet...#ifdef USE_MOTIFWidgetConstructWidgets(Widget toplevel) {  Pixmap icon =    XCreateBitmapFromData(XtDisplay(toplevel),                          RootWindowOfScreen(XtScreen(toplevel)),                          (char *)gaview_bits, gaview_width, gaview_height);  Pixmap mask =    XCreateBitmapFromData(XtDisplay(toplevel),                          RootWindowOfScreen(XtScreen(toplevel)),                          (char *)gaview_bits, gaview_width, gaview_height);  Widget shell =    XtVaCreatePopupShell("shell", topLevelShellWidgetClass, toplevel, 			 XmNiconPixmap, icon,                         XmNiconMask, mask,			 NULL);  Widget form =     XtVaCreateManagedWidget("form", xmFormWidgetClass, shell, NULL);  Pixmap pix;  Pixel fg, bg;  unsigned int depth;  XtVaGetValues(form, XtNforeground, &fg, XtNbackground, &bg, 		XtNdepth, &depth, NULL);  pix =     XCreatePixmapFromBitmapData(XtDisplay(form),				RootWindowOfScreen(XtScreen(form)),				(char *)bm[bmRewind].bits,				bm[bmRewind].width, bm[bmRewind].height,				fg, bg, depth);  Widget rewind =     XtVaCreateManagedWidget("rewind", xmPushButtonWidgetClass, form,			    XmNleftAttachment, XmATTACH_FORM,			    XmNbottomAttachment, XmATTACH_FORM,			    XmNlabelType, XmPIXMAP,			    XmNlabelPixmap, pix,			    NULL);  pix =     XCreatePixmapFromBitmapData(XtDisplay(form),				RootWindowOfScreen(XtScreen(form)),				(char *)bm[bmStop].bits,				bm[bmStop].width, bm[bmStop].height,				fg, bg, depth);  Widget stop =     XtVaCreateManagedWidget("stop", xmPushButtonWidgetClass, form,			    XmNleftAttachment, XmATTACH_WIDGET,			    XmNleftWidget, rewind,			    XmNbottomAttachment, XmATTACH_FORM,			    XmNlabelType, XmPIXMAP,			    XmNlabelPixmap, pix,			    NULL);  pix =     XCreatePixmapFromBitmapData(XtDisplay(form),				RootWindowOfScreen(XtScreen(form)),				(char *)bm[bmForwardStop].bits,				bm[bmForwardStop].width,				bm[bmForwardStop].height,				fg, bg, depth);  Widget step =     XtVaCreateManagedWidget("step", xmPushButtonWidgetClass, form,			    XmNleftAttachment, XmATTACH_WIDGET,			    XmNleftWidget, stop,			    XmNbottomAttachment, XmATTACH_FORM,			    XmNlabelType, XmPIXMAP,			    XmNlabelPixmap, pix,			    NULL);  pix =     XCreatePixmapFromBitmapData(XtDisplay(form),				RootWindowOfScreen(XtScreen(form)),				(char *)bm[bmFastForwardStop].bits,				bm[bmFastForwardStop].width,				bm[bmFastForwardStop].height,				fg, bg, depth);  Widget some =     XtVaCreateManagedWidget("some", xmPushButtonWidgetClass, form,			    XmNleftAttachment, XmATTACH_WIDGET,			    XmNleftWidget, step,			    XmNbottomAttachment, XmATTACH_FORM,			    XmNlabelType, XmPIXMAP,			    XmNlabelPixmap, pix,			    NULL);  pix =     XCreatePixmapFromBitmapData(XtDisplay(form),				RootWindowOfScreen(XtScreen(form)),				(char *)bm[bmFastForward].bits,				bm[bmFastForward].width,				bm[bmFastForward].height,				fg, bg, depth);  Widget evolve =     XtVaCreateManagedWidget("evolve", xmPushButtonWidgetClass, form,			    XmNleftAttachment, XmATTACH_WIDGET,			    XmNleftWidget, some,			    XmNbottomAttachment, XmATTACH_FORM,			    XmNlabelType, XmPIXMAP,			    XmNlabelPixmap, pix,			    NULL);  XtAddCallback(rewind, XmNactivateCallback, ResetCB,      		(XtPointer)&theAppData);  XtAddCallback(stop,   XmNactivateCallback, StopCB,       		(XtPointer)&theAppData);  XtAddCallback(step,   XmNactivateCallback, StepCB,       		(XtPointer)&theAppData);  XtAddCallback(some,   XmNactivateCallback, EvolveSomeCB, 		(XtPointer)&theAppData);  XtAddCallback(evolve, XmNactivateCallback, EvolveCB,     		(XtPointer)&theAppData);  Widget params =     XtVaCreateManagedWidget("params", xmPushButtonWidgetClass, form,			    XmNrightAttachment, XmATTACH_FORM,			    XmNbottomAttachment, XmATTACH_FORM,			    NULL);  Widget stats =     XtVaCreateManagedWidget("stats", xmPushButtonWidgetClass, form,			    XmNrightAttachment, XmATTACH_WIDGET,			    XmNrightWidget, params,			    XmNbottomAttachment, XmATTACH_FORM,			    NULL);  Widget score =    XtVaCreateManagedWidget("score", xmPushButtonWidgetClass, form,			    XmNrightAttachment, XmATTACH_WIDGET,			    XmNrightWidget, stats,			    XmNbottomAttachment, XmATTACH_FORM,			    NULL);  XtAddCallback(params,  XmNactivateCallback, DumpParamsCB, 		(XtPointer)theAppData.ga);  XtAddCallback(stats,   XmNactivateCallback, DumpStatsCB,  		(XtPointer)theAppData.ga);  XtAddCallback(score,   XmNactivateCallback, DumpScoreCB,  		(XtPointer)theAppData.ga);  theAppData.counter =     XtVaCreateManagedWidget("counter", xmLabelWidgetClass, form,			    XmNtopAttachment, XmATTACH_FORM,			    XmNleftAttachment, XmATTACH_FORM,			    XmNrightAttachment, XmATTACH_FORM,			    XmNalignment, XmALIGNMENT_CENTER,			    NULL);  Widget frame =    XtVaCreateManagedWidget("frame", xmFrameWidgetClass, form,			    XmNtopAttachment, XmATTACH_WIDGET,			    XmNtopWidget, theAppData.counter,			    XmNrightAttachment, XmATTACH_FORM,			    XmNleftAttachment, XmATTACH_FORM,			    XmNbottomAttachment, XmATTACH_WIDGET,			    XmNbottomWidget, rewind,			    NULL);  theAppData.canvas =    XtVaCreateManagedWidget("canvas", xmDrawingAreaWidgetClass, frame,			    NULL);  XtAddCallback(theAppData.canvas, XmNexposeCallback, DrawCB, 		(XtPointer)&theAppData);  XtGCMask gcvalmask = GCFunction | GCForeground;  XGCValues gcval;  gcval.function = GXcopy;  gcval.foreground = theAppData.bestcolor;  theAppData.bestgc =     XCreateGC(XtDisplay(toplevel), RootWindowOfScreen(XtScreen(toplevel)),	      gcvalmask, &gcval );  for(int kk=0; kk<MAX_POPS; kk++) {    gcval.foreground = theAppData.popcolor[kk];    theAppData.dotgc[kk] =       XCreateGC(XtDisplay(toplevel), RootWindowOfScreen(XtScreen(toplevel)),		gcvalmask, &gcval );  }  return shell;}#elsevoidExposureEH(Widget w, XtPointer cd, XEvent*, Boolean*) {  DrawCB(w,cd,0);}WidgetConstructWidgets(Widget toplevel) {  Pixmap icon =    XCreateBitmapFromData(XtDisplay(toplevel),                          RootWindowOfScreen(XtScreen(toplevel)),                          (char *)gaview_bits, gaview_width, gaview_height);  Pixmap mask =    XCreateBitmapFromData(XtDisplay(toplevel),                          RootWindowOfScreen(XtScreen(toplevel)),                          (char *)gaview_bits, gaview_width, gaview_height);  XtVaSetValues(toplevel, XtNiconPixmap, icon, XtNiconMask, mask, NULL);  Widget form =     XtVaCreateManagedWidget("form", formWidgetClass, toplevel, NULL);  theAppData.counter =     XtVaCreateManagedWidget("counter", labelWidgetClass, form,			    NULL);  theAppData.canvas =    XtVaCreateManagedWidget("canvas", widgetClass, form,			    XtNfromVert, theAppData.counter,			    NULL);  XtAddEventHandler(theAppData.canvas, ExposureMask, False,		    ExposureEH, (XtPointer)&theAppData);  Widget ctrlbox =     XtVaCreateManagedWidget("controls", boxWidgetClass, form, 			    XtNfromVert, theAppData.canvas,			    XtNorientation, "vertical",			    NULL);  Widget rewind =     XtVaCreateManagedWidget("rewind", commandWidgetClass, ctrlbox, NULL);  Widget stop =     XtVaCreateManagedWidget("stop", commandWidgetClass, ctrlbox, NULL);  Widget step =     XtVaCreateManagedWidget("step", commandWidgetClass, ctrlbox, NULL);  Widget some =     XtVaCreateManagedWidget("some", commandWidgetClass, ctrlbox, NULL);  Widget evolve =     XtVaCreateManagedWidget("evolve", commandWidgetClass, ctrlbox, NULL);  XtAddCallback(rewind, XtNcallback, ResetCB,      (XtPointer)&theAppData);  XtAddCallback(stop,   XtNcallback, StopCB,       (XtPointer)&theAppData);  XtAddCallback(step,   XtNcallback, StepCB,       (XtPointer)&theAppData);  XtAddCallback(some,   XtNcallback, EvolveSomeCB, (XtPointer)&theAppData);  XtAddCallback(evolve, XtNcallback, EvolveCB,     (XtPointer)&theAppData);  Widget params =     XtVaCreateManagedWidget("params", commandWidgetClass, ctrlbox, NULL);  Widget stats =     XtVaCreateManagedWidget("stats", commandWidgetClass, ctrlbox, NULL);  Widget score =     XtVaCreateManagedWidget("score", commandWidgetClass, ctrlbox, NULL);  XtAddCallback(params,  XtNcallback, DumpParamsCB, (XtPointer)theAppData.ga);  XtAddCallback(stats,   XtNcallback, DumpStatsCB,  (XtPointer)theAppData.ga);  XtAddCallback(score,   XtNcallback, DumpScoreCB,  (XtPointer)theAppData.ga);  Widget quit =     XtVaCreateManagedWidget("quit", commandWidgetClass, ctrlbox, NULL);  XtAddCallback(quit,   XtNcallback, QuitCB,  (XtPointer)0);  XtGCMask gcvalmask = GCFunction | GCForeground;  XGCValues gcval;  gcval.function = GXcopy;  gcval.foreground = theAppData.bestcolor;  theAppData.bestgc =     XCreateGC(XtDisplay(toplevel), RootWindowOfScreen(XtScreen(toplevel)),	      gcvalmask, &gcval );  for(int kk=0; kk<MAX_POPS; kk++) {    gcval.foreground = theAppData.popcolor[kk];    theAppData.dotgc[kk] =       XCreateGC(XtDisplay(toplevel), RootWindowOfScreen(XtScreen(toplevel)),		gcvalmask, &gcval );  }  return toplevel;}#endif

⌨️ 快捷键说明

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