www.pudn.com > dgpc.rar > gp.h
/*======================================================================+
| PGPC: Parallel Genetic Programming in C |
| (c) 1995 Genetic Algorithm Technology Corp. all rights reserved |
| written by David Andre |
+======================================================================*/
/*======================================================================+
| FILE: gp.h |
| DESCRIPTION: Parameters and structures for GP. |
| |
| REVISIONS: |
| Jan 24, 1995: Works as of today, no known bugs. |
| Jan 26, 1995: Added N Plus 2 Repro functionality |
| Mar 15, 1995: Adding Constrained Syntactic Structure.... |
| Mar 15, 1995: Adding Function Weights |
| Mar 21, 1995: Added Randomizer Options, control |
| Apr 1, 1995: Changing to better user interface.... |
+======================================================================*/
/* gp.h */
/***********************************************************************/
/* Definitions of some stuff to be used as tokens */
#define ON 1
#define OFF 0
#define TRUE 1
#define FALSE 0
#define TOP_OF_TREE -37
/***********************************************************************/
#include "_prbname.xxx"
/*
#define INCXFILENAME #INCXFILE
#include INCXFILENAME
*/
#include _PRB_H
#if __STDC__ || defined(__cplusplus)
#define P_(s) s
#define ANSI_FUNC
#else
#define P_(s) ()
#ifdef ANSI_FUNC
#undef ANSI_FUNC
#endif
#endif
#ifndef RAND
#define RAND Randu_Rand /*rand*/ /*PM_Rand*/ /*rand*/ /*MyRand*/
#endif
#ifndef SRAND
#define SRAND Randu_Srand /*srand*/ /*PM_Srand*/ /*srand*/ /*MySrand*/
#endif
#ifndef DRAND_MAX
#define DRAND_MAX Randu_MaxRand() /*RAND_MAX*/ /*PM_MaxRand()*/ /*RAND_MAX*/ /*MyMaxRand*/
#endif
#ifndef GET_RAND
#define GET_RAND GetRanduRand /*GetSystemSeed*/ /*GetMyRand*/ /*GetSeed*/
#endif
#ifndef FRAND
#define FRAND Randu_Rand /*rand*/ /*MyRand*/
#endif
#ifndef FSRAND
#define FSRAND Randu_Srand /*srand*/ /*MySrand*/
#endif
#ifndef FDRAND_MAX
#define FDRAND_MAX Randu_MaxRand() /*RAND_MAX*/ /*MyMaxRand()*/
#endif
#ifndef FGET_RAND
#define FGET_RAND GetRanduRand /*GetMyRand*/ /*GetSeed*/
#endif
#define _FIRST_ADF ((int) 0)
#define _LAST_ADF ((int) MAX_NUM_ADFS-1)
#define _FIRST_ADF_ARG ((int) MAX_NUM_ADFS)
#define _LAST_ADF_ARG ((int) MAX_NUM_ADFS + MAX_NUM_ADF_ARGS - 1)
#define _dmax(x,y) (((x)>(y))?(x):(y))
#define _dmin(x,y) (((x)<(y))?(x):(y))
#define _dabs(n) (((n)>0)?(n):(-(n)))
#define PGPC_MAXFLOAT (1370000037275507027000000000000000000.0)
#define PGPC_MAXINT 1073741354
/*#ifndef __BORLANDC__
#define BOSS_NODE_SENDS_FIT_CASES 1
#endif*/
#ifndef RUN_END_CRITERION
#define RUN_END_CRITERION 0.001
#endif
/********************************************************************/
/* VARIOUS USEFUL PROBLEM DESIGN MACROS*/
#define _dmax(x,y) (((x)>(y))?(x):(y))
#define _dmin(x,y) (((x)<(y))?(x):(y))
#define _set_code(cd) (*pop).func_table[func_index].code = cd;
#define _set_weight(wt) (*pop).func_table[func_index].weight = wt;
#define _set_macro(x) (*pop).func_table[func_index].macro = x;
#define _set_constant(x) (*pop).func_table[func_index].constant = x;
#define _set_print_name(dn) sprintf((*pop).func_table[func_index].print_name, #dn);
#define _set_arity(da) (*pop).func_table[func_index].arity = da;
#define _declare_adf(dn,cd,wt) _set_code(cd);_set_weight(wt);_set_macro(1);\
_set_constant(0);_set_print_name(dn);\
_set_arity(MIN_NUM_ADF_ARGS);\
if (func_index >= MAX_NUM_ADFS )\
{\
gpi_SendError("Error! Cannot have another ADF without increasing MAX_NUM_ADFS\n");\
exit(6);\
}\
func_index++;
#define _declare_arg(dn,cd,wt) if (func_index != MAX_NUM_ADFS)\
{\
gpi_SendError("Error! Did not declare enough ADFS");\
exit(6);\
}\
_set_code(cd); _set_weight(wt); _set_macro(0);\
_set_constant(0); _set_arity(0); _set_print_name(dn);\
if (func_index >= (MAX_NUM_ADFS + MAX_NUM_ADF_ARGS ) )\
{\
gpi_SendError("Error! Cannot have another ADF_ARG without increasing MAX_NUM_ADF_ARGS\n");\
exit(6);\
}\
func_index++;
#define _declare_func(cd,da,dm,dw,dn) if (func_index < MAX_NUM_ADFS+MAX_NUM_ADF_ARGS)\
{\
gpi_SendError("Error! Did not declare enough ADFs or ADF args\n");\
exit(6);\
}\
_set_code(cd); _set_weight(dw); _set_macro(dm);\
_set_constant(0); _set_arity(da); _set_print_name(dn);\
func_index++;
#define _declare_random(dw) _set_code(random_func); _set_weight(dw); _set_macro(0);\
_set_constant(1); _set_arity(0); _set_print_name("%%f");\
func_index++;\
if (func_index != TOTAL_NUMBER_OF_FUNCTIONS) \
{\
gpi_SendError("Error! Wrong number of functions in .h\n");\
gpi_SendError("Either TOTAL_NUMBER_OF_FUNCTIONS is wrong");\
gpi_SendError(" or some function is not declared!\n"); \
sprintf(str,"Total number declared:%d, TOTAL_NUMBER_OF_FUNCTIONS:%d\n",\
func_index,TOTAL_NUMBER_OF_FUNCTIONS); \
gpi_SendError(str);\
exit(5);\
}\
else {\
for (i=0;iind->index_ptr)++
#if (SHORT_CIRCUIT_EVAL ==1)
#define _SHORT_CIRC_CODE if (g_done_early) return(-1);
#else
#define _SHORT_CIRC_CODE
#endif
#define _start_def_func(fname) GTYPE fname P_((Branch *br)); \
GTYPE fname(Branch *br){ GTYPE largs[MAX_NUM_ARGS]; br->ind->index_ptr++; _SHORT_CIRC_CODE {
#define _start_def_macro(fname) GTYPE fname P_((Branch *br)); \
GTYPE fname(Branch *br){br->ind->index_ptr++; _SHORT_CIRC_CODE {
#define _eval_args() {int opcode;\
int dind_ea; \
opcode = br->tree[br->ind->index_ptr-1].opcode;\
for(dind_ea=0;dind_ea<_function_arity(opcode);dind_ea++)\
{\
largs[dind_ea] = _eval_subtree(br,ppop); }}
#define _get_args(n) (largs[n])
#define _end_def_func(fname) }}
#define _end_def_macro(fname) }}
/********************************************************************/
#define FULL 1
#define GROWTH 2
#define RAMPED 3
#define OP_DONE -1
#define CROSSOVER 101
#define MUTATION 102
#define BRCREATE 103
#define DECOMPRESS 104
#define BRDUP 105
#define BRDEL 106
#define ARGDUP 107
#define ARGDEL 108
#define CROSSOVER_ON_NODES 111
#define CROSSOVER_ON_LEAVES 112
#define IPB_BRC 113
#define COPY 190
#define START_CREATION 561
#define END_CREATION 562
#define START_EVAL 563
#define END_EVAL 564
#define START_REPRO 565
#define END_REPRO 566
#define START_EVAL_DUDE 567
#define END_EVAL_DUDE 568
#define IN_CREATION 661
#define IN_EVAL 662
#define IN_REPRO 663
#define ADF_TYPE_IPB 91
#define ADF_TYPE_ADF 92
#define UNDEFINED -999
#define MAXNUMFUNCTIONS 256
#define MAXNUMFORARANDOMCONSTANT 245
#define NUM_OPS_TO_KEEP 51
#ifndef GOOD_TOURNAMENT_SIZE
#define GOOD_TOURNAMENT_SIZE 7
#endif
#define BAD_TOURNAMENT_SIZE 1
#define SEARCH_LIMIT_FOR_CROSSOVER 250
#define KLUDGE_MAX_NUM_FIT_CASES 256
#define MAX_DEPTH_FOR_TREE 40
#define MAX_ATTEMPTS 100
#define MAX_NUM_EMIGRANTS_PER_GENERATION 1000
#define NUM_EXTRA_SLOTS 1
#define EMPTY -1
#define ROOT TOTAL_NUMBER_OF_FUNCTIONS
#define _max(x,y) ((x)>(y)?(x):(y))
#define _min(x,y) ((x)>(y)?(y):(x))
/* These must be defined for my data structure */
#define _fv_map(n) (((int)(n) >= (TOTAL_NUMBER_OF_FUNCTIONS-1))?(TOTAL_NUMBER_OF_FUNCTIONS-1):((int)(n)))
#define _function_arity(n) ((int) (br->function_vector[(int)(_fv_map((int)(n)))]))
#define _function_is_macro(pop,n) (*pop).func_table[(int)_fv_map((n))].macro
#define _function_code(pop,n) (*pop).func_table[(int)_fv_map((n))].code
#define _function_is_constant(n) (((n) >= (TOTAL_NUMBER_OF_FUNCTIONS-1)) ? 1 :0)
#define _function_is_adf(n) ((n)= MAX_NUM_ADFS))
#define _function_is_general(n) ((n)>=(MAX_NUM_ADFS+MAX_NUM_ADF_ARGS) && ((n)<(TOTAL_NUMBER_OF_FUNCTIONS-1)))
#define _function_printname(n) gpop.func_table[(int)_fv_map((n))].print_name
#define _eval_subtree(br,dpop) ((*(_function_code(dpop,((br)->tree[(br)->ind->index_ptr]).opcode)))(br))
#define _skip_subtree(br) br->ind->index_ptr = br->tree[br->ind->index_ptr].jump
#define MAXSTRING 100
typedef unsigned char Snode;
typedef struct fast_node {
int opcode;
int jump;
} Fnode;
typedef struct fte {
int arity;
int macro;
int constant;
int weight;
GTYPE (*code) P_(( ));
/*char *print_name;*/
char print_name[50];
} FunctionTableEntry;
typedef struct fast_branch {
int branchnum;
int num_nodes;
Fnode * tree;
struct ind * ind;
int function_vector[TOTAL_NUMBER_OF_FUNCTIONS];
} Branch;
typedef struct ind {
Branch rpbs[_max(1,NUM_RPBS)];
Branch adfs[_max(1,MAX_NUM_ADFS)];
int current_number_of_adfs;
float s_fitness;
int adf_arity[_max(1,MAX_NUM_ADFS)];
int hits;
float beauty;
Fnode code[MAX_NUM_NODES_PER_DUDE];
int index_ptr;
#if (USE_MULTI_TYPED_ADFS)
int adf_type[_max(1,MAX_NUM_ADFS)];
#endif
} Individual; /* Structure for an Individual */
typedef struct small_ind {
#if (USE_MULTI_TYPED_ADFS)
signed char adf_type[_max(1,MAX_NUM_ADFS)];
#endif
signed char current_number_of_adfs;
float s_fitness;
int hits;
float beauty;
#if (POINT_TYPING)
signed char function_vector[MAX_NUM_ADFS + NUM_RPBS][_max(1,MAX_NUM_ADFS + MAX_NUM_ADF_ARGS)];
#endif
Snode code[MAX_NUM_NODES_PER_DUDE];
int usage;
struct small_ind * forward;
struct small_ind * backward;
struct repro * op_list;
} CompInd;
typedef struct pinformation {
int num_parents;
int parents[MAX_NUM_NODES_PER_DUDE/2];
int branches[MAX_NUM_NODES_PER_DUDE/2];
} ParentInfo;
typedef struct repro {
/*short*/ int repro_op_type;
int parent1;
int parent2;
int brnum1;
int brnum2; /*could be removed*/
int point1;
struct repro * next_for_parent1;
struct repro * next_for_parent2;
} ReproOpInfo;
typedef struct {
GTYPE g_arg[MAX_NUM_ARGS];
} PopGlobals;
typedef struct start_msg
{
int num_nodes; /* -- should this be in the default.in file or gleaned from the cfs script? */
int num_individuals;
int num_generations;
int run_num;
char problem[20];
int max_new_tree_depth;
int max_depth_for_mutation;
int num_primed_individuals;
float crossover_fraction_for_leaves;
float crossover_fraction_for_node;
float mutation_fraction;
float copy_fraction;
float branch_creation_fraction;
float branch_duplication_fraction;
float branch_deletion_fraction;
float arg_duplication_fraction;
float arg_deletion_fraction;
float arg_creation_fraction;
float early_crossover_fraction_for_leaves;
float early_crossover_fraction_for_node;
float early_mutation_fraction;
float early_copy_fraction;
float early_branch_creation_fraction;
float early_branch_duplication_fraction;
float early_branch_deletion_fraction;
float early_arg_duplication_fraction;
float early_arg_deletion_fraction;
float early_arg_creation_fraction;
#if (USE_IPBCO || USE_ITERATION_GROUP_CREATION)
float early_ipbco_fraction;
float ipbco_fraction;
#endif
int growth_method; /* RAMPED, FULL, or GROWTH */
int as_if_number;
long perpetual_number;
long fit_case_seed;
long seed;
GTYPE random_constant_table[MAXNUMFUNCTIONS];
Individual primed_individual;
/*Can Add Additional Flags (and the code to deal with them in both
the boss and breeder, as desired. */
/*These may want to end up being set by the default.in file, perhaps?*/
/*At the moment a function on the boss called (set-bn-debug flags*/
/*will suffice*/
} StartupInfo;
typedef struct hist {
int histogram[NUM_BUCKETS];
} HistogramInfo;
typedef struct psi {
int extra_ints[NUM_EXTRA_SLOTS];
float extra_floats[NUM_EXTRA_SLOTS];
#if (USER_EXTRA_BEST_INFO)
UserExtraInfo user_extra_info[NUM_EXTRA_SLOTS];
#endif
} ExtraBestDudeInfo;
typedef struct eog_msg
{
Individual ind;
Individual spec_ind;
float out_fitness;
float out_beauty;
int out_hits;
int spec_ind_flag;
int gen;
int out_flag;
float best_fitness;
int best_hits;
float worst_fitness;
int worst_hits;
float average_fitness;
int average_hits;
float variance_of_fitness;
float variance_of_hits;
ExtraBestDudeInfo best_extra;
float delta_avf;
#if (HISTOGRAMS)
HistogramInfo histogram;
#endif
int ps_count;
/*Maybe add the histogram?*/
} EndGenInfo;
typedef struct eureka
{
Individual ind;
int gen;
ExtraBestDudeInfo best_extra;
} EurekaInfo;
typedef struct text_string
{
char buf[500];
} TextInfo;
typedef struct constraint {
int children[TOTAL_NUMBER_OF_FUNCTIONS];
} ConstraintInfo;
typedef struct pop{
char problem[20];
int num_nodes;
int pop_size;
CompInd * members;
CompInd * free1;
CompInd * free2;
ReproOpInfo * repro_op_list;
CompInd * free_list;
CompInd * one_list;
CompInd * two_list;
CompInd * more_list;
CompInd childx1;
CompInd childx2;
Individual parent1;
Individual parent2;
Individual tempind;
int * culled_population;
int number_in_culled_pop;
Individual best_so_far;
Individual best_out_so_far;
char outfilename[50];
int best_of_run_num;
int best_dude_index;
ExtraBestDudeInfo temp_extra;
ExtraBestDudeInfo best_extra;
ExtraBestDudeInfo out_sample_extra;
float best_fitness;
float best_out_fitness;
FunctionTableEntry func_table[MAXNUMFUNCTIONS];
int num_general_functions;
int num_functions[NUM_RPBS+MAX_NUM_ADFS];
int function_assignment[NUM_RPBS+MAX_NUM_ADFS][MAXNUMFUNCTIONS];
char read_files[NUM_RPBS+MAX_NUM_ADFS][MAXSTRING];
GTYPE constants[MAXNUMFUNCTIONS];
int brstart[NUM_RPBS+MAX_NUM_ADFS];
int brend[NUM_RPBS+MAX_NUM_ADFS];
int brsize[NUM_RPBS+MAX_NUM_ADFS];
int brweight[NUM_RPBS+MAX_NUM_ADFS];
int num_constants;
int primed_constants;
FILE * out_file;
int emigrants[MAX_NUM_EMIGRANTS_PER_GENERATION];
int num_emigrants;
int num_immigrants;
float last_avf;
StartupInfo pop_startup_info;
PopGlobals pop_globals;
EndGenInfo pop_eog;
EurekaInfo pop_eureka;
ConstraintInfo con_parents[TOTAL_NUMBER_OF_FUNCTIONS+1];
int new_best;
} Population; /* Structure for a Population */
#ifdef _ICC
typedef struct {
char problem[20];
int run_number;
char start_time[256];
int rows;
int cols;
int num_meshnodes;
int num_gens_to_graph;
int max_hits;
float best_fitness;
float max_fitness;
int highest_gen_so_far;
int better_hits_alert;
#if (HISTOGRAMS)
HistogramInfo overall_histogram; /*single histogram*/
HistogramInfo * node_histograms; /*array N = num_meshnodes*/
#endif
int * gen_of_each_node; /*array N = num_meshnodes*/
int * status_of_each_node; /*array N = num_meshnodes*/
int * hits_of_each_node; /*array N = num_meshnodes*/
int * time_of_last_report; /*array N = num_meshnodes*/
float * fit_of_each_node; /*array N = num_meshnodes*/
int * best_hits_of_each_gen; /*array N = num_gens*/
float * best_fit_of_each_gen; /*array N = num_gens*/
}GraphicInfo; /*Structure providing info for graphing on
transputer machine*/
#endif
#include "gpshared.h"
#include "prbproto.h"