File: restart.c

package info (click to toggle)
pgapack 1.1-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 2,344 kB
  • ctags: 1,786
  • sloc: ansic: 10,331; fortran: 2,985; sh: 486; makefile: 462; perl: 105
file content (306 lines) | stat: -rw-r--r-- 9,251 bytes parent folder | download | duplicates (3)
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
/*
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: restart.c: This file contains the routines needed to handle
*                      the restart operator, and restarting the GA.
*
*     Authors: David M. Levine, Philip L. Hallstrom, David M. Noelle,
*              Brian P. Walenz
*****************************************************************************/

#include "pgapack.h"

/*U****************************************************************************
   PGARestart - reseeds a population from the best string

   Category: Operators

   Inputs:
      val         - the probability of changing an allele when copying the
                    best string to the new population
      source_pop  - the source population
      dest_pop    - symbolic constant of the destination population

   Outputs:
      dest_pop is modified by side-effect.

   Example:
      Perform an unspecified test to determine if the current evolution is
      not evolving fast enough, and if so, restart the evolution.

      PGAContext *ctx;	    PGAEvaluateMS(ctx, PGA_OLDPOP, f, comm);
	    PGAFitness   (ctx, PGA_OLDPOP);
	    }

      :
      if (StagnantEvolution()) {
          PGARestart(ctx, PGA_OLDPOP, PGA_NEWPOP);
          PGAEvaluate(ctx, PGA_NEWPOP, EvalFunc);
          PGAUpdateGeneration(ctx);
      }

****************************************************************************U*/
void PGARestart(PGAContext *ctx, int source_pop, int dest_pop)
{
    /* For integers and reals, the amount by which to change is set with
       PGASetMutationIntegerValue and PGASetMutationRealValue, respectively.
       For binary strings, the bits are complemented. */

    int dest_p, old_mut_type, source_p;
    double val;
    
    PGADebugEntered("PGARestart");
    
    printf("Restarting the algorithm . . . \n");
    fflush(stdout);
    source_p = PGAGetBestIndex(ctx, source_pop);
    if (source_p != 0 || source_pop != dest_pop)
	PGACopyIndividual(ctx, source_p, source_pop, 0, dest_pop);
    PGASetEvaluationUpToDateFlag(ctx, 0, dest_pop, PGA_FALSE);
    old_mut_type = PGAGetMutationType(ctx);
    ctx->ga.MutationType = PGA_MUTATION_UNIFORM;
    val = ctx->ga.restartAlleleProb;
    
    if (ctx->fops.Mutation) {
	for (dest_p = 2; dest_p <= ctx->ga.PopSize; dest_p++) {
	    PGACopyIndividual(ctx, 0, dest_pop, dest_p-1, dest_pop);
	    (*ctx->fops.Mutation)(&ctx, &dest_p, &dest_pop, &val);
	    PGASetEvaluationUpToDateFlag(ctx, dest_p-1, dest_pop, PGA_FALSE);
	}
    } else {
	for (dest_p = 1; dest_p < ctx->ga.PopSize; dest_p++) {
	    PGACopyIndividual(ctx, 0, dest_pop, dest_p, dest_pop);
	    (*ctx->cops.Mutation)(ctx, dest_p, dest_pop, val);
	    PGASetEvaluationUpToDateFlag(ctx, dest_p, dest_pop, PGA_FALSE);
	}
    }
    ctx->ga.MutationType = old_mut_type;
    
    PGADebugExited("PGARestart");
}

/*U****************************************************************************
  PGASetRestartFlag - specifies whether the algorithm should employ
  the restart operator

   Category: Operators

   Inputs:
      ctx - context variable
      val - boolean variable

   Outputs:
      None

   Example:
      PGAContext *ctx;
      :
      PGASetRestartFlag(ctx, PGA_TRUE);

****************************************************************************U*/
void PGASetRestartFlag(PGAContext *ctx, int val)
{
    PGADebugEntered("PGASetRestartFlag");

    switch (val)
    {
    case PGA_TRUE:
    case PGA_FALSE:
         ctx->ga.restart = val;
         break;
    default:
         PGAError(ctx, "PGASetRestartFlag: Invalid value for restart:",
                  PGA_FATAL, PGA_INT, (void *) &val);
         break;
    }

    PGADebugExited("PGASetRestartFlag");
}

/*U****************************************************************************
   PGAGetRestartFlag - returns whether the algorithm should employ the
   restart operator

   Category: Operators

   Inputs:
      ctx - context variable

   Outputs:
      PGA_TRUE if restarting is enabled, otherwise PGA_FALSE.

   Example:
      PGAContext *ctx;
      int val;
      :
      val = PGAGetRestartFlag(ctx);

****************************************************************************U*/
int PGAGetRestartFlag(PGAContext *ctx)
{
    PGADebugEntered("PGAGetRestartFlag");
    PGAFailIfNotSetUp("PGAGetRestartFlag");

    PGADebugExited("PGAGetRestartFlag");

    return (ctx->ga.restart);
}

/*U****************************************************************************
  PGASetRestartFrequencyValue - specifies the number of iterations of no
  change in the best string after which the algorithm should restart

  Category: Operators

  Inputs:
      ctx - context variable
      numiter - number of changeless iterations

  Outputs:
      None

  Example:
      PGAContext *ctx;
      :
      PGASetRestartFrequencyValue(ctx, 100);

****************************************************************************U*/
void PGASetRestartFrequencyValue(PGAContext *ctx, int numiter)
{
    PGADebugEntered("PGASetRestartFrequencyValue");

    if (numiter > 0)
         ctx->ga.restartFreq = numiter;
    else
         PGAError(ctx, "PGASetRestartFrequencyValue: Invalid value for "
                  "restart freqency:", PGA_FATAL, PGA_INT, (void *) &numiter);

    PGADebugExited("PGASetRestartFrequencyValue");
}

/*U****************************************************************************
  PGAGetRestartFrequencyValue - returns the number of iterations of no
  change in the best string after which the algorithm should restart

  Category: Operators

  Inputs:
      ctx     - context variable
      numiter - number of changeless iterations

  Outputs:
      The number of iteration of no change required for a restart.

  Example:
      PGAContext *ctx;
      :
      numiter = PGAGetRestartFrequencyValue(ctx);

****************************************************************************U*/
int PGAGetRestartFrequencyValue(PGAContext *ctx)
{
    PGADebugEntered("PGAGetRestartFrequencyValue");
    PGAFailIfNotSetUp("PGAGetRestartFrequencyValue");

    PGADebugExited("PGAGetRestartFrequencyValue");

    return (ctx->ga.restartFreq);
}

/*U****************************************************************************
  PGASetRestartAlleleChangeProb - specifies the probability with which
  an allele will be mutated during a restart

  Category: Operators

  Inputs:
      ctx - context variable
      prob - probability of mutation

  Outputs:
      None

  Example:
      PGAContext *ctx;
      :
      PGASetRestartAlleleChangeProb(ctx, 0.5);

****************************************************************************U*/
void PGASetRestartAlleleChangeProb(PGAContext *ctx, double prob)
{
    PGADebugEntered("PGASetRestartAlleleChangeProb");

    if (prob >= 0.0 && prob <= 1.0)
         ctx->ga.restartAlleleProb = prob;
    else
         PGAError(ctx, "PGASetRestartAlleleChangeProb: Invalid probability:",
                  PGA_FATAL, PGA_DOUBLE, (void *) &prob);

    PGADebugExited("PGASetRestartAlleleChangeProb");
}

/*U****************************************************************************
  PGAGetRestartAlleleChangeProb - returns the probability with which
  an allele will be mutated during a restart

  Category: Operators

  Inputs:
      ctx - context variable

  Outputs:
      The probability of mutating an allele during a restart.

  Example:
      PGAContext *ctx;
      :
      prob = PGASetRestartAlleleChangeProb(ctx);

****************************************************************************U*/
double PGAGetRestartAlleleChangeProb(PGAContext *ctx)
{
    PGADebugEntered("PGAGetRestartAlleleChangeProb");
    PGAFailIfNotSetUp("PGAGetRestartAlleleChangeProb");

    PGADebugExited("PGAGetRestartAlleleChangeProb");

    return (ctx->ga.restartAlleleProb);
}