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: restart.c: This file contains the routines needed to handle |
---|
66 | * the restart operator, and restarting 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 | int *pgapack_user_cmd_restart = NULL; |
---|
75 | /*U**************************************************************************** |
---|
76 | PGARestart - reseeds a population from the best string |
---|
77 | |
---|
78 | Category: Operators |
---|
79 | |
---|
80 | Inputs: |
---|
81 | val - the probability of changing an allele when copying the |
---|
82 | best string to the new population |
---|
83 | source_pop - the source population |
---|
84 | dest_pop - symbolic constant of the destination population |
---|
85 | |
---|
86 | Outputs: |
---|
87 | dest_pop is modified by side-effect. |
---|
88 | |
---|
89 | Example: |
---|
90 | Perform an unspecified test to determine if the current evolution is |
---|
91 | not evolving fast enough, and if so, restart the evolution. |
---|
92 | |
---|
93 | PGAContext *ctx; PGAEvaluateMS(ctx, PGA_OLDPOP, f, comm); |
---|
94 | PGAFitness (ctx, PGA_OLDPOP); |
---|
95 | } |
---|
96 | |
---|
97 | : |
---|
98 | if (StagnantEvolution()) { |
---|
99 | PGARestart(ctx, PGA_OLDPOP, PGA_NEWPOP); |
---|
100 | PGAEvaluate(ctx, PGA_NEWPOP, EvalFunc); |
---|
101 | PGAUpdateGeneration(ctx); |
---|
102 | } |
---|
103 | |
---|
104 | ****************************************************************************U*/ |
---|
105 | void PGARestart(PGAContext *ctx, int source_pop, int dest_pop) |
---|
106 | { |
---|
107 | /* For integers and reals, the amount by which to change is set with |
---|
108 | PGASetMutationIntegerValue and PGASetMutationRealValue, respectively. |
---|
109 | For binary strings, the bits are complemented. */ |
---|
110 | |
---|
111 | int dest_p, old_mut_type, source_p; |
---|
112 | double val; |
---|
113 | |
---|
114 | PGADebugEntered("PGARestart"); |
---|
115 | |
---|
116 | printf("Restarting the algorithm . . . \n"); |
---|
117 | fflush(stdout); |
---|
118 | source_p = PGAGetBestIndex(ctx, source_pop); |
---|
119 | if (source_p != 0 || source_pop != dest_pop) |
---|
120 | PGACopyIndividual(ctx, source_p, source_pop, 0, dest_pop); |
---|
121 | PGASetEvaluationUpToDateFlag(ctx, 0, dest_pop, PGA_FALSE); |
---|
122 | old_mut_type = PGAGetMutationType(ctx); |
---|
123 | ctx->ga.MutationType = PGA_MUTATION_UNIFORM; |
---|
124 | val = ctx->ga.restartAlleleProb; |
---|
125 | |
---|
126 | if (ctx->fops.Mutation) { |
---|
127 | for (dest_p = 2; dest_p <= ctx->ga.PopSize; dest_p++) { |
---|
128 | PGACopyIndividual(ctx, 0, dest_pop, dest_p-1, dest_pop); |
---|
129 | (*ctx->fops.Mutation)(&ctx, &dest_p, &dest_pop, &val); |
---|
130 | PGASetEvaluationUpToDateFlag(ctx, dest_p-1, dest_pop, PGA_FALSE); |
---|
131 | } |
---|
132 | } else { |
---|
133 | for (dest_p = 1; dest_p < ctx->ga.PopSize; dest_p++) { |
---|
134 | PGACopyIndividual(ctx, 0, dest_pop, dest_p, dest_pop); |
---|
135 | (*ctx->cops.Mutation)(ctx, dest_p, dest_pop, val); |
---|
136 | PGASetEvaluationUpToDateFlag(ctx, dest_p, dest_pop, PGA_FALSE); |
---|
137 | } |
---|
138 | } |
---|
139 | ctx->ga.MutationType = old_mut_type; |
---|
140 | |
---|
141 | PGADebugExited("PGARestart"); |
---|
142 | } |
---|
143 | |
---|
144 | /*U**************************************************************************** |
---|
145 | PGASetRestartFlag - specifies whether the algorithm should employ |
---|
146 | the restart operator |
---|
147 | |
---|
148 | Category: Operators |
---|
149 | |
---|
150 | Inputs: |
---|
151 | ctx - context variable |
---|
152 | val - boolean variable |
---|
153 | |
---|
154 | Outputs: |
---|
155 | None |
---|
156 | |
---|
157 | Example: |
---|
158 | PGAContext *ctx; |
---|
159 | : |
---|
160 | PGASetRestartFlag(ctx, PGA_TRUE); |
---|
161 | |
---|
162 | ****************************************************************************U*/ |
---|
163 | void PGASetRestartFlag(PGAContext *ctx, int val) |
---|
164 | { |
---|
165 | PGADebugEntered("PGASetRestartFlag"); |
---|
166 | |
---|
167 | switch (val) |
---|
168 | { |
---|
169 | case PGA_TRUE: |
---|
170 | case PGA_FALSE: |
---|
171 | ctx->ga.restart = val; |
---|
172 | break; |
---|
173 | default: |
---|
174 | PGAError(ctx, "PGASetRestartFlag: Invalid value for restart:", |
---|
175 | PGA_FATAL, PGA_INT, (void *) &val); |
---|
176 | break; |
---|
177 | } |
---|
178 | |
---|
179 | PGADebugExited("PGASetRestartFlag"); |
---|
180 | } |
---|
181 | |
---|
182 | /*U**************************************************************************** |
---|
183 | PGAGetRestartFlag - returns whether the algorithm should employ the |
---|
184 | restart operator |
---|
185 | |
---|
186 | Category: Operators |
---|
187 | |
---|
188 | Inputs: |
---|
189 | ctx - context variable |
---|
190 | |
---|
191 | Outputs: |
---|
192 | PGA_TRUE if restarting is enabled, otherwise PGA_FALSE. |
---|
193 | |
---|
194 | Example: |
---|
195 | PGAContext *ctx; |
---|
196 | int val; |
---|
197 | : |
---|
198 | val = PGAGetRestartFlag(ctx); |
---|
199 | |
---|
200 | ****************************************************************************U*/ |
---|
201 | int PGAGetRestartFlag(PGAContext *ctx) |
---|
202 | { |
---|
203 | PGADebugEntered("PGAGetRestartFlag"); |
---|
204 | PGAFailIfNotSetUp("PGAGetRestartFlag"); |
---|
205 | |
---|
206 | PGADebugExited("PGAGetRestartFlag"); |
---|
207 | |
---|
208 | return (ctx->ga.restart); |
---|
209 | } |
---|
210 | |
---|
211 | /*U**************************************************************************** |
---|
212 | PGASetRestartFrequencyValue - specifies the number of iterations of no |
---|
213 | change in the best string after which the algorithm should restart |
---|
214 | |
---|
215 | Category: Operators |
---|
216 | |
---|
217 | Inputs: |
---|
218 | ctx - context variable |
---|
219 | numiter - number of changeless iterations |
---|
220 | |
---|
221 | Outputs: |
---|
222 | None |
---|
223 | |
---|
224 | Example: |
---|
225 | PGAContext *ctx; |
---|
226 | : |
---|
227 | PGASetRestartFrequencyValue(ctx, 100); |
---|
228 | |
---|
229 | ****************************************************************************U*/ |
---|
230 | void PGASetRestartFrequencyValue(PGAContext *ctx, int numiter) |
---|
231 | { |
---|
232 | PGADebugEntered("PGASetRestartFrequencyValue"); |
---|
233 | |
---|
234 | if (numiter > 0) |
---|
235 | ctx->ga.restartFreq = numiter; |
---|
236 | else |
---|
237 | PGAError(ctx, "PGASetRestartFrequencyValue: Invalid value for " |
---|
238 | "restart freqency:", PGA_FATAL, PGA_INT, (void *) &numiter); |
---|
239 | |
---|
240 | PGADebugExited("PGASetRestartFrequencyValue"); |
---|
241 | } |
---|
242 | |
---|
243 | /*U**************************************************************************** |
---|
244 | PGAGetRestartFrequencyValue - returns the number of iterations of no |
---|
245 | change in the best string after which the algorithm should restart |
---|
246 | |
---|
247 | Category: Operators |
---|
248 | |
---|
249 | Inputs: |
---|
250 | ctx - context variable |
---|
251 | numiter - number of changeless iterations |
---|
252 | |
---|
253 | Outputs: |
---|
254 | The number of iteration of no change required for a restart. |
---|
255 | |
---|
256 | Example: |
---|
257 | PGAContext *ctx; |
---|
258 | : |
---|
259 | numiter = PGAGetRestartFrequencyValue(ctx); |
---|
260 | |
---|
261 | ****************************************************************************U*/ |
---|
262 | int PGAGetRestartFrequencyValue(PGAContext *ctx) |
---|
263 | { |
---|
264 | PGADebugEntered("PGAGetRestartFrequencyValue"); |
---|
265 | PGAFailIfNotSetUp("PGAGetRestartFrequencyValue"); |
---|
266 | |
---|
267 | PGADebugExited("PGAGetRestartFrequencyValue"); |
---|
268 | |
---|
269 | return (ctx->ga.restartFreq); |
---|
270 | } |
---|
271 | |
---|
272 | /*U**************************************************************************** |
---|
273 | PGASetRestartAlleleChangeProb - specifies the probability with which |
---|
274 | an allele will be mutated during a restart |
---|
275 | |
---|
276 | Category: Operators |
---|
277 | |
---|
278 | Inputs: |
---|
279 | ctx - context variable |
---|
280 | prob - probability of mutation |
---|
281 | |
---|
282 | Outputs: |
---|
283 | None |
---|
284 | |
---|
285 | Example: |
---|
286 | PGAContext *ctx; |
---|
287 | : |
---|
288 | PGASetRestartAlleleChangeProb(ctx, 0.5); |
---|
289 | |
---|
290 | ****************************************************************************U*/ |
---|
291 | void PGASetRestartAlleleChangeProb(PGAContext *ctx, double prob) |
---|
292 | { |
---|
293 | PGADebugEntered("PGASetRestartAlleleChangeProb"); |
---|
294 | |
---|
295 | if (prob >= 0.0 && prob <= 1.0) |
---|
296 | ctx->ga.restartAlleleProb = prob; |
---|
297 | else |
---|
298 | PGAError(ctx, "PGASetRestartAlleleChangeProb: Invalid probability:", |
---|
299 | PGA_FATAL, PGA_DOUBLE, (void *) &prob); |
---|
300 | |
---|
301 | PGADebugExited("PGASetRestartAlleleChangeProb"); |
---|
302 | } |
---|
303 | |
---|
304 | /*U**************************************************************************** |
---|
305 | PGAGetRestartAlleleChangeProb - returns the probability with which |
---|
306 | an allele will be mutated during a restart |
---|
307 | |
---|
308 | Category: Operators |
---|
309 | |
---|
310 | Inputs: |
---|
311 | ctx - context variable |
---|
312 | |
---|
313 | Outputs: |
---|
314 | The probability of mutating an allele during a restart. |
---|
315 | |
---|
316 | Example: |
---|
317 | PGAContext *ctx; |
---|
318 | : |
---|
319 | prob = PGASetRestartAlleleChangeProb(ctx); |
---|
320 | |
---|
321 | ****************************************************************************U*/ |
---|
322 | double PGAGetRestartAlleleChangeProb(PGAContext *ctx) |
---|
323 | { |
---|
324 | PGADebugEntered("PGAGetRestartAlleleChangeProb"); |
---|
325 | PGAFailIfNotSetUp("PGAGetRestartAlleleChangeProb"); |
---|
326 | |
---|
327 | PGADebugExited("PGAGetRestartAlleleChangeProb"); |
---|
328 | |
---|
329 | return (ctx->ga.restartAlleleProb); |
---|
330 | } |
---|
331 | |
---|
332 | void PGASetRestartUserAction(int *userRestartCommand) |
---|
333 | { |
---|
334 | pgapack_user_cmd_restart = userRestartCommand; |
---|
335 | } |
---|
336 | |
---|
337 | int PGARestartCondition(PGAContext *ctx){ |
---|
338 | int ifRestart = PGA_FALSE; |
---|
339 | if ( (pgapack_user_cmd_restart!=NULL && *pgapack_user_cmd_restart==PGA_TRUE) || |
---|
340 | ( |
---|
341 | (ctx->ga.restart == PGA_TRUE) && |
---|
342 | (ctx->ga.ItersOfSame % ctx->ga.restartFreq == 0) |
---|
343 | ) |
---|
344 | ) { |
---|
345 | ifRestart = PGA_TRUE; |
---|
346 | } |
---|
347 | |
---|
348 | if ((ctx->ga.restart == PGA_TRUE) && |
---|
349 | (ctx->ga.ItersOfSame % ctx->ga.restartFreq == 0)) { |
---|
350 | ctx->ga.ItersOfSame++; |
---|
351 | } |
---|
352 | if (pgapack_user_cmd_restart!=NULL){ |
---|
353 | *pgapack_user_cmd_restart = PGA_FALSE; |
---|
354 | } |
---|
355 | return ifRestart; |
---|
356 | } |
---|
357 | |
---|