1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
|
/*
COPYRIGHT
The following is a notice of limited availability of the code, and disclaimer
which must be included in the prologue of the code and in all source listings
of the code.
(C) COPYRIGHT 2008 University of Chicago
Permission is hereby granted to use, reproduce, prepare derivative works, and
to redistribute to others. This software was authored by:
D. Levine
Mathematics and Computer Science Division
Argonne National Laboratory Group
with programming assistance of participants in Argonne National
Laboratory's SERS program.
GOVERNMENT LICENSE
Portions of this material resulted from work developed under a
U.S. Government Contract and are subject to the following license: the
Government is granted for itself and others acting on its behalf a paid-up,
nonexclusive, irrevocable worldwide license in this computer software to
reproduce, prepare derivative works, and perform publicly and display
publicly.
DISCLAIMER
This computer code material was prepared, in part, as an account of work
sponsored by an agency of the United States Government. Neither the United
States, nor the University of Chicago, nor any of their employees, makes any
warranty express or implied, or assumes any legal liability or responsibility
for the accuracy, completeness, or usefulness of any information, apparatus,
product, or process disclosed, or represents that its use would not infringe
privately owned rights.
*/
/*****************************************************************************
* FILE: utility.c: This file contains routines that perform utility
* functions.
*
* Authors: David M. Levine, Philip L. Hallstrom, David M. Noelle,
* Brian P. Walenz
*****************************************************************************/
#include <pgapack.h>
/*U****************************************************************************
PGAMean - calculates the mean value of an array of elements
Category: Utility
Inputs:
ctx - context variable
a - array to take the mean of
n - number of elements in array a
Outputs:
The mean of the n elements in array a
Example:
PGAContext *ctx;
double a[100], mean;
:
mean = PGAMean(ctx, a, 100);
****************************************************************************U*/
double PGAMean ( PGAContext *ctx, double *a, int n)
{
int i;
double result;
PGADebugEntered("PGAMean");
result = 0.;
for( i=n-1; i>=0; i-- )
result += a[i];
PGADebugExited("PGAMean");
return (result / (double) n );
}
/*U****************************************************************************
PGAStddev - calculates the standard deviation of an array of elements
Category: Utility
Inputs:
ctx - context variable
a - array to take the standard deviation of
n - number of elements in array a
mean - the mean of the elements in array a
Outputs:
The standard deviation of the n elements in array a
Example:
PGAContext *ctx;
double a[100], mean, sigma;
:
mean = PGAMean(ctx, a, 100);
sigma = PGAStddev(ctx, a, 100, mean);
****************************************************************************U*/
double PGAStddev ( PGAContext *ctx, double *a, int n, double mean)
{
int i;
double result;
PGADebugEntered("PGAStddev");
result = 0;
for(i=n-1; i>=0; i--)
result += (a[i] - mean) * (a[i] - mean);
result = sqrt(result/n);
PGADebugExited("PGAStddev");
return (result);
}
/*U****************************************************************************
PGARound - Mathematically round a double to an integer, using 0.5 as the
cutoff value.
Category: Utility
Inputs:
ctx - context variable
x - the number to be rounded
Outputs:
The rounded number.
Example:
PGAContext *ctx;
int y;
y = PGARound(ctx, -78.6);
****************************************************************************U*/
int PGARound(PGAContext *ctx, double x)
{
double ipart, frac;
PGADebugEntered("PGARound");
frac = modf(x, &ipart);
if (frac <= -0.5)
ipart--;
else if (frac >= 0.5)
ipart++;
PGADebugExited("PGARound");
return ((int)ipart);
}
/*U****************************************************************************
PGACopyIndividual - copies string p1 in population pop1 to position p2 in
population pop2
Category: Generation
Inputs:
ctx - context variable
p1 - string to copy
pop1 - symbolic constant of population containing string p1
p2 - string to copy p1 to
pop2 - symbolic constant of population containing string p2
Outputs:
String p2 is an exact copy of string p1.
Example:
PGAContext *ctx;
int i,j;
:
PGACopyIndividual(ctx, i, PGA_OLDPOP, j, PGA_NEWPOP);
****************************************************************************U*/
void PGACopyIndividual( PGAContext *ctx, int p1, int pop1, int p2, int pop2)
{
PGAIndividual *source, *dest;
PGADebugEntered("PGACopyIndividual");
source = PGAGetIndividual ( ctx, p1, pop1 );
dest = PGAGetIndividual ( ctx, p2, pop2 );
dest->evalfunc = source->evalfunc;
dest->fitness = source->fitness;
dest->evaluptodate = source->evaluptodate;
(*ctx->cops.CopyString)(ctx, p1, pop1, p2, pop2);
PGADebugExited("PGACopyIndividual");
}
/*U****************************************************************************
PGACheckSum - maps a string to a number to be used a verification check
PGA_DATATYPE_USER is not supported.
Category: Utility
Inputs:
ctx - context variable
p - string index
pop - symbolic constant for the population
Outputs:
An integer representing the "value" of the string.
Example:
PGAContext *ctx;
int p, sum;
:
sum = PGACheckSum(ctx, p, PGA_NEWPOP);
****************************************************************************U*/
int PGACheckSum(PGAContext *ctx, int p, int pop)
{
long stringlen, totalchars, charbits, i, j, checksum, totalbytes, out_bit;
unsigned char *message, specimen;
PGADebugEntered("PGACheckSum");
stringlen = PGAGetStringLength(ctx);
switch (ctx->ga.datatype) {
case PGA_DATATYPE_BINARY:
totalbytes = ctx->ga.tw * sizeof(PGABinary);
break;
case PGA_DATATYPE_INTEGER:
totalbytes = stringlen * sizeof(PGAInteger);
break;
case PGA_DATATYPE_REAL:
totalbytes = stringlen * sizeof(PGAReal);
break;
case PGA_DATATYPE_CHARACTER:
totalbytes = stringlen * sizeof(PGACharacter);
break;
default:
totalbytes = 0;
PGAError(ctx, "PGACheckSum: User datatype checksum may be invalid.",
PGA_WARNING, PGA_VOID, NULL);
break;
}
message = (unsigned char *)PGAGetIndividual(ctx, p, pop)->chrom;
totalchars = totalbytes / sizeof(unsigned char);
charbits = sizeof(unsigned char) * 8;
checksum = 0;
for (i = 0; i < totalchars; i++) {
specimen = *(message + i);
for (j = 0; j < charbits; j++) {
out_bit = (checksum & 0x80000000) == 1;
checksum = (checksum << 1) + ((specimen & 0x80) == 0x80);
if (out_bit)
checksum ^= 0x04c11db7;
specimen <<= 1;
}
}
PGADebugExited("PGACheckSum");
return (checksum);
}
/*U***************************************************************************
PGAGetWorstIndex - returns the index of the string with the worst
evaluation function value in population pop
Category: Utility
Inputs:
ctx - context variable
pop - symbolic constant of the population to find the worst string in
Outputs:
Index of the string with the worst evaluation function value
Example:
PGAContext *ctx;
int worst;
:
worst = PGAGetWorstIndex(ctx,PGA_OLDPOP);
***************************************************************************U*/
int PGAGetWorstIndex(PGAContext *ctx, int pop)
{
int p, worst_indx = 0;
double eval, worst_eval;
PGADebugEntered("PGAGetWorstIndex");
for (p = 0; p < ctx->ga.PopSize; p++)
if (!PGAGetEvaluationUpToDateFlag(ctx, p, pop))
PGAError(ctx, "PGAGetWorstIndex: Evaluate function not up to "
"date:", PGA_FATAL, PGA_INT, (void *) &p);
worst_eval = PGAGetEvaluation(ctx, 0, pop);
switch (PGAGetOptDirFlag(ctx)) {
case PGA_MAXIMIZE:
for (p = 1; p < ctx->ga.PopSize; p++) {
eval = PGAGetEvaluation(ctx, p, pop);
if (eval < worst_eval) {
worst_indx = p;
worst_eval = eval;
}
}
break;
case PGA_MINIMIZE:
for (p = 1; p < ctx->ga.PopSize; p++) {
eval = PGAGetEvaluation(ctx, p, pop);
if (eval > worst_eval) {
worst_indx = p;
worst_eval = eval;
}
}
break;
}
PGADebugExited("PGAGetWorstIndex");
return (worst_indx);
}
/*U***************************************************************************
PGAGetBestIndex - returns the index of the string with the best evaluation
function value in population pop
Category: Utility
Inputs:
ctx - context variable
pop - symbolic constant of the population to find the best string in
Outputs:
Index of the string with the best evaluation function value
Example:
PGAContext *ctx;
int best;
:
best = PGAGetBestIndex(ctx,PGA_OLDPOP);
***************************************************************************U*/
int PGAGetBestIndex(PGAContext *ctx, int pop)
{
int p, Best_indx = 0;
double eval, Best_eval;
PGADebugEntered("PGAGetBestIndex");
for (p = 0; p < ctx->ga.PopSize; p++)
if (!PGAGetEvaluationUpToDateFlag(ctx, p, pop))
PGAError(ctx, "PGAGetBestIndex: Evaluate function not up to "
"date:", PGA_FATAL, PGA_INT, (void *) &p);
Best_eval = PGAGetEvaluation(ctx, 0, pop);
switch (PGAGetOptDirFlag(ctx)) {
case PGA_MAXIMIZE :
for (p = 1; p < ctx->ga.PopSize; p++) {
eval = PGAGetEvaluation(ctx, p, pop);
if (eval > Best_eval) {
Best_indx = p;
Best_eval = eval;
}
}
break;
case PGA_MINIMIZE :
for (p = 1; p < ctx->ga.PopSize; p++) {
eval = PGAGetEvaluation(ctx, p, pop);
if (eval < Best_eval) {
Best_indx = p;
Best_eval = eval;
}
}
break;
}
PGADebugExited("PGAGetBestIndex");
return (Best_indx);
}
/*I****************************************************************************
PGAGetIndividual - translate string index and population symbolic constant
into pointer to an individual
Inputs:
ctx - context variable
p - string index
pop - symbolic constant of the population the string is in
Outputs:
Address of the PGAIndividual structure for string p in population pop
Example:
PGAIndividual *source;
PGAContext *ctx;
int p;
:
source = PGAGetIndividual ( ctx, p, PGA_NEWPOP );
****************************************************************************I*/
PGAIndividual *PGAGetIndividual ( PGAContext *ctx, int p, int pop)
{
PGAIndividual *ind;
PGADebugEntered("PGAGetIndividual");
#ifdef OPTIMIZE
ind = (pop == PGA_OLDPOP) ? ctx->ga.oldpop : ctx->ga.newpop;
if (p>=0)
ind += p;
else
ind += (p == PGA_TEMP1) ? ctx->ga.PopSize : ctx->ga.PopSize+1;
#else
if (pop == PGA_OLDPOP)
ind = ctx->ga.oldpop;
else
if (pop == PGA_NEWPOP)
ind = ctx->ga.newpop;
else
PGAError(ctx, "PGAGetIndividual: Invalid value of pop:",
PGA_FATAL, PGA_INT, (void *) &pop );
if (p>0 && p<ctx->ga.PopSize)
ind += p;
else
if (p == PGA_TEMP1)
ind += ctx->ga.PopSize;
else
if (p == PGA_TEMP2)
ind += ctx->ga.PopSize + 1;
else
PGAError(ctx, "PGAGetIndividual: Invalid value of p:",
PGA_FATAL, PGA_INT, (void *) &p );
#endif
PGADebugExited("PGAGetIndividual");
return(ind);
}
/*I****************************************************************************
PGAUpdateAverage - Updates the average fitness statistic for reporting.
Inputs:
ctx - context variable
pop - symbolic constant of the population
Outputs:
Example:
**************************************************************************I*/
void PGAUpdateAverage(PGAContext *ctx, int pop)
{
double ThisGensTotal = 0;
int p;
PGADebugEntered("PGAUpdateAverage");
for (p = 0; p < ctx->ga.PopSize; p++)
if (!PGAGetEvaluationUpToDateFlag(ctx, p, pop))
PGAError(ctx, "PGAUpdateOnline: Evaluate function not up to "
"date:", PGA_FATAL, PGA_INT, (void *) &p);
for (p = 0; p < ctx->ga.PopSize; p++)
ThisGensTotal += PGAGetEvaluation(ctx, p, pop);
ctx->rep.Average = ThisGensTotal / (double)ctx->ga.PopSize;
PGADebugExited("PGAUpdateAverage");
}
/*I****************************************************************************
PGAUpdateOnline - Updates the online value based on the results in
the new generation
Inputs:
ctx - context variable
pop - symbolic constant of the population whose statistics to use
Outputs:
Updates an internal field in the context variable
Example:
PGAContext *ctx;
:
PGAUpdateOnline(ctx,PGA_NEWPOP);
**************************************************************************I*/
void PGAUpdateOnline(PGAContext *ctx, int pop)
{
double ThisGensTotal = 0;
int p;
PGADebugEntered("PGAUpdateOnline");
for (p = 0; p < ctx->ga.PopSize; p++)
if (!PGAGetEvaluationUpToDateFlag(ctx, p, pop))
PGAError(ctx, "PGAUpdateOnline: Evaluate function not up to "
"date:", PGA_FATAL, PGA_INT, (void *) &p);
for (p = 0; p < ctx->ga.PopSize; p++)
ThisGensTotal += PGAGetEvaluation(ctx, p, pop);
PGADebugPrint(ctx, PGA_DEBUG_PRINTVAR, "PGAUpdateOnline",
"ThisGensTotal = ", PGA_DOUBLE, (void *) &ThisGensTotal);
ctx->rep.Online = (ctx->rep.Online * ctx->ga.PopSize * (ctx->ga.iter - 1)
+ ThisGensTotal) / ctx->ga.iter / ctx->ga.PopSize;
PGADebugExited("PGAUpdateOnline");
}
/*I****************************************************************************
PGAUpdateOffline - Updates the offline value based on the results in
the new generation
Inputs:
ctx - context variable
pop - symbolic constant of the population whose statistics to use
Outputs:
Updates an internal field in the context variable
Example:
PGAContext *ctx;
:
PGAUpdateOffline(ctx,PGA_NEWPOP);
**************************************************************************I*/
void PGAUpdateOffline(PGAContext *ctx, int pop)
{
int p;
PGADebugEntered("PGAUpdateOffline");
for (p = 0; p < ctx->ga.PopSize; p++)
if (!PGAGetEvaluationUpToDateFlag(ctx, p, pop))
PGAError(ctx, "PGAUpdateOffline: Evaluate function not up to "
"date:", PGA_FATAL, PGA_INT, (void *) &p);
p = PGAGetBestIndex(ctx, pop);
ctx->rep.Offline = ((ctx->ga.iter - 1) * ctx->rep.Offline +
PGAGetEvaluation(ctx, p, pop)) / ctx->ga.iter;
PGADebugExited("PGAUpdateOffline");
}
/*I****************************************************************************
PGAComputeSimilarity - computes the percentage of the population that have
the same evaluation function
Inputs:
ctx - context variable
pop - symbolic constant of the population whose statistics to use
Outputs:
returns a count of the number of population members that have the same
evaluation function value
Example:
PGAContext *ctx;
:
PGAComputeSimilarity(ctx,PGA_NEWPOP);
**************************************************************************I*/
int PGAComputeSimilarity(PGAContext *ctx, PGAIndividual *pop)
{
int max = 0, curr = 1, i;
double prev;
PGADebugEntered("PGAComputeSimilarity");
for(i=0; i < ctx->ga.PopSize; i++)
{
ctx->scratch.dblscratch[i] = (pop + i)->evalfunc;
ctx->scratch.intscratch[i] = i;
}
PGADblHeapSort(ctx, ctx->scratch.dblscratch, ctx->scratch.intscratch,
ctx->ga.PopSize);
prev = ctx->scratch.dblscratch[0];
for(i = 1; i < ctx->ga.PopSize; i++)
{
if (ctx->scratch.dblscratch[i] == prev)
curr++;
else
{
if (curr > max)
max = curr;
curr = 1;
}
}
PGADebugExited("PGAComputeSimilarity");
return(100 * max / ctx->ga.PopSize);
}
|