Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <math.h>
- using namespace std;
- struct Chromosome
- {
- float fitness = 100000 ;
- vector <float> Genes ; // a0 , a1 , a2 , ...... , an = Polynomial Degree + 1
- };
- struct Point
- {
- float x ;
- float y ;
- };
- int const numIndividuals = 1000 ;
- int const numGenerations = 10 ;
- int numPoints ;
- int numCases ;
- int PolyDeg ;
- float Total_Fitness ;
- vector <Point> Points ;
- vector <Chromosome> Generation ;
- vector <Chromosome> nxtGeneration ;
- void read()
- {
- freopen("input.txt", "rt", stdin);
- cin >> numCases ;
- for (int c = 0 ; c<numCases ; c++)
- {
- cin >> numPoints >> PolyDeg ;
- Point Temp ;
- for (int j = 0 ; j<numPoints ; j++)
- {
- cin >> Temp.x >> Temp.y ;
- Points.push_back(Temp);
- }
- }
- }
- void Intial_Population()
- {
- Generation.resize(numIndividuals);
- for (int i = 0 ; i<numIndividuals ; i++)
- {
- Generation[i].Genes.resize(PolyDeg+1) ;
- for ( int j = 0 ; j <= PolyDeg ; j++)
- {
- float r = -10 + static_cast <float> (rand()) /( static_cast <float> (RAND_MAX/(10-(-10)))); //Generate Random Float Number between -10 to 10
- Generation[i].Genes[j] = r ;
- }
- }
- }
- void CalcFitness ()
- {
- float error = 0 ;
- float sum = 0 ;
- float sum2 = 0 ;
- Total_Fitness = 0 ;
- for ( int z = 0 ; z < numIndividuals ; z++)
- {
- for ( int i = 0 ; i < numPoints ; i++)
- {
- float yCalc = 0 ;
- float x = Points[i].x ;
- for ( int j = 0 ; j <= PolyDeg ; j++)
- {
- yCalc = (float)(yCalc + (Generation[z].Genes[j])* (pow(x,j))) ;
- }
- sum = (float)((yCalc) - Points[i].y) ; // Sum = Ycalc - Yactual
- sum2 = (pow(sum,2)); //Sum2 = sum^2
- }
- float val = 1 / (float) numPoints;
- error = (val*sum2) ; // Error = 1/n MSE
- Generation[z].fitness = error ;
- Total_Fitness += Generation[z].fitness ;
- }
- }
- int Roulette ()
- {
- float r = static_cast <float> (rand()) / (static_cast <float> (RAND_MAX/Total_Fitness)); //Generate Random Number Between 0 and Total Case Fitness
- int sum = 0 ;
- for ( int i = 0 ; i < numIndividuals ; ++i)
- {
- sum += Generation[i].fitness;
- if ( sum > r ) // The First Chromosome to affect the fitness will be returned
- {
- return i ;
- }
- }
- return -1 ;
- }
- void Mutate (Chromosome & Chr)
- {
- float Upper = 10.0 , Lower = -10.0 ,dUpper , dLower ;
- for (int i = 0; i <Chr.Genes.size(); ++i)
- {
- int m = rand() % 1000;
- if (m < 1)
- {
- dUpper = Upper - Chr.Genes[i] ;
- dLower = Chr.Genes[i] - Lower ;
- int r = rand() % 10;
- if (r < 5 )
- {
- float r2 = static_cast <float> (rand()) / (static_cast <float> (RAND_MAX/dUpper));
- Chr.Genes[i] += r2 ;
- }
- else
- {
- float r3 = static_cast <float> (rand()) / (static_cast <float> (RAND_MAX/dLower));
- Chr.Genes[i] -= r3 ;
- }
- }
- }
- }
- void CrossOver (int Cindex1 , int Cindex2)
- {
- Chromosome Chr1 = Generation[Cindex1]; //Takes First Chromosome From the population
- Chromosome Chr2 = Generation[Cindex2]; //Takes Second Chromosome From the population
- int r = rand()%10 ; //Generate Random Number from 0 to 10
- if ( r < 6 )
- {
- int r = rand()%PolyDeg; //Generate Random Number from 0 to Number of Items
- r++;
- for ( int i = 0 ; i < r ; i++)
- swap(Chr1.Genes[i],Chr2.Genes[i]);
- Mutate(Chr1); //Makes Mutation To First Chromosome
- Mutate(Chr2); //Makes Mutation To Second Chromosome
- }
- nxtGeneration.push_back(Chr1); //Store The Chromosome in The offspring
- nxtGeneration.push_back(Chr2); //Store The Chromosome in The offspring
- }
- void Reproduce()
- {
- while (nxtGeneration.size() < numIndividuals) //Until offspring size is equal to number of individuals
- {
- int chromosome_index1 = Roulette(); //choose random chromosome
- int chromosome_index2 = Roulette(); //choose random chromosome
- CrossOver(chromosome_index1, chromosome_index2); //Makes crossover between them
- }
- }
- int Get_Fit()
- {
- int best_fitness = 1000000, index = -1;
- for (int i = 0; i < numIndividuals; ++i)
- {
- if (best_fitness > Generation[i].fitness)
- {
- best_fitness = Generation[i].fitness;
- index = i;
- }
- }
- return index;
- }
- void Print ()
- {
- int BestFit = Get_Fit();
- float Result = Generation[BestFit].fitness;
- for ( int i = 0 ; i<= PolyDeg ; i++)
- cout << Generation[BestFit].Genes[i] << ", ";
- cout << endl << "It's Fitness : " << Result << endl << endl ;
- }
- void Compute ()
- {
- read(); // First Read The Chromosome from the file
- for (int c = 0 ; c < numCases ; c++)
- {
- nxtGeneration.clear(); //To Empty the offspring
- Intial_Population(); // Generates The Initial population
- int Gen = 0 ;
- while ( Gen < numGenerations) //Continue Until I Reach Maximum Generation Number
- {
- if ( Gen != 0 )
- {
- Generation = nxtGeneration ; //Replace the old generation chromosomes with the offspring ones
- nxtGeneration.clear(); //And Empty the offspring
- }
- CalcFitness(); // Calculate fitness of each chromosome
- Gen++ ;
- if ( Gen < numGenerations)
- {
- Reproduce(); // Makes New Generation
- }
- }
- Print(); // Print This Case Output
- }
- }
- int main()
- {
- Compute();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement