source: trunk/packages/optimizer/src/pgapack/pgapack/source/stop.c @ 1271

Last change on this file since 1271 was 1271, checked in by liveletlive, 15 years ago

Changes made to API and pgapack for addition of new stoppage criteria. TIMEElapsed criteria is not yet added.

File size: 17.4 KB
Line 
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: stop.c: This file contains routines related to the stopping
66*                   conditions for the GA.
67*
68*     Authors: David M. Levine, Philip L. Hallstrom, David M. Noelle,
69*              Brian P. Walenz
70*****************************************************************************/
71
72#include "pgapack.h"
73
74int *pgapack_abortPtr = NULL;
75
76/*U****************************************************************************
77  PGADone - Returns PGA_TRUE if the stopping conditions have been met,
78  otherwise returns false.  Calls exactly one of the user defined C or
79  fortran or system (PGACheckStoppingConditions) stopping condition functions.
80
81  Category: Generation
82
83  Inputs:
84     ctx  - context variable
85     comm - an MPI communicator
86
87  Outputs:
88     returns PGA_TRUE if at least one of the termination conditions has been
89     met.  Otherwise, returns PGA_FALSE
90
91  Example:
92    PGAContext *ctx;
93    :
94    PGADone(ctx, comm);
95
96****************************************************************************U*/
97int PGADone(PGAContext *ctx, MPI_Comm comm)
98{
99    int rank, size, done;
100
101    PGADebugEntered("PGADone");
102
103    rank = PGAGetRank(ctx, comm);
104    size = PGAGetNumProcs(ctx, comm);
105
106    if (rank == 0) {
107                if (ctx->fops.StopCond)
108                    done = (*ctx->fops.StopCond)(&ctx);
109                else if (ctx->cops.StopCond)
110                    done = (*ctx->cops.StopCond)(ctx);
111                else
112                    done = PGACheckStoppingConditions(ctx);
113    }
114
115    if (size > 1)
116        MPI_Bcast(&done, 1, MPI_INT, 0, comm);
117
118    PGADebugExited("PGADone");
119
120    return(done);
121}
122
123/*U****************************************************************************
124  PGACheckStoppingConditions - returns boolean to indicate if the PGAPack
125  termination conditions -- PGA_STOP_MAXITER, PGA_STOP_TOOSIMILAR,
126  PGA_STOP_NOCHANGE -- have been met.
127
128  Category: Generation
129
130  Inputs:
131     ctx  - context variable
132
133  Outputs:
134     returns PGA_TRUE if at least one of the termination conditions has been
135     met.  Otherwise, returns PGA_FALSE
136
137  Example:
138    PGAContext *ctx;
139    :
140    PGACheckStoppingConditions(ctx);
141
142****************************************************************************U*/
143int PGACheckStoppingConditions( PGAContext *ctx)
144{
145    int done = PGA_FALSE;
146    double diff;
147
148    PGADebugEntered("PGACheckStoppingConditions");
149
150    if (((ctx->ga.StoppingRule & PGA_STOP_MAXITER) == PGA_STOP_MAXITER) &&
151        (ctx->ga.iter > ctx->ga.MaxIter))
152        done |= PGA_TRUE;
153   
154    if (((ctx->ga.StoppingRule & PGA_STOP_NOCHANGE) == PGA_STOP_NOCHANGE) &&
155        (ctx->ga.ItersOfSame >= ctx->ga.MaxNoChange))
156        done |= PGA_TRUE;
157       
158    if (((ctx->ga.StoppingRule & PGA_STOP_TOOSIMILAR) == PGA_STOP_TOOSIMILAR) &&
159        (ctx->ga.PercentSame >= ctx->ga.MaxSimilarity))
160        done |= PGA_TRUE;
161       
162        if(((ctx->ga.StoppingRule & PGA_STOP_AV_FITNESS) == PGA_STOP_AV_FITNESS)){
163                if(ctx->ga.TgtFitnessVal == PGA_UNINITIALIZED_DOUBLE){
164                        fprintf(stderr,"Uninitialized Value 'Target Average'");
165                        done |= PGA_TRUE;
166                }else{
167                        diff = ((ctx->ga.TgtFitnessVal - ctx->rep.Average)/(ctx->ga.TgtFitnessVal))*100;
168                        if(fabs(diff) <= ctx->ga.FitnessTol){
169                                done |= PGA_TRUE;
170                        }
171                }
172        }
173       
174        if(((ctx->ga.StoppingRule & PGA_STOP_BEST_FITNESS) == PGA_STOP_BEST_FITNESS)){
175                if(ctx->ga.TgtFitnessVal == PGA_UNINITIALIZED_DOUBLE){
176                        fprintf(stderr,"Uninitialized Value 'Target Best Fitness'");
177                        done |= PGA_TRUE;
178                }else{
179                        diff =(((double)ctx->ga.TgtFitnessVal -(double)ctx->rep.Best)/((double)ctx->ga.TgtFitnessVal))*100;
180                        if(fabs(diff) <= ctx->ga.FitnessTol){
181                                done |= PGA_TRUE;
182                        }
183                }
184        }
185       
186        if(((ctx->ga.StoppingRule & PGA_STOP_VARIANCE) == PGA_STOP_VARIANCE)){
187                if(ctx->ga.TgtFitnessVar == PGA_UNINITIALIZED_DOUBLE){
188                        fprintf(stderr,"Uninitialized Value 'Target Fitness Variance'");
189                        done |= PGA_TRUE;
190                }else{
191                        diff =(((double)ctx->ga.TgtFitnessVar -(double)ctx->rep.Variance)/((double)ctx->ga.TgtFitnessVar))*100;
192                        if(fabs(diff) <= ctx->ga.VarTol){
193                                done |= PGA_TRUE;
194                        }
195                }
196        }
197       
198        if(((ctx->ga.StoppingRule & PGA_STOP_TIMEELAPSED) == PGA_STOP_TIMEELAPSED)){
199                //TODO :GTG Add code for setting initial time and current time and comparing
200                done |= PGA_TRUE;
201        }
202
203    PGADebugExited("PGACheckStoppingConditions");
204    return(done);
205}
206
207/*U****************************************************************************
208   PGASetStoppingRuleType - specify a stopping criterion.  If called more than
209   once the different stopping criterion are ORed together.  Valid choices
210   are PGA_STOP_MAXITER, PGA_STOP_TOOSIMILAR, or PGA_STOP_NOCHANGE to
211   specify iteration limit reached, population too similar, or no change in
212   the best solution found in a given number of iterations, respectively.
213   The default is to stop when a maximum iteration limit is reached (by
214   default, 1000 iterations).
215
216   Category: Generation
217
218   Inputs:
219      ctx      - context variable
220      stoprule - symbolic constant to specify stopping rule
221
222   Outputs:
223      None
224
225   Example:
226      PGAContext *ctx;
227      :
228      PGASetStoppingRuleType(ctx, PGA_STOP_TOOSIMILAR);
229
230****************************************************************************U*/
231void PGASetStoppingRuleType (PGAContext *ctx, int stoprule)
232{
233
234    PGADebugEntered("PGASetStoppingRuleType");
235    PGAFailIfSetUp("PGASetStoppingRuleType");
236
237    switch (stoprule) {
238        case PGA_STOP_MAXITER  :
239    case PGA_STOP_NOCHANGE :
240        case PGA_STOP_TOOSIMILAR :
241        case PGA_STOP_AV_FITNESS :
242        case PGA_STOP_BEST_FITNESS :
243        case PGA_STOP_VARIANCE :
244        case PGA_STOP_TIMEELAPSED :
245            ctx->ga.StoppingRule |= stoprule;
246            break;
247        default:
248            PGAError( ctx,
249                     "PGASetStoppingRuleType: Invalid value of stoprule:",
250                     PGA_FATAL, PGA_INT, (void *) &stoprule );
251    }
252
253    PGADebugExited("PGASetStoppingRuleType");
254}
255
256/*U***************************************************************************
257   PGAGetStoppingRuleType - Returns a symbolic constant that defines the
258   termination criteria.
259
260   Category: Generation
261
262   Inputs:
263      ctx - context variable
264
265   Outputs:
266      Returns an integer which is an ORed mask of the symbolic constants
267      used to specify the stopping rule(s).
268
269   Example:
270      PGAContext *ctx;
271      int stop;
272      :
273      stop = PGAGetStoppingRuleType(ctx);
274      if (stop & PGA_STOP_MAXITER)
275          printf("Stopping Rule = PGA_STOP_MAXITER\n");
276      if (stop & PGA_STOP_NOCHANGE)
277          printf("Stopping Rule = PGA_STOP_NOCHANGE\n");
278      if (stop & PGA_STOP_TOOSIMILAR)
279          printf("Stopping Rule = PGA_STOP_TOOSIMILAR\n");
280
281***************************************************************************U*/
282int PGAGetStoppingRuleType (PGAContext *ctx)
283{
284    PGADebugEntered("PGAGetStoppingRuleType");
285    PGAFailIfNotSetUp("PGAGetStoppingRuleType");
286
287    PGADebugExited("PGAGetStoppingRuleType");
288
289    return(ctx->ga.StoppingRule);
290}
291
292/*U****************************************************************************
293   PGASetMaxGAIterValue - specify the maximum number of iterations for the
294   stopping rule PGA_STOP_MAXITER (which, by itself, is the default stopping
295   rule and is always in effect).  The default value is 1000 iterations.
296
297   Category: Generation
298
299   Inputs:
300      ctx     - context variable
301      maxiter - the maximum number of GA iterations to run before stopping
302
303   Outputs:
304      None
305
306   Example:
307      PGAContext *ctx;
308      :
309      PGASetMaxGAIterValue(ctx,5000);
310
311****************************************************************************U*/
312void PGASetMaxGAIterValue(PGAContext *ctx, int maxiter)
313{
314
315    PGADebugEntered("PGASetMaxGAIterValue");
316    PGAFailIfSetUp("PGASetMaxGAIterValue");
317
318    if (maxiter < 1)
319        PGAError( ctx, "PGASetMaxGAIterValue: Invalid value of maxiter:",
320                 PGA_FATAL, PGA_INT, (void *) &maxiter );
321    else
322        ctx->ga.MaxIter = maxiter;
323   
324    PGADebugExited("PGASetMaxGAIterValue");
325}
326
327/*U***************************************************************************
328   PGAGetMaxGAIterValue - Returns the maximum number of iterations to run
329
330   Category: Generation
331
332   Inputs:
333      ctx - context variable
334
335   Outputs:
336      The maximum number of iterations to run
337
338   Example:
339      PGAContext *ctx;
340      int maxiter;
341      :
342      maxiter = PGAGetMaxGAIterValue(ctx);
343
344***************************************************************************U*/
345int PGAGetMaxGAIterValue (PGAContext *ctx)
346{
347    PGADebugEntered("PGAGetMaxGAIterValue");
348    PGAFailIfNotSetUp("PGAGetMaxGAIterValue");
349
350    PGADebugExited("PGAGetMaxGAIterValue");
351
352    return(ctx->ga.MaxIter);
353}
354
355/*U****************************************************************************
356   PGASetMaxNoChangeValue - specifiy maximum number of iterations of no change
357   in the evaluation function value of the best string before stopping.  The
358   default value is 50.  The stopping rule PGA_STOP_NOCHANGE must have been
359   set by PGASetStoppingRuleType for this function call to have any effect.
360
361   Category: Generation
362
363   Inputs:
364      ctx     - context variable
365      maxiter - the maximum number of GA iterations allowed with no change
366                in the best evaluation function value.
367
368   Outputs:
369      None
370
371   Example:
372      PGAContext *ctx;
373      :
374      PGASetMaxGAIterValue(ctx,5000);
375
376****************************************************************************U*/
377void PGASetMaxNoChangeValue(PGAContext *ctx, int max_no_change)
378{
379    PGADebugEntered("PGASetMaxNoChangeValue");
380    PGAFailIfSetUp("PGASetMaxNoChangeValue");
381
382    if (max_no_change <= 0)
383        PGAError(ctx, "PGASetMaxNoChangeValue: max_no_change invalid",
384                 PGA_FATAL, PGA_INT, (void *)&max_no_change);
385   
386    ctx->ga.MaxNoChange = max_no_change;
387   
388    PGADebugExited("PGASetMaxNoChangeValue");
389}
390
391/*U****************************************************************************
392   PGASetMaxSimilarityValue - Specifiy the maximum percent of homogeneity of
393   the population before stopping.  The similarity measure is the same
394   evaluation function value.  The default value is 95 percent.  The stopping
395   rule PGA_STOP_TOOSIMILAR must have been set by PGASetStoppingRuleType for
396   this function call to have any effect.
397
398   Category: Generation
399
400   Inputs:
401      ctx            - context variable
402      max_similarity - the maximum percent of the population that can share
403                       the same evaluation function value
404
405   Outputs:
406      None
407
408   Example:
409      PGAContext *ctx;
410      :
411      PGASetMaxSimilarityValue(ctx,99);
412
413****************************************************************************U*/
414void PGASetMaxSimilarityValue(PGAContext *ctx, int max_similarity)
415{
416    PGADebugEntered("PGASetMaxSimilarityValue");
417    PGAFailIfSetUp("PGASetMaxSimilarityValue");
418
419    if ((max_similarity <= 0) || (max_similarity > 100))
420        PGAError(ctx, "PGASetMaxSimilarityValue: max_similarity invalid",
421                 PGA_FATAL, PGA_INT, (void *) &max_similarity);
422   
423    ctx->ga.MaxSimilarity = max_similarity;
424
425    PGADebugExited("PGASetMaxSimilarityValue");
426}
427
428void PGASetAbortVar(int *abortPtr)
429{
430    pgapack_abortPtr = abortPtr;
431}
432
433
434/*U****************************************************************************
435   PGASetTgtFitnessVal
436
437   Category: Initialization
438
439   Inputs:
440      ctx         - context variable
441      tgt_fitness_val - tgt fitness required for stopping. either target average or best fitness depending
442                                on stoppage criteria.
443                                Requires setting fitness tolerance using PGASetFitnessTol in order to specify
444                                what % difeerence between current and target fitness is acceptable.
445
446   Outputs:
447      None
448
449   Example:
450      PGAContext *ctx;
451      :
452      PGASetTgtFitnessVal(ctx,0);
453
454****************************************************************************U*/
455void PGASetTgtFitnessVal(PGAContext *ctx, double tgt_fitness_val){
456        PGADebugEntered("PGASetTgtFitnessVal");
457    PGAFailIfSetUp("PGASetTgtFitnessVal");
458        ctx->ga.TgtFitnessVal = tgt_fitness_val;
459        PGADebugExited("PGASetTgtFitnessVal");
460}
461
462/*U****************************************************************************
463   PGASetFitnessTol
464
465   Category: Initialization
466
467   Inputs:
468      ctx - context variable
469      fitness_tol - tolerance between current pop fitness (avg or best, depending on stoppage criteria)
470                target fitness set using PGASetTgtFitnessVal. once this tolerance is achieved, the GA can be stopped.
471
472   Outputs:
473      None
474
475   Example:
476      PGAContext *ctx;
477      :
478      PGASetFitnessTol(ctx,0);
479
480****************************************************************************U*/
481void PGASetFitnessTol(PGAContext *ctx,double fitness_tol){
482        PGADebugEntered("PGASetFitnessTol");
483    PGAFailIfSetUp("PGASetFitnessTol");
484        ctx->ga.FitnessTol = fitness_tol;
485        PGADebugExited("PGASetFitnessTol");
486}
487
488
489/*U****************************************************************************
490   PGASetTgtFitnessVariance
491
492   Category: Initialization
493
494   Inputs:
495      ctx - context variable
496      tgt_fitness_var - tgt fitness variance required for stopping.
497                                Requires setting fitness variance tolerance using PGASetVarTol in order to specify
498                                what % difeerence between current and target fitness variance is acceptable.
499   Outputs:
500      None
501
502   Example:
503      PGAContext *ctx;
504      :
505      PGASetTgtFitnessVariance(ctx,0);
506
507****************************************************************************U*/
508void PGASetTgtFitnessVariance(PGAContext *ctx,double tgt_fitness_var){
509        PGADebugEntered("PGASetTgtFitnessVariance");
510    PGAFailIfSetUp("PGASetTgtFitnessVariance");
511        ctx->ga.TgtFitnessVar = tgt_fitness_var;
512        PGADebugExited("PGASetTgtFitnessVariance");
513}
514
515/*U****************************************************************************
516   PGASetVarTol
517
518   Category: Initialization
519
520   Inputs:
521      ctx - context variable
522      var_tol - tolerance between current pop fitness variance and target fitness set using PGASetTgtFitnessVariance.
523                        Once this tolerance is achieved, the GA can be stopped.
524   Outputs:
525      None
526
527   Example:
528      PGAContext *ctx;
529      :
530      PGASetVarTol(ctx,0);
531
532****************************************************************************U*/
533void PGASetVarTol(PGAContext *ctx,double var_tol){
534        PGADebugEntered("PGASetVarTol");
535    PGAFailIfSetUp("PGASetVarTol");
536        ctx->ga.VarTol = var_tol;
537        PGADebugExited("PGASetVarTol");
538}
539
540/*U****************************************************************************
541   PGASetTgtElapsedTime
542
543   Category: Initialization
544
545   Inputs:
546      ctx - context variable
547      tgt_elapsed_time - stop the GA if stoppage criteria is PGA_STOP_TIMEELAPSED
548                                         and time between start of execution and end of current evaluation
549                                         is equal to tgt_elapsed_time.
550   Outputs:
551      None
552
553   Example:
554      PGAContext *ctx;
555      :
556      PGASetTgtElapsedTime(ctx,0);
557
558****************************************************************************U*/
559void PGASetTgtElapsedTime(PGAContext *ctx,double tgt_elapsed_time){
560        PGADebugEntered("PGASetTgtElapsedTime");
561    PGAFailIfSetUp("PGASetTgtElapsedTime");
562        ctx->ga.TgtElapsedTime = tgt_elapsed_time;
563        PGADebugExited("PGASetTgtElapsedTime");
564}
Note: See TracBrowser for help on using the repository browser.