String Evolver, My First Genetic Algorithm

When reading Evolutionary Computation for Modeling and Optimization[1], I found following problem in section 1.2.3:

A string evolver is an evolutionary algorithm that tries to match a reference string from a population of random strings. The underlying character set of the string evolver is the alphabet from which the strings are drawn.

The solution given in the context is:

  1. Start with a reference string and a population of random strings.
  2. The fitness of a string is the number of positions in which it has the same character as the reference string.
  3. To evolve the population, split it into small random groups called tournaments.
  4. Copy the most fit string(break ties by picking at random among the most fit strings) over the least fit string in each tournament.
  5. Then change one randomly chosen character in each copy(mutation).
  6. Repeat until an exact match with the reference string is obtained.

When trying this solution, I noticed that it won't converge even after a long time. Therefore, I modified the crossover and mutation strategy.A subset of population(strings) will be selected based on fitness in a tournament, and the bottom-ranked 50% of each tournament will be deleted. New population is formed by crossing the remaining high 20 individuals in a tournament, while the high-ranked parents are kept unchanged[2]. Finally, a random selected string in a tournament will be mutated.


My codes are:

#include 
#include 
#include 

#define POP 20 
#define TNMT 4	    // Total number of tournaments
#define MAXSTR 80
char strPop[POP][MAXSTR]; // population of strings
char* REF="Hello,world!"; // Reference string
char* mostFit[TNMT]; 	  // the most fit string of each tournament
char* leastFit[TNMT]; 	// the least fit string of each 

tournament

char GenRandChar()
{
	return (char)(32+rand()%96);
}

char * GenRandStr(char* str, unsigned int len)
{
	int i;
	for(i=0;i

And the output is:

---------------- Generation 0 ----------------
Tournament 0:
	F~AKK[b;tV]	Fitness: 0
	\\agA_q>vr	Fitness: 0
	OGG&Gy*(|yj	Fitness: 0
	6s[rEA8|45/	Fitness: 0
	W.e[:bYXlCz	Fitness: 0

Tournament 1:
	6n?y9o??VN#	Fitness: 0
	8mmZ~f:Dfn<	Fitness: 0
	i-nV!P`{oQ	Fitness: 0
	8%p%)\3-j;(	Fitness: 0
	92S2}&jU"f	Fitness: 0

Tournament 2:
	38IrA~lrHq|	Fitness: 0
	RF51Vo7V9]v	Fitness: 0
	Z(tUXN)Kt_Pm	Fitness: 0
	i>Z+F0@6/'L	Fitness: 0

Hello,world!	Reference
Z(tUXvr	Fitness: 0
	OGG&Gy*(|yj	Fitness: 0
	\\agA_q>5]	Fitness: 0
	F~AKK[b;tCr	Fitness: 0

Tournament 1:
	6n?y9o??VN#	Fitness: 0
	kmmZ~f:Dfn<	Fitness: 0
	i-nV!P`{oQ	Fitness: 0
	8mm%9o??VN#	Fitness: 0
	6n?2~f:Dfn<	Fitness: 0

Tournament 2:
	Z(tUXvr	Fitness: 0
	OGG&Gy*(|yj	Fitness: 0
	\\agA_q>v]	Fitness: 0
	F~AeK[b;tVr	Fitness: 0

Tournament 1:
	Pn?y9o??VN#	Fitness: 0
	kmmZ~f:Dfn<	Fitness: 0
	i-nV!P`{oQ	Fitness: 0
	kmmZ~f??VN#	Fitness: 0
	6n?y9o:Dfn<	Fitness: 0

Tournament 2:
	RFoDXvr	Fitness: 0
	OGG&Gy*(|yj	Fitness: 0
	\\KK[b;tVx	Fitness: 0
	F~Aag~_q>vr	Fitness: 0

Tournament 1:
	Pn?y9o??VN#	Fitness: 0
	kmmZ~f:Dfn<@	Fitness: 0
	i-nV!P`{oQ	Fitness: 0
	km?y9o??VN#	Fitness: 0
	PnmZ~f:Dfn<	Fitness: 0

Tournament 2:
	RFoDXvr	Fitness: 0
	OGG&Gy*(|yj	Fitness: 0
	\\KK[b;tVx	Fitness: 0
	F~AagA_q>vr	Fitness: 0

Tournament 1:
	Pn?y9o??VN#	Fitness: 0
	kmmZ~f:Dfn<@	Fitness: 0
	i-nV!P`{oQ	Fitness: 0
	kmmZ~o??VN#	Fitness: 0
	Pnsy9f:Dfn<@	Fitness: 0

Tournament 2:
	RFoDX


-----------------------------------------------------------------

References:

[1]: Daniel Ashlock, Evolutionary Computation for Modeling and Optimization, Springer, 2005.

[2]: FORREST, S., WEIMER, W., NGUYEN, T., AND GOUES, C. L., A Genetic Programming Approach to Automated Software Repair, In GECCO (July 2009).

你可能感兴趣的:(C/C++,Evolutionary,Computation,string,algorithm,generation,random,reference,character)