遗传算法之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
1 #include <gaul.h> 2 3 4 boolean struggle_score(population *pop, entity *entity) 5 { 6 int k; /* Loop variable over all alleles. */ 7 8 entity->fitness = 0.0; 9 10 /* Loop over alleles in chromosome. */ 11 for (k = 0; k < pop->len_chromosomes; k++) 12 { 13 if ( ((char *)entity->chromosome[0])[k] == target_text[k]) 14 entity->fitness+=1.0; 15 /* 16 * Component to smooth function, which helps a lot in this case: 17 * Comment it out if you like. 18 */ 19 entity->fitness += (127.0-fabs(((char *)entity->chromosome[0])[k] 20 -target_text[k]))/50.0; 21 } 22 23 return TRUE; 24 } 25 26 int main(int argc, char **argv) 27 { 28 population *pop=NULL; /* The population of solutions. */ 29 30 random_seed(2003); /* Random seed requires any integer parameter. */ 31 pop = ga_genesis_char( 32 250, /* const int population_size */ 33 1, /* const int num_chromo */ 34 strlen(target_text), /* const int len_chromo */ 35 NULL, /* GAgeneration_hook generation_hook */ 36 NULL, /* GAiteration_hook iteration_hook */ 37 NULL, /* GAdata_destructor data_destructor */ 38 NULL, /* GAdata_ref_incrementor data_ref_incrementor */ 39 struggle_score, /* GAevaluate evaluate */ 40 ga_seed_printable_random, /* GAseed seed */ 41 NULL, /* GAadapt adapt */ 42 ga_select_one_sus, /* GAselect_one select_one */ 43 ga_select_two_sus, /* GAselect_two select_two */ 44 ga_mutate_printable_singlepoint_drift, /* GAmutate mutate */ 45 ga_crossover_char_allele_mixing, /* GAcrossover crossover */ 46 NULL, /* GAreplace replace */ 47 NULL /* void * userdata */ 48 ); 49 50 ga_population_set_parameters( 51 pop, /* population *pop */ 52 GA_SCHEME_DARWIN, /* const ga_class_type class */ 53 GA_ELITISM_PARENTS_DIE, /* const ga_elitism_type elitism */ 54 0.9, /* double crossover */ 55 0.2, /* double mutation */ 56 0.0 /* double migration */ 57 ); 58 ga_evolution( 59 pop, /* population *pop */ 60 500 /* const int max_generations */ 61 ); 62 printf( "The final solution found was:\n", i); 63 printf( "%s\n", ga_chromosome_char_to_staticstring(pop, ga_get_entity_from_rank(pop,0))); 64 printf( "Fitness score = %f\n", ga_get_entity_from_rank(pop,0)->fitness); 65 66 ga_extinction(pop); /* Deallocates all memory associated with 67 * the population and it's entities. 68 */ 69 70 exit(EXIT_SUCCESS); 71 }
好了,当你的程序中需要用到GAUL时,你就可以参照上面这个小实例进行哦。这块的知识后面再进行补充。。
浙公网安备 33010602011771号