1 /* ************************************************************************* * 2 bugsx - (C) Copyright 1990-1997 Joshua R. Smith (jrs@media.mit.edu) 3 http://physics.www.media.mit.edu/~jrs 4 5 (C) Copyright 1995-1997 Robert Gasch (Robert_Gasch@peoplesoft.com) 6 http://www.peoplesoft.com/peoplepages/g/robert_gasch/index.htm 7 8 Permission to use, copy, modify and distribute this software for any 9 purpose and without fee is hereby granted, provided that this copyright 10 notice appear in all copies as well as supporting documentation. All 11 work developed as a consequence of the use of this program should duly 12 acknowledge such use. 13 14 No representations are made about the suitability of this software for 15 any purpose. This software is provided "as is" without express or implied 16 warranty. 17 18 See the GNU General Public Licence for more information. 19 * ************************************************************************* */ 20 21 22 23 /* ****** include files ****** */ 24 #include <X11/Xlib.h> 25 #include <X11/Xutil.h> 26 #include <X11/Xos.h> 27 #include <X11/Xresource.h> 28 #include <math.h> 29 #ifndef DYNIX 30 # include <stdlib.h> 31 #endif 32 #ifdef __STDC__ 33 # include <stdarg.h> 34 #else 35 # include <varargs.h> 36 #endif 37 #include <stdio.h> 38 39 40 /* ******************************************************************** */ 41 /* ******************** general program constants ********************* */ 42 /* ******************************************************************** */ 43 #define TRUE 1 44 #define FALSE 0 45 #define STRLENGTH 255 /* length of a string */ 46 #define NOGOOD -1 /* to distinguish nogood from false */ 47 #define MAXFS 50 /* maximum number of file systems */ 48 #define QUIT_KEY 'q' /* quit key */ 49 #define INIT_KEY 'i' /* initialize key */ 50 #define BREED_KEY 'b' /* breed key */ 51 #define CLASS_NAME "Bugsx" /* How do we look for resources */ 52 #define DEFAULT_FONT "6x13" /* default font */ 53 #define UPDINTERVAL 5 54 #define MSTIME 100000L /* sleep time for msleep () */ 55 56 #ifndef APP_DEFAULTS_DIR 57 #define APP_DEFAULTS_DIR "/usr/X11R6/lib/X11/app-defaults/" 58 #endif 59 60 /* well, I did write the damn thing */ 61 #define PRINT_COPYRIGHT printf ("\ 62 bugsx v1.08 - (C) Copyright 1990-1997 Joshua R. Smith (jrs@media.mit.edu)\n\ 63 http://physics.www.media.mit.edu/~jrs \n\ 64 (C) Copyright 1995-1997 Robert Gasch (Robert_Gasch@peoplesoft.com)\n\ 65 http://www.peoplesoft.com/peoplepages/g/robert_gasch/index.htm") 66 67 68 /* ****** define window constants - these are positions and sizes ****** */ 69 #define DEF_BORDER_WIDTH 4 /* Default border width. */ 70 #define MAIN_BORDER 20 71 #define WIN_X 700 72 #define WIN_Y 700 73 #define MIN_WIN_X 200 74 #define MIN_WIN_Y 200 75 #define MENU_ITEMS 3 76 #define MENU_Y 10 77 #define OFF_X 10 78 #define OFF_Y 10 79 #define DRAW_ROWS 4 80 #define DRAW_COLUMNS 4 81 #define DRAW_WINS (DRAW_ROWS*DRAW_COLUMNS) 82 #define MAX_DRAW_WINS (10*10) 83 #define MENU_HEIGHT(w) (LETTER_HEIGHT(w)) 84 #define MIN_MENU_HEIGHT 4 85 #define LETTER_HEIGHT(w) ((w).font_info->ascent + (w).font_info->descent) 86 #define LETTER_ASCENT(w) ((w).font_info->ascent) 87 #define LETTER_SPACE(w) (LETTER_HEIGHT(w) + 5 ) 88 #define FREE_LETTER_SP(w) (LETTER_SPACE(w)-LETTER_HEIGHT(w)) 89 #define NFS_TEXT_Y (fs_win[i].y-(LETTER_SPACE(fs_win[i])-\ 90 (LETTER_HEIGHT(fs_win[i])+1))) 91 #define SM_MENU_WIDTH(w) (XTextWidth((w).font_info, (w).text, \ 92 strlen((w).text))) 93 #define MENU_SPACE(w) (MENU_HEIGHT(w)*(MENU_ITEMS+2)) 94 #define INTERVAL(w) ((MENU_HEIGHT(w)+FREE_LETTER_SP(w))*2) 95 #define MIN_INTERVAL(w) (MIN_MENU_HEIGHT+FREE_LETTER_SP(w)+\ 96 MENU_HEIGHT(w)) 97 #define DRAW_WIDTH ((main_win.width-(MAIN_BORDER*2))/Draw_Columns) 98 #define DRAW_HEIGHT ((main_win.height-(MAIN_BORDER*2)-MENU_SPACE(menu[0]))/\ 99 Draw_Rows) 100 101 #define MAX_CHROM_SIZE 30 /* *** chromosome can have up to 30 genes *** */ 102 #define MAX_POP 100 /* *** Population can be up to 100 *** */ 103 #define ORG_X 4 /* *** # orgs horizontally *** */ 104 #define ORG_Y 4 /* *** # orgs vertically *** */ 105 #define INIT_POP (ORG_X*ORG_Y) /* *** Initial population *** */ 106 107 108 /* *** defines, typedefs, and externs needed by genetic algorithm *** */ 109 /* *** This should contain almost all the parameters which are *** */ 110 /* *** interesting to play with. Do a 'make' after changing them, *** */ 111 #define WEIGHT_BASE 1.4 /* *** How much of curve we see *** */ 112 #define ORG_T_MIN 0.0 /* *** Length of organism *** */ 113 #define ORG_T_MAX (8*3.14159265358979323846) 114 #define ORG_X_MIN ((double)(DRAW_WIDTH/2*-1))/* Coordinate range for organism window */ 115 #define ORG_X_MAX (double)(DRAW_WIDTH/2) 116 #define ORG_Y_MIN ((double)(DRAW_HEIGHT/2*-1)) 117 #define ORG_Y_MAX (double)(DRAW_HEIGHT/2) 118 #define GENE_BORDER 10 119 #define ORG_SEGMENTS (150.0) /* *** # of segments used drawing org *** */ 120 #define CHROM_WIND 0.1 /* *** Fraction of window to draw genes *** */ 121 #define CHROM_OFFSET 10 /* *** How much to offset bargraphs *** */ 122 #define INIT_CHROM 8 /* *** Initial chromosome size *** */ 123 #define pCROSS 1.0 /* *** Probability of Crossover *** */ 124 #define pMUTATION 0.067 /* *** Probability of Mutation *** */ 125 #define MUTATION_STD 0.01 /* *** Controls size of mutations *** */ 126 #define INIT_SWITCH_DEF 0 /* *** Whether switches start on or off *** */ 127 #define INIT_SHOW_GENES 1 /* *** Whether gene window is on *** */ 128 #define INIT_PRINT_OUT 0 /* *** Whether print out is enabled *** */ 129 #define MAX_SEGMENTS 2500 130 #define MIN_SEGMENTS 5 131 /* Not interesting to play with if breeder is running interactively */ 132 #define INIT_FIT_THRESH 0.5 /* *** Default fitness threshold *** */ 133 134 135 /* ****** define constants used to identify menus and windows(buttons) ****** */ 136 #define INITIALIZE 0 137 #define BREED 1 138 #define QUIT 2 139 #define PLUS 3 140 #define MIN 4 141 #define OK 5 142 #define SHOW 6 143 /* ******************** define the warning bitmap size ********************** */ 144 #define warn_width 4 145 #define warn_height 10 146 147 148 149 /* ******************************************************************** */ 150 /* ********************* program data structures ********************** */ 151 /* ******************************************************************** */ 152 153 typedef char MyString[STRLENGTH]; 154 155 /* ****** Structures related to the genetic scheme of things ****** */ 156 typedef double Gene; 157 typedef struct org { 158 int name; /* *** Keep track of names, just for fun *** */ 159 int size_chrom; /* *** Actual # genes in chromosome *** */ 160 Gene X_Chrom[MAX_CHROM_SIZE];/* *** Genetic material itself *** */ 161 Gene Y_Chrom[MAX_CHROM_SIZE]; 162 double fitness; /* *** Fitness of organism *** */ 163 int mom; /* *** Keep track of parents just for fun *** */ 164 int dad; 165 } Organism; 166 167 typedef Organism Population; 168 169 /* ****** XWindow struct - this simplifies function calls ****** */ 170 typedef struct { 171 Window win; /* window ID */ 172 GC gc; /* window graphics content */ 173 XFontStruct* font_info; /* Font the window is using. */ 174 char text[40]; /* title (for menus) */ 175 int x, y, /* position */ 176 width, height, /* size */ 177 line_thick; /* line thickness of window border */ 178 unsigned long fg, bg; /* foreground and background */ 179 long event_mask, /* which events will be registered */ 180 flags; /* window flags */ 181 } WinType, *WinTypePtr; 182 183 184 /* ******************************************************************** */ 185 /* ********************** function declarations *********************** */ 186 /* ******************************************************************** */ 187 188 void do_event_loop( 189 #if NeedFunctionPrototypes 190 int argc, 191 char** argv 192 #endif 193 ); 194 195 void handle_NFS_change( 196 #if NeedFunctionPrototypes 197 int ppnfs, 198 int OFS 199 #endif 200 ); 201 202 void fix_menu_pos( 203 #if NeedFunctionPrototypes 204 int width 205 #endif 206 ); 207 208 void redraw_main_win( 209 #if NeedFunctionPrototypes 210 void 211 #endif 212 ); 213 214 void create_window ( 215 #if NeedFunctionPrototypes 216 Window parent_win, 217 WinTypePtr this_win, 218 unsigned long foreg, 219 unsigned long backg 220 #endif 221 ); 222 223 int highlight_menu( 224 #if NeedFunctionPrototypes 225 WinType menu[], 226 int menu_num, 227 int hightlight 228 #endif 229 ); 230 231 int expose_menu( 232 #if NeedFunctionPrototypes 233 WinType menu[], 234 int menu_num 235 #endif 236 ); 237 238 int expose_win( 239 #if NeedFunctionPrototypes 240 WinType win[], 241 int win_num 242 #endif 243 ); 244 245 void destroy_menu( 246 #if NeedFunctionPrototypes 247 WinType menu[], 248 int menu_num 249 #endif 250 ); 251 252 int which_button_pressed( 253 #if NeedFunctionPrototypes 254 WinType menu[], 255 int menu_num 256 #endif 257 ); 258 259 void select_breeding_sub_pop( 260 #if NeedFunctionPrototypes 261 void 262 #endif 263 ); 264 265 void highlight_org_window( 266 #if NeedFunctionPrototypes 267 int i 268 #endif 269 ); 270 271 void handle_resize( 272 #if NeedFunctionPrototypes 273 int x, 274 int y 275 #endif 276 ); 277 278 void process_databases( 279 #if NeedFunctionPrototypes 280 int argc, 281 char** argv, 282 XrmDatabase commandlineDB 283 #endif 284 ); 285 286 void do_help( 287 #if NeedFunctionPrototypes 288 void 289 #endif 290 ); 291 292 void init_all_windows( 293 #if NeedFunctionPrototypes 294 void 295 #endif 296 ); 297 298 Bool getBoolResource( 299 #if NeedFunctionPrototypes 300 XrmDatabase db, 301 char* str_name, 302 char* str_class, 303 Bool deflt 304 #endif 305 ); 306 307 int getIntResource( 308 #if NeedFunctionPrototypes 309 XrmDatabase db, 310 char* str_name, 311 char* str_class, 312 int deflt 313 #endif 314 ); 315 316 unsigned long getColorResource( 317 #if NeedFunctionPrototypes 318 XrmDatabase db, 319 char* str_name, 320 char* str_class, 321 unsigned long deflt 322 #endif 323 ); 324 325 XFontStruct* getFontResource( 326 #if NeedFunctionPrototypes 327 XrmDatabase db, 328 char* str_name, 329 char* str_class, 330 XFontStruct* deflt 331 #endif 332 ); 333 334 char* catlist( 335 #if NeedFunctionPrototypes 336 char*, 337 ... 338 #endif 339 ); 340 341 void randomize_org( 342 #if NeedFunctionPrototypes 343 Organism *org, 344 int name, 345 int size_chrom 346 #endif 347 ); 348 349 void randomize_pop( 350 #if NeedFunctionPrototypes 351 void 352 #endif 353 ); 354 355 int rnd( 356 #if NeedFunctionPrototypes 357 int low, 358 int high 359 #endif 360 ); 361 362 int flip( 363 #if NeedFunctionPrototypes 364 double p 365 #endif 366 ); 367 368 void copy_org( 369 #if NeedFunctionPrototypes 370 Organism *org1, 371 Organism *org2 372 #endif 373 ); 374 375 void copy_pop( 376 #if NeedFunctionPrototypes 377 Population *pop1, 378 Population *pop2, 379 int size_pop 380 #endif 381 ); 382 383 void erase_org( 384 #if NeedFunctionPrototypes 385 Organism *org 386 #endif 387 ); 388 389 int select_org( 390 #if NeedFunctionPrototypes 391 int size_pop 392 #endif 393 ); 394 395 double mutation( 396 #if NeedFunctionPrototypes 397 double allele 398 #endif 399 ); 400 401 void crossover( 402 #if NeedFunctionPrototypes 403 Organism *parent1, 404 Organism *parent2, 405 Organism *child1, 406 Organism *child2, 407 int first_born_name 408 #endif 409 ); 410 411 void breed( 412 #if NeedFunctionPrototypes 413 void 414 #endif 415 ); 416 417 int fitness( 418 #if NeedFunctionPrototypes 419 int i 420 #endif 421 ); 422 423 void set_toggle_to_default( 424 #if NeedFunctionPrototypes 425 int i 426 #endif 427 ); 428 429 void print_pop( 430 #if NeedFunctionPrototypes 431 void 432 #endif 433 ); 434 435 void print_org( 436 #if NeedFunctionPrototypes 437 Organism *org 438 #endif 439 ); 440 441 double dpow( 442 #if NeedFunctionPrototypes 443 double x, 444 int n 445 #endif 446 ); 447 448 void resize( 449 #if NeedFunctionPrototypes 450 void 451 #endif 452 ); 453 454 void grow_pop( 455 #if NeedFunctionPrototypes 456 void 457 #endif 458 ); 459 460 int grow( 461 #if NeedFunctionPrototypes 462 int org 463 #endif 464 ); 465 466 void develop( 467 #if NeedFunctionPrototypes 468 int org, 469 double t, 470 int *X_scr, 471 int *Y_scr 472 #endif 473 ); 474 475 void developF( 476 #if NeedFunctionPrototypes 477 int org, 478 double t, 479 int *X_scr, 480 int *Y_scr 481 #endif 482 ); 483 484 void developFG( 485 #if NeedFunctionPrototypes 486 int org, 487 double t, 488 int *X_scr, 489 int *Y_scr 490 #endif 491 ); 492 493 void display_genes( 494 #if NeedFunctionPrototypes 495 int org 496 #endif 497 ); 498 499 void graph_chrom( 500 #if NeedFunctionPrototypes 501 Gene *chrom, 502 int size_chrom, 503 int Xl, 504 int Xh, 505 int Yl, 506 int Yh, 507 int org 508 #endif 509 ); 510 511 double noise( 512 #if NeedFunctionPrototypes 513 double mean, 514 double std 515 #endif 516 ); 517 518 void init( 519 #if NeedFunctionPrototypes 520 void 521 #endif 522 ); 523 524 void msleep( 525 #if NeedFunctionPrototypes 526 long n 527 #endif 528 ); 529 530