1 | /* |
---|
2 | * |
---|
3 | * ********************************************************************* |
---|
4 | * (C) COPYRIGHT 1995 UNIVERSITY OF CHICAGO |
---|
5 | * ********************************************************************* |
---|
6 | * |
---|
7 | * This software was authored by |
---|
8 | * |
---|
9 | * D. Levine |
---|
10 | * Mathematics and Computer Science Division Argonne National Laboratory |
---|
11 | * Argonne IL 60439 |
---|
12 | * levine@mcs.anl.gov |
---|
13 | * (708) 252-6735 |
---|
14 | * (708) 252-5986 (FAX) |
---|
15 | * |
---|
16 | * with programming assistance of participants in Argonne National |
---|
17 | * Laboratory's SERS program. |
---|
18 | * |
---|
19 | * This program contains material protectable under copyright laws of the |
---|
20 | * United States. Permission is hereby granted to use it, reproduce it, |
---|
21 | * to translate it into another language, and to redistribute it to |
---|
22 | * others at no charge except a fee for transferring a copy, provided |
---|
23 | * that you conspicuously and appropriately publish on each copy the |
---|
24 | * University of Chicago's copyright notice, and the disclaimer of |
---|
25 | * warranty and Government license included below. Further, permission |
---|
26 | * is hereby granted, subject to the same provisions, to modify a copy or |
---|
27 | * copies or any portion of it, and to distribute to others at no charge |
---|
28 | * materials containing or derived from the material. |
---|
29 | * |
---|
30 | * The developers of the software ask that you acknowledge its use in any |
---|
31 | * document referencing work based on the program, such as published |
---|
32 | * research. Also, they ask that you supply to Argonne National |
---|
33 | * Laboratory a copy of any published research referencing work based on |
---|
34 | * the software. |
---|
35 | * |
---|
36 | * Any entity desiring permission for further use must contact: |
---|
37 | * |
---|
38 | * J. Gleeson |
---|
39 | * Industrial Technology Development Center Argonne National Laboratory |
---|
40 | * Argonne IL 60439 |
---|
41 | * gleesonj@smtplink.eid.anl.gov |
---|
42 | * (708) 252-6055 |
---|
43 | * |
---|
44 | * ******************************************************************** |
---|
45 | * DISCLAIMER |
---|
46 | * |
---|
47 | * THIS PROGRAM WAS PREPARED AS AN ACCOUNT OF WORK SPONSORED BY AN AGENCY |
---|
48 | * OF THE UNITED STATES GOVERNMENT. NEITHER THE UNIVERSITY OF CHICAGO, |
---|
49 | * THE UNITED STATES GOVERNMENT NOR ANY OF THEIR EMPLOYEES MAKE ANY |
---|
50 | * WARRANTY, EXPRESS OR IMPLIED, OR ASSUMES ANY LEGAL LIABILITY OR |
---|
51 | * RESPONSIBILITY FOR THE ACCURACY, COMPLETENESS, OR USEFULNESS OF ANY |
---|
52 | * INFORMATION OR PROCESS DISCLOSED, OR REPRESENTS THAT ITS USE WOULD NOT |
---|
53 | * INFRINGE PRIVATELY OWNED RIGHTS. |
---|
54 | * |
---|
55 | * ********************************************************************** |
---|
56 | * GOVERNMENT LICENSE |
---|
57 | * |
---|
58 | * The Government is granted for itself and others acting on its behalf a |
---|
59 | * paid-up, non-exclusive, irrevocable worldwide license in this computer |
---|
60 | * software to reproduce, prepare derivative works, and perform publicly |
---|
61 | * and display publicly. |
---|
62 | */ |
---|
63 | |
---|
64 | /***************************************************************************** |
---|
65 | * FILE: fitness.c: This file contains the routines that have to do with |
---|
66 | * fitness calculations. |
---|
67 | * |
---|
68 | * Authors: David M. Levine, Philip L. Hallstrom, David M. Noelle, |
---|
69 | * Brian P. Walenz |
---|
70 | *****************************************************************************/ |
---|
71 | |
---|
72 | #include "pgapack.h" |
---|
73 | |
---|
74 | /*U**************************************************************************** |
---|
75 | PGAFitness - Maps the user's evaluation function value to a fitness value. |
---|
76 | First, the user's evaluation function value is translated to all positive |
---|
77 | values if any are negative. Next, this positive sequence is translated to |
---|
78 | a maximization problem if the user's optimization direction was minimization. |
---|
79 | This positive sequence is then mapped to a fitness value using linear |
---|
80 | ranking, linear normalization fitness, or the identity (i.e., the evaluation |
---|
81 | function value). This routine is usually used after PGAEvaluate is called. |
---|
82 | |
---|
83 | Category: Fitness & Evaluation |
---|
84 | |
---|
85 | Inputs: |
---|
86 | ctx - context variable |
---|
87 | pop - symbolic constant of the population to calculate fitness for |
---|
88 | |
---|
89 | Outputs: |
---|
90 | Calculates the fitness for each string in the population via side effect |
---|
91 | |
---|
92 | Example: |
---|
93 | Calculate the fitness of all strings in population PGA_NEWPOP after |
---|
94 | calling PGAEvaluate to calculate the strings evaluation value. |
---|
95 | |
---|
96 | double energy(PGAContext *ctx, int p, int pop); |
---|
97 | PGAContext *ctx; |
---|
98 | : |
---|
99 | PGAEvaluate(ctx, PGA_NEWPOP, energy); |
---|
100 | PGAFitness (ctx, PGA_NEWPOP); |
---|
101 | |
---|
102 | ****************************************************************************U*/ |
---|
103 | void PGAFitness ( PGAContext *ctx, int popindex ) |
---|
104 | { |
---|
105 | int i; |
---|
106 | double mineval; |
---|
107 | PGAIndividual *pop; |
---|
108 | |
---|
109 | PGADebugEntered("PGAFitness"); |
---|
110 | |
---|
111 | /* set pointer to appropriate population */ |
---|
112 | |
---|
113 | switch (popindex) { |
---|
114 | case PGA_OLDPOP: |
---|
115 | pop = ctx->ga.oldpop; |
---|
116 | break; |
---|
117 | case PGA_NEWPOP: |
---|
118 | pop = ctx->ga.newpop; |
---|
119 | break; |
---|
120 | default: |
---|
121 | PGAError( ctx, "PGAFitness: Invalid value of popindex:", |
---|
122 | PGA_FATAL, PGA_INT, (void *) &popindex ); |
---|
123 | break; |
---|
124 | } |
---|
125 | |
---|
126 | /* make sure all evaluation function values are up-to-date */ |
---|
127 | |
---|
128 | for( i=0; i<ctx->ga.PopSize; i++ ) { |
---|
129 | /*printf("i = %d, evaluptodate = %d\n",i,(pop+i)->evaluptodate);*/ |
---|
130 | if ( (pop+i)->evaluptodate != PGA_TRUE ) |
---|
131 | PGAError( ctx, "PGAFitness: evaluptodate not PGA_TRUE for:", |
---|
132 | PGA_FATAL, PGA_INT, (void *) &i ); |
---|
133 | } |
---|
134 | |
---|
135 | /* put raw fitness into fitness field */ |
---|
136 | |
---|
137 | for( i=0; i<ctx->ga.PopSize; i++ ) |
---|
138 | (pop+i)->fitness = (pop+i)->evalfunc; |
---|
139 | |
---|
140 | /* translate to all positive sequence (if necessary) */ |
---|
141 | |
---|
142 | mineval = ctx->sys.PGAMaxDouble; |
---|
143 | for( i=0; i<ctx->ga.PopSize; i++ ) |
---|
144 | if ( (pop+i)->fitness < mineval ) |
---|
145 | mineval =(pop+i)->fitness; |
---|
146 | if ( mineval < 0.0 ) { |
---|
147 | mineval = (-1.01) * mineval; |
---|
148 | for( i=0; i<ctx->ga.PopSize; i++ ) |
---|
149 | (pop+i)->fitness = (pop+i)->fitness + mineval; |
---|
150 | } |
---|
151 | |
---|
152 | /* translate to maximization problem (if necessary) */ |
---|
153 | |
---|
154 | if ( ctx->ga.optdir == PGA_MINIMIZE ) { |
---|
155 | switch (ctx->ga.FitnessMinType) { |
---|
156 | case PGA_FITNESSMIN_RECIPROCAL: |
---|
157 | PGAFitnessMinReciprocal( ctx, pop ); |
---|
158 | break; |
---|
159 | case PGA_FITNESSMIN_CMAX: |
---|
160 | PGAFitnessMinCmax ( ctx, pop ); |
---|
161 | break; |
---|
162 | default: |
---|
163 | PGAError( ctx, |
---|
164 | "PGAFitness: Invalid FitnessMinType:", |
---|
165 | PGA_FATAL, |
---|
166 | PGA_INT, |
---|
167 | (void *) &(ctx->ga.FitnessMinType) ); |
---|
168 | break; |
---|
169 | } |
---|
170 | } |
---|
171 | |
---|
172 | /* last step in fitness calculation */ |
---|
173 | |
---|
174 | if ( ctx->fops.Rank ) { |
---|
175 | (ctx->fops.Rank)(&ctx, &popindex); |
---|
176 | } else if ( ctx->cops.Rank ) { |
---|
177 | (ctx->cops.Rank)(ctx, popindex); |
---|
178 | } else { |
---|
179 | switch (ctx->ga.FitnessType) { |
---|
180 | case PGA_FITNESS_RAW: |
---|
181 | break; |
---|
182 | case PGA_FITNESS_NORMAL: |
---|
183 | PGAFitnessLinearNormal ( ctx, pop ); |
---|
184 | break; |
---|
185 | case PGA_FITNESS_RANKING: |
---|
186 | PGAFitnessLinearRank ( ctx, pop ); |
---|
187 | break; |
---|
188 | default: |
---|
189 | PGAError( ctx, |
---|
190 | "PGAFitness: Invalid FitnessType:", |
---|
191 | PGA_FATAL, |
---|
192 | PGA_INT, |
---|
193 | (void *) &(ctx->ga.FitnessType) ); |
---|
194 | break; |
---|
195 | } |
---|
196 | } |
---|
197 | PGADebugExited("PGAFitness"); |
---|
198 | } |
---|
199 | |
---|
200 | |
---|
201 | /*U**************************************************************************** |
---|
202 | PGARank - returns the rank of a string in a population. This is a value |
---|
203 | between 1,...,N (the population size). The most fit string has rank 1, |
---|
204 | the least fit string has rank N. |
---|
205 | |
---|
206 | Category: Fitness & Evaluation |
---|
207 | |
---|
208 | Inputs: |
---|
209 | ctx - context variable |
---|
210 | p - the index of the string whose rank is desired |
---|
211 | order - an array containing a unique rank for each string |
---|
212 | n - the size of the array order |
---|
213 | |
---|
214 | Outputs: |
---|
215 | The rank of string p |
---|
216 | |
---|
217 | Example: |
---|
218 | Determine the rank of string p. |
---|
219 | |
---|
220 | PGAContext *ctx; |
---|
221 | int i, popsize, rank, *order; |
---|
222 | double *fitness; |
---|
223 | |
---|
224 | popsize = PGAGetPopsize(ctx); |
---|
225 | order = (int *) malloc(sizeof(int) * popsize); |
---|
226 | fitness = (double *)malloc(sizeof(double) * popsize); |
---|
227 | |
---|
228 | for(i=0;i<popsize; i++) { |
---|
229 | fitness[i] = PGAGetFitness(ctx, p, PGA_OLDPOP); |
---|
230 | order[i] = i; |
---|
231 | } |
---|
232 | |
---|
233 | PGADblHeapSort(ctx, fitness, order, popsize); |
---|
234 | rank = PGARank(ctx, p, order, popsize) |
---|
235 | |
---|
236 | ****************************************************************************U*/ |
---|
237 | int PGARank( PGAContext *ctx, int p, int *order, int n ) |
---|
238 | { |
---|
239 | int i; |
---|
240 | |
---|
241 | PGADebugEntered("PGARank"); |
---|
242 | |
---|
243 | /* If the user gives us PGA_TEMP1 or PGA_TEMP2 (or, gasp, some random |
---|
244 | * number that is not in the population), fail. |
---|
245 | */ |
---|
246 | if ((p<0) || (p > PGAGetPopSize(ctx))) |
---|
247 | PGAError(ctx, "PGARank: Not a valid population member, p = ", |
---|
248 | PGA_FATAL, PGA_INT, (void *)&p); |
---|
249 | |
---|
250 | /* Search through all the orderings until we find the one that |
---|
251 | * matches the given string. Return the index number. If we do not |
---|
252 | * find one, something is _very_ bad; terminate with a fatal error. |
---|
253 | */ |
---|
254 | for(i=0; i<n; i++) |
---|
255 | if (order[i] == p) { |
---|
256 | PGADebugExited("PGARank"); |
---|
257 | return(i+1); |
---|
258 | } |
---|
259 | |
---|
260 | /* Ideally, we should print out the order array, but, well, ideally, |
---|
261 | * we should never get here anyway...Also, to make some compilers |
---|
262 | * shut up, return(0) is here, even though PGAError doesn't return. |
---|
263 | */ |
---|
264 | PGAError( ctx, "PGARank: Bottom of loop in rank, p = ", PGA_FATAL, |
---|
265 | PGA_INT, (void *) &p ); |
---|
266 | return(0); |
---|
267 | } |
---|
268 | |
---|
269 | /*U*************************************************************************** |
---|
270 | PGAGetFitness - returns the fitness value for a string |
---|
271 | |
---|
272 | Category: Fitness & Evaluation |
---|
273 | |
---|
274 | Inputs: |
---|
275 | ctx - context variable |
---|
276 | p - string index |
---|
277 | pop - symbolic constant of the population the string is in |
---|
278 | |
---|
279 | Outputs: |
---|
280 | The fitness value for string p in population pop |
---|
281 | |
---|
282 | Example: |
---|
283 | PGAContext *ctx; |
---|
284 | int p; |
---|
285 | double fit; |
---|
286 | : |
---|
287 | fit = PGAGetFitness(ctx, p, PGA_NEWPOP); |
---|
288 | |
---|
289 | ***************************************************************************U*/ |
---|
290 | double PGAGetFitness ( PGAContext *ctx, int p, int pop ) |
---|
291 | { |
---|
292 | PGAIndividual *ind; |
---|
293 | |
---|
294 | PGADebugEntered("PGAGetFitness"); |
---|
295 | PGADebugPrint( ctx, PGA_DEBUG_PRINTVAR,"PGAGetFitness", "p = ", |
---|
296 | PGA_INT, (void *) &p ); |
---|
297 | PGADebugPrint( ctx, PGA_DEBUG_PRINTVAR,"PGAGetFitness", "pop = ", |
---|
298 | PGA_INT, (void *) &pop ); |
---|
299 | |
---|
300 | ind = PGAGetIndividual ( ctx, p, pop ); |
---|
301 | |
---|
302 | PGADebugExited("PGAGetFitness"); |
---|
303 | |
---|
304 | return(ind->evalfunc); |
---|
305 | } |
---|
306 | |
---|
307 | /*U*************************************************************************** |
---|
308 | PGAGetFitnessType - Returns the type of fitness transformation used. |
---|
309 | |
---|
310 | Category: Fitness & Evaluation |
---|
311 | |
---|
312 | Inputs: |
---|
313 | ctx - context variable |
---|
314 | |
---|
315 | Outputs: |
---|
316 | Returns the integer corresponding to the symbolic constant |
---|
317 | used to specify the type of fitness transformation used |
---|
318 | |
---|
319 | Example: |
---|
320 | PGAContext *ctx; |
---|
321 | int fittype; |
---|
322 | : |
---|
323 | fittype = PGAGetFitnessType(ctx); |
---|
324 | switch (fittype) { |
---|
325 | case PGA_FITNESS_RAW: |
---|
326 | printf("Fitness Type = PGA_FITNESS_RAW\n"); |
---|
327 | break; |
---|
328 | case PGA_FITNESS_NORMAL: |
---|
329 | printf("Fitness Type = PGA_FITNESS_NORMAL\n"); |
---|
330 | break; |
---|
331 | case PGA_FITNESS_RANKING: |
---|
332 | printf("Fitness Type = PGA_FITNESS_RANKING\n"); |
---|
333 | break; |
---|
334 | } |
---|
335 | |
---|
336 | ***************************************************************************U*/ |
---|
337 | int PGAGetFitnessType (PGAContext *ctx) |
---|
338 | { |
---|
339 | PGADebugEntered("PGAGetFitnessType"); |
---|
340 | PGAFailIfNotSetUp("PGAGetFitnessType"); |
---|
341 | |
---|
342 | PGADebugExited("PGAGetFitnessType"); |
---|
343 | |
---|
344 | return(ctx->ga.FitnessType); |
---|
345 | } |
---|
346 | |
---|
347 | /*U*************************************************************************** |
---|
348 | PGAGetFitnessMinType - Returns the type of fitness transformation used |
---|
349 | for minimization problems. |
---|
350 | |
---|
351 | Category: Fitness & Evaluation |
---|
352 | |
---|
353 | Inputs: |
---|
354 | ctx - context variable |
---|
355 | |
---|
356 | Outputs: |
---|
357 | Returns the integer corresponding to the symbolic constant |
---|
358 | used to specify the type of fitness transformation used |
---|
359 | for minimization problems |
---|
360 | |
---|
361 | Example: |
---|
362 | PGAContext *ctx; |
---|
363 | int fitmintype; |
---|
364 | : |
---|
365 | fitmintype = PGAGetFitnessMinType(ctx); |
---|
366 | switch (fitmintype) { |
---|
367 | case PGA_FITNESSMIN_RECIPROCAL: |
---|
368 | printf("Fitness Minimization Type = PGA_FITNESSMIN_RECIPROCAL\n"); |
---|
369 | break; |
---|
370 | case PGA_FITNESSMIN_CMAX: |
---|
371 | printf("Fitness Minimization Type = PGA_FITNESSMIN_CMAX\n"); |
---|
372 | break; |
---|
373 | } |
---|
374 | |
---|
375 | ***************************************************************************U*/ |
---|
376 | int PGAGetFitnessMinType (PGAContext *ctx) |
---|
377 | { |
---|
378 | PGADebugEntered("PGAGetFitnessMinType"); |
---|
379 | PGAFailIfNotSetUp("PGAGetFitnessType"); |
---|
380 | |
---|
381 | PGADebugExited("PGAGetFitnessMinType"); |
---|
382 | |
---|
383 | return(ctx->ga.FitnessMinType); |
---|
384 | } |
---|
385 | |
---|
386 | /*U*************************************************************************** |
---|
387 | PGAGetMaxFitnessRank - returns the maximum value used in rank-based |
---|
388 | fitness. |
---|
389 | |
---|
390 | Category: Fitness & Evaluation |
---|
391 | |
---|
392 | Inputs: |
---|
393 | ctx - context variable |
---|
394 | |
---|
395 | Outputs: |
---|
396 | The value of MAX used in rank-based fitness |
---|
397 | |
---|
398 | Example: |
---|
399 | PGAContext *ctx; |
---|
400 | double max; |
---|
401 | : |
---|
402 | max = PGAGetMaxFitnessRank(ctx); |
---|
403 | |
---|
404 | ***************************************************************************U*/ |
---|
405 | double PGAGetMaxFitnessRank (PGAContext *ctx) |
---|
406 | { |
---|
407 | PGADebugEntered("PGAGetMaxFitnessRank"); |
---|
408 | PGAFailIfNotSetUp("PGAGetFitnessType"); |
---|
409 | |
---|
410 | PGADebugExited("PGAGetMaxFitnessRank"); |
---|
411 | |
---|
412 | return(ctx->ga.FitnessRankMax); |
---|
413 | } |
---|
414 | |
---|
415 | /*U**************************************************************************** |
---|
416 | PGASetFitnessType - Set the type of fitness algorithm to use. Valid choices |
---|
417 | are PGA_FITNESS_RAW, PGA_FITNESS_NORMAL, or PGA_FITNESS_RANKING for |
---|
418 | raw fitness (the evaluation function value), linear normalization, or |
---|
419 | linear ranking, respectively. The default is PGA_FITNESS_RAW. |
---|
420 | |
---|
421 | Category: Fitness & Evaluation |
---|
422 | |
---|
423 | Inputs: |
---|
424 | ctx - context variable |
---|
425 | fitness_type - symbolic constant to specify fitness type |
---|
426 | |
---|
427 | Outputs: |
---|
428 | None |
---|
429 | |
---|
430 | Example: |
---|
431 | PGAContext *ctx; |
---|
432 | : |
---|
433 | PGASetFitnessType(ctx, PGA_FITNESS_RANKING); |
---|
434 | |
---|
435 | ****************************************************************************U*/ |
---|
436 | void PGASetFitnessType( PGAContext *ctx, int fitness_type) |
---|
437 | { |
---|
438 | |
---|
439 | PGADebugEntered("PGASetFitnessType"); |
---|
440 | |
---|
441 | switch (fitness_type) { |
---|
442 | case PGA_FITNESS_RAW: |
---|
443 | case PGA_FITNESS_NORMAL: |
---|
444 | case PGA_FITNESS_RANKING: |
---|
445 | ctx->ga.FitnessType = fitness_type; |
---|
446 | break; |
---|
447 | default: |
---|
448 | PGAError(ctx, "PGASetFitnessType: Invalid value of fitness_type:", |
---|
449 | PGA_FATAL, PGA_INT, (void *) &fitness_type); |
---|
450 | break; |
---|
451 | } |
---|
452 | |
---|
453 | PGADebugExited("PGASetFitnessType"); |
---|
454 | } |
---|
455 | |
---|
456 | /*U**************************************************************************** |
---|
457 | PGASetFitnessMinType - sets the type of algorithm used if a minimization |
---|
458 | problem is specified to determine how values are remapped for maximization. |
---|
459 | Valid choices are PGA_FITNESSMIN_RECIPROCAL and PGA_FITNESSMIN_CMAX to do |
---|
460 | the mapping using the reciprocal of the evaluation function, or by |
---|
461 | subtracting the worst evaluation function value from each evaluation |
---|
462 | function value, respectively. The default is PGA_FITNESSMIN_CMAX |
---|
463 | |
---|
464 | Category: Fitness & Evaluation |
---|
465 | |
---|
466 | Inputs: |
---|
467 | ctx - context variable |
---|
468 | fitness_type - symbolic constant to specify fitness minimization type |
---|
469 | |
---|
470 | Outputs: |
---|
471 | None |
---|
472 | |
---|
473 | Example: |
---|
474 | PGAContext *ctx; |
---|
475 | : |
---|
476 | PGASetFitnessMinType(ctx, PGA_FITNESSMIN_CMAX); |
---|
477 | |
---|
478 | ****************************************************************************U*/ |
---|
479 | void PGASetFitnessMinType( PGAContext *ctx, int fitness_type) |
---|
480 | { |
---|
481 | |
---|
482 | PGADebugEntered("PGASetFitnessMinType"); |
---|
483 | |
---|
484 | switch (fitness_type) { |
---|
485 | case PGA_FITNESSMIN_RECIPROCAL: |
---|
486 | case PGA_FITNESSMIN_CMAX: |
---|
487 | ctx->ga.FitnessMinType = fitness_type; |
---|
488 | break; |
---|
489 | default: |
---|
490 | PGAError ( ctx, |
---|
491 | "PGASetFitnessMinType: Invalid value of fitness_type:", |
---|
492 | PGA_FATAL, PGA_INT, (void *) &fitness_type); |
---|
493 | break; |
---|
494 | } |
---|
495 | |
---|
496 | PGADebugExited("PGASetFitnessMinType"); |
---|
497 | } |
---|
498 | |
---|
499 | /*U**************************************************************************** |
---|
500 | PGASetMaxFitnessRank - The value of the parameter Max when using linear |
---|
501 | ranking for fitness determination. The default value is 1.2. The value |
---|
502 | must be from the interval [1.0, 2.0]. The fitness type must have been set |
---|
503 | to PGA_FITNESS_RANKING with PGASetFitnessType for this function call |
---|
504 | to have any effect. |
---|
505 | |
---|
506 | Category: Fitness & Evaluation |
---|
507 | |
---|
508 | Inputs: |
---|
509 | ctx - context variable |
---|
510 | max - the value of the parameter Max when using linear ranking |
---|
511 | |
---|
512 | Outputs: |
---|
513 | None |
---|
514 | |
---|
515 | Example: |
---|
516 | PGAContext *ctx; |
---|
517 | : |
---|
518 | PGASetMaxFitnessRank(ctx, 1.1); |
---|
519 | |
---|
520 | ****************************************************************************U*/ |
---|
521 | void PGASetMaxFitnessRank( PGAContext *ctx, double fitness_rank_max) |
---|
522 | { |
---|
523 | PGADebugEntered("PGASetMaxFitnessRank"); |
---|
524 | |
---|
525 | if ((fitness_rank_max < 1.0) || (fitness_rank_max > 2.0)) |
---|
526 | PGAError ( ctx, |
---|
527 | "PGASetMaxFitnessRank: Invalid value of fitness_rank_max:", |
---|
528 | PGA_FATAL, PGA_DOUBLE, (void *) &fitness_rank_max); |
---|
529 | else |
---|
530 | ctx->ga.FitnessRankMax = fitness_rank_max; |
---|
531 | |
---|
532 | PGADebugExited("PGASetMaxFitnessRank"); |
---|
533 | } |
---|
534 | |
---|
535 | |
---|
536 | |
---|
537 | /*I**************************************************************************** |
---|
538 | PGAFitnessLinearNormal - Calculates fitness using a ranking method and |
---|
539 | linear' ordering. The fitness function is of the form |
---|
540 | u(x) = K - ( rank * sigma ) with the constant K equal to the mean of the |
---|
541 | evaluation functions, and the decrement sigma equal to the standard |
---|
542 | deviation of the same. |
---|
543 | Ref: L. Davis, Handbook of Genetic Algorithms, pg. 33 |
---|
544 | |
---|
545 | Inputs: |
---|
546 | ctx - context variable |
---|
547 | pop - population pointer to calculate fitness for |
---|
548 | |
---|
549 | Outputs: |
---|
550 | Calculates the fitness for each string in the population via side effect |
---|
551 | |
---|
552 | Example: |
---|
553 | |
---|
554 | ****************************************************************************I*/ |
---|
555 | void PGAFitnessLinearNormal ( PGAContext *ctx, PGAIndividual *pop ) |
---|
556 | { |
---|
557 | |
---|
558 | int i; |
---|
559 | double K, sigma, mean; |
---|
560 | |
---|
561 | PGADebugEntered("PGAFitnessLinearNormal"); |
---|
562 | |
---|
563 | /* fill arrays for sorting */ |
---|
564 | |
---|
565 | for(i=0;i<ctx->ga.PopSize;i++) { |
---|
566 | ctx->scratch.dblscratch[i] = (pop+i)->fitness; |
---|
567 | ctx->scratch.intscratch[i] = i; |
---|
568 | } |
---|
569 | |
---|
570 | /* calculate parameters for linear normalization */ |
---|
571 | |
---|
572 | mean = PGAMean ( ctx, ctx->scratch.dblscratch, ctx->ga.PopSize ); |
---|
573 | sigma = PGAStddev ( ctx, ctx->scratch.dblscratch, ctx->ga.PopSize, mean ); |
---|
574 | if (sigma == 0) |
---|
575 | sigma = 1; |
---|
576 | K = sigma * (double) ctx->ga.PopSize; |
---|
577 | PGADblHeapSort ( ctx, ctx->scratch.dblscratch, |
---|
578 | ctx->scratch.intscratch, |
---|
579 | ctx->ga.PopSize); |
---|
580 | |
---|
581 | for( i=0; i<ctx->ga.PopSize; i++ ) |
---|
582 | (pop+i)->fitness = K - ( sigma * |
---|
583 | (double) PGARank(ctx,i,ctx->scratch.intscratch,ctx->ga.PopSize) ); |
---|
584 | |
---|
585 | PGADebugExited("PGAFitnessLinearNormal"); |
---|
586 | } |
---|
587 | |
---|
588 | /*I**************************************************************************** |
---|
589 | PGAFitnessLinearRank - Calculates fitness using linear ranking. The fitness |
---|
590 | function is of the form 1/N * ( max - (max-min) * ( (i-1)/(N-1) ) ) where |
---|
591 | min = 2-max and 1 <= max <= 2. |
---|
592 | Ref: J. Baker: Adaptive selection methods for GAs |
---|
593 | Ref: J. Baker: Extended selection mechanism in GAs |
---|
594 | Ref: J. Grefenstte: A critical look at implicit parallelism |
---|
595 | Ref: D. Whitley's linear() function on pp. 121 of ICGA |
---|
596 | |
---|
597 | Inputs: |
---|
598 | ctx - context variable |
---|
599 | pop - population pointer to calculate fitness for |
---|
600 | |
---|
601 | Outputs: |
---|
602 | Calculates the fitness for each string in the population via side effect |
---|
603 | |
---|
604 | Example: |
---|
605 | |
---|
606 | ****************************************************************************I*/ |
---|
607 | void PGAFitnessLinearRank ( PGAContext *ctx, PGAIndividual *pop ) |
---|
608 | { |
---|
609 | double max, min, popsize, rpopsize; |
---|
610 | int i; |
---|
611 | |
---|
612 | PGADebugEntered("PGAFitnessLinearRank"); |
---|
613 | |
---|
614 | max = ctx->ga.FitnessRankMax; |
---|
615 | min = 2. - max; |
---|
616 | popsize = (double) ctx->ga.PopSize; |
---|
617 | rpopsize = 1.0/popsize; |
---|
618 | |
---|
619 | for(i=0;i<ctx->ga.PopSize;i++) { |
---|
620 | ctx->scratch.dblscratch[i] = (pop+i)->fitness; |
---|
621 | ctx->scratch.intscratch[i] = i; |
---|
622 | } |
---|
623 | |
---|
624 | PGADblHeapSort ( ctx, ctx->scratch.dblscratch, |
---|
625 | ctx->scratch.intscratch, |
---|
626 | ctx->ga.PopSize); |
---|
627 | |
---|
628 | for(i=0;i<ctx->ga.PopSize;i++) { |
---|
629 | (pop+i)->fitness = rpopsize * ( max - |
---|
630 | ( (max - min) * |
---|
631 | ( ( (double) PGARank(ctx,i,ctx->scratch.intscratch,ctx->ga.PopSize) |
---|
632 | - 1. ) / ( popsize - 1. ) ) ) ); |
---|
633 | |
---|
634 | } |
---|
635 | |
---|
636 | PGADebugExited("PGAFitnessLinearRank"); |
---|
637 | } |
---|
638 | |
---|
639 | |
---|
640 | /*I**************************************************************************** |
---|
641 | PGAFitnessMinReciprocal - Calculates fitness in the case of a minimization |
---|
642 | problem using the reciprocal of the evaluation function. This is a power law |
---|
643 | u(x) = ( a f(x) + b )^k with a=1, b=0, k=-1 |
---|
644 | |
---|
645 | Inputs: |
---|
646 | ctx - context variable |
---|
647 | pop - population pointer to calculate fitness for |
---|
648 | |
---|
649 | Outputs: |
---|
650 | Calculates the fitness for each string in the population via side effect |
---|
651 | |
---|
652 | Example: |
---|
653 | |
---|
654 | ****************************************************************************I*/ |
---|
655 | void PGAFitnessMinReciprocal ( PGAContext *ctx, PGAIndividual *pop ) |
---|
656 | { |
---|
657 | int i; |
---|
658 | |
---|
659 | PGADebugEntered("PGAFitnessMinReciprocal"); |
---|
660 | |
---|
661 | for( i=0; i<ctx->ga.PopSize; i++ ) { |
---|
662 | if ( (pop+i)->fitness != 0. ) |
---|
663 | (pop+i)->fitness = 1. / (pop+i)->fitness; |
---|
664 | else |
---|
665 | PGAError( ctx, |
---|
666 | "PGAFitnessReciprocal: Value 0.0 for fitness member:", |
---|
667 | PGA_FATAL, |
---|
668 | PGA_INT, |
---|
669 | (void *) &i ); |
---|
670 | } |
---|
671 | |
---|
672 | PGADebugExited("PGAFitnessMinReciprocal"); |
---|
673 | } |
---|
674 | |
---|
675 | |
---|
676 | /*I**************************************************************************** |
---|
677 | PGAFitnessMinCmax - Calculates fitness in the case of a minimization |
---|
678 | problem by subtracting the worst evaluation function value from each |
---|
679 | evaluation function. This is a dynamic linear fitness function |
---|
680 | u(x) = a f(x) + b(t) with a=-1, b(t) = 1.1 * max f(x) |
---|
681 | |
---|
682 | Inputs: |
---|
683 | ctx - context variable |
---|
684 | pop - population pointer to calculate fitness for |
---|
685 | |
---|
686 | Outputs: |
---|
687 | Calculates the fitness for each string in the population via side effect |
---|
688 | |
---|
689 | Example: |
---|
690 | |
---|
691 | ****************************************************************************I*/ |
---|
692 | void PGAFitnessMinCmax ( PGAContext *ctx, PGAIndividual *pop ) |
---|
693 | { |
---|
694 | int i; |
---|
695 | double cmax; |
---|
696 | |
---|
697 | PGADebugEntered("PGAFitnessMinCmax"); |
---|
698 | |
---|
699 | cmax = 0.; |
---|
700 | |
---|
701 | for(i=0; i<ctx->ga.PopSize; i++) |
---|
702 | if ( (pop+i)->evalfunc > cmax ) |
---|
703 | cmax = (pop+i)->evalfunc; |
---|
704 | |
---|
705 | cmax *= ctx->ga.FitnessCmaxValue; /* so worst string has nonzero fitness */ |
---|
706 | |
---|
707 | for(i=0;i<ctx->ga.PopSize;i++) |
---|
708 | (pop+i)->fitness = cmax - (pop+i)->evalfunc; |
---|
709 | |
---|
710 | PGADebugExited("PGAFitnessMinCmax"); |
---|
711 | } |
---|
712 | |
---|
713 | |
---|
714 | /*U**************************************************************************** |
---|
715 | PGASetFitnessCmaxValue - The value of the multiplier used by |
---|
716 | PGAFitnessMinCmax so that the worst string has a nonzero fitness. |
---|
717 | The default value is 1.01. |
---|
718 | |
---|
719 | Category: Fitness & Evaluation |
---|
720 | |
---|
721 | Inputs: |
---|
722 | ctx - context variable |
---|
723 | val - the value of the multiplier |
---|
724 | |
---|
725 | Outputs: |
---|
726 | None |
---|
727 | |
---|
728 | Example: |
---|
729 | PGAContext *ctx; |
---|
730 | : |
---|
731 | PGASetFitnessCmaxValue(ctx, 1.2); |
---|
732 | |
---|
733 | ****************************************************************************U*/ |
---|
734 | void PGASetFitnessCmaxValue( PGAContext *ctx, double val) |
---|
735 | { |
---|
736 | PGADebugEntered("PGASetFitnessCmaxValue"); |
---|
737 | ctx->ga.FitnessCmaxValue = val; |
---|
738 | PGADebugExited("PGASetFitnessCmaxValue"); |
---|
739 | } |
---|
740 | |
---|
741 | |
---|
742 | |
---|
743 | /*U*************************************************************************** |
---|
744 | PGAGetFitnessCmaxValue - returns the value of the multiplier used by |
---|
745 | PGAFitnessMinCmax. |
---|
746 | |
---|
747 | Category: Fitness & Evaluation |
---|
748 | |
---|
749 | Inputs: |
---|
750 | ctx - context variable |
---|
751 | |
---|
752 | Outputs: |
---|
753 | The value of Cmax used in |
---|
754 | |
---|
755 | Example: |
---|
756 | PGAContext *ctx; |
---|
757 | double cmax; |
---|
758 | : |
---|
759 | cmax = PGAGetFitnessCmaxValue(ctx); |
---|
760 | |
---|
761 | ***************************************************************************U*/ |
---|
762 | double PGAGetFitnessCmaxValue (PGAContext *ctx) |
---|
763 | { |
---|
764 | PGADebugEntered("PGAGetFitnessCmaxValue"); |
---|
765 | PGAFailIfNotSetUp("PGAGetFitnessType"); |
---|
766 | PGADebugExited("PGAGetFitnessCmaxValue"); |
---|
767 | return(ctx->ga.FitnessCmaxValue); |
---|
768 | } |
---|
769 | |
---|