#include #include #include #include "gene.h" static Window window; static Pixmap backmap; static unsigned int width, height; static short TaskTable[NJobs][NMachines]; static unsigned long JobColor[NJobs]; static char *colorNames[] = { "red", "green", "blue", "magenta", "yellow", "cyan", "white", "gray", "orange", "pink", "violet", "SkyBlue", NULL }; extern Display *dpy; extern int scr; extern GC gc; extern unsigned int Drawfg, Drawbg; extern double drand48(); fitness(ind) individual *ind; { int i, j, m, job, mch, tm, te, tlatest = 0; for (m = 0; m < NMachines; m ++) for (j = 0; j < NJobs; j ++) ind->body.time[j][m] = -32768; for (i = 0; i < NTasks; i ++) { job = ind->chromosome.task[i].job; mch = ind->chromosome.task[i].machine; for (tm = m = 0; m < NMachines; m ++) if (m != mch) { te = ind->body.time[job][m] + TaskTable[job][m]; if (tm < te) tm = te; } for (j = 0; j < NJobs; j ++) if (j != job) { te = ind->body.time[j][mch] + TaskTable[j][mch]; if (tm < te) tm = te; } ind->body.time[job][mch] = tm; te = tm + TaskTable[job][mch]; if (tlatest < te) tlatest = te; } ind->fitness = (double)100./tlatest; ind->modified = False; } redisplay(e) XExposeEvent *e; { XCopyArea(dpy,backmap,window,gc,e->x,e->y, e->width,e->height,e->x,e->y); } gene *random_gene() { int i, j, m; Task c; static gene new; for (i = m = 0; m < NMachines; m ++) for (j = 0; j < NJobs; j ++, i++) { new.task[i].job = (char)j; new.task[i].machine = (char)m; } for (i = 0; i < NTasks - 1; i ++) { j = (lrand48() % (NTasks - i - 1)) + i + 1; if (j != i) { c = new.task[i]; new.task[i] = new.task[j]; new.task[j] = c; } } return &new; } domain_init(win) Window win; { Window root; Colormap cmap = DefaultColormap(dpy,scr); XColor scol, ecol; int x, y; unsigned int border, depth; window = win; XGetGeometry(dpy,window,&root,&x,&y,&width,&height,&border,&depth); if (depth < 8) { fprintf(stderr,"Sorry, this program works only on depth >= 8.\n"); exit(1); } backmap = XCreatePixmap(dpy,window,width,height,depth); scol.flags = ecol.flags = DoRed | DoGreen | DoBlue; for (x = 0; x < NJobs && colorNames[x]; x ++) { XAllocNamedColor(dpy,cmap,colorNames[x],&scol,&ecol); JobColor[x] = scol.pixel; } } init_table() { int j, m; for (m = 0; m < NMachines; m ++) for (j = 0; j < NJobs; j ++) TaskTable[j][m] = (lrand48() % 6) + 1; } static void draw_individual(id,x,y) int id, x, y; { int j, m, tm, te, u, v, w, h = height/3/NMachines-2; int ww = width / 3 - 5; individual *ind = (individual *)get_ith_individual(id); te = 0; for (j = 0; j < NJobs; j ++) for (m = 0; m < NMachines; m ++) { tm = ind->body.time[j][m] + TaskTable[j][m]; if (te < tm) te = tm; } for (j = 0; j < NJobs; j ++) { XSetForeground(dpy,gc,JobColor[j]); for (m = 0; m < NMachines; m ++) { u = ww * ind->body.time[j][m] / te + x; w = ww * TaskTable[j][m] / te; v = h * m + y; XFillRectangle(dpy,backmap,gc,u,v,w,h); } } } domain_draw() { int i, j; XSetForeground(dpy,gc,Drawbg); XFillRectangle(dpy,backmap,gc,0,0,width,height); XSetForeground(dpy,gc,Drawfg); for (i = 0; i < 3; i ++) for (j = 0; j < 3; j ++) draw_individual(i*3+j,j*width/3,i*height/3); XCopyArea(dpy,backmap,window,gc,0,0,width,height,0,0); } fitness_string(value,str) double value; char *str; { sprintf(str,"%.3f",value); }