📄 ga_chromo.c
字号:
char *ga_chromosome_char_to_string( const population *pop, const entity *joe, char *text, size_t *textlen) { int i; /* Loop over chromosome, alleles. */ int k=0; /* Pointer into 'text'. */ if (!pop) die("Null pointer to population structure passed."); if (!joe) die("Null pointer to entity structure passed."); if (*textlen < pop->len_chromosomes * pop->num_chromosomes + 1) { *textlen = pop->len_chromosomes * pop->num_chromosomes + 1; text = s_realloc(text, sizeof(char) * *textlen); } if (!joe->chromosome) { text[0] = '\0'; } else { for(i=0; i<pop->num_chromosomes; i++) { memcpy(&(text[k]), joe->chromosome[0], pop->len_chromosomes * sizeof(char)); k += pop->len_chromosomes; } text[k] = '\0'; } return text; }/********************************************************************** ga_chromosome_bitstring_allocate() synopsis: Allocate the chromosomes for an entity. Initial contents are garbage (there is no need to zero them). parameters: return: last updated: 30/06/01 **********************************************************************/boolean ga_chromosome_bitstring_allocate(population *pop, entity *embryo) { int i; /* Loop variable over all chromosomes */ if (!pop) die("Null pointer to population structure passed."); if (!embryo) die("Null pointer to entity structure passed."); if (embryo->chromosome!=NULL) die("This entity already contains chromosomes."); embryo->chromosome = s_malloc(pop->num_chromosomes*sizeof(byte *)); for (i=0; i<pop->num_chromosomes; i++) embryo->chromosome[i] = ga_bit_new(pop->len_chromosomes); return TRUE; }/********************************************************************** ga_chromosome_bitstring_deallocate() synopsis: Deallocate the chromosomes for an entity. parameters: return: last updated: 30/06/01 **********************************************************************/void ga_chromosome_bitstring_deallocate(population *pop, entity *corpse) { int i; /* Loop variable over all chromosomes */ if (!pop) die("Null pointer to population structure passed."); if (!corpse) die("Null pointer to entity structure passed."); if (corpse->chromosome==NULL) die("This entity already contains no chromosomes."); for (i=0; i<pop->num_chromosomes; i++) ga_bit_free(corpse->chromosome[i]); s_free(corpse->chromosome); corpse->chromosome=NULL; return; }/********************************************************************** ga_chromosome_bitstring_replicate() synopsis: Duplicate a chromosome exactly. parameters: return: last updated: 30/06/01 **********************************************************************/void ga_chromosome_bitstring_replicate( const population *pop, entity *parent, entity *child, const int chromosomeid ) { if (!pop) die("Null pointer to population structure passed."); if (!parent || !child) die("Null pointer to entity structure passed."); if (!parent->chromosome || !child->chromosome) die("Entity has no chromsomes."); ga_bit_clone( child->chromosome[chromosomeid], parent->chromosome[chromosomeid], pop->len_chromosomes ); return; }/********************************************************************** ga_chromosome_bitstring_to_bytes() synopsis: Convert to contiguous form. parameters: return: last updated: 30/06/01 **********************************************************************/unsigned int ga_chromosome_bitstring_to_bytes(const population *pop, entity *joe, byte **bytes, unsigned int *max_bytes) { int num_bytes; /* Actual size of genes. */ int i; /* Loop variable over all chromosomes */ if (!pop) die("Null pointer to population structure passed."); if (!joe) die("Null pointer to entity structure passed."); num_bytes = ga_bit_sizeof(pop->len_chromosomes) * pop->num_chromosomes; if (num_bytes>*max_bytes) { *max_bytes = num_bytes; *bytes = s_realloc(*bytes, *max_bytes*sizeof(byte)); /* sizeof(byte) should always be 1 */ } if (!joe->chromosome) { *bytes = (byte *)0; return 0; } for(i=0; i<pop->num_chromosomes; i++) { ga_bit_copy( *bytes, joe->chromosome[i], i*pop->len_chromosomes, 0, pop->len_chromosomes ); } return num_bytes; }/********************************************************************** ga_chromosome_bitstring_from_bytes() synopsis: Convert from contiguous form. In this case, a trivial process. parameters: return: last updated: 13/06/01 **********************************************************************/void ga_chromosome_bitstring_from_bytes( const population *pop, entity *joe, byte *bytes ) { int i; /* Loop variable over all chromosomes */ if (!pop) die("Null pointer to population structure passed."); if (!joe) die("Null pointer to entity structure passed."); if (!joe->chromosome) die("Entity has no chromsomes."); for(i=0; i<pop->num_chromosomes; i++) { ga_bit_copy( joe->chromosome[i], bytes, 0, i*pop->len_chromosomes, pop->len_chromosomes ); } return; }/********************************************************************** ga_chromosome_bitstring_to_string() synopsis: Convert to human readable form. parameters: return: last updated: 19 Aug 2002 **********************************************************************/char *ga_chromosome_bitstring_to_string( const population *pop, const entity *joe, char *text, size_t *textlen) { int i, j; /* Loop over chromosome, alleles. */ int k=0; /* Pointer into 'text'. */ if (!pop) die("Null pointer to population structure passed."); if (!joe) die("Null pointer to entity structure passed."); if (!text || *textlen < pop->len_chromosomes * pop->num_chromosomes + 1) { *textlen = pop->len_chromosomes * pop->num_chromosomes + 1; text = s_realloc(text, sizeof(char) * *textlen); } if (!joe->chromosome) { text[0] = '\0'; } else { for(i=0; i<pop->num_chromosomes; i++) { for(j=0; j<pop->len_chromosomes; j++) { text[k++] = ga_bit_get(joe->chromosome[i],j)?'1':'0'; } } text[k] = '\0'; } return text; }/********************************************************************** ga_chromosome_list_allocate() synopsis: Allocate the chromosomes for an entity. Initial contents are set to null. parameters: return: last updated: 05 Oct 2004 **********************************************************************/boolean ga_chromosome_list_allocate(population *pop, entity *embryo) { int i; /* Loop variable over all chromosomes */ if (!pop) die("Null pointer to population structure passed."); if (!embryo) die("Null pointer to entity structure passed."); if (embryo->chromosome!=NULL) die("This entity already contains chromosomes."); embryo->chromosome = s_malloc(pop->num_chromosomes*sizeof(DLList *)); for (i=0; i<pop->num_chromosomes; i++) embryo->chromosome[i] = NULL; return TRUE; }/********************************************************************** ga_chromosome_list_deallocate() synopsis: Deallocate the chromosomes for an entity. parameters: return: last updated: 05 Oct 2004 **********************************************************************/void ga_chromosome_list_deallocate(population *pop, entity *corpse) { int i; /* Loop variable over all chromosomes */ if (!pop) die("Null pointer to population structure passed."); if (!corpse) die("Null pointer to entity structure passed."); if (corpse->chromosome==NULL) die("This entity already contains no chromosomes."); for (i=0; i<pop->num_chromosomes; i++) dlink_free_all(corpse->chromosome[i]); s_free(corpse->chromosome); corpse->chromosome=NULL; return; }/********************************************************************** ga_chromosome_list_replicate() synopsis: Duplicate a chromosome exactly. Currently unimplemented at present. parameters: return: last updated: 05 Oct 2004 **********************************************************************/void ga_chromosome_list_replicate( const population *pop, entity *parent, entity *child, const int chromosomeid ) { if (!pop) die("Null pointer to population structure passed."); if (!parent || !child) die("Null pointer to entity structure passed."); if (!parent->chromosome || !child->chromosome) die("Entity has no chromsomes."); child->chromosome[chromosomeid] = dlink_clone( parent->chromosome[chromosomeid] ); return; }/********************************************************************** ga_chromosome_list_to_bytes() synopsis: Convert to contiguous form. Currently unimplemented at present. FIXME: Need a user-defined callback to implement this according to contents of the list. parameters: return: last updated: 05 Oct 2004 **********************************************************************/unsigned int ga_chromosome_list_to_bytes(const population *pop, entity *joe, byte **bytes, unsigned int *max_bytes) { int num_bytes=0; /* Actual size of genes. */ if (!pop) die("Null pointer to population structure passed."); if (!joe) die("Null pointer to entity structure passed."); die("ga_chromosome_list_to_bytes() is not implemented."); /* Avoid compiler warnings. */ **bytes = 0; *max_bytes = 0; return num_bytes; }/********************************************************************** ga_chromosome_list_from_bytes() synopsis: Convert from contiguous form. In this case, a trivial process. Currently unimplemented at present. FIXME: Need a user-defined callback to implement this according to contents of the list. parameters: return: last updated: 05 Oct 2004 **********************************************************************/void ga_chromosome_list_from_bytes( const population *pop, entity *joe, byte *bytes ) { if (!pop) die("Null pointer to population structure passed."); if (!joe) die("Null pointer to entity structure passed."); if (!joe->chromosome) die("Entity has no chromsomes."); die("ga_chromosome_list_from_bytes() is not implemented."); /* Avoid compiler warning. */ *bytes = 0; return; }/********************************************************************** ga_chromosome_list_to_string() synopsis: Convert to human readable form. Currently unimplemented at present. FIXME: Need a user-defined callback to implement this according to contents of the list. parameters: return: last updated: 05 Oct 2004 **********************************************************************/char *ga_chromosome_list_to_string( const population *pop, const entity *joe, char *text, size_t *textlen) { if (!pop) die("Null pointer to population structure passed."); if (!joe) die("Null pointer to entity structure passed."); if (!text || *textlen < 14) { *textlen = 14; text = s_realloc(text, sizeof(char) * *textlen); } strncpy(text, "<unavailable>", 14); return text; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -