#ifndef lint static char *rcsid = "$Header: /tmp_mnt/vida/disk5/users/terry/r/gassy/arith/RCS/arith.c,v 1.1 1992/08/09 22:30:18 terry Exp terry $"; #endif /* * The GA implemented here searches for a solution to nine equations. * * 1) ab + cd = def * 2) ag + hh = dia * 3) ig + af = die (original was ig + af = dia) * 4) ba + gg = dec (original was be + gg = dec) * 5) dc * eh = gec * 6) ie * hb = fff * 7) ie * hd = bee * 8) fd * di = abi * 9) ed * df = bhf * * In these, each letter from a to i represents one of the digits from * 1 to 9. None of the letters corresponds to zero. In the solution * (that I found), each digit occurs once, but I did not know that * this was so beforehand. I have a suspicion that it was probably * part of the original problem definition, but it came to me this * way. Not only that, but the person who told me of the problem said * that he thought he had copied it down slightly incorrectly! But he * didn't know where. The GA found solutions to 7 of the 9 original * equations, and putting those values into the two equations that * weren't satisfied, I found that they contained trivial errors. I * then ran the GA on the problem as I guess it should have been, and * it found a solution to all 9 equations. * * The fitness of an individual is the number of equations it solves * correctly. One could imagine more informative fitness functions * that gave partial credit to solutions that had the least significant * digits correct etc. * * The performance of a GA on this problem seems very dependent on the * mutation rate. 0.001 seems to get you nowhere, but 0.01 with * exactly the same arguments will solve the problem very readily. * * The following arguments (for example) solve the problem: * -p 1000 -g 150 -elitist -mp 0.01 -seed 713752859 -si */ #include "types.h" #include "public.h" #define GENOME_SIZE 9 INT app_individual_size() { return GENOME_SIZE; } FITNESS app_evaluate_individual(who, context) INT who; CONTEXT *context; { FITNESS fitness = (FITNESS) 0; INT a, b, c, d, e, f, g, h, i; register INDIVIDUAL_TYPE genome = context->population[who].genome; a = genome[0] - '0'; b = genome[1] - '0'; c = genome[2] - '0'; d = genome[3] - '0'; e = genome[4] - '0'; f = genome[5] - '0'; g = genome[6] - '0'; h = genome[7] - '0'; i = genome[8] - '0'; if (10 * a + b + 10 * c + d - 100 * d - 10 * e - f == 0){ fitness++; } if (10 * a + g + 10 * h + h - 100 * d - 10 * i - a == 0){ fitness++; } if (10 * i + g + 10 * a + f - 100 * d - 10 * i - a == 0){ fitness++; } if (10 * b + e + 10 * g + g - 100 * d - 10 * e - c == 0){ fitness++; } if ((10 * d + c) * (10 * e + h) - (100 * g + 10 * e + c) == 0){ fitness++; } if ((10 * i + e) * (10 * h + b) - (100 * f + 10 * f + f) == 0){ fitness++; } if ((10 * i + e) * (10 * h + d) - (100 * b + 10 * e + e) == 0){ fitness++; } if ((10 * f + d) * (10 * d + i) - (100 * a + 10 * b + i) == 0){ fitness++; } if ((10 * e + d) * (10 * d + f) - (100 * b + 10 * h + f) == 0){ fitness++; } return fitness; } VOID app_allele_possibilities(alleles) LOCUS *alleles; { register INT i; static STRING digits = "123456789"; for (i = 0; i < GENOME_SIZE; i++){ alleles[i].possible_alleles = digits; } return; }