source: trunk/packages/optimizer/src/pgapack/pgapack/source/cross.c @ 5673

Last change on this file since 5673 was 5673, checked in by ldelgass, 9 years ago

Fix line endings, set eol-style to native on all C/C++ sources.

  • Property svn:eol-style set to native
File size: 11.3 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: crossover.c: This file contains the data structure neutral
66*                        crossover routines.
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   PGACrossover - performs crossover on two parent strings to create two
76   child strings (via side-effect).  The type of crossover performed is
77   either the default or that specified by PGASetCrossoverType
78
79   Category: Operators
80
81   Inputs:
82      ctx  - context variable
83      p1   - the first parent string
84      p2   - the second parent string
85      pop1 - symbolic constant of the population containing string p1 and p2
86      c1   - the first child string
87      c2   - the second child string
88      pop2 - symbolic constant of the population to contain string c1 and c2
89
90   Outputs:
91      c1 and c2 in pop2 are children of p1 and p2 in pop1.  p1 and p2 are not
92      modified.
93
94   Example:
95      Perform crossover on the two parent strings mom and dad in population
96      PGA_OLDPOP, and insert the child strings, child1 and child1, in
97      population PGA_NEWPOP.
98
99      PGAContext *ctx;
100      int mom, dad, child1, child2;
101      :
102      PGACrossover(ctx, mom, dad, PGA_OLDPOP, child1, child2, PGA_NEWPOP);
103
104****************************************************************************U*/
105void PGACrossover ( PGAContext *ctx, int p1, int p2, int pop1,
106                    int c1, int c2, int pop2 )
107{
108    int fp1, fp2, fc1, fc2;
109
110    PGADebugEntered("PGACrossover");
111    PGADebugPrint( ctx, PGA_DEBUG_PRINTVAR, "PGACrossover", " p1 = ",
112                  PGA_INT, (void *) &p1 );
113    PGADebugPrint( ctx, PGA_DEBUG_PRINTVAR, "PGACrossover", " p2 = ",
114                  PGA_INT, (void *) &p2 );
115    PGADebugPrint( ctx, PGA_DEBUG_PRINTVAR, "PGACrossover", " c1 = ",
116                  PGA_INT, (void *) &c1 );
117    PGADebugPrint( ctx, PGA_DEBUG_PRINTVAR, "PGACrossover", " c2 = ",
118                  PGA_INT, (void *) &c2 );
119
120    if (ctx->fops.Crossover) {
121        fp1 = ((p1 == PGA_TEMP1) || (p1 == PGA_TEMP2)) ? p1 : p1+1;
122        fp2 = ((p2 == PGA_TEMP1) || (p2 == PGA_TEMP2)) ? p2 : p2+1;
123        fc1 = ((c1 == PGA_TEMP1) || (c1 == PGA_TEMP2)) ? c1 : c1+1;
124        fc2 = ((c2 == PGA_TEMP1) || (c2 == PGA_TEMP2)) ? c2 : c2+1;
125        (*ctx->fops.Crossover)(&ctx, &fp1, &fp2, &pop1, &fc1, &fc2, &pop2);
126    } else {
127        (*ctx->cops.Crossover)(ctx, p1, p2, pop1, c1, c2, pop2);
128    }
129
130    PGASetEvaluationUpToDateFlag(ctx, c1, pop2, PGA_FALSE);
131    PGASetEvaluationUpToDateFlag(ctx, c2, pop2, PGA_FALSE);
132   
133    PGADebugExited("PGACrossover");
134}
135
136/*U***************************************************************************
137   PGAGetCrossoverType - Returns the type of crossover selected
138
139   Category: Operators
140
141   Inputs:
142      ctx - context variable
143
144   Outputs:
145      Returns the integer corresponding to the symbolic constant
146      used to specify the crossover type
147
148   Example:
149      PGAContext *ctx;
150      int crosstype;
151      :
152      crosstype = PGAGetCrossoverType(ctx);
153      switch (crosstype) {
154      case PGA_CROSSOVER_ONEPT:
155          printf("Crossover Type = PGA_CROSSOVER_ONEPT\n");
156          break;
157      case PGA_CROSSOVER_TWOPT:
158          printf("Crossover Type = PGA_CROSSOVER_TWOPT\n");
159          break;
160      case PGA_CROSSOVER_UNIFORM:
161          printf("Crossover Type = PGA_CROSSOVER_UNIFORM\n");
162          break;
163      }
164
165***************************************************************************U*/
166int PGAGetCrossoverType (PGAContext *ctx)
167{
168    PGADebugEntered("PGAGetCrossoverType");
169
170    PGAFailIfNotSetUp("PGAGetCrossoverType");
171
172    PGADebugExited("PGAGetCrossoverType");
173
174    return(ctx->ga.CrossoverType);
175}
176
177/*U***************************************************************************
178   PGAGetCrossoverProb - Returns the crossover probability
179
180   Category: Operators
181
182   Inputs:
183      ctx - context variable
184
185   Outputs:
186      The crossover probability
187
188   Example:
189      PGAContext *ctx;
190      double pc;
191      :
192      pc = PGAGetCrossoverProb(ctx);
193
194***************************************************************************U*/
195double PGAGetCrossoverProb (PGAContext *ctx)
196{
197    PGADebugEntered("PGAGetCrossoverProb");
198
199    PGAFailIfNotSetUp("PGAGetCrossoverProb");
200
201    PGADebugExited("PGAGetCrossoverProb");
202
203    return(ctx->ga.CrossoverProb);
204}
205
206/*U***************************************************************************
207   PGAGetUniformCrossoverProb - returns the probability of a bit being
208   selected from the first child string in uniform crossover
209
210   Category: Operators
211
212   Inputs:
213      ctx - context variable
214
215   Outputs:
216      The uniform crossover probability
217
218   Example:
219      PGAContext *ctx;
220      double pu;
221      :
222      pu = PGAGetUniformCrossoverProb(ctx);
223
224***************************************************************************U*/
225double PGAGetUniformCrossoverProb (PGAContext *ctx)
226{
227    PGADebugEntered("PGAGetUniformCrossoverProb");
228
229    PGAFailIfNotSetUp("PGAGetUniformCrossoverProb");
230
231    PGADebugExited("PGAGetUniformCrossoverProb");
232
233    return(ctx->ga.UniformCrossProb);
234}
235
236/*U****************************************************************************
237   PGASetCrossoverType - specify the type of crossover to use. Valid choices
238   are PGA_CROSSOVER_ONEPT, PGA_CROSSOVER_TWOPT, or PGA_CROSSOVER_UNIFORM for
239   one-point, two-point, and uniform crossover, respectively.  The default is
240   PGA_CROSSOVER_TWOPT.
241
242   Category: Operators
243
244   Inputs:
245      ctx            - context variable
246      crossover_type - symbolic constant to specify crossover type
247
248   Outputs:
249      None
250
251   Example:
252      Use uniform crossover when crossingover strings.
253
254      PGAContext *ctx;
255      :
256      PGASetCrossoverType(ctx, PGA_CROSSOVER_UNIFORM);
257
258****************************************************************************U*/
259void PGASetCrossoverType (PGAContext *ctx, int crossover_type)
260{
261
262    PGADebugEntered("PGASetCrossoverType");
263
264    switch (crossover_type) {
265        case PGA_CROSSOVER_ONEPT:
266        case PGA_CROSSOVER_TWOPT:
267        case PGA_CROSSOVER_UNIFORM:
268        case PGA_CROSSOVER_SBX:
269        case PGA_CROSSOVER_TRIANGULAR:
270            ctx->ga.CrossoverType = crossover_type;
271            break;
272        default:
273            PGAError( ctx,
274                     "PGASetCrossoverType: Invalid value of crossover_type:",
275                      PGA_FATAL, PGA_INT, (void *) &crossover_type );
276    };
277
278    PGADebugExited("PGASetCrossoverType");
279}
280
281
282/*U****************************************************************************
283   PGASetCrossoverProb - Probability that a selected string will undergo
284   crossover.  The default is 0.85.
285
286   Category: Operators
287
288   Inputs:
289      ctx - context variable
290      p   - the crossover probability
291
292   Outputs:
293      None
294
295   Example:
296      Make crossover happen infrequently.
297
298      PGAContext *ctx;
299      :
300      PGASetCrossoverProb(ctx,0.001);
301
302****************************************************************************U*/
303void PGASetCrossoverProb( PGAContext *ctx, double crossover_prob)
304{
305    PGADebugEntered("PGASetCrossoverProb");
306
307    if ((crossover_prob < 0.0) || (crossover_prob > 1.0))
308        PGAError ( ctx,
309                  "PGASetCrossoverProb: Invalid value of crossover_prob:",
310                   PGA_FATAL, PGA_DOUBLE, (void *) &crossover_prob);
311    else
312        ctx->ga.CrossoverProb = crossover_prob;
313
314    PGADebugExited("PGASetCrossoverProb");
315}
316
317/*U****************************************************************************
318   PGASetUniformCrossoverProb - Probability used in uniform crossover
319   to specify that an allele value value be selected from a particular
320   parent. The default is 0.6.  The crossover type must have been set
321   to PGA_CROSSOVER_UNIFORM with PGASetCrossoverType for this function
322   call to have any effect.
323
324   Category: Operators
325
326   Inputs:
327      ctx - context variable
328      p   - the crossover probability
329
330   Outputs:
331      None
332
333   Example:
334      PGAContext *ctx;
335      :
336      PGASetUniformCrossoverProb(ctx,0.9);
337
338****************************************************************************U*/
339void PGASetUniformCrossoverProb( PGAContext *ctx, double uniform_cross_prob)
340{
341    PGADebugEntered("PGASetUniformCrossoverProb");
342
343    if ((uniform_cross_prob < 0.0) || (uniform_cross_prob > 1.0))
344        PGAError ( ctx,
345                  "PGASetUniformCrossoverProb: Invalid value of "
346                  "uniform_cross_prob:", PGA_FATAL, PGA_DOUBLE,
347                  (void *) &uniform_cross_prob);
348    else
349        ctx->ga.UniformCrossProb = uniform_cross_prob;
350
351    PGADebugExited("PGASetUniformCrossoverProb");
352}
Note: See TracBrowser for help on using the repository browser.