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

Last change on this file since 1202 was 1202, checked in by liveletlive, 14 years ago

Changes made to include the following items:
1) Hard restart
2) SBX
3) Random population replacement proportion
4) Definitions and macros for the above functions in pgapack.h

File size: 11.2 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            ctx->ga.CrossoverType = crossover_type;
270            break;
271        default:
272            PGAError( ctx,
273                     "PGASetCrossoverType: Invalid value of crossover_type:",
274                      PGA_FATAL, PGA_INT, (void *) &crossover_type );
275    };
276
277    PGADebugExited("PGASetCrossoverType");
278}
279
280
281/*U****************************************************************************
282   PGASetCrossoverProb - Probability that a selected string will undergo
283   crossover.  The default is 0.85.
284
285   Category: Operators
286
287   Inputs:
288      ctx - context variable
289      p   - the crossover probability
290
291   Outputs:
292      None
293
294   Example:
295      Make crossover happen infrequently.
296
297      PGAContext *ctx;
298      :
299      PGASetCrossoverProb(ctx,0.001);
300
301****************************************************************************U*/
302void PGASetCrossoverProb( PGAContext *ctx, double crossover_prob)
303{
304    PGADebugEntered("PGASetCrossoverProb");
305
306    if ((crossover_prob < 0.0) || (crossover_prob > 1.0))
307        PGAError ( ctx,
308                  "PGASetCrossoverProb: Invalid value of crossover_prob:",
309                   PGA_FATAL, PGA_DOUBLE, (void *) &crossover_prob);
310    else
311        ctx->ga.CrossoverProb = crossover_prob;
312
313    PGADebugExited("PGASetCrossoverProb");
314}
315
316/*U****************************************************************************
317   PGASetUniformCrossoverProb - Probability used in uniform crossover
318   to specify that an allele value value be selected from a particular
319   parent. The default is 0.6.  The crossover type must have been set
320   to PGA_CROSSOVER_UNIFORM with PGASetCrossoverType for this function
321   call to have any effect.
322
323   Category: Operators
324
325   Inputs:
326      ctx - context variable
327      p   - the crossover probability
328
329   Outputs:
330      None
331
332   Example:
333      PGAContext *ctx;
334      :
335      PGASetUniformCrossoverProb(ctx,0.9);
336
337****************************************************************************U*/
338void PGASetUniformCrossoverProb( PGAContext *ctx, double uniform_cross_prob)
339{
340    PGADebugEntered("PGASetUniformCrossoverProb");
341
342    if ((uniform_cross_prob < 0.0) || (uniform_cross_prob > 1.0))
343        PGAError ( ctx,
344                  "PGASetUniformCrossoverProb: Invalid value of "
345                  "uniform_cross_prob:", PGA_FATAL, PGA_DOUBLE,
346                  (void *) &uniform_cross_prob);
347    else
348        ctx->ga.UniformCrossProb = uniform_cross_prob;
349
350    PGADebugExited("PGASetUniformCrossoverProb");
351}
Note: See TracBrowser for help on using the repository browser.