📄 ga_mutate.c
字号:
*/ for (chromo=0; chromo<pop->num_chromosomes; chromo++) { for (point=0; point<pop->len_chromosomes; point++) { if (random_boolean_prob(pop->allele_mutation_prob)) { ((char *)son->chromosome[chromo])[point] += dir;/* Don't need these because char's **should** wrap safely. if (((char *)son->chromosome[chromo])[point]>CHAR_MAX) ((char *)son->chromosome[chromo])[point]=CHAR_MIN; if (((char *)son->chromosome[chromo])[point]<CHAR_MIN) ((char *)son->chromosome[chromo])[point]=CHAR_MAX;*/ } } } return; }/********************************************************************** ga_mutate_printable_singlepoint_drift() synopsis: Cause a single mutation event in which a single allele is cycled. parameters: return: last updated: 16/06/01 **********************************************************************/void ga_mutate_printable_singlepoint_drift( population *pop, entity *father, entity *son ) { int i; /* Loop variable over all chromosomes */ int chromo; /* Index of chromosome to mutate */ int point; /* Index of allele to mutate */ int dir=random_boolean()?-1:1; /* The direction of drift. *//* Checks */ if (!father || !son) die("Null pointer to entity structure passed");/* Select mutation locus. */ chromo = (int) random_int(pop->num_chromosomes); point = (int) random_int(pop->len_chromosomes);/* * Copy unchanged data. */ for (i=0; i<pop->num_chromosomes; i++) { memcpy(son->chromosome[i], father->chromosome[i], pop->len_chromosomes*sizeof(char)); if (i!=chromo) { ga_copy_data(pop, son, father, i); } else { ga_copy_data(pop, son, NULL, i); } }/* * Mutate by tweaking a single allele. */ ((char *)son->chromosome[chromo])[point] += dir; if (((char *)son->chromosome[chromo])[point]>'~') ((char *)son->chromosome[chromo])[point]=' '; if (((char *)son->chromosome[chromo])[point]<' ') ((char *)son->chromosome[chromo])[point]='~'; return; }/********************************************************************** ga_mutate_printable_singlepoint_randomize() synopsis: Cause a single mutation event in which a single allele is randomized. parameters: return: last updated: 16/06/01 **********************************************************************/void ga_mutate_printable_singlepoint_randomize( population *pop, entity *father, entity *son ) { int i; /* Loop variable over all chromosomes */ int chromo; /* Index of chromosome to mutate */ int point; /* Index of allele to mutate *//* Checks */ if (!father || !son) die("Null pointer to entity structure passed");/* Select mutation locus. */ chromo = (int) random_int(pop->num_chromosomes); point = (int) random_int(pop->len_chromosomes);/* Copy unchanging data. */ for (i=0; i<pop->num_chromosomes; i++) { memcpy(son->chromosome[i], father->chromosome[i], pop->len_chromosomes*sizeof(char)); if (i!=chromo) { ga_copy_data(pop, son, father, i); } else { ga_copy_data(pop, son, NULL, i); } } ((char *)son->chromosome[chromo])[point] = (int) random_int('~'-' ')+' '; return; }/********************************************************************** ga_mutate_printable_multipoint() synopsis: Cause a number of mutation events. This is equivalent to the more common 'bit-drift' mutation. parameters: return: last updated: 16 Feb 2005 **********************************************************************/void ga_mutate_printable_multipoint(population *pop, entity *father, entity *son) { int i; /* Loop variable over all chromosomes */ int chromo; /* Index of chromosome to mutate */ int point; /* Index of allele to mutate */ int dir=random_boolean()?-1:1; /* The direction of drift. *//* Checks */ if (!father || !son) die("Null pointer to entity structure passed");/* Copy chromosomes of parent to offspring. */ for (i=0; i<pop->num_chromosomes; i++) { memcpy(son->chromosome[i], father->chromosome[i], pop->len_chromosomes*sizeof(char)); }/* * Mutate by tweaking alleles. */ for (chromo=0; chromo<pop->num_chromosomes; chromo++) { for (point=0; point<pop->len_chromosomes; point++) { if (random_boolean_prob(pop->allele_mutation_prob)) { ((char *)son->chromosome[chromo])[point] += dir; if (((char *)son->chromosome[chromo])[point]>'~') ((char *)son->chromosome[chromo])[point]=' '; if (((char *)son->chromosome[chromo])[point]<' ') ((char *)son->chromosome[chromo])[point]='~'; } } } return; }/********************************************************************** ga_mutate_printable_allpoint() synopsis: Cause a number of mutation events. Each allele has equal probability of being incremented, decremented, or remaining the same. parameters: return: last updated: 10 Sep 2003 **********************************************************************/void ga_mutate_printable_allpoint(population *pop, entity *father, entity *son) { int i; /* Loop variable over all chromosomes */ int chromo; /* Index of chromosome to mutate */ int point; /* Index of allele to mutate *//* Checks */ if (!father || !son) die("Null pointer to entity structure passed");/* Copy chromosomes of parent to offspring. */ for (i=0; i<pop->num_chromosomes; i++) { memcpy(son->chromosome[i], father->chromosome[i], pop->len_chromosomes*sizeof(char)); }/* * Mutate by incrementing or decrementing alleles. */ for (chromo=0; chromo<pop->num_chromosomes; chromo++) { for (point=0; point<pop->len_chromosomes; point++) { switch (random_int(3)) { case (1): (((char *)son->chromosome[chromo])[point])++; if (((char *)son->chromosome[chromo])[point]>'~') ((char *)son->chromosome[chromo])[point]=' '; break; case (2): (((char *)son->chromosome[chromo])[point])--; if (((char *)son->chromosome[chromo])[point]<' ') ((char *)son->chromosome[chromo])[point]='~'; break; default: /* Do nothing. */ break; } } } return; }/********************************************************************** ga_mutate_bitstring_singlepoint() synopsis: Cause a single mutation event in which a single allele is flipped. parameters: return: last updated: 30/06/01 **********************************************************************/void ga_mutate_bitstring_singlepoint( population *pop, entity *father, entity *son ) { int i; /* Loop variable over all chromosomes */ int chromo; /* Index of chromosome to mutate */ int point; /* Index of allele to mutate *//* Checks */ if (!father || !son) die("Null pointer to entity structure passed");/* Select mutation locus. */ chromo = (int) random_int(pop->num_chromosomes); point = (int) random_int(pop->len_chromosomes);/* Copy unchanging data. */ for (i=0; i<pop->num_chromosomes; i++) { ga_bit_clone(son->chromosome[i], father->chromosome[i], pop->len_chromosomes); if (i!=chromo) { ga_copy_data(pop, son, father, i); } else { ga_copy_data(pop, son, NULL, i); } }/* The singlepoint mutation. */ ga_bit_invert(son->chromosome[chromo],point); return; }/********************************************************************** ga_mutate_bitstring_multipoint() synopsis: Cause a number of mutation events. parameters: return: last updated: 16 Feb 2005 **********************************************************************/void ga_mutate_bitstring_multipoint(population *pop, entity *father, entity *son) { int i; /* Loop variable over all chromosomes */ int chromo; /* Index of chromosome to mutate */ int point; /* Index of allele to mutate *//* Checks */ if (!father || !son) die("Null pointer to entity structure passed");/* Copy chromosomes of parent to offspring. */ for (i=0; i<pop->num_chromosomes; i++) { ga_bit_clone(son->chromosome[i], father->chromosome[i], pop->len_chromosomes); }/* * Mutate by flipping random bits. */ for (chromo=0; chromo<pop->num_chromosomes; chromo++) { for (point=0; point<pop->len_chromosomes; point++) { if (random_boolean_prob(pop->allele_mutation_prob)) { ga_bit_invert(son->chromosome[chromo],point); } } } return; }/********************************************************************** ga_mutate_double_singlepoint_drift() synopsis: Cause a single mutation event in which a single allele is adjusted. (Unit Gaussian distribution.) parameters: return: last updated: 17 Feb 2005 **********************************************************************/void ga_mutate_double_singlepoint_drift( population *pop, entity *father, entity *son ) { int i; /* Loop variable over all chromosomes */ int chromo; /* Index of chromosome to mutate */ int point; /* Index of allele to mutate */ double amount=random_unit_gaussian(); /* The amount of drift. (FIXME: variance should be user-definable) *//* Checks */ if (!father || !son) die("Null pointer to entity structure passed");/* Select mutation locus. */ chromo = (int) random_int(pop->num_chromosomes); point = (int) random_int(pop->len_chromosomes);/* * Copy unchanged data. */ for (i=0; i<pop->num_chromosomes; i++) { memcpy(son->chromosome[i], father->chromosome[i], pop->len_chromosomes*sizeof(double)); if (i!=chromo) { ga_copy_data(pop, son, father, i); } else { ga_copy_data(pop, son, NULL, i); } }/* * Mutate by tweaking a single allele. */ ((double *)son->chromosome[chromo])[point] += amount; if (((double *)son->chromosome[chromo])[point] > pop->allele_max_double) ((double *)son->chromosome[chromo])[point] -= (pop->allele_max_double-pop->allele_min_double); if (((double *)son->chromosome[chromo])[point] < pop->allele_min_double) ((double *)son->chromosome[chromo])[point] += (pop->allele_max_double-pop->allele_min_double); return; }/********************************************************************** ga_mutate_double_singlepoint_randomize() synopsis: Cause a single mutation event in which a single allele is randomized. (Unit Gaussian distribution.) parameters: return: last updated: 19 Apr 2002 **********************************************************************/void ga_mutate_double_singlepoint_randomize( population *pop, entity *father, entity *son ) { int i; /* Loop variable over all chromosomes */ int chromo; /* Index of chromosome to mutate */ int point; /* Index of allele to mutate *//* Checks */ if (!father || !son) die("Null pointer to entity structure passed");/* Select mutation locus. */ chromo = (int) random_int(pop->num_chromosomes); point = (int) random_int(pop->len_chromosomes);/* Copy unchanging data. */ for (i=0; i<pop->num_chromosomes; i++) { memcpy(son->chromosome[i], father->chromosome[i], pop->len_chromosomes*sizeof(double)); if (i!=chromo) { ga_copy_data(pop, son, father, i); } else { ga_copy_data(pop, son, NULL, i); } } ((double *)son->chromosome[chromo])[point] = random_unit_gaussian(); return; }/********************************************************************** ga_mutate_double_multipoint() synopsis: Cause a number of mutation events. This is equivalent to the more common 'bit-drift' mutation. (Unit Gaussian distribution.) parameters: return: last updated: 17 Feb 2005 **********************************************************************/void ga_mutate_double_multipoint(population *pop, entity *father, entity *son) { int i; /* Loop variable over all chromosomes */ int chromo; /* Index of chromosome to mutate */ int point; /* Index of allele to mutate *//* Checks */ if (!father || !son) die("Null pointer to entity structure passed");/* Copy chromosomes of parent to offspring. */ for (i=0; i<pop->num_chromosomes; i++) { memcpy(son->chromosome[i], father->chromosome[i], pop->len_chromosomes*sizeof(double)); }/* * Mutate by tweaking alleles. */ for (chromo=0; chromo<pop->num_chromosomes; chromo++) { for (point=0; point<pop->len_chromosomes; point++) { if (random_boolean_prob(pop->allele_mutation_prob)) { ((double *)son->chromosome[chromo])[point] += random_unit_gaussian(); if (((double *)son->chromosome[chromo])[point] > pop->allele_max_double) ((double *)son->chromosome[chromo])[point] -= (pop->allele_max_double-pop->allele_min_double); if (((double *)son->chromosome[chromo])[point] < pop->allele_min_double) ((double *)son->chromosome[chromo])[point] += (pop->allele_max_double-pop->allele_min_double); } } } return; }/********************************************************************** ga_mutate_double_allpoint() synopsis: Cause a number of mutation events. Each allele's value will drift. (Unit Gaussian distribution.) parameters: return: last updated: 17 Feb 2005 **********************************************************************/void ga_mutate_double_allpoint(population *pop, entity *father, entity *son) { int i; /* Loop variable over all chromosomes */ int chromo; /* Index of chromosome to mutate */ int point; /* Index of allele to mutate *//* Checks */ if (!father || !son) die("Null pointer to entity structure passed");/* Copy chromosomes of parent to offspring. */ for (i=0; i<pop->num_chromosomes; i++) { memcpy(son->chromosome[i], father->chromosome[i], pop->len_chromosomes*sizeof(double)); }/* * Mutate by adjusting all alleles. */ for (chromo=0; chromo<pop->num_chromosomes; chromo++) { for (point=0; point<pop->len_chromosomes; point++) { (((double *)son->chromosome[chromo])[point]) += random_unit_gaussian(); if (((double *)son->chromosome[chromo])[point] > pop->allele_max_double) ((double *)son->chromosome[chromo])[point] -= (pop->allele_max_double-pop->allele_min_double); if (((double *)son->chromosome[chromo])[point] < pop->allele_min_double) ((double *)son->chromosome[chromo])[point] += (pop->allele_max_double-pop->allele_min_double); } } return; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -