main.c

来自「Genetic Programing of music」· C语言 代码 · 共 814 行 · 第 1/2 页

C
814
字号
          fset[i].terminal_count = 0;          for ( j = 0; j < fset[i].size; ++j )          {               if ( fset[i].cset[j].arity == -1 )               {		    /* change the arity of evaluation tokens from -1		       to the number of argument tokens in the called tree. */                    fset[i].cset[j].arity = fset[tree_map[fset[i].cset[j].evaltree].fset].num_args;                    if ( fset[i].cset[j].arity == 0 )			 /* if there are no argument tokens in the tree,			    mark this as a terminal. */                         fset[i].cset[j].type = EVAL_TERM;               }	       /* update count of functions and terminals. */               if ( fset[i].cset[j].arity )                    ++fset[i].function_count;               else                    ++fset[i].terminal_count;          }	  /* now sort the function set so that all the functions	     come first. */          qsort ( fset[i].cset, fset[i].size, sizeof ( function ),                 function_compare );     }          #ifdef DEBUG     /* dump the function sets to stdout. */     for ( i = 0; i < fset_count; ++i )     {          printf ( "FUNCTION SET %d\n", i );          printf ( "   %d functions; %d terminals; %d arguments\n",                  fset[i].function_count, fset[i].terminal_count,                  fset[i].num_args );          for ( j = 0; j < fset[i].size; ++j )               printf ( "%10s %06x %06x %06x arity: %3d evaltree: %3d index: %3d type: %3d\n",                       fset[i].cset[j].string,                       fset[i].cset[j].code,                       fset[i].cset[j].ephem_gen,                       fset[i].cset[j].ephem_str,                       fset[i].cset[j].arity,                       fset[i].cset[j].evaltree,                       fset[i].cset[j].index,                       fset[i].cset[j].type );     }#endif                            oprintf ( OUT_SYS, 30, "    function set complete.\n" );     return 0;}/* function_compare() * * comparison function for qsort() that puts all functions ahead of * all terminals. */int function_compare ( const void *a, const void *b ){     int aa, ba;     aa = !!(((function *)a)->arity);     ba = !!(((function *)b)->arity);     return ba-aa;}/* free_function_sets() * * free up internal copies of function sets and tree maps. */void free_function_sets ( void ){     int i, j;     for ( i = 0; i < fset_count; ++i )     {          for ( j = 0; j < fset[i].function_count + fset[i].terminal_count; ++j )               FREE ( fset[i].cset[j].string );          FREE ( fset[i].cset );     }     FREE ( fset );     for ( i = 0; i < tree_count; ++i )          FREE ( tree_map[i].name );     FREE ( tree_map );          fset = NULL;     tree_map = NULL;}/* read_tree_limits() * * read limits on tree node count and/or depth from the parameter * database and fill in the appropriate fields of the tree_map * array. */void read_tree_limits ( void ){     int i, j;     char pnamebuf[100];     char *param;     for ( i = 0; i < tree_count; ++i )     {	  /* read the node limit for this tree. */          sprintf ( pnamebuf, "tree[%d].max_nodes", i );          param = get_parameter ( pnamebuf );          if ( param == NULL )               tree_map[i].nodelimit = -1;          else               tree_map[i].nodelimit = atoi ( param );	  /* read the depth limit for this tree. */          sprintf ( pnamebuf, "tree[%d].max_depth", i );          param = get_parameter ( pnamebuf );          if ( param == NULL )               tree_map[i].depthlimit = -1;          else               tree_map[i].depthlimit = atoi ( param );     }     /* read the node limit for the whole individual. */     param = get_parameter ( "max_nodes" );     if ( param == NULL )          ind_nodelimit = -1;     else          ind_nodelimit = atoi ( param );     /* read the depth limit for the whole individual.  note that	this is implemented just as a cap on the maximum depth of	any single tree in the individual. */     param = get_parameter ( "max_depth" );     if ( param )     {          j = atoi ( param );          if ( j >= 0 )               for ( i = 0; i < tree_count; ++i )                    if ( tree_map[i].depthlimit < 0 ||                        tree_map[i].depthlimit > j )                         tree_map[i].depthlimit = j;     }}/* initialize_random() * * initialize the random number generator. */                    void initialize_random ( void ){     char *param;     int seed;     /* look for a seed parameter. */     param = get_parameter ( "random_seed" );     if ( param == NULL )     {	  /* if it's not found... */#ifdef RANDOMSEEDTIME	  /* ...use the current time. */	  seed = time(NULL);#else	  /* ...use 1. */	  seed = 1;#endif	  /* print out what we're using. */	  oprintf ( OUT_SYS, 20,		   "    no random number seed specfied; using %d.\n",		   seed );     }     else     {	  /* the parameter was found; use it. */	  seed = atoi ( param );	  oprintf ( OUT_SYS, 20,		   "    seeding random number generator with %d.\n",		   seed );     }     random_seed ( seed );}/* pre_parameter_defaults() * * used to place values into the parameter database before any application * code is called or any command line options are processed. */void pre_parameter_defaults ( void ){     add_parameter ( "output.basename",          "lilgp", PARAM_COPY_NONE );     add_parameter ( "output.stt_interval",      "1", PARAM_COPY_NONE );     add_parameter ( "output.detail",            "50", PARAM_COPY_NONE );     add_parameter ( "output.bestn",             "1", PARAM_COPY_NONE );     add_parameter ( "output.digits",            "4", PARAM_COPY_NONE );          add_parameter ( "init.method",              "half_and_half",                    PARAM_COPY_NONE );     add_parameter ( "init.depth",               "2-6", PARAM_COPY_NONE );     add_parameter ( "init.random_attempts",     "100", PARAM_COPY_NONE );          add_parameter ( "checkpoint.filename",      "gp%06d.ckp",                    PARAM_COPY_NONE );          /* default problem uses a single population. */     add_parameter ( "multiple.subpops", "1", PARAM_COPY_NONE );}/* post_parameter_defaults() * * add/change values in the parameter database after all command line options * are parsed.  can be used to set defaults based on values of other * parameters. */void post_parameter_defaults ( void ){     binary_parameter ( "probabilistic_operators", 1 );}/* process_commandline() * * parses the command line. */int process_commandline ( int argc, char **argv, int *gen,                          multipop **mpop ){     int i;     int errorflag = 0;     int startfromcheckpoint = 0;     *mpop = NULL;     *gen = 0;     /* if there are no arguments, print out a brief statement of usage	and exit. */     if ( argc < 2 )     {          fprintf ( stderr, "usage: %s options\nValid options are:\n", argv[0] );	  fprintf ( stderr, "      [-f parameterfile]     read named parameter file\n" );	  fprintf ( stderr, "      [-c checkpointfile]    restart from name checkpoint file\n" );	  fprintf ( stderr, "      [-p name=value]        set parameter name to value\n" );	  fprintf ( stderr, "      [-q]                   run in quiet mode\n" );	  fprintf ( stderr, "      [-d symbol]            define symbol\n" );	  fprintf ( stderr, "      [-u symbol]            undefine symbol\n" );          exit(1);     }          /* load hardcoded defaults into database. */     pre_parameter_defaults();     for ( i = 1; i < argc; ++i )     {	  /* all options begin with '-' and have two characters,	     except "-d" and "-u" which may have more. */          if ( argv[i][0] != '-' || ( argv[i][1] != 'd' && argv[i][1] != 'u' && argv[i][2] != 0 ) )          {                error ( E_ERROR, "unrecognized command line option: \"%s\".",                      argv[i] );               errorflag = 1;               continue;          }          switch ( argv[i][1] )          {             case 'f':	       /* load a parameter file, named in the next argument. */               read_parameter_file ( argv[++i] );               break;             case 'p':	       /* parse a single parameter, in the next argument. */               if ( parse_one_parameter ( argv[++i] ) )               {                    errorflag = 1;                    error ( E_ERROR, "malformed parameter: \"%s\".", argv[i] );               }               break;             case 'c':	       /* load a checkpoint file, named in the next argument. */               if ( startfromcheckpoint )               {		    /* error if a checkpoint has already been loaded. */                    error ( E_ERROR, "can't load multiple checkpoint files." );                    errorflag = 1;                    continue;               }               read_checkpoint ( argv[++i], gen, mpop );               startfromcheckpoint = 1;               break;             case 'q':	       /* turn on quiet mode (don't dup OUT_SYS to stdout). */               quietmode = 1;               break;             case 'd':	       /* define a symbol. */               if ( argv[i][2] )		    /* of the form "-dsymbol". */                    define_directive ( argv[i]+2 );               else		    /* of the form "-d symbol". */                    define_directive ( argv[++i] );               break;             case 'u':	       /* undefine a symbol. */               if ( argv[i][2] )		    /* of the form "-usymbol". */                    undefine_directive ( argv[i]+2 );               else		    /* of the form "-u symbol". */                    undefine_directive ( argv[++i] );               break;             default:               error ( E_ERROR, "unrecognized command line option: \"%s\".",                      argv[i] );               errorflag = 1;               break;          }     }     if ( errorflag )          error ( E_FATAL_ERROR, "command line errors occurred.  dying." );     if ( !startfromcheckpoint )	  post_parameter_defaults();     return startfromcheckpoint;     }/* output_system_stats() * * print statistics about memory use, execution time, etc. to OUT_SYS * at conclusion of run. */void output_system_stats ( event *t_total, event *t_eval, event *t_breed ){     int total, free, max, mallocc, reallocc, freec;     int ercused, ercfree, ercblocks, ercalloc;     int i;     get_ephem_stats ( &ercused, &ercfree, &ercblocks, &ercalloc );          oprintf ( OUT_SYS, 30, "\nSYSTEM STATISTICS\n" );#ifdef TRACK_MEMORY     /* if memory tracking available, then get and print the numbers. */     get_memory_stats ( &total, &free, &max, &mallocc, &reallocc, &freec );     oprintf ( OUT_SYS, 30, "\n------- memory -------\n" );     oprintf ( OUT_SYS, 30, "           allocated:      %d\n", total );     oprintf ( OUT_SYS, 30, "               freed:      %d\n", free );     oprintf ( OUT_SYS, 30, "           not freed:      %d\n", total-free );     oprintf ( OUT_SYS, 30, "       max allocated:      %d\n", max );     oprintf ( OUT_SYS, 30, "    malloc'ed blocks:      %d\n", mallocc );     oprintf ( OUT_SYS, 30, "   realloc'ed blocks:      %d\n", reallocc );       oprintf ( OUT_SYS, 30, "      free'ed blocks:      %d\n", freec );#endif#ifdef TIMING_AVAILABLE     /* if timing is available, the get and print the numbers. */     oprintf ( OUT_SYS, 30, "\n------- time -------\n" );     oprintf ( OUT_SYS, 30, "             overall:      %s\n",              event_string ( t_total ) );     oprintf ( OUT_SYS, 30, "          evaluation:      %s\n",              event_string ( t_eval ) );     oprintf ( OUT_SYS, 30, "            breeding:      %s\n",              event_string ( t_breed ) );#endif          /* show how large the generation spaces grew. */     oprintf ( OUT_SYS, 30, "\n------- generation spaces -------\n" );     for ( i = 0; i < GENSPACE_COUNT; ++i )          oprintf ( OUT_SYS, 30, "      space %3d size:      %d\n",                   i, gensp[i].size );     /* if any ERCs were used, then show these stats. */     if ( ercused > 0 )     {          oprintf ( OUT_SYS, 30, "\n------- ephemeral random constants -------\n" );          oprintf ( OUT_SYS, 30, "                used:      %d\n", ercused );          oprintf ( OUT_SYS, 30, "               freed:      %d\n", ercfree );          oprintf ( OUT_SYS, 30, "           allocated:      %d\n", ercalloc );          oprintf ( OUT_SYS, 30, "              blocks:      %d\n", ercblocks );     }}   /* initial_message() * * show startup and copyright messages. */void initial_message ( void ){     oputs ( OUT_SYS, 0,            "\n[ lil-gp Genetic Programming System.\n" );     oputs ( OUT_SYS, 0,            "[ Portions copyright (c) 1995 Michigan State University.  All rights reserved.\n" );     oputs ( OUT_SYS, 0,            "[ kernel version 1.0; 11 July 1995.\n\n\n" );              }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?