/***************************default rules**************************/ default dir = dir; default pcross = pcross; default pmut = pmut; default fitness = fitness; default string = string; default found = found; /**************the constants are defined here**********************/ int VAR_NUM = 16; int MAX_CLAUSE = 48; int MUT_RATE = 14; // 14/16 == 87.5% int CROSS_RATE = 14; // 8/16 == 50.0% int CLAUSE[] = {16,1024,4,16384,64,1024,8,256,1,8192,32768,16384,4096,8,8,512,64,8,4,32768,512,256,2,4,2,512,8192,1,2048,256,32768,32,8,2048,128,8,512,32768,16,2048,128,4096,16384,1,2,256,16384,64,8192,128,512,512,8192,128,4096,512,512,1,16384,256,4,8192,2048,256,32,2,1,128,512,1024,16,2,1024,32768,16384,8,1024,8,64,32768,1,256,2,16384,512,4096,32768,1,32,32768,4096,2,1024,4096,8,128,2,2,128,64,8,2048,2,2048,1024,4,1,4096,4096,2048,4,1,1,8192,2,128,8192,512,2048,1,16384,1,4,4,2,512,16384,2048,16384,16384,2,1,8,2048,2048,1,8192,32,4096,512,16,64,8192,1}; int VALUE[] = {0,1,1,0,1,0,1,1,1,1,1,1,1,0,0,1,0,0,0,1,0,1,0,1,0,1,1,0,0,0,1,0,0,1,0,1,1,0,1,0,0,1,1,0,1,1,1,1,1,1,1,0,1,0,1,0,0,0,1,1,0,1,0,1,1,0,0,0,0,1,1,1,1,1,1,1,1,1,0,0,1,1,1,0,1,0,0,0,0,1,0,1,1,0,0,0,1,1,1,0,1,0,1,1,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,1,1,0,1,0,1,0,1,1,1,1,1,0,0,0,1,0,0,1,0,1,0,0,0,1}; /*********the temporary variables are defined here*****************/ int tmpFit; nbr p; int tmpStr; /****************the functions are defined here********************/ int IsSame(int i, j) { if(i == 0 && j == 0) return 1; else if(i >= 1 && j == 1) return 1; else return 0; } int EvalFitness(int str) { int count; int k; count = 0; for(k = 0; k < MAX_CLAUSE; k++) { if(IsSame(str & CLAUSE[3 * k], VALUE[3 * k]) == 1 || IsSame(str & CLAUSE[3 * k + 1], VALUE[3 * k + 1]) == 1 || IsSame(str & CLAUSE[3 * k + 2], VALUE[3 * k + 2]) == 1) { count++; } } return MAX_CLAUSE - count; } nbr DecidePartner() { nbr n; nbr partner; int value; value = MAX_CLAUSE + 1; over each other n: { if(dir == 0) return n:; if(n:fitness < value) partner: = n:; } return partner:; } void Randomize() { dir = no:dir ^ ne:dir ^ ea:dir ^ se:dir ^ so:dir ^ sw:dir ^ we:dir ^ nw:dir; pcross = no:pcross ^ ne:pcross ^ ea:pcross ^ se:pcross ^ so:pcross ^ sw:pcross ^ we:pcross ^ nw:pcross; pmut = no:pmut ^ ne:pmut ^ ea:pmut ^ se:pmut ^ so:pmut ^ sw:pmut ^ we:pmut ^ nw:pmut; crossPos = no:crossPos ^ ne:crossPos ^ ea:crossPos ^ se:crossPos ^ so:crossPos ^ sw:crossPos ^ we:crossPos ^ nw:crossPos; } int Crossover(int str1, str2) { int mask1, mask2; int i, v; mask1 = 0; mask2 = 0; v = 1; for(i = 0; i < VAR_NUM - crossPos - 1; i++) { mask1 = mask1 | v; v = 2 * v; } v = 1; for(i = 0; i <= crossPos; i++) { mask2 = mask2 | v; mask1 = mask1 * 2; v = v * 2; } return ((str1 & mask1) | (str2 & mask2)); } int Mutation(int str) { int t, i, p; t = 1; p = pmut; for(i = 1; i <= p; i++) t = t * 2; return str ^ t; } /***********************main body for GA***************************/ Randomize(); if(found == 0) { p: = DecidePartner():; if(p:fitness < fitness) { tmpStr = string; if(pcross <= CROSS_RATE) tmpStr = Crossover(string, p:string); tmpStr = Mutation(tmpStr); tmpFit = EvalFitness(tmpStr); string = tmpStr; fitness = tmpFit; if(tmpFit == 0) found = 1; } }