source: trunk/optimizer/src/pgapack/pgapack/source/stop.c @ 816

Last change on this file since 816 was 816, checked in by liveletlive, 16 years ago

Committing the newer version of PGAPack.

File size: 11.9 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
74/*U****************************************************************************
75  PGADone - Returns PGA_TRUE if the stopping conditions have been met,
76  otherwise returns false.  Calls exactly one of the user defined C or
77  fortran or system (PGACheckStoppingConditions) stopping condition functions.
78
79  Category: Generation
80
81  Inputs:
82     ctx  - context variable
83     comm - an MPI communicator
84
85  Outputs:
86     returns PGA_TRUE if at least one of the termination conditions has been
87     met.  Otherwise, returns PGA_FALSE
88
89  Example:
90    PGAContext *ctx;
91    :
92    PGADone(ctx, comm);
93
94****************************************************************************U*/
95int PGADone(PGAContext *ctx, MPI_Comm comm)
96{
97    int rank, size, done;
98
99    PGADebugEntered("PGADone");
100
101    rank = PGAGetRank(ctx, comm);
102    size = PGAGetNumProcs(ctx, comm);
103
104    if (rank == 0) {
105        if (ctx->fops.StopCond)
106            done = (*ctx->fops.StopCond)(&ctx);
107        else if (ctx->cops.StopCond)
108            done = (*ctx->cops.StopCond)(ctx);
109        else
110            done = PGACheckStoppingConditions(ctx);
111    }
112
113    if (size > 1)
114        MPI_Bcast(&done, 1, MPI_INT, 0, comm);
115
116    PGADebugExited("PGADone");
117
118    return(done);
119}
120
121/*U****************************************************************************
122  PGACheckStoppingConditions - returns boolean to indicate if the PGAPack
123  termination conditions -- PGA_STOP_MAXITER, PGA_STOP_TOOSIMILAR,
124  PGA_STOP_NOCHANGE -- have been met.
125
126  Category: Generation
127
128  Inputs:
129     ctx  - context variable
130
131  Outputs:
132     returns PGA_TRUE if at least one of the termination conditions has been
133     met.  Otherwise, returns PGA_FALSE
134
135  Example:
136    PGAContext *ctx;
137    :
138    PGACheckStoppingConditions(ctx);
139
140****************************************************************************U*/
141int PGACheckStoppingConditions( PGAContext *ctx)
142{
143    int done = PGA_FALSE;
144
145    PGADebugEntered("PGACheckStoppingConditions");
146
147    if (((ctx->ga.StoppingRule & PGA_STOP_MAXITER) == PGA_STOP_MAXITER) &&
148        (ctx->ga.iter > ctx->ga.MaxIter))
149        done |= PGA_TRUE;
150   
151    if (((ctx->ga.StoppingRule & PGA_STOP_NOCHANGE) == PGA_STOP_NOCHANGE) &&
152        (ctx->ga.ItersOfSame >= ctx->ga.MaxNoChange))
153        done |= PGA_TRUE;
154       
155    if (((ctx->ga.StoppingRule & PGA_STOP_TOOSIMILAR) == PGA_STOP_TOOSIMILAR) &&
156        (ctx->ga.PercentSame >= ctx->ga.MaxSimilarity))
157        done |= PGA_TRUE;
158
159    PGADebugExited("PGACheckStoppingConditions");
160    return(done);
161}
162
163/*U****************************************************************************
164   PGASetStoppingRuleType - specify a stopping criterion.  If called more than
165   once the different stopping criterion are ORed together.  Valid choices
166   are PGA_STOP_MAXITER, PGA_STOP_TOOSIMILAR, or PGA_STOP_NOCHANGE to
167   specify iteration limit reached, population too similar, or no change in
168   the best solution found in a given number of iterations, respectively.
169   The default is to stop when a maximum iteration limit is reached (by
170   default, 1000 iterations).
171
172   Category: Generation
173
174   Inputs:
175      ctx      - context variable
176      stoprule - symbolic constant to specify stopping rule
177
178   Outputs:
179      None
180
181   Example:
182      PGAContext *ctx;
183      :
184      PGASetStoppingRuleType(ctx, PGA_STOP_TOOSIMILAR);
185
186****************************************************************************U*/
187void PGASetStoppingRuleType (PGAContext *ctx, int stoprule)
188{
189
190    PGADebugEntered("PGASetStoppingRuleType");
191    PGAFailIfSetUp("PGASetStoppingRuleType");
192
193    switch (stoprule) {
194        case PGA_STOP_MAXITER  :
195        case PGA_STOP_NOCHANGE :
196        case PGA_STOP_TOOSIMILAR :
197            ctx->ga.StoppingRule |= stoprule;
198            break;
199        default:
200            PGAError( ctx,
201                     "PGASetStoppingRuleType: Invalid value of stoprule:",
202                     PGA_FATAL, PGA_INT, (void *) &stoprule );
203    }
204
205    PGADebugExited("PGASetStoppingRuleType");
206}
207
208/*U***************************************************************************
209   PGAGetStoppingRuleType - Returns a symbolic constant that defines the
210   termination criteria.
211
212   Category: Generation
213
214   Inputs:
215      ctx - context variable
216
217   Outputs:
218      Returns an integer which is an ORed mask of the symbolic constants
219      used to specify the stopping rule(s).
220
221   Example:
222      PGAContext *ctx;
223      int stop;
224      :
225      stop = PGAGetStoppingRuleType(ctx);
226      if (stop & PGA_STOP_MAXITER)
227          printf("Stopping Rule = PGA_STOP_MAXITER\n");
228      if (stop & PGA_STOP_NOCHANGE)
229          printf("Stopping Rule = PGA_STOP_NOCHANGE\n");
230      if (stop & PGA_STOP_TOOSIMILAR)
231          printf("Stopping Rule = PGA_STOP_TOOSIMILAR\n");
232
233***************************************************************************U*/
234int PGAGetStoppingRuleType (PGAContext *ctx)
235{
236    PGADebugEntered("PGAGetStoppingRuleType");
237    PGAFailIfNotSetUp("PGAGetStoppingRuleType");
238
239    PGADebugExited("PGAGetStoppingRuleType");
240
241    return(ctx->ga.StoppingRule);
242}
243
244/*U****************************************************************************
245   PGASetMaxGAIterValue - specify the maximum number of iterations for the
246   stopping rule PGA_STOP_MAXITER (which, by itself, is the default stopping
247   rule and is always in effect).  The default value is 1000 iterations.
248
249   Category: Generation
250
251   Inputs:
252      ctx     - context variable
253      maxiter - the maximum number of GA iterations to run before stopping
254
255   Outputs:
256      None
257
258   Example:
259      PGAContext *ctx;
260      :
261      PGASetMaxGAIterValue(ctx,5000);
262
263****************************************************************************U*/
264void PGASetMaxGAIterValue(PGAContext *ctx, int maxiter)
265{
266
267    PGADebugEntered("PGASetMaxGAIterValue");
268    PGAFailIfSetUp("PGASetMaxGAIterValue");
269
270    if (maxiter < 1)
271        PGAError( ctx, "PGASetMaxGAIterValue: Invalid value of maxiter:",
272                 PGA_FATAL, PGA_INT, (void *) &maxiter );
273    else
274        ctx->ga.MaxIter = maxiter;
275   
276    PGADebugExited("PGASetMaxGAIterValue");
277}
278
279/*U***************************************************************************
280   PGAGetMaxGAIterValue - Returns the maximum number of iterations to run
281
282   Category: Generation
283
284   Inputs:
285      ctx - context variable
286
287   Outputs:
288      The maximum number of iterations to run
289
290   Example:
291      PGAContext *ctx;
292      int maxiter;
293      :
294      maxiter = PGAGetMaxGAIterValue(ctx);
295
296***************************************************************************U*/
297int PGAGetMaxGAIterValue (PGAContext *ctx)
298{
299    PGADebugEntered("PGAGetMaxGAIterValue");
300    PGAFailIfNotSetUp("PGAGetMaxGAIterValue");
301
302    PGADebugExited("PGAGetMaxGAIterValue");
303
304    return(ctx->ga.MaxIter);
305}
306
307/*U****************************************************************************
308   PGASetMaxNoChangeValue - specifiy maximum number of iterations of no change
309   in the evaluation function value of the best string before stopping.  The
310   default value is 50.  The stopping rule PGA_STOP_NOCHANGE must have been
311   set by PGASetStoppingRuleType for this function call to have any effect.
312
313   Category: Generation
314
315   Inputs:
316      ctx     - context variable
317      maxiter - the maximum number of GA iterations allowed with no change
318                in the best evaluation function value.
319
320   Outputs:
321      None
322
323   Example:
324      PGAContext *ctx;
325      :
326      PGASetMaxGAIterValue(ctx,5000);
327
328****************************************************************************U*/
329void PGASetMaxNoChangeValue(PGAContext *ctx, int max_no_change)
330{
331    PGADebugEntered("PGASetMaxNoChangeValue");
332    PGAFailIfSetUp("PGASetMaxNoChangeValue");
333
334    if (max_no_change <= 0)
335        PGAError(ctx, "PGASetMaxNoChangeValue: max_no_change invalid",
336                 PGA_FATAL, PGA_INT, (void *)&max_no_change);
337   
338    ctx->ga.MaxNoChange = max_no_change;
339   
340    PGADebugExited("PGASetMaxNoChangeValue");
341}
342
343/*U****************************************************************************
344   PGASetMaxSimilarityValue - Specifiy the maximum percent of homogeneity of
345   the population before stopping.  The similarity measure is the same
346   evaluation function value.  The default value is 95 percent.  The stopping
347   rule PGA_STOP_TOOSIMILAR must have been set by PGASetStoppingRuleType for
348   this function call to have any effect.
349
350   Category: Generation
351
352   Inputs:
353      ctx            - context variable
354      max_similarity - the maximum percent of the population that can share
355                       the same evaluation function value
356
357   Outputs:
358      None
359
360   Example:
361      PGAContext *ctx;
362      :
363      PGASetMaxSimilarityValue(ctx,99);
364
365****************************************************************************U*/
366void PGASetMaxSimilarityValue(PGAContext *ctx, int max_similarity)
367{
368    PGADebugEntered("PGASetMaxSimilarityValue");
369    PGAFailIfSetUp("PGASetMaxSimilarityValue");
370
371    if ((max_similarity <= 0) || (max_similarity > 100))
372        PGAError(ctx, "PGASetMaxSimilarityValue: max_similarity invalid",
373                 PGA_FATAL, PGA_INT, (void *) &max_similarity);
374   
375    ctx->ga.MaxSimilarity = max_similarity;
376
377    PGADebugExited("PGASetMaxSimilarityValue");
378}
Note: See TracBrowser for help on using the repository browser.