📄 ga_intrinsics.c
字号:
/********************************************************************** ga_intrinsics.c ********************************************************************** ga_intrinics - Genetic algorithm routine intrinsics. Copyright ©2000-2004, Stewart Adcock <stewart@linux-domain.com> All rights reserved. The latest version of this program should be available at: http://gaul.sourceforge.net/ This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. Alternatively, if your project is incompatible with the GPL, I will probably agree to requests for permission to use the terms of any other license. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY WHATSOEVER. A full copy of the GNU General Public License should be in the file "COPYING" provided with this distribution; if not, see: http://www.gnu.org/ ********************************************************************** Synopsis: Wrappers around the routines for handling populations and performing GA operations using S-Lang scripting. Internally, and in the public C interface, pointers are used to identify the population and entity structures. However, in the scripting interface these pointers are unusable, so identifing integers are used instead. To do: More error checking. Needs some tidying. Add any missing wrappers. In particular, need facility for mating/mutating entities. Functions for defining heuristic search algorithm parameters. **********************************************************************/#include "gaul/ga_intrinsics.h"#if HAVE_SLANG==1/********************************************************************** ga_population_new_slang() synopsis: Allocates and initialises a new population structure, and assigns a new population id to it. parameters: return: unsigned int population id for this new structure. last updated: 13 Feb 2002 **********************************************************************/static int ga_population_new_slang( int *stable_size, int *num_chromosome, int *len_chromosome) { return ga_get_population_id(ga_population_new(*stable_size, *num_chromosome, *len_chromosome)); }/********************************************************************** ga_entity_seed_slang() synopsis: Fills a population structure with (hopefully) vaguely reasonable, starting genes. Most 'real' work is done in a user-specified function. parameters: none return: boolean success. last updated: 22/01/01 **********************************************************************/static int ga_entity_seed_slang(int *pop_id, int *id) { population *pop; /* Active population structure. */ pop = ga_get_population_from_id(*pop_id); return ga_entity_seed(pop, ga_get_entity_from_id(pop, *id)); }/********************************************************************** ga_entity_id_from_rank_slang() synopsis: Finds an entity's id from it's rank. parameters: return: last updated: 22/01/01 **********************************************************************/static int ga_entity_id_from_rank_slang(int *pop_id, int *rank) { return ga_get_entity_id_from_rank( ga_get_population_from_id(*pop_id), *rank ); }/********************************************************************** ga_entity_rank_from_id_slang() synopsis: Finds an entity's id from it's rank. parameters: return: last updated: 22/01/01 **********************************************************************/static int ga_entity_rank_from_id_slang(int *pop_id, int *id) { return ga_get_entity_rank_from_id( ga_get_population_from_id(*pop_id), *id ); }/********************************************************************** ga_population_seed_slang() synopsis: Fills a population structure with vaguely reasonable, random, starting genes. Most 'real' work is done in a user-specified function. parameters: none return: boolean success. last updated: 22/01/01 **********************************************************************/static int ga_population_seed_slang(int *pop_id) { return ga_population_seed(ga_get_population_from_id(*pop_id)); }/********************************************************************** ga_population_write_slang() synopsis: Writes entire population and it's genetic data to disk. parameters: return: last updated: 30 May 2002 **********************************************************************/static int ga_population_write_slang(int *pop_id, char *fname) { return ga_population_write(ga_get_population_from_id(*pop_id), fname); }/********************************************************************** ga_population_read_slang() synopsis: Reads entire population and it's genetic data back from disk. parameters: return: last updated: 22/01/01 **********************************************************************/static int ga_population_read_slang(char *fname) { return ga_get_population_id(ga_population_read(fname)); }/********************************************************************** ga_entity_write_slang() synopsis: Writes an entity to disk. parameters: return: last updated: 30 May 2002 **********************************************************************/static int ga_entity_write_slang(int *pop_id, int *entity_id, char *fname) { population *pop; pop = ga_get_population_from_id(*pop_id); return ga_entity_write(pop, ga_get_entity_from_id(pop, *entity_id), fname); }/********************************************************************** ga_entity_read_slang() synopsis: Reads an entity from disk. parameters: return: last updated: 30 May 2002 **********************************************************************/static int ga_entity_read_slang(int *pop_id, char *fname) { population *pop; pop = ga_get_population_from_id(*pop_id); return ga_get_entity_id(pop, ga_entity_read(pop, fname)); }/********************************************************************** ga_entity_kill_slang() synopsis: Marks an entity structure as unused (dereferences it). Any contents of entities data field are freed. parameters: return: last updated: 22/01/01 **********************************************************************/static int ga_entity_kill_slang(int *pop_id, int *id) { population *pop; /* Active population structure. */ pop = ga_get_population_from_id(*pop_id); return ga_entity_dereference_by_rank( pop, ga_get_entity_rank_from_id(pop, *id) ); }/********************************************************************** ga_entity_erase_slang() synopsis: Clears the entity's data. Equivalent to an optimised pair of ga_entity_kill_slang() and ga_entity_new_slang() calls. Chromosomes are guarenteed to be intact, but may be overwritten by user. Also decrements population size. parameters: return: last updated: 22/01/01 **********************************************************************/static int ga_entity_erase_slang(int *pop_id, int *id) { population *pop; /* Active population structure. */ pop = ga_get_population_from_id(*pop_id); ga_entity_blank(pop, ga_get_entity_from_id(pop, *id)); return TRUE; }/********************************************************************** ga_entity_new_slang() synopsis: Returns handle of a new, unused, entity structure. Also increments population size. parameters: return: last updated: 22/01/01 **********************************************************************/static int ga_entity_new_slang(int *pop_id) { population *pop; /* Active population structure. */ pop = ga_get_population_from_id(*pop_id); return ga_get_entity_id(pop, ga_get_free_entity(pop)); }/********************************************************************** ga_entity_clone_slang() synopsis: Returns to a new entity structure with the genes and structural data copied from the parent. Increments population size also. parameters: return: last updated: 22/01/01 **********************************************************************/static int ga_entity_clone_slang(int *pop_id, int *parent) { entity *child; /* Destination entity. */ population *pop = ga_get_population_from_id(*pop_id); /* Population. */ child = ga_get_free_entity(pop); ga_entity_copy(pop, child, ga_get_entity_from_id(pop, *parent)); return ga_get_entity_id(pop, child); }/********************************************************************** ga_entity_copy_chromosome_slang() synopsis: Copy genetic data between entity structures. parameters: return: last updated: 29 Nov 2001 **********************************************************************/static int ga_entity_copy_chromosome_slang(int *pop_id, int *parent, int *child, int *chromosome) { population *pop; /* Active population structure. */ pop = ga_get_population_from_id(*pop_id); return ga_entity_copy_chromosome(pop, ga_get_entity_from_id(pop, *child), ga_get_entity_from_id(pop, *parent), *chromosome); }/********************************************************************** ga_entity_copy_all_chromosomes_slang() synopsis: Copy genetic data between entity structures. parameters: return: last updated: 29 Nov 2001 **********************************************************************/static int ga_entity_copy_all_chromosomes_slang(int *pop_id, int *parent, int *child) { population *pop; /* Active population structure. */ pop = ga_get_population_from_id(*pop_id); return ga_entity_copy_all_chromosomes(pop, ga_get_entity_from_id(pop, *child), ga_get_entity_from_id(pop, *parent)); }/********************************************************************** ga_entity_migrate_slang() synopsis: Copy entity from one population into another - does not delete the original. The caller should do that, if required. FIXME: Need check to confirm that populations are compatible. parameters: return: last updated: 14/02/01 **********************************************************************/static int ga_entity_migrate_slang(int *srcpop_id, int *destpop_id, int *jacques) { population *srcpop; /* Original population. */ population *destpop; /* Destination population. */ entity *jack; /* Migrated entity. */ srcpop = ga_get_population_from_id(*srcpop_id); destpop = ga_get_population_from_id(*destpop_id); jack = ga_get_free_entity(destpop); ga_entity_copy(srcpop, jack, ga_get_entity_from_id(srcpop, *jacques)); return ga_get_entity_id(destpop, jack); }#if 0/********************************************************************** ga_singlepoint_crossover_chromosome() synopsis: `Mates' two chromosomes by single-point crossover. parameters: return: last updated: 18/10/00 **********************************************************************/boolean ga_singlepoint_crossover_chromosome(int *father, int *mother, int *son, int *daughter) { }/********************************************************************** ga_crossover_chromosome_singlepoints_slang() synopsis: `Mates' two genotypes by single-point crossover of each chromosome. parameters: return: last updated: 14/02/01 **********************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -