Advertisement
Guest User

Untitled

a guest
Oct 7th, 2015
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.67 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <stdlib.h>
  5.  
  6. using namespace std;
  7.  
  8. class Individuo
  9. {
  10.  
  11. public:
  12.  
  13.     int genome_size = 5;
  14.  
  15.  
  16.     Individuo(): genoma(genome_size,0) {}
  17.  
  18.     vector<bool> genoma;
  19.  
  20.     vector<bool> SetGenoma(int x);
  21.  
  22.  
  23.     double fitness();
  24.  
  25.  
  26. };
  27.  
  28. void parseargs(int argc, char** argv,int& numofsample, double& fitnessMin);
  29.  
  30. int main(int argc, char** argv)
  31. {
  32.     int numberSamples=1000;
  33.     double fitnessMin=0.9;
  34.     int numberSurvivors = 0;
  35.     double globalFitness;
  36.     double fitnessLocal;
  37.  
  38.     parseargs(argc, argv, numberSamples, fitnessMin);
  39.  
  40.     vector<Individuo> poblation(numberSamples,Individuo());
  41.  
  42.  
  43.     for (int count = 0; count < (numberSamples/2); ++count)
  44.     {
  45.         poblation[count].genoma = poblation[count].SetGenoma(1);
  46.  
  47.     }
  48.  
  49.  
  50.     for (int count = 0; count < numberSamples; ++count)
  51.     {
  52.  
  53.         fitnessLocal=poblation[count].fitness();
  54.  
  55.         if (fitnessLocal > fitnessMin)
  56.         {
  57.  
  58.  
  59.             numberSurvivors+= 1;
  60.             globalFitness+=fitnessLocal;
  61.  
  62.         }
  63.  
  64.  
  65.  
  66.     }
  67.  
  68.     globalFitness/= numberSurvivors;
  69.  
  70.     cout << "Number of specimens with fitness > fitnessMin = " << numberSurvivors << ".\nThe global fitness of survivors is = " << globalFitness << "." << endl;
  71.     return 0;
  72. }
  73.  
  74.  
  75. vector<bool> Individuo::SetGenoma(int x)
  76. {
  77.  
  78.  
  79.     for (int count = 0; count < genome_size; ++count)
  80.     {
  81.         genoma[count]=x;
  82.  
  83.     }
  84.  
  85.     return genoma;
  86.  
  87. }
  88. double Individuo::fitness()
  89. {
  90.  
  91.     double fitness = 0.0;
  92.     int genome_size = 5;
  93.  
  94.  
  95.     for (int count = 0; count < genome_size; ++count)
  96.     {
  97.         fitness+=genoma[count];
  98.  
  99.     }
  100.  
  101.     fitness/= double(genoma.size());
  102.  
  103.     return fitness;
  104. }
  105.  
  106. void parseargs(int argc, char** argv,int& numofsample, double& fitnessMin)
  107. {
  108.  
  109.     bool pass=true;
  110.  
  111.     for(int i=1; i<argc; ++i)
  112.     {
  113.  
  114.         if((argv[i]== string("-numofsample")) )
  115.         {
  116.  
  117.             if (i!=argc-1) // it is not the last argument
  118.             {
  119.                 numofsample=atoi(argv[++i]);
  120.                 if(numofsample<1) pass=false;
  121.             }
  122.             continue;
  123.         }
  124.         if((argv[i]== string("-fitnessMin")) )
  125.         {
  126.  
  127.             if (i!=argc-1) // it is not the last argument
  128.             {
  129.                 fitnessMin=atoi(argv[++i]);
  130.                 if(fitnessMin<0) pass=false;
  131.             }
  132.             continue;
  133.         }
  134.  
  135.         cerr<<"\n ERROR:"<<argv[i]<< " is not a valid command line parameter\n"<<endl;
  136.         exit(1);
  137.  
  138.     } // close for
  139.  
  140.  
  141.     if(!pass)
  142.     {
  143.         string msg = "\n ERROR: Check argument values...\n";
  144.         cout<<msg<<endl;
  145.  
  146.     }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement