The following code simulates the evolution of flowers.
The number of flowers is 10, and 50 generations have been made.
By running the program, you will find that through continuous evolution, the overall adaptability of the population to the environment is gradually increasing (fitness value is reduced ).
Implementation Code:
Using system;
Using system. Collections. Generic;
Using system. text;
Namespace ga
{
Class Program
{
Static void main (string [] ARGs)
{
World = New World ();
World. INIT ();
For (INT I = 0; I <50; I ++)
{
World. Evolve ();
Console. writeline (I );
World. Show ();
}
}
}
Class world
{
Int kmaxflowers = 11;
Random RND = new random ();
Public int [] temperature;
Public int [] Water;
Public int [] sunlight;
Public int [] nutrient;
Public int [] beneficialinsect;
Public int [] harmfulinsect;
Public int currenttemperature;
Public int currentwater;
Public int currentsunlight;
Public int currentnutrient;
Public int currentbeneficialinsect;
Public int currentharmfulinsect;
Public world ()
{
Temperature = new int [kmaxflowers];
Water = new int [kmaxflowers];
Sunlight = new int [kmaxflowers];
Nutrient = new int [kmaxflowers];
Beneficialinsect = new int [kmaxflowers];
Harmfulinsect = new int [kmaxflowers];
}
/**////
/// Initialize the genetic structure of the first generation of flowers
///
Public void Init ()
{
For (INT I = 1; I <kmaxflowers; I ++)
{
Temperature [I] = RND. Next (1, 75 );
Water [I] = RND. Next (1, 75 );
Sunlight [I] = RND. Next (1, 75 );
Nutrient [I] = RND. Next (1, 75 );
Beneficialinsect [I] = RND. Next (1, 75 );
Harmfulinsect [I] = RND. Next (1, 75 );
}
Currenttemperature = RND. Next (1, 75 );
Currentwater = RND. Next (1, 75 );
Currentsunlight = RND. Next (1, 75 );
Currentnutrient = RND. Next (1, 75 );
Currentbeneficialinsect = RND. Next (1, 75 );
Currentharmfulinsect = RND. Next (1, 75 );
}
/**////
/// The larger the value indicates that the adaptability of flowers to the environment is poor, and the better the adaptability of novels to the environment
///
///
///
Private int fitness (INT flower)
{
Int thefitness = 0;
Thefitness = math. Abs (temperature [flower]-currenttemperature );
Thefitness = thefitness + math. Abs (water [flower]-currentwater );
Thefitness = thefitness + math. Abs (sunlight [flower]-
Currentsunlight );
Thefitness = thefitness + math. Abs (nutrient [flower]-
Currentnutrient );
Thefitness = thefitness + math. Abs (beneficialinsect [flower]-
Currentbeneficialinsect );
Thefitness = thefitness + math. Abs (harmfulinsect [flower]-
Currentharmfulinsect );
Return (thefitness );
}
/**////
/// Exclude the flowers with poor adaptability, so that the flowers with strong adaptability can breed and generate the next generation. At the same time, there is a certain probability variation.
///
Public void evolve ()
{
Int [] fittemperature = new int [kmaxflowers];
Int [] fitwater = new int [kmaxflowers];
Int [] fitsunlight = new int [kmaxflowers];
Int [] fitnutrient = new int [kmaxflowers];
Int [] fitbeneficialinsect = new int [kmaxflowers];
Int [] fitharmfulinsect = new int [kmaxflowers];
Int [] fitness = new int [kmaxflowers];
Int I;
Int leastfit = 0;
Int leastfitindex = 1;
For (I = 1; I <kmaxflowers; I ++)
If (fitness (I)> leastfit)
{
Leastfit = fitness (I );
Leastfitindex = I;
}
Temperature [leastfitindex] = temperature [RND. Next (1, 10)];
Water [leastfitindex] = water [RND. Next (1, 10)];
Sunlight [leastfitindex] = sunlight [RND. Next (1, 10)];
Nutrient [leastfitindex] = nutrient [RND. Next (1, 10)];
Beneficialinsect [leastfitindex] = beneficialinsect [RND. Next (1, 10)];
Harmfulinsect [leastfitindex] = harmfulinsect [RND. Next (1, 10)];
For (I = 1; I <kmaxflowers; I ++)
{
Fittemperature [I] = temperature [RND. Next (1, 10)];
Fitwater [I] = water [RND. Next (1, 10)];
Fitsunlight [I] = sunlight [RND. Next (1, 10)];
Fitnutrient [I] = nutrient [RND. Next (1, 10)];
Fitbeneficialinsect [I] = beneficialinsect [RND. Next (1, 10)];
Fitharmfulinsect [I] = harmfulinsect [RND. Next (1, 10)];
}
For (I = 1; I <kmaxflowers; I ++)
{
Temperature [I] = fittemperature [I];
Water [I] = fitwater [I];
Sunlight [I] = fitsunlight [I];
Nutrient [I] = fitnutrient [I];
Beneficialinsect [I] = fitbeneficialinsect [I];
Harmfulinsect [I] = fitharmfulinsect [I];
}
For (I = 1; I <kmaxflowers; I ++)
{
If (RND. Next (1,100) = 1)
Temperature [I] = RND. Next (1, 75 );
If (RND. Next (1,100) = 1)
Water [I] = RND. Next (1, 75 );
If (RND. Next (1,100) = 1)
Sunlight [I] = RND. Next (1, 75 );
If (RND. Next (1,100) = 1)
Nutrient [I] = RND. Next (1, 75 );
If (RND. Next (1,100) = 1)
Beneficialinsect [I] = RND. Next (1, 75 );
If (RND. Next (1,100) = 1)
Harmfulinsect [I] = RND. Next (1, 75 );
}
}
/**////
/// Display the adaptability of individual to the environment in the population, and the sum of adaptability of all individuals to the environment.
///
Public void show ()
{
Int sum = 0;
For (INT I = 1; I <kmaxflowers; I ++)
{
Int fitness = fitness (I );
Sum + = fitness;
Console. writeline ("no." + I + "'s fitness is" + fitness );
}
Console. writeline ("Fitness sum is" + sum );
}
}
}