GAの探索過程表示プログラム
/* The Program that displays the process of GA */
/* by K. Tatsumi 2005.4.1 */
//
// gcc gant.c -lm -L/usr/X11R6/lib -lX11 -I/usr/X11R6/include
// 2005/4/28 K.Tatsumi
#include
#include
#include
#include
#include
#include
#include
#include
#define SCALE 30 // the maximum unit size in the coordinate axis
#define ORG 5 // the origin of the chart
int max_fit=80;
int min_fit=20;
Display *gDisp;
int gScreen;
Font gFont;
XEvent gEv;
// Attributes of GC
XSetWindowAttributes gGcAtt;
// base GC
GC gGc;
//
// Select Color
//
unsigned long XW_RGB( int r, int g, int b )
{
Colormap cmap;
XColor col;
cmap = DefaultColormap( gDisp, 0 );
col.red = 256 * r;
col.green = 256 * g;
col.blue = 256 * b;
XAllocColor( gDisp, cmap, &col );
return col.pixel ;
}
void XW_SetColor( int r, int g, int b )
{
XSetForeground( gDisp, gGc, XW_RGB( r, g, b) );
}
// Open window
void XW_EndXWindow( Window gWin)
{
XFreeGC( gDisp, gGc );
XDestroyWindow( gDisp, gWin );
}
void XW_Text( Window gWin,char *str, int x, int y )
{
XDrawString( gDisp, gWin, gGc, x, y, str, strlen(str) );
}
void XW_Line( Window gWin, int x1, int y1, int x2, int y2 )
{
XSetLineAttributes( gDisp, gGc, 1, LineSolid, CapRound, JoinRound );
XDrawLine( gDisp, gWin, gGc, x1, y1, x2, y2 );
}
void XW_BoxFill(Window gWin, int x1, int y1, int x2, int y2 )
{
XSetLineAttributes( gDisp, gGc, 1, LineSolid, CapRound, JoinRound );
XFillRectangle( gDisp, gWin, gGc, x1, y1, x2-x1, y2-y1 );
}
int displayGA(){
int ds_pop;
int gx=8;
int gy=8;
int *ds_fitness, x, y;
char string[10];
Window gWin;
Pixmap gPmap;
KeySym key;
int gf_y, gf_x;
int offsety = 20;
int offsetx = ORG;
int t =1;
ds_pop = gx * gy;
gf_x = (SCALE)*gx+ORG+offsetx;
gf_y = (SCALE)*gy+ORG+offsety;
ds_fitness=(int *)malloc(sizeof(int)*gx*gy);
for(x=0; x<gx; x++){
for(y=0; y<gy; y++){
*(ds_fitness+x*gx+y)=(int)((rand()/(double)RAND_MAX)*(max_fit - min_fit -10 )) + min_fit +10;
printf("%d ", *(ds_fitness+x*gx+y));
}
}
gDisp = XOpenDisplay( NULL ); /* X$B%5!<%P$K@\B3$9$k(B */
gScreen = DefaultScreen( gDisp ); /* $B%9%/%j!<%s@_Dj(B */
gWin = XCreateSimpleWindow( /* $B%&%#%s%I%&$r3+$/(B */
gDisp, /* X$B%5!<%P(B */
RootWindow(gDisp,gScreen), /* $B?F%&%#%s%I%&(B */
0, 0, /* $BI=<(;~$N:8>e6y$N:BI8(B x, y */
gf_x, gf_y, /* $B%&%#%s%I%&$NI}$H9b$5(B */
// gdata.height, wide, /* $B%&%#%s%I%&$NI}$H9b$5(B */
1, BlackPixel(gDisp,gScreen),/* $B%\!<%@!<$NI}$H?'(B */
WhitePixel(gDisp,gScreen) /* $B%&%#%s%I%&$NGX7J?'(B */
);
gPmap = XCreatePixmap(gDisp, gWin, gf_x, gf_x,
DefaultDepth(gDisp, 0));
XSetStandardProperties(gDisp, gWin, "Gantt chart -- After GA", "CanttChart -- After GA",
None, NULL, 0, NULL);
XSelectInput( gDisp, gWin, ExposureMask |
KeyPressMask );
XMapWindow( gDisp, gWin ); /* $B%&%#%s%I%&$N%^%C%T%s%0(B */
// XW_initWin(gWin, (gdata.x_size)+(offset), all_y*(SCALE));
gGc = DefaultGC( gDisp, gScreen ); /* $B%0%i%U%#%C%/%3%s%F%-%9%H$N@_Dj(B */
XSetForeground( gDisp, gGc, BlackPixel(gDisp,gScreen) ); /* $BIA2h?'@_Dj(B */
XSetBackground( gDisp, gGc, WhitePixel(gDisp,gScreen));
while( 1 ){
XNextEvent( gDisp, &gEv );
switch( gEv.type ){
case Expose:
XW_SetColor( 255,255,255 ); // $BGr?'(B
XW_BoxFill( gPmap, 0, 0, gf_x, gf_y);
for(x=0; x<gx; x++){
for(y=0; y<gy; y++){
*(ds_fitness+x*gx+y)=(int)((rand()/(double)RAND_MAX)*((100 - t) + max_fit - min_fit -10 )) + min_fit +10 -100 +t;
printf("%d ", *(ds_fitness+x*gx+y));
}
}
XW_SetColor( 0, 0, 0 ); // $BGr?'(B
show_ftnss(gPmap, ds_fitness, gx, gy, t);
sprintf(string, "gene = %d", t);
XW_Text( gWin, string, (gf_x)/2 -30, (gf_y - 5));
// Drawing the chart
// Draw_axes(gPmap, &pb_size, &gdata, k);
// show_description(gPmap, color, &gdata, pb_size.j_num);
XCopyArea(gDisp, gPmap, gWin, gGc, 0, 0, gf_x, gf_y, 0, 0);
XFlush(gDisp);
case KeyPress:
XLookupString((XKeyEvent *)&gEv, string, sizeof(string), &key, NULL);
if ( string[0] == 'q' ){
goto FINISH;
}
if ( string[0] == 'n' ){
XW_SetColor( 255,255,255 ); // $BGr?'(B
XW_BoxFill( gPmap, 0, 0, gf_x, gf_y);
/* $B$3$3$G%i%s%@%`$K!!(B36$B8D(Bds_ftnss $B$rM?$($F$$$k!!(B*/
for(x=0; x<gx; x++){
for(y=0; y<gy; y++){
*(ds_fitness+x*gx+y)=(int)((rand()/(double)RAND_MAX)*((100 - t) + max_fit - min_fit -10 )) + min_fit +10 -100 +t;
printf("%d ", *(ds_fitness+x*gx+y));
}
}
/* $B$3$3$^$G!!(B*/
XW_SetColor( 255, 0, 0 ); // $BGr?'(B
show_ftnss(gPmap, ds_fitness, gx, gy, t);
sprintf(string, "gene = %d", t);
XW_Text( gWin, string, (gf_x)/2, (gf_y - 5));
t++;
XCopyArea(gDisp, gPmap, gWin, gGc, 0, 0, gf_x, gf_y, 0, 0);
XFlush(gDisp);
}
break;
default:
;
}
}
FINISH:
XW_EndXWindow(gWin); // End of Drawing
return 0;
}
int show_ftnss(Window gWin, int *ds_fitness, int gx, int gy, int t){
int x, y;
char strg[10];
for(x=0; x<gx; x++){
for(y=0; y<gy; y++){
XW_SetColor( (int)(255*((*(ds_fitness+gx*x+y) - min_fit)/(double)(max_fit - min_fit))),//
(int)(255*((*(ds_fitness+gx*x+y) - min_fit)/(double)(max_fit - min_fit))),//
(int)(255*((max_fit - *(ds_fitness+gx*x+y) + min_fit)/(double)(max_fit - min_fit))));
XW_BoxFill( gWin, ORG+ x*(SCALE), ORG + y*(SCALE), //
(ORG)+ (x+1)*(SCALE), ORG + (y+1)*(SCALE));
printf("|%d%d|", x, y);
XW_SetColor( 255,255,255 ); // $BGr?'(B
sprintf(strg, "%d", *(ds_fitness+x*gx+y));
XW_Text( gWin, strg, ORG+(x+0.4)*(SCALE), (y+0.4)*(SCALE)+(ORG));
}
}
return 0;
}
main(){
displayGA();
}