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 | |
---|
10 | void MyInitString(PGAContext *ctx, int p, int pop); |
---|
11 | void MyCrossover(PGAContext *ctx, int p1, int p2, int p_pop, int c1, |
---|
12 | int c2, int c_pop); |
---|
13 | int MyMutation(PGAContext *ctx, int p, int pop, double mr); |
---|
14 | int MyDuplicateString(PGAContext *ctx, int p1, int pop1, int p2, |
---|
15 | int pop2); |
---|
16 | void MyPrintString(PGAContext *ctx, FILE *fp, int p, int pop); |
---|
17 | int MyDone(PGAContext *ctx); |
---|
18 | void MyEndOfGen(PGAContext *ctx); |
---|
19 | |
---|
20 | double MyEvaluate(PGAContext *ctx, int p, int pop); |
---|
21 | |
---|
22 | |
---|
23 | int 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 | */ |
---|
46 | int 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. */ |
---|
58 | void 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. */ |
---|
66 | void 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 | */ |
---|
75 | int 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. */ |
---|
84 | void 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. */ |
---|
91 | int 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. */ |
---|
101 | void 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. */ |
---|
110 | double 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 | |
---|