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 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
|
/*
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: fitness.c: This file contains the routines that have to do with
* fitness calculations.
*
* Authors: David M. Levine, Philip L. Hallstrom, David M. Noelle,
* Brian P. Walenz
*****************************************************************************/
#include "pgapack.h"
/*U****************************************************************************
PGAFitness - Maps the user's evaluation function value to a fitness value.
First, the user's evaluation function value is translated to all positive
values if any are negative. Next, this positive sequence is translated to
a maximization problem if the user's optimization direction was minimization.
This positive sequence is then mapped to a fitness value using linear
ranking, linear normalization fitness, or the identity (i.e., the evaluation
function value). This routine is usually used after PGAEvaluate is called.
Category: Fitness & Evaluation
Inputs:
ctx - context variable
pop - symbolic constant of the population to calculate fitness for
Outputs:
Calculates the fitness for each string in the population via side effect
Example:
Calculate the fitness of all strings in population PGA_NEWPOP after
calling PGAEvaluate to calculate the strings evaluation value.
double energy(PGAContext *ctx, int p, int pop);
PGAContext *ctx;
:
PGAEvaluate(ctx, PGA_NEWPOP, energy);
PGAFitness (ctx, PGA_NEWPOP);
****************************************************************************U*/
void PGAFitness ( PGAContext *ctx, int popindex )
{
int i;
double mineval;
PGAIndividual *pop;
PGADebugEntered("PGAFitness");
/* set pointer to appropriate population */
switch (popindex) {
case PGA_OLDPOP:
pop = ctx->ga.oldpop;
break;
case PGA_NEWPOP:
pop = ctx->ga.newpop;
break;
default:
PGAError( ctx, "PGAFitness: Invalid value of popindex:",
PGA_FATAL, PGA_INT, (void *) &popindex );
break;
}
/* make sure all evaluation function values are up-to-date */
for( i=0; i<ctx->ga.PopSize; i++ ) {
/*printf("i = %d, evaluptodate = %d\n",i,(pop+i)->evaluptodate);*/
if ( (pop+i)->evaluptodate != PGA_TRUE )
PGAError( ctx, "PGAFitness: evaluptodate not PGA_TRUE for:",
PGA_FATAL, PGA_INT, (void *) &i );
}
/* put raw fitness into fitness field */
for( i=0; i<ctx->ga.PopSize; i++ )
(pop+i)->fitness = (pop+i)->evalfunc;
/* translate to all positive sequence (if necessary) */
mineval = ctx->sys.PGAMaxDouble;
for( i=0; i<ctx->ga.PopSize; i++ )
if ( (pop+i)->fitness < mineval )
mineval =(pop+i)->fitness;
if ( mineval < 0.0 ) {
mineval = (-1.01) * mineval;
for( i=0; i<ctx->ga.PopSize; i++ )
(pop+i)->fitness = (pop+i)->fitness + mineval;
}
/* translate to maximization problem (if necessary) */
if ( ctx->ga.optdir == PGA_MINIMIZE ) {
switch (ctx->ga.FitnessMinType) {
case PGA_FITNESSMIN_RECIPROCAL:
PGAFitnessMinReciprocal( ctx, pop );
break;
case PGA_FITNESSMIN_CMAX:
PGAFitnessMinCmax ( ctx, pop );
break;
default:
PGAError( ctx,
"PGAFitness: Invalid FitnessMinType:",
PGA_FATAL,
PGA_INT,
(void *) &(ctx->ga.FitnessMinType) );
break;
}
}
/* last step in fitness calculation */
switch (ctx->ga.FitnessType) {
case PGA_FITNESS_RAW:
break;
case PGA_FITNESS_NORMAL:
PGAFitnessLinearNormal ( ctx, pop );
break;
case PGA_FITNESS_RANKING:
PGAFitnessLinearRank ( ctx, pop );
break;
default:
PGAError( ctx,
"PGAFitness: Invalid FitnessType:",
PGA_FATAL,
PGA_INT,
(void *) &(ctx->ga.FitnessType) );
break;
}
PGADebugExited("PGAFitness");
}
/*U****************************************************************************
PGARank - returns the rank of a string in a population. This is a value
between 1,...,N (the population size). The most fit string has rank 1,
the least fit string has rank N.
Category: Fitness & Evaluation
Inputs:
ctx - context variable
p - the index of the string whose rank is desired
order - an array containing a unique rank for each string
n - the size of the array order
Outputs:
The rank of string p
Example:
Determine the rank of string p.
PGAContext *ctx;
int i, popsize, rank, *order;
double *fitness;
popsize = PGAGetPopsize(ctx);
order = (int *) malloc(sizeof(int) * popsize);
fitness = (double *)malloc(sizeof(double) * popsize);
for(i=0;i<popsize; i++) {
fitness[i] = PGAGetFitness(ctx, p, PGA_OLDPOP);
order[i] = i;
}
PGADblHeapSort(ctx, fitness, order, popsize);
rank = PGARank(ctx, p, order, popsize)
****************************************************************************U*/
int PGARank( PGAContext *ctx, int p, int *order, int n )
{
int i;
PGADebugEntered("PGARank");
/* If the user gives us PGA_TEMP1 or PGA_TEMP2 (or, gasp, some random
* number that is not in the population), fail.
*/
if ((p<0) || (p > PGAGetPopSize(ctx)))
PGAError(ctx, "PGARank: Not a valid population member, p = ",
PGA_FATAL, PGA_INT, (void *)&p);
/* Search through all the orderings until we find the one that
* matches the given string. Return the index number. If we do not
* find one, something is _very_ bad; terminate with a fatal error.
*/
for(i=0; i<n; i++)
if (order[i] == p) {
PGADebugExited("PGARank");
return(i+1);
}
/* Ideally, we should print out the order array, but, well, ideally,
* we should never get here anyway...Also, to make some compilers
* shut up, return(0) is here, even though PGAError doesn't return.
*/
PGAError( ctx, "PGARank: Bottom of loop in rank, p = ", PGA_FATAL,
PGA_INT, (void *) &p );
return(0);
}
/*U***************************************************************************
PGAGetFitness - returns the fitness value for a string
Category: Fitness & Evaluation
Inputs:
ctx - context variable
p - string index
pop - symbolic constant of the population the string is in
Outputs:
The fitness value for string p in population pop
Example:
PGAContext *ctx;
int p;
double fit;
:
fit = PGAGetFitness(ctx, p, PGA_NEWPOP);
***************************************************************************U*/
double PGAGetFitness ( PGAContext *ctx, int p, int pop )
{
PGAIndividual *ind;
PGADebugEntered("PGAGetFitness");
PGADebugPrint( ctx, PGA_DEBUG_PRINTVAR,"PGAGetFitness", "p = ",
PGA_INT, (void *) &p );
PGADebugPrint( ctx, PGA_DEBUG_PRINTVAR,"PGAGetFitness", "pop = ",
PGA_INT, (void *) &pop );
ind = PGAGetIndividual ( ctx, p, pop );
PGADebugExited("PGAGetFitness");
return(ind->evalfunc);
}
/*U***************************************************************************
PGAGetFitnessType - Returns the type of fitness transformation used.
Category: Fitness & Evaluation
Inputs:
ctx - context variable
Outputs:
Returns the integer corresponding to the symbolic constant
used to specify the type of fitness transformation used
Example:
PGAContext *ctx;
int fittype;
:
fittype = PGAGetFitnessType(ctx);
switch (fittype) {
case PGA_FITNESS_RAW:
printf("Fitness Type = PGA_FITNESS_RAW\n");
break;
case PGA_FITNESS_NORMAL:
printf("Fitness Type = PGA_FITNESS_NORMAL\n");
break;
case PGA_FITNESS_RANKING:
printf("Fitness Type = PGA_FITNESS_RANKING\n");
break;
}
***************************************************************************U*/
int PGAGetFitnessType (PGAContext *ctx)
{
PGADebugEntered("PGAGetFitnessType");
PGAFailIfNotSetUp("PGAGetFitnessType");
PGADebugExited("PGAGetFitnessType");
return(ctx->ga.FitnessType);
}
/*U***************************************************************************
PGAGetFitnessMinType - Returns the type of fitness transformation used
for minimization problems.
Category: Fitness & Evaluation
Inputs:
ctx - context variable
Outputs:
Returns the integer corresponding to the symbolic constant
used to specify the type of fitness transformation used
for minimization problems
Example:
PGAContext *ctx;
int fitmintype;
:
fitmintype = PGAGetFitnessMinType(ctx);
switch (fitmintype) {
case PGA_FITNESSMIN_RECIPROCAL:
printf("Fitness Minimization Type = PGA_FITNESSMIN_RECIPROCAL\n");
break;
case PGA_FITNESSMIN_CMAX:
printf("Fitness Minimization Type = PGA_FITNESSMIN_CMAX\n");
break;
}
***************************************************************************U*/
int PGAGetFitnessMinType (PGAContext *ctx)
{
PGADebugEntered("PGAGetFitnessMinType");
PGAFailIfNotSetUp("PGAGetFitnessType");
PGADebugExited("PGAGetFitnessMinType");
return(ctx->ga.FitnessMinType);
}
/*U***************************************************************************
PGAGetMaxFitnessRank - returns the maximum value used in rank-based
fitness.
Category: Fitness & Evaluation
Inputs:
ctx - context variable
Outputs:
The value of MAX used in rank-based fitness
Example:
PGAContext *ctx;
double max;
:
max = PGAGetMaxFitnessRank(ctx);
***************************************************************************U*/
double PGAGetMaxFitnessRank (PGAContext *ctx)
{
PGADebugEntered("PGAGetMaxFitnessRank");
PGAFailIfNotSetUp("PGAGetFitnessType");
PGADebugExited("PGAGetMaxFitnessRank");
return(ctx->ga.FitnessRankMax);
}
/*U****************************************************************************
PGASetFitnessType - Set the type of fitness algorithm to use. Valid choices
are PGA_FITNESS_RAW, PGA_FITNESS_NORMAL, or PGA_FITNESS_RANKING for
raw fitness (the evaluation function value), linear normalization, or
linear ranking, respectively. The default is PGA_FITNESS_RAW.
Category: Fitness & Evaluation
Inputs:
ctx - context variable
fitness_type - symbolic constant to specify fitness type
Outputs:
None
Example:
PGAContext *ctx;
:
PGASetFitnessType(ctx, PGA_FITNESS_RANKING);
****************************************************************************U*/
void PGASetFitnessType( PGAContext *ctx, int fitness_type)
{
PGADebugEntered("PGASetFitnessType");
switch (fitness_type) {
case PGA_FITNESS_RAW:
case PGA_FITNESS_NORMAL:
case PGA_FITNESS_RANKING:
ctx->ga.FitnessType = fitness_type;
break;
default:
PGAError(ctx, "PGASetFitnessType: Invalid value of fitness_type:",
PGA_FATAL, PGA_INT, (void *) &fitness_type);
break;
}
PGADebugExited("PGASetFitnessType");
}
/*U****************************************************************************
PGASetFitnessMinType - sets the type of algorithm used if a minimization
problem is specified to determine how values are remapped for maximization.
Valid choices are PGA_FITNESSMIN_RECIPROCAL and PGA_FITNESSMIN_CMAX to do
the mapping using the reciprocal of the evaluation function, or by
subtracting the worst evaluation function value from each evaluation
function value, respectively. The default is PGA_FITNESSMIN_CMAX
Category: Fitness & Evaluation
Inputs:
ctx - context variable
fitness_type - symbolic constant to specify fitness minimization type
Outputs:
None
Example:
PGAContext *ctx;
:
PGASetFitnessMinType(ctx, PGA_FITNESSMIN_CMAX);
****************************************************************************U*/
void PGASetFitnessMinType( PGAContext *ctx, int fitness_type)
{
PGADebugEntered("PGASetFitnessMinType");
switch (fitness_type) {
case PGA_FITNESSMIN_RECIPROCAL:
case PGA_FITNESSMIN_CMAX:
ctx->ga.FitnessMinType = fitness_type;
break;
default:
PGAError ( ctx,
"PGASetFitnessMinType: Invalid value of fitness_type:",
PGA_FATAL, PGA_INT, (void *) &fitness_type);
break;
}
PGADebugExited("PGASetFitnessMinType");
}
/*U****************************************************************************
PGASetMaxFitnessRank - The value of the parameter Max when using linear
ranking for fitness determination. The default value is 1.2. The value
must be from the interval [1.0, 2.0]. The fitness type must have been set
to PGA_FITNESS_RANKING with PGASetFitnessType for this function call
to have any effect.
Category: Fitness & Evaluation
Inputs:
ctx - context variable
max - the value of the parameter Max when using linear ranking
Outputs:
None
Example:
PGAContext *ctx;
:
PGASetMaxFitnessRank(ctx, 1.1);
****************************************************************************U*/
void PGASetMaxFitnessRank( PGAContext *ctx, double fitness_rank_max)
{
PGADebugEntered("PGASetMaxFitnessRank");
if ((fitness_rank_max < 1.0) || (fitness_rank_max > 2.0))
PGAError ( ctx,
"PGASetMaxFitnessRank: Invalid value of fitness_rank_max:",
PGA_FATAL, PGA_DOUBLE, (void *) &fitness_rank_max);
else
ctx->ga.FitnessRankMax = fitness_rank_max;
PGADebugExited("PGASetMaxFitnessRank");
}
/*I****************************************************************************
PGAFitnessLinearNormal - Calculates fitness using a ranking method and
linear' ordering. The fitness function is of the form
u(x) = K - ( rank * sigma ) with the constant K equal to the mean of the
evaluation functions, and the decrement sigma equal to the standard
deviation of the same.
Ref: L. Davis, Handbook of Genetic Algorithms, pg. 33
Inputs:
ctx - context variable
pop - population pointer to calculate fitness for
Outputs:
Calculates the fitness for each string in the population via side effect
Example:
****************************************************************************I*/
void PGAFitnessLinearNormal ( PGAContext *ctx, PGAIndividual *pop )
{
int i;
double K, sigma, mean;
PGADebugEntered("PGAFitnessLinearNormal");
/* fill arrays for sorting */
for(i=0;i<ctx->ga.PopSize;i++) {
ctx->scratch.dblscratch[i] = (pop+i)->fitness;
ctx->scratch.intscratch[i] = i;
}
/* calculate parameters for linear normalization */
mean = PGAMean ( ctx, ctx->scratch.dblscratch, ctx->ga.PopSize );
sigma = PGAStddev ( ctx, ctx->scratch.dblscratch, ctx->ga.PopSize, mean );
if (sigma == 0)
sigma = 1;
K = sigma * (double) ctx->ga.PopSize;
PGADblHeapSort ( ctx, ctx->scratch.dblscratch,
ctx->scratch.intscratch,
ctx->ga.PopSize);
for( i=0; i<ctx->ga.PopSize; i++ )
(pop+i)->fitness = K - ( sigma *
(double) PGARank(ctx,i,ctx->scratch.intscratch,ctx->ga.PopSize) );
PGADebugExited("PGAFitnessLinearNormal");
}
/*I****************************************************************************
PGAFitnessLinearRank - Calculates fitness using linear ranking. The fitness
function is of the form 1/N * ( max - (max-min) * ( (i-1)/(N-1) ) ) where
min = 2-max and 1 <= max <= 2.
Ref: J. Baker: Adaptive selection methods for GAs
Ref: J. Baker: Extended selection mechanism in GAs
Ref: J. Grefenstte: A critical look at implicit parallelism
Ref: D. Whitley's linear() function on pp. 121 of ICGA
Inputs:
ctx - context variable
pop - population pointer to calculate fitness for
Outputs:
Calculates the fitness for each string in the population via side effect
Example:
****************************************************************************I*/
void PGAFitnessLinearRank ( PGAContext *ctx, PGAIndividual *pop )
{
double max, min, popsize, rpopsize;
int i;
PGADebugEntered("PGAFitnessLinearRank");
max = ctx->ga.FitnessRankMax;
min = 2. - max;
popsize = (double) ctx->ga.PopSize;
rpopsize = 1.0/popsize;
for(i=0;i<ctx->ga.PopSize;i++) {
ctx->scratch.dblscratch[i] = (pop+i)->fitness;
ctx->scratch.intscratch[i] = i;
}
PGADblHeapSort ( ctx, ctx->scratch.dblscratch,
ctx->scratch.intscratch,
ctx->ga.PopSize);
for(i=0;i<ctx->ga.PopSize;i++) {
(pop+i)->fitness = rpopsize * ( max -
( (max - min) *
( ( (double) PGARank(ctx,i,ctx->scratch.intscratch,ctx->ga.PopSize)
- 1. ) / ( popsize - 1. ) ) ) );
}
PGADebugExited("PGAFitnessLinearRank");
}
/*I****************************************************************************
PGAFitnessMinReciprocal - Calculates fitness in the case of a minimization
problem using the reciprocal of the evaluation function. This is a power law
u(x) = ( a f(x) + b )^k with a=1, b=0, k=-1
Inputs:
ctx - context variable
pop - population pointer to calculate fitness for
Outputs:
Calculates the fitness for each string in the population via side effect
Example:
****************************************************************************I*/
void PGAFitnessMinReciprocal ( PGAContext *ctx, PGAIndividual *pop )
{
int i;
PGADebugEntered("PGAFitnessMinReciprocal");
for( i=0; i<ctx->ga.PopSize; i++ ) {
if ( (pop+i)->fitness != 0. )
(pop+i)->fitness = 1. / (pop+i)->fitness;
else
PGAError( ctx,
"PGAFitnessReciprocal: Value 0.0 for fitness member:",
PGA_FATAL,
PGA_INT,
(void *) &i );
}
PGADebugExited("PGAFitnessMinReciprocal");
}
/*I****************************************************************************
PGAFitnessMinCmax - Calculates fitness in the case of a minimization
problem by subtracting the worst evaluation function value from each
evaluation function. This is a dynamic linear fitness function
u(x) = a f(x) + b(t) with a=-1, b(t) = 1.1 * max f(x)
Inputs:
ctx - context variable
pop - population pointer to calculate fitness for
Outputs:
Calculates the fitness for each string in the population via side effect
Example:
****************************************************************************I*/
void PGAFitnessMinCmax ( PGAContext *ctx, PGAIndividual *pop )
{
int i;
double cmax;
PGADebugEntered("PGAFitnessMinCmax");
cmax = 0.;
for(i=0; i<ctx->ga.PopSize; i++)
if ( (pop+i)->evalfunc > cmax )
cmax = (pop+i)->evalfunc;
cmax *= ctx->ga.FitnessCmaxValue; /* so worst string has nonzero fitness */
for(i=0;i<ctx->ga.PopSize;i++)
(pop+i)->fitness = cmax - (pop+i)->evalfunc;
PGADebugExited("PGAFitnessMinCmax");
}
/*U****************************************************************************
PGASetFitnessCmaxValue - The value of the multiplier used by
PGAFitnessMinCmax so that the worst string has a nonzero fitness.
The default value is 1.01.
Category: Fitness & Evaluation
Inputs:
ctx - context variable
val - the value of the multiplier
Outputs:
None
Example:
PGAContext *ctx;
:
PGASetFitnessCmaxValue(ctx, 1.2);
****************************************************************************U*/
void PGASetFitnessCmaxValue( PGAContext *ctx, double val)
{
PGADebugEntered("PGASetFitnessCmaxValue");
ctx->ga.FitnessCmaxValue = val;
PGADebugExited("PGASetFitnessCmaxValue");
}
/*U***************************************************************************
PGAGetFitnessCmaxValue - returns the value of the multiplier used by
PGAFitnessMinCmax.
Category: Fitness & Evaluation
Inputs:
ctx - context variable
Outputs:
The value of Cmax used in
Example:
PGAContext *ctx;
double cmax;
:
cmax = PGAGetFitnessCmaxValue(ctx);
***************************************************************************U*/
double PGAGetFitnessCmaxValue (PGAContext *ctx)
{
PGADebugEntered("PGAGetFitnessCmaxValue");
PGAFailIfNotSetUp("PGAGetFitnessType");
PGADebugExited("PGAGetFitnessCmaxValue");
return(ctx->ga.FitnessCmaxValue);
}
|