#include #include #include "gene.h" #define AllocUnit 128 #define MaxGrid 16 static Cell *FreeCell = NULL; static double Distance2; extern Individual *Population; extern int PopulationSize; extern double CellSize; extern unsigned long PopSize; mem_init() { char *mem; int mem_size, i; Population = (Individual *)malloc(sizeof(Individual) * PopulationSize); if (!Population) { perror("malloc"); exit(1); } bzero(Population,sizeof(Individual) * PopulationSize); Distance2 = CellSize * CellSize * 3.99; } mem_reset() { int i, j; Individual *ind; Cell *p; for (ind = Population, i = 0; i < PopulationSize; i ++, ind ++) { if (ind->cells) { for (p = ind->cells; p->next; p = p->next); p->next = FreeCell; FreeCell = ind->cells; ind->cells = NULL; } ind->id = i; } PopSize = 0; } Cell *alloc_cell(ind) Individual *ind; { int i; Cell *c = FreeCell; if (!c) { if (!(c = FreeCell = (Cell *)malloc(sizeof(Cell)*AllocUnit))) { perror("cell memory"); exit(1); } for (i = 0; i < AllocUnit-1; i ++) FreeCell[i].next = FreeCell + i + 1; FreeCell[AllocUnit-1].next = NULL; } FreeCell = FreeCell->next; c->next = ind->cells; ind->cells = c; PopSize ++; return c; } if_conflict(ind,x,y,z) Individual *ind; double x, y, z; { Cell *c; double dx,dy,dz; for (c = ind->cells; c; c = c->next) { dx = x - c->x; dy = y - c->y; dz = z - c->z; if (dx*dx + dy*dy + dz*dz < Distance2) return 1; } return 0; } for_all_cells(ind,proc) Individual *ind; void (*proc)(); { Cell *c; for (c = ind->cells; c; c = c->next) (*proc)(c); }