⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 app.c

📁 Genetic Programing of music
💻 C
📖 第 1 页 / 共 2 页
字号:
  */        ret = function_sets_init ( fset, 1, tree_map,                               tree_name, 1 );     FREE ( fset );     FREE ( tree_map );     FREE ( tree_name );     return ret;}/* app_eval_fitness() * * this function should evaluate the fitness of the individual.  typically * this function will loop over all the fitness cases.  the following * fields in the (individual *) should be filled in: *    r_fitness    (raw fitness) *    s_fitness    (standardized fitness) *    a_fitness    (adjusted fitness) *    hits         (hits) *    evald        (always set to EVAL_CACHE_VALID) */void app_eval_fitness ( individual *ind ){  DATATYPE noteSequence;  int i;  FILE *xmFile;  char response[10];  static int numEvals=1;  globaldata *g;  /* get the global data structure */  g=get_globaldata();    set_current_individual ( ind );    ind->r_fitness = 0.0;  ind->hits = 1;    /* evaluate a tree of the individual.  this example uses tree 0,     but you can evaluate any tree as many times as you want. */  noteSequence = evaluate_tree ( ind->tr[0].data, 0 );  /*   *  find fitness of sequence   */  xmFile=fopen("pat.bin","w+");    fwriteXMPatternInfo(xmFile, noteSequence);  printf("Pattern Information:\n");  fseek(xmFile, 0, SEEK_SET);  fprintXMPatternInfo(xmFile, stdout, 4, 1);    fclose(xmFile);  /*  pretty_print_individual(ind, stdout);    response[0]='r';  response[1]=0x0;  while(response[0]=='r') {    printf("Eval %d: Wait until the tune finishes, then "	   "rate (1-100, r=replay): ", numEvals);    fflush(stdout);        system("play_pattern pat.bin");      gets(response);  }   sscanf(response,"%lg",&(ind->r_fitness));  printf("Read fitness value of %lg\n\n", ind->r_fitness);*/	   /*fl_activate_object(g->fd_Main->Main);*/    play_indiv(g->fd_Main->Play_Button, 0);    if (g->fd_Main->Exit_Button == fl_do_forms()) exit(0);  ind->r_fitness=fl_get_slider_value(g->fd_Main->Ind_Rate_Slider);/*  fl_deactivate_object(g->fd_Main->Main);  */  numEvals++;    /* compute the standardized and raw fitness. */  ind->s_fitness = 100 - ind->r_fitness;  ind->a_fitness = 1/(1+ind->s_fitness);    /* always leave this line in. */  ind->evald = EVAL_CACHE_VALID;  /* free the note sequence */  FREE(noteSequence);  }/* app_end_of_evaluation() * * this is called every generation after the entire popualation has * been evaluated.  "mpop" is a pointer to basically everything -- * every individual in every subpopulation, exchange topology for * multipop runs, etc.  see the documentation for useful things to * find inside it. * * run_stats[0].best[0]->ind is always a pointer to the best-of-run * individual.  if the "newbest" flag is 1, it indicates that this * is a NEW best-of-run individual. * * similarly, gen_stats[0].best[0]->ind points to the best-of-generation * individual. * * DO NOT MODIFY ANY OF THE STRUCTURES YOU ARE PASSED POINTERS TO. * * should return 1 to indicate that the termination criterion has been * met and the run should terminate, 0 otherwise.  you do not need to * test the generation number against the maximum -- this is done by * the kernel. * * you can dynamically modify breeding parameters and/or subpop exchange * topology by making the appropriate changes to the parameter database. * see the manual for more information.  after making changes, you need * to call: *    rebuild_breeding_table ( mpop ); * or *    rebuild_exchange_topology ( mpop ); * for your changes to take effect. */ int app_end_of_evaluation ( int gen, multipop *mpop, int newbest,                           popstats *gen_stats, popstats *run_stats ){  FILE *xmFile;  char *noteSequence;  char filename[100];  char copyCommand[100];    /* get note sequence for best of generation individual */  noteSequence = evaluate_tree ( gen_stats[0].best[0]->ind->tr[0].data, 0 );    /* write XM pattern for best individual in this generation */  sprintf(filename,"best-gen-%02d.bin",gen);  xmFile=fopen(filename,"w+");    fwriteXMPatternInfo(xmFile, noteSequence);  fclose(xmFile);  /* if this is the new best overall, copy it to the overall pattern file */  if(newbest) {    sprintf(copyCommand,"cp best-gen-%02d.bin best-run.bin",gen);    system(copyCommand);      }        return 0;}/* app_end_of_breeding() * * this is called every generation after the next population is created * (but before it is evaluated). * * DO NOT MODIFY ANY OF THE STRUCTURES YOU ARE PASSED POINTERS TO. */void app_end_of_breeding ( int gen, multipop *mpop ){     return;}/* app_create_output_streams() * * if you are going to create any custom output streams, do it here. * this is not documented yet, look at the source for the symbolic * regression problem for a guide. * * return 0 if init went OK and the run can proceed, or 1 to abort. */int app_create_output_streams(){     return 0;} /* app_initialize() * * this should perform any application-specific initialization the user * needs to do. * * return 0 if init went OK and the run can proceed, or 1 to abort. */int app_initialize ( int startfromcheckpoint, int argc, char **argv ){  globaldata *g;  /* get the global data structure */  g=get_globaldata();  fl_initialize(&argc, argv, "GPMusic", 0, 0);  g->fd_Main = create_form_Main();    /* fill-in form initialization code */  fl_set_object_label(g->fd_Main->Gen_Number, "0/5");  fl_set_object_label(g->fd_Main->Ind_Number, "0/16");    /* show the first form */  fl_show_form(g->fd_Main->Main,FL_PLACE_CENTER,FL_FULLBORDER,"GP Music");  /* deactivate the form *//*  fl_deactivate_object(g->fd_Main->Main);  */  return 0;}/* app_uninitialize() * * perform application cleanup (free memory, etc.) */void app_uninitialize ( void ){  return;}/* app_read_checkpoint() * * read state information from a checkpoint file.  it is passed a handle * for a file that has been opened for read in text mode.  it should * leave the file pointer at the end of the user information. */void app_read_checkpoint ( FILE *f ){     return;}/* app_write_checkpoint() * * write state information to a checkpoint file.  it is passed a handle * for a file that has been opened for write in text mode.  it should * leave the file pointer at the end of the user information. */void app_write_checkpoint ( FILE *f ){     return;}    

⌨️ 快捷键说明

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