-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathga.h
212 lines (185 loc) · 7.67 KB
/
ga.h
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
/*============================================================================
| (c) Copyright Arthur L. Corcoran, 1992, 1993. All rights reserved.
| (c) Copyright IA UPM - Group 5, 2020. All rights reserved.
|
| Genetic Algorithm Definitions
|
| Goldberg's Terminology
| ----------------------
| Chromosome = string
| Gene = feature, character, detector
| Allele = feature value
| Locus = string position
| Genotype = structure
| Phenotype = parameter set, alternative solution, a decoded structure
| Epistasis = nonlinearity
============================================================================*/
/*----------------------------------------------------------------------------
| Header files
----------------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <string.h>
#if defined(__BORLANDC__)
#include <process.h>
#include <alloc.h>
#elif !defined(__STDC__)
#include <malloc.h>
#endif
/*----------------------------------------------------------------------------
| Constants
----------------------------------------------------------------------------*/
#define VERSION "1.01"
#define COPYRIGHT "(c) Copyright Arthur L. Corcoran, 1992, 1993. All rights reserved."
#define COPYRIGHT2 "(c) Copyright IA UPM - Group 5, 2020. All rights reserved."
#define FALSE 0
#define TRUE 1
#define OK 0
#define GA_ERROR !(OK)
#define UNSPECIFIED -1
/*--- Data type --- */
#define DT_BIT 0 /* Bit string */
#define DT_INT 1 /* Integers */
#define DT_INT_PERM 2 /* Integer Permutation */
#define DT_REAL 3 /* Reals */
/*--- Method to generate initial pool --- */
#define IP_NONE 0x00
#define IP_INTERACTIVE 0x01
#define IP_FROM_FILE 0x02
#define IP_RANDOM 0x04
#define IP_RANDOM01 0x05
/*--- Type of output report --- */
#define RP_NONE 0
#define RP_MINIMAL 1
#define RP_SHORT 2
#define RP_LONG 3
/*--- Magic cookies for validation ---*/
#define NL_cookie 0x00000000 /* NULL cookie */
#define CF_cookie 0x11111111 /* ga_info (config) cookie */
#define PL_cookie 0x22222222 /* pool cookie */
#define CH_cookie 0x33333333 /* chrom cookie */
/*----------------------------------------------------------------------------
| Type definitions
----------------------------------------------------------------------------*/
/*--- A function pointer ---*/
typedef int (*FN_Ptr)();
/*--- A function table ---*/
typedef struct {
char *name; /* Function name */
FN_Ptr fun; /* Function pointer */
} FN_Table_Type, *FN_Table_Ptr;
/*--- A Gene (or allele) is a bit, int, float, etc. ---*/
typedef double Gene_Type, *Gene_Ptr;
/*--- A Chromosome ---*/
typedef struct {
long magic_cookie; /* For validation */
Gene_Ptr gene; /* Encoding */
int length; /* Length of gene */
double fitness; /* Fitness value of chromosome */
float ptf; /* Percent of total fitness */
int index; /* My index */
int idx_min, idx_max; /* Reserved */
int parent_1, parent_2; /* Indices of parents */
int xp1, xp2; /* Crossover points */
} Chrom_Type, *Chrom_Ptr;
/*--- A Pool ---*/
typedef struct {
long magic_cookie; /* For validation */
Chrom_Ptr *chrom; /* Chromosomes */
int size, max_size; /* Number of chromosomes */
double total_fitness; /* Total fitness of pool */
double min, max, ave, var, dev; /* Current pool fitness stats */
int min_index, max_index; /* Index of min/max chromosomes */
int best_index; /* Index of best chromosome */
int minimize; /* Minimize pool [y/n]? */
int sorted; /* Is pool sorted [y/n]? */
} Pool_Type, *Pool_Ptr;
/*--- GA configuration info ---*/
typedef struct {
/*--- Basic info ---*/
long magic_cookie; /* For validation */
char user_data[80]; /* User data file (unused) */
int function_index; /* Index for the objetive function */
int rand_seed; /* Seed for random number generator */
int datatype; /* Data type flag */
int ip_flag; /* Initial pool generation method flag */
char ip_data[80]; /* Data file name (IP_FROM_FILE) */
int pool_size; /* Pool size (IP_RANDOM) */
int chrom_len; /* Chromosome size (IP_RANDOM) */
int iter, max_iter; /* Number of iterations for ga */
int minimize; /* Minimize EV_fun? */
int elitist; /* Use elitism? */
int converged; /* Has ga converged? */
int use_convergence; /* Use convergence? */
float bias; /* Selection bias */
float gap; /* Generation gap */
float x_rate; /* Crossover rate */
float mu_rate; /* Mutation rate */
float scale_factor; /* Scale for fitness <= 0 */
float pert_range; /* Range of the perturb. -- Introduced by Claudio*/
float *mut_bias; /* displace center of pert -- Introd. by Claudio*/
/*--- Functions ---*/
FN_Ptr GA_fun; /* GA */
FN_Ptr SE_fun; /* Selection */
FN_Ptr X_fun; /* Crossover */
FN_Ptr MU_fun; /* Mutation */
FN_Ptr EV_fun; /* Evaluation */
FN_Ptr RE_fun; /* Replacement */
/*--- Reports ---*/
int rp_type; /* Type of output report */
int rp_interval; /* Output report interval */
FILE *rp_fid; /* Output report fid */
char rp_file[80]; /* Output report file name */
/*--- Pools ---*/
Pool_Ptr old_pool, new_pool;
/*--- Stats ---*/
Chrom_Ptr best; /* Best chromosome */
int num_mut, tot_mut; /* Mutation statistics */
} GA_Info_Type, *GA_Info_Ptr;
/*----------------------------------------------------------------------------
| Pseudo-functions
----------------------------------------------------------------------------*/
/*--- random number in [0..1] ---*/
#if defined(__BORLANDC__)
#define SEED_RAND(seed) (srand((seed)))
#define RAND_FRAC() ((double)rand()/RAND_MAX)
#else
#define SEED_RAND(seed) (srand((seed)))
#define RAND_FRAC() ((double)rand()*(1.0/RAND_MAX))
#endif
/*--- random number in domain [lo..hi] ---*/
#define RAND_DOM(lo,hi) ((int) floor(RAND_FRAC()*(((hi)-(lo))+0.999999))+(lo))
//#define RAND_DOM(lo,hi) ((int) floor(RAND_FRAC()*((hi)-(lo)))+(lo))
/*--- random bit ---*/
#define RAND_BIT() ((RAND_FRAC()>=.5)? 1 : 0 )
/*--- min and max ---*/
#define MIN(a,b) ((a < b) ? (a) : (b))
//#define MAX(a,b) ((a > b) ? (a) : (b))
#define UT_warn(message) {fprintf(stderr,"WARNING: %s\n", message);}
#define UT_error(message) {fprintf(stderr,"GA_ERROR: %s\n", message); exit(1);}
#define UT_iswap(a, b) {int tmp; tmp = *(a); *(a) = *(b); *(b) = tmp;}
#ifdef __cplusplus
extern "C" {
#endif
/*----------------------------------------------------------------------------
| Function prototypes
----------------------------------------------------------------------------*/
char *GA_name(), *SE_name(), *X_name(), *MU_name(), *RE_name();
char *FN_name();
Chrom_Ptr SE_fun(), CH_alloc();
Pool_Ptr PL_alloc();
GA_Info_Ptr GA_config(char *cfg_name,int (*EV_fun)(Chrom_Ptr chrom));
GA_Info_Ptr CF_alloc();
//extern int obj_fun( Chrom_Ptr chrom);
int GA_run( GA_Info_Ptr ga_info);
int GA_init_trial(GA_Info_Ptr ga_info);
int GA_gap(GA_Info_Ptr ga_info);
void GA_trial(GA_Info_Ptr ga_info);
void GA_gen_init(GA_Info_Ptr ga_info);
void GA_reset(GA_Info_Ptr ga_info, char *cfg_name);
void re_evaluate_pop(GA_Info_Ptr ga_info);
#ifdef __cplusplus
}
#endif