Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <string>
- #include <stdlib.h>
- using namespace std;
- class Individuo
- {
- public:
- int genome_size = 5;
- Individuo(): genoma(genome_size,0) {}
- vector<bool> genoma;
- vector<bool> SetGenoma(int x);
- double fitness();
- };
- void parseargs(int argc, char** argv,int& numofsample, double& fitnessMin);
- int main(int argc, char** argv)
- {
- int numberSamples=1000;
- double fitnessMin=0.9;
- int numberSurvivors = 0;
- double globalFitness;
- double fitnessLocal;
- parseargs(argc, argv, numberSamples, fitnessMin);
- vector<Individuo> poblation(numberSamples,Individuo());
- for (int count = 0; count < (numberSamples/2); ++count)
- {
- poblation[count].genoma = poblation[count].SetGenoma(1);
- }
- for (int count = 0; count < numberSamples; ++count)
- {
- fitnessLocal=poblation[count].fitness();
- if (fitnessLocal > fitnessMin)
- {
- numberSurvivors+= 1;
- globalFitness+=fitnessLocal;
- }
- }
- globalFitness/= numberSurvivors;
- cout << "Number of specimens with fitness > fitnessMin = " << numberSurvivors << ".\nThe global fitness of survivors is = " << globalFitness << "." << endl;
- return 0;
- }
- vector<bool> Individuo::SetGenoma(int x)
- {
- for (int count = 0; count < genome_size; ++count)
- {
- genoma[count]=x;
- }
- return genoma;
- }
- double Individuo::fitness()
- {
- double fitness = 0.0;
- int genome_size = 5;
- for (int count = 0; count < genome_size; ++count)
- {
- fitness+=genoma[count];
- }
- fitness/= double(genoma.size());
- return fitness;
- }
- void parseargs(int argc, char** argv,int& numofsample, double& fitnessMin)
- {
- bool pass=true;
- for(int i=1; i<argc; ++i)
- {
- if((argv[i]== string("-numofsample")) )
- {
- if (i!=argc-1) // it is not the last argument
- {
- numofsample=atoi(argv[++i]);
- if(numofsample<1) pass=false;
- }
- continue;
- }
- if((argv[i]== string("-fitnessMin")) )
- {
- if (i!=argc-1) // it is not the last argument
- {
- fitnessMin=atoi(argv[++i]);
- if(fitnessMin<0) pass=false;
- }
- continue;
- }
- cerr<<"\n ERROR:"<<argv[i]<< " is not a valid command line parameter\n"<<endl;
- exit(1);
- } // close for
- if(!pass)
- {
- string msg = "\n ERROR: Check argument values...\n";
- cout<<msg<<endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement