遗传算法之GAUL

时间:2023-03-08 21:59:30
遗传算法之GAUL

遗传算法之GAUL简介

  • 简介

       GAUL(遗传算法工具库的简称)

  GAUL is an open source programming library, released under the GNU General Public License.  It is designed to assist in the development of code that requires evolutionary algorithms.

  GAUL官网地址为:http://gaul.sourceforge.net/

目前发行版本为:devel-0.1849, examples-0.1849

  GAUL是一个灵活的代码库,它旨在帮助我们在开发项目过程中,使用遗传或进化算法。它提供的数据结构和处理函数以及数据操作依赖于一系列串行和并行的遗传算法。附加的随机算法用与遗传算法进行比较,通过s-lang接口可以实现很多功能。


  • 下载、编译

  下载时有两个包,一个是开发版,一个是演示例子版,这里我们使用开发版本 gaul-devel-0.1849-0

  编译执行以下命令:

        ./configure ; make

  当你没有安装s-lang时需要执行:

        ./configure --enable-slang=no ; make

  编译成功后则可以使用其工具库了,在tests目录下有实例,可以直接通过命令./test_xxx对其进行运行


  • 示例程序

以下为GAUL官方提供的最简单的一个示例程序:

struggle.c

 #include <gaul.h>

 boolean struggle_score(population *pop, entity *entity)
{
int k; /* Loop variable over all alleles. */ entity->fitness = 0.0; /* Loop over alleles in chromosome. */
for (k = ; k < pop->len_chromosomes; k++)
{
if ( ((char *)entity->chromosome[])[k] == target_text[k])
entity->fitness+=1.0;
/*
* Component to smooth function, which helps a lot in this case:
* Comment it out if you like.
*/
entity->fitness += (127.0-fabs(((char *)entity->chromosome[])[k]
-target_text[k]))/50.0;
} return TRUE;
} int main(int argc, char **argv)
{
population *pop=NULL; /* The population of solutions. */ random_seed(); /* Random seed requires any integer parameter. */
pop = ga_genesis_char(
, /* const int population_size */
, /* const int num_chromo */
strlen(target_text), /* const int len_chromo */
NULL, /* GAgeneration_hook generation_hook */
NULL, /* GAiteration_hook iteration_hook */
NULL, /* GAdata_destructor data_destructor */
NULL, /* GAdata_ref_incrementor data_ref_incrementor */
struggle_score, /* GAevaluate evaluate */
ga_seed_printable_random, /* GAseed seed */
NULL, /* GAadapt adapt */
ga_select_one_sus, /* GAselect_one select_one */
ga_select_two_sus, /* GAselect_two select_two */
ga_mutate_printable_singlepoint_drift, /* GAmutate mutate */
ga_crossover_char_allele_mixing, /* GAcrossover crossover */
NULL, /* GAreplace replace */
NULL /* void * userdata */
); ga_population_set_parameters(
pop, /* population *pop */
GA_SCHEME_DARWIN, /* const ga_class_type class */
GA_ELITISM_PARENTS_DIE, /* const ga_elitism_type elitism */
0.9, /* double crossover */
0.2, /* double mutation */
0.0 /* double migration */
);
ga_evolution(
pop, /* population *pop */
/* const int max_generations */
);
printf( "The final solution found was:\n", i);
printf( "%s\n", ga_chromosome_char_to_staticstring(pop, ga_get_entity_from_rank(pop,)));
printf( "Fitness score = %f\n", ga_get_entity_from_rank(pop,)->fitness); ga_extinction(pop); /* Deallocates all memory associated with
* the population and it's entities.
*/ exit(EXIT_SUCCESS);
}

  好了,当你的程序中需要用到GAUL时,你就可以参照上面这个小实例进行哦。这块的知识后面再进行补充。。