File: cross.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 (326 lines) | stat: -rw-r--r-- 10,144 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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
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: crossover.c: This file contains the data structure neutral
*                        crossover routines.
*
*     Authors: David M. Levine, Philip L. Hallstrom, David M. Noelle,
*              Brian P. Walenz
*****************************************************************************/

#include "pgapack.h"

/*U****************************************************************************
   PGACrossover - performs crossover on two parent strings to create two
   child strings (via side-effect).  The type of crossover performed is
   either the default or that specified by PGASetCrossoverType

   Category: Operators

   Inputs:
      ctx  - context variable
      p1   - the first parent string
      p2   - the second parent string
      pop1 - symbolic constant of the population containing string p1 and p2
      c1   - the first child string
      c2   - the second child string
      pop2 - symbolic constant of the population to contain string c1 and c2

   Outputs:
      c1 and c2 in pop2 are children of p1 and p2 in pop1.  p1 and p2 are not
      modified.

   Example:
      Perform crossover on the two parent strings mom and dad in population
      PGA_OLDPOP, and insert the child strings, child1 and child1, in
      population PGA_NEWPOP.

      PGAContext *ctx;
      int mom, dad, child1, child2;
      :
      PGACrossover(ctx, mom, dad, PGA_OLDPOP, child1, child2, PGA_NEWPOP);

****************************************************************************U*/
void PGACrossover ( PGAContext *ctx, int p1, int p2, int pop1,
                    int c1, int c2, int pop2 )
{
    int fp1, fp2, fc1, fc2;

    PGADebugEntered("PGACrossover");
    PGADebugPrint( ctx, PGA_DEBUG_PRINTVAR, "PGACrossover", " p1 = ",
		  PGA_INT, (void *) &p1 );
    PGADebugPrint( ctx, PGA_DEBUG_PRINTVAR, "PGACrossover", " p2 = ",
		  PGA_INT, (void *) &p2 );
    PGADebugPrint( ctx, PGA_DEBUG_PRINTVAR, "PGACrossover", " c1 = ",
		  PGA_INT, (void *) &c1 );
    PGADebugPrint( ctx, PGA_DEBUG_PRINTVAR, "PGACrossover", " c2 = ",
		  PGA_INT, (void *) &c2 );

    if (ctx->fops.Crossover) {
	fp1 = ((p1 == PGA_TEMP1) || (p1 == PGA_TEMP2)) ? p1 : p1+1;
	fp2 = ((p2 == PGA_TEMP1) || (p2 == PGA_TEMP2)) ? p2 : p2+1;
	fc1 = ((c1 == PGA_TEMP1) || (c1 == PGA_TEMP2)) ? c1 : c1+1;
	fc2 = ((c2 == PGA_TEMP1) || (c2 == PGA_TEMP2)) ? c2 : c2+1;
	(*ctx->fops.Crossover)(&ctx, &fp1, &fp2, &pop1, &fc1, &fc2, &pop2);
    } else {
	(*ctx->cops.Crossover)(ctx, p1, p2, pop1, c1, c2, pop2);
    }

    PGASetEvaluationUpToDateFlag(ctx, c1, pop2, PGA_FALSE);
    PGASetEvaluationUpToDateFlag(ctx, c2, pop2, PGA_FALSE);
    
    PGADebugExited("PGACrossover");
}

/*U***************************************************************************
   PGAGetCrossoverType - Returns the type of crossover selected

   Category: Operators

   Inputs:
      ctx - context variable

   Outputs:
      Returns the integer corresponding to the symbolic constant
      used to specify the crossover type

   Example:
      PGAContext *ctx;
      int crosstype;
      :
      crosstype = PGAGetCrossoverType(ctx);
      switch (crosstype) {
      case PGA_CROSSOVER_ONEPT:
          printf("Crossover Type = PGA_CROSSOVER_ONEPT\n");
          break;
      case PGA_CROSSOVER_TWOPT:
          printf("Crossover Type = PGA_CROSSOVER_TWOPT\n");
          break;
      case PGA_CROSSOVER_UNIFORM:
          printf("Crossover Type = PGA_CROSSOVER_UNIFORM\n");
          break;
      }

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

    PGAFailIfNotSetUp("PGAGetCrossoverType");

    PGADebugExited("PGAGetCrossoverType");

    return(ctx->ga.CrossoverType);
}

/*U***************************************************************************
   PGAGetCrossoverProb - Returns the crossover probability

   Category: Operators

   Inputs:
      ctx - context variable

   Outputs:
      The crossover probability

   Example:
      PGAContext *ctx;
      double pc;
      :
      pc = PGAGetCrossoverProb(ctx);

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

    PGAFailIfNotSetUp("PGAGetCrossoverProb");

    PGADebugExited("PGAGetCrossoverProb");

    return(ctx->ga.CrossoverProb);
}

/*U***************************************************************************
   PGAGetUniformCrossoverProb - returns the probability of a bit being
   selected from the first child string in uniform crossover

   Category: Operators

   Inputs:
      ctx - context variable

   Outputs:
      The uniform crossover probability

   Example:
      PGAContext *ctx;
      double pu;
      :
      pu = PGAGetUniformCrossoverProb(ctx);

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

    PGAFailIfNotSetUp("PGAGetUniformCrossoverProb");

    PGADebugExited("PGAGetUniformCrossoverProb");

    return(ctx->ga.UniformCrossProb);
}

/*U****************************************************************************
   PGASetCrossoverType - specify the type of crossover to use. Valid choices
   are PGA_CROSSOVER_ONEPT, PGA_CROSSOVER_TWOPT, or PGA_CROSSOVER_UNIFORM for
   one-point, two-point, and uniform crossover, respectively.  The default is
   PGA_CROSSOVER_TWOPT.

   Category: Operators

   Inputs:
      ctx            - context variable
      crossover_type - symbolic constant to specify crossover type

   Outputs:
      None

   Example:
      Use uniform crossover when crossingover strings.

      PGAContext *ctx;
      :
      PGASetCrossoverType(ctx, PGA_CROSSOVER_UNIFORM);

****************************************************************************U*/
void PGASetCrossoverType (PGAContext *ctx, int crossover_type)
{

    PGADebugEntered("PGASetCrossoverType");

    switch (crossover_type) {
        case PGA_CROSSOVER_ONEPT:
        case PGA_CROSSOVER_TWOPT:
        case PGA_CROSSOVER_UNIFORM:
            ctx->ga.CrossoverType = crossover_type;
            break;
        default:
            PGAError( ctx,
                     "PGASetCrossoverType: Invalid value of crossover_type:",
                      PGA_FATAL, PGA_INT, (void *) &crossover_type );
    };

    PGADebugExited("PGASetCrossoverType");
}


/*U****************************************************************************
   PGASetCrossoverProb - Probability that a selected string will undergo
   crossover.  The default is 0.85.

   Category: Operators

   Inputs:
      ctx - context variable
      p   - the crossover probability

   Outputs:
      None

   Example:
      Make crossover happen infrequently.

      PGAContext *ctx;
      :
      PGASetCrossoverProb(ctx,0.001);

****************************************************************************U*/
void PGASetCrossoverProb( PGAContext *ctx, double crossover_prob)
{
    PGADebugEntered("PGASetCrossoverProb");

    if ((crossover_prob < 0.0) || (crossover_prob > 1.0))
        PGAError ( ctx,
                  "PGASetCrossoverProb: Invalid value of crossover_prob:",
                   PGA_FATAL, PGA_DOUBLE, (void *) &crossover_prob);
    else
        ctx->ga.CrossoverProb = crossover_prob;

    PGADebugExited("PGASetCrossoverProb");
}

/*U****************************************************************************
   PGASetUniformCrossoverProb - Probability used in uniform crossover
   to specify that an allele value value be selected from a particular
   parent. The default is 0.6.  The crossover type must have been set
   to PGA_CROSSOVER_UNIFORM with PGASetCrossoverType for this function
   call to have any effect.

   Category: Operators

   Inputs:
      ctx - context variable
      p   - the crossover probability

   Outputs:
      None

   Example:
      PGAContext *ctx;
      :
      PGASetUniformCrossoverProb(ctx,0.9);

****************************************************************************U*/
void PGASetUniformCrossoverProb( PGAContext *ctx, double uniform_cross_prob)
{
    PGADebugEntered("PGASetUniformCrossoverProb");

    if ((uniform_cross_prob < 0.0) || (uniform_cross_prob > 1.0))
        PGAError ( ctx,
                  "PGASetUniformCrossoverProb: Invalid value of "
                  "uniform_cross_prob:", PGA_FATAL, PGA_DOUBLE,
                  (void *) &uniform_cross_prob);
    else
        ctx->ga.UniformCrossProb = uniform_cross_prob;

    PGADebugExited("PGASetUniformCrossoverProb");
}