/*Generalized GP, for many bins. From gp9.c, on 20 August 1997.*/ #include #define MAX_BIN 16 /* max. no. of possible bins */ #define MAX_PRINTS 200 /* max. no. of times fitness values will be printed */ #define MAX_NUMBER_OF_NODES_POSSIBLE 200 #define MAX_POPULATION_SIZE 1000 #define aa 16807 #define mm 2147483647 #define qq 127773 #define rr 2836 long int jran = 2147483647; long int fpseed = 2; /*all these help in random number generation*/ extern double drand48(); int nodecount=30; int bincount=2; /*2 for bipartitioning */ int single_bin_count; int op_choice=1; char outfilename[80]; int mut_prob = 5; /*EXPRESSED AS A PERCENTAGE (5%) */ float crossover_prob = 100; /* EXPRESSED AS A PERCENTAGE (100%) */ float pop_sum_fitness=0.0; float pop_sum_cost = 0.0; int max_num_of_generations, pop_size, location_of_best_individual=0; double cost[MAX_POPULATION_SIZE], w[MAX_NUMBER_OF_NODES_POSSIBLE][MAX_NUMBER_OF_NODES_POSSIBLE], fitness[MAX_POPULATION_SIZE]; int current_population[MAX_POPULATION_SIZE][MAX_NUMBER_OF_NODES_POSSIBLE], bestsol[MAX_NUMBER_OF_NODES_POSSIBLE]; FILE *fp; /*---------------------------------------------------------*/ float myrand() /* returns a float >= 0 and <1 */ { long int n; jran = (1283 + (jran * 106)) % 6075; /* 0 <= jran < 6075 */ return ((float) (jran) / 6075 ); }/***********************************************************************/ int preflip(prob) /* To help in random number generation -----------------*/ float prob; { long int lo, hi, test; float u; hi = fpseed / qq; lo = fpseed % (long int) qq; test = aa*lo - rr*hi; if (test > 0) fpseed = test; else fpseed = test + mm; return ( (srand(fpseed) / mm) >= prob); }/***********************************************************************/ int rnd(low, high) /* Generates an integer between low and high (inclusive) */ int low, high; { int n; if (low == high) return low; else { if (low >high) {n=low; low=high; high=n;} n = (int)( drand48() *(high-low+1)) + low; return( (n>high)? high: n); } }/* ============================================================ */ void printind(xx) int xx; /* print current_population[xx] ----------------*/ {int j; for (j=0; j zmax) zmax = cost[i]; if (cost[i] < zmin) { zmin = cost[i]; location_of_best_individual = i; } } avg_cost = pop_sum_cost/pop_size; for (i=0; i rnd(0,100)) xp[i]= (int)(drand48()*bincount); }/* ------------------------------------------------------------ */ void adjust_elt(xp) int *xp; /* To balance all bins */ { int i,big,temp, count[MAX_BIN], bin,bigval, smallval; float epsilon=0.0001; for (temp=0; temp= nodecount-1) ?0 : big+1); while (i= single_bin_count);i++) smallval = ((smallval >= bincount-1) ?0 : smallval+1); if (count[smallval] >= single_bin_count) printf("Didn't find small val!\n"); /*This shouldn't really happen*/ bigval = *(xp+big); *(xp+big)=smallval; (count[bigval])--; (count[smallval])++; big = (int)( drand48() * (nodecount-epsilon)); for (i=0;(i= nodecount-1) ?0 : big+1); } }/* ------------------------------------------------------------ */ void onept_xover(dadp, momp, xdp, xmp) /* combined with mutation and adjustment */ int dadp, momp, *xdp, *xmp; { int i, startp, lnth, temp, count[MAX_BIN],bin; for (i=0; i= rnd(0,100)) { startp = rnd(0, nodecount-1); for (i=startp; i= rnd(0,100))) { startp = rnd(0, nodecount-1); endp = rnd(0, nodecount-1); if (startp>endp) {temp=startp; startp=endp; endp=temp;} for (i=startp; i0.5) {*(xdp+i) = current_population[dadp][i]; *(xmp+i) = current_population[momp][i]; } else {*(xmp+i) = current_population[dadp][i]; *(xdp+i) = current_population[momp][i]; } mutate(xdp); mutate(xmp); adjust_elt(xdp); adjust_elt(xmp); }/* ------------------------------------------------------------ */ int get_parent() /*roulette wheel*/ {float current= fitness[0]; int i=0; float randval = drand48(); while (randval > current) { i++; current += fitness[i];} return((i MAX_PRINTS) { print_count = MAX_PRINTS; print_freq = max_num_of_generations/print_count;} if ((fp = fopen(argv[8], "w+t")) == NULL) { printf("Could not open output file; please retype filename:\n"); scanf("%d",outfilename); if ((fp = fopen(outfilename, "w+t")) == NULL) { printf("Could not open file again; please check directory permissions. Bye.\n"); exit(0); } } rewind(fp); }/* closing else */ single_bin_count= nodecount/bincount; generate_graph(); fprintf(fp, "Nodes = %d, Population Size = %d, Max. no. of generations = %d, Operator = ", nodecount, pop_size, max_num_of_generations); switch (op_choice) { case 2: fprintf(fp, "2PTX"); break; case 3: fprintf(fp, "UX"); break; default: fprintf(fp, "1PTX"); } clock = time(0); for (i=0; i< print_count; i++) BestCost[i] = AvCost[i] = 0; generate_initial_set(); av_cost = calc_pop_cost(); fprintf(fp,"\nINITIAL avg.= %10.2f, best = %10.2f,", av_cost, cost[location_of_best_individual]); fprintf(fp, " individual= "); printind(location_of_best_individual); fprintf(fp, "\n"); sum_init_cost = sum_init_cost + av_cost; sum_best_cost = sum_best_cost + cost[location_of_best_individual]; for (i=0; i< max_num_of_generations/print_freq; i++) { for (j=0; j