source: trunk/optimizer/src/pgapack/pgapack/examples/templates/uf_native.c @ 816

Last change on this file since 816 was 816, checked in by liveletlive, 16 years ago

Committing the newer version of PGAPack.

File size: 3.4 KB
Line 
1/*  Stub functions for using PGAPack with a native datatype, but user defined
2 *  operators.
3 *
4 *  Simple example (with no actual code) that shows how one would go about
5 *  setting PGAPack up to evolve "strings" that use a native datatype, but
6 *  need to use custom evolutionary operators.
7 */
8#include <pgapack.h>
9
10void         MyInitString(PGAContext *ctx, int p, int pop);
11void         MyCrossover(PGAContext *ctx, int p1, int p2, int p_pop, int c1,
12                         int c2, int c_pop);
13int          MyMutation(PGAContext *ctx, int p, int pop, double mr);
14int          MyDuplicateString(PGAContext *ctx, int p1, int pop1, int p2,
15                               int pop2);
16void         MyPrintString(PGAContext *ctx, FILE *fp, int p, int pop);
17int          MyDone(PGAContext *ctx);
18void         MyEndOfGen(PGAContext *ctx);
19
20double       MyEvaluate(PGAContext *ctx, int p, int pop);
21 
22
23int main(int argc, char **argv) {
24  PGAContext *ctx;
25
26  ctx = PGACreate(&argc, argv, PGA_DATATYPE, 1, PGA_MAXIMIZE);
27
28  PGASetUserFunction(ctx, PGA_USERFUNCTION_MUTATION,    MyMutation);
29  PGASetUserFunction(ctx, PGA_USERFUNCTION_CROSSOVER,   MyCrossover);
30  PGASetUserFunction(ctx, PGA_USERFUNCTION_PRINTSTRING, MyPrintString);
31  PGASetUserFunction(ctx, PGA_USERFUNCTION_DUPLICATE,   MyDuplicateString);
32  PGASetUserFunction(ctx, PGA_USERFUNCTION_INITSTRING,  MyInitString);
33  PGASetUserFunction(ctx, PGA_USERFUNCTION_DONE,        MyDone);
34  PGASetUserFunction(ctx, PGA_USERFUNCTION_ENDOFGEN,    MyEndOfGen);
35 
36  PGASetUp(ctx);
37  PGARun(ctx, MyEvaluate);
38  PGADestroy(ctx);
39  return(0);
40}
41
42
43/*  Perform mutation on a "string".  It is important to keep count of the
44 *  number of mutations performed and to return that value.
45 */
46int MyMutation(PGAContext *ctx, int p, int pop, double mr) {
47  int         count;
48
49  /*  Insert code to mutate Data here.  Remember to count the number
50   *  of mutations that happen, and return that value!
51   */
52
53  return(count);
54}
55
56
57/*  Perform crossover from two parents to two children.  */
58void MyCrossover(PGAContext *ctx, int p1, int p2, int p_pop, int c1, int c2,
59                 int c_pop) {
60   
61    /*  Perform crossover from p1 and p2 into c1 and c2 here.  */
62}
63
64
65/*  Print a "string" to the file fp.  */
66void MyPrintString(PGAContext *ctx, FILE *fp, int p, int pop) {
67
68    /*  Print the string referenced by p and pop to the file fp.  */
69}
70
71
72/*  Determine if two strings are the same.  If so, return non-zero, otherwise
73 *  return zero.  If contiguous data, usually a call to memcmp.
74 */
75int MyDuplicateString(PGAContext *ctx, int p1, int pop1, int p2, int pop2) {
76
77    /*  Compare strings p1 and p2 to see if they are different.  If they
78     *  are, return non-zero; else, return 0.
79     */
80}
81
82
83/*  Randomly initialize a string.  */
84void MyInitString(PGAContext *ctx, int p, int pop) {
85
86  /*  Insert code to randomly initialize p in popultion pop here.  */
87}
88
89
90/*  Check if a GA has found an acceptable solution.  */
91int MyDone(PGAContext *ctx) {
92    int done = PGA_FALSE;
93
94    /*  Check for "doneness".  */
95
96    return(done);
97}
98
99
100/*  After each generation, this funciton will get called.  */
101void MyEndOfGen(PGAContext *ctx) {
102
103    /*  Do something useful; display the population on a graphics output,
104     *  let the user adjust the population, etc.
105     */
106}
107
108
109/*  The evaluation function.    */
110double MyEvaluate(PGAContext *ctx, int p, int pop) {
111   
112    /*  Evaluate the string here, and return a double representing
113     *  the quality of the solution.
114     */
115}
116
Note: See TracBrowser for help on using the repository browser.