Advertisement
ASabry

Untitled

Mar 13th, 2020
437
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.39 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cstdlib>
  4. #include <ctime>
  5. #include <algorithm>
  6. #include <cmath>
  7. using namespace std;
  8.  
  9. //Global variables
  10. vector<float> yActual;
  11. vector<float> x;
  12. int size;
  13. //Prototypes
  14.  
  15. float RandomFloat(float a, float b) {
  16. float random = ((float) rand()) / (float) RAND_MAX;
  17. float diff = b - a;
  18. float r = random * diff;
  19. return a + r;
  20. }
  21. class Chromosome
  22. {
  23.  
  24. public:
  25. vector<float> bits; //co-efficients
  26. vector<float> yPredicted;
  27. float MSE;
  28. float getMSE(){return MSE;}
  29. Chromosome()
  30. {
  31. for(int i = 0 ; i < x.size(); i++)
  32. yPredicted.push_back(0);
  33. for(int i = 0 ; i < size; i++)
  34. bits.push_back(0);
  35.  
  36. randomFill();
  37. //calculateYPredicted();
  38. //calculateMSE();
  39. }
  40. void setBits(vector<int> v)
  41. {
  42. for(int i = 0 ; i < size; i++)
  43. bits[i] = v[i];
  44. }
  45. vector<float> getBits(){return bits;}
  46. void fill()
  47. {
  48. //bits.clear();
  49. cout << "Enter Bits Of Chromosome" << endl;
  50. for(int i = 0; i < size; i++)
  51. {
  52. float num;
  53. cin >> num;
  54. bits[i] = num;
  55. }
  56.  
  57. calculateYPredicted();
  58. calculateMSE();
  59. }
  60. void randomFill()
  61. {
  62. for(int i = 0; i < size; i++)
  63. {
  64. float num = RandomFloat(-10.0,10.0);
  65. /*float num = rand()%10;
  66. if(rand()%2 == 1)
  67. num -= 2 * num;
  68. */
  69. bits[i] = num;
  70. }
  71.  
  72. calculateYPredicted();
  73. calculateMSE();
  74. }
  75. void reRandomFill()
  76. {
  77. for(int i = 0; i < size; i++)
  78. {
  79. float num = RandomFloat(-10.0,10.0);
  80. bits[i] = num;
  81. }
  82. calculateYPredicted();
  83. calculateMSE();
  84. }
  85. //Fill vector of y predicted using the equation below
  86. void calculateYPredicted()
  87. {
  88. //F(X) = y = a0 + a1 x^1 + a2 x^2 + a3 x^3
  89. for(int k = 0 ; k < yPredicted.size(); k++)
  90. {
  91. float y =0;
  92. for(int i = 0 ; i < bits.size() ; i++)
  93. {
  94. float degreedX = x[k];
  95. degreedX = pow(degreedX,i);
  96. //// x^1 x^2 x^3
  97. y += ( bits[i] * degreedX );
  98. }
  99. yPredicted[k] = y;
  100. }
  101. }
  102. //Calculating Mean Square Error using mse equation
  103. void calculateMSE()
  104. {
  105. float error = 0;
  106. for(int i = 0 ; i < yPredicted.size(); i++)
  107. error += pow( (yPredicted[i] - yActual[i]) ,2);
  108. error = error/yPredicted.size();
  109. MSE = error;
  110. }
  111. void printBits()
  112. {
  113. for(int i = 0; i < bits.size(); i++)
  114. cout << bits[i] << " " ;
  115. }
  116. void printFullChromosome()
  117. {
  118. for(int i = 0; i < bits.size(); i++)
  119. cout << bits[i] << " ";
  120. cout << endl << "Mean Square Error : " << MSE << endl;
  121.  
  122. }
  123.  
  124. Chromosome setChromosome(Chromosome c)
  125. {
  126. for(int i = 0 ; i < bits.size(); i++)
  127. (*this).bits[i] = c.bits[i];
  128. for(int i = 0 ; i < bits.size(); i++)
  129. (*this).yPredicted[i] = c.yPredicted[i];
  130.  
  131. (*this).MSE = c.MSE;
  132. }
  133. /*
  134. Try filling chromosome yourself
  135. void trying()
  136. {
  137. fill();
  138. printBits();
  139. cout << endl << "y predicted " << endl;
  140. for(int i = 0 ; i < yPredicted.size();i++ )
  141. cout << yPredicted[i] << " ";
  142. cout << endl;
  143. cout << endl << "y actual " << endl;
  144. for(int i = 0 ; i < yActual.size(); i++)
  145. cout << yActual[i] << " " ;
  146. cout << endl;
  147. }
  148. */
  149. };
  150.  
  151. vector<float> createCumulativeTable( vector<Chromosome> population)
  152. {
  153. vector<float> CumMSE;
  154. float temp;
  155.  
  156. CumMSE.push_back(population[0].MSE);
  157.  
  158. temp = CumMSE[0];
  159.  
  160. for(int i=1; i < population.size(); i++)
  161. {
  162. CumMSE.push_back(population[i].MSE + temp);
  163.  
  164. temp = CumMSE[i];
  165.  
  166. }
  167. return CumMSE;
  168. }
  169. Chromosome mutation(Chromosome ch);
  170. Chromosome crossOver(Chromosome ch1,Chromosome ch2)
  171. {
  172. Chromosome child1,child2;
  173. //vector<int> ChoosenVector;
  174.  
  175. int splitSize = rand()%ch1.bits.size();
  176.  
  177.  
  178. while(splitSize == 0 || splitSize == ch1.bits.size() - 1)
  179. splitSize = rand()%ch1.bits.size();
  180.  
  181. /*for(int i = 0; i < Choosen.size(); i++)
  182. {
  183. ChoosenVector.push_back(Choosen[i]);
  184. }*/
  185.  
  186. cout << "splitSize : " << splitSize << endl << endl;
  187.  
  188. //cout << "LOL" << endl;
  189.  
  190.  
  191. for(int k = 0; k < splitSize; k++)
  192. {
  193. child1.bits.push_back(ch1.bits[k]) ;
  194. child2.bits.push_back(ch2.bits[k]) ;
  195. }
  196.  
  197. for(int k = splitSize; k <size; k++)
  198. {
  199. child1.bits.push_back(ch2.bits[k]);
  200. child2.bits.push_back(ch1.bits[k]);
  201. }
  202. // cout << "-------------------------" << endl;
  203. // cout << "r : " << r <<endl;
  204. // cout << "r2 : " << r2 << endl;
  205.  
  206.  
  207. // cout << "~~~~~~~~~~~~~~~~~" << endl;
  208. // cout << "Child 1 : ";
  209. // for(int q = 0; q < ch1.size; q++)
  210. // {
  211. // cout << ch1.bits[q] << ",";
  212. // }
  213. // cout << endl;
  214. //
  215. // cout << "Child 2 : ";
  216. // for(int q = 0; q < ch2.size; q++)
  217. // {
  218. // cout << ch2.bits[q] << ",";
  219. // }
  220. // cout << endl;
  221. // cout << "~~~~~~~~~~~~~~~~~" << endl;
  222. // cout << endl;
  223.  
  224. child1 = mutation(child1);
  225. child2 = mutation(child2);
  226.  
  227. child1.calculateYPredicted();
  228. child1.calculateMSE();
  229.  
  230. child2.calculateYPredicted();
  231. child2.calculateMSE();
  232. //
  233. if(child1.MSE > child2.MSE)
  234. {
  235. return child1;
  236. }
  237. else
  238. {
  239. return child2;
  240.  
  241. }
  242.  
  243.  
  244.  
  245.  
  246.  
  247. // cout << "~~~~~~~~~~~~~~~~~" << endl;
  248. // cout << "Child 1 after mutation : ";
  249. // for(int q = 0; q < ch1.size; q++)
  250. // {
  251. // cout << ch1.bits[q] << ",";
  252. // }
  253. // cout << endl;
  254. //
  255. // cout << "Child 2 after mutation : ";
  256. // for(int q = 0; q < ch2.size; q++)
  257. // {
  258. // cout << ch2.bits[q] << ",";
  259. // }
  260. // cout << endl;
  261. // cout << "~~~~~~~~~~~~~~~~~" << endl;
  262. // cout << endl;
  263.  
  264.  
  265. }
  266. //---------------------------------------------------------//
  267. Chromosome mutation(Chromosome ch)
  268. {
  269.  
  270. int r_int;
  271. float r_float,UB = 10.0,LB = -10.0,DU,DL;
  272.  
  273. r_float = (rand()%10 + 1.0)/10.0;
  274. r_int = rand()%size;
  275.  
  276. cout << "r_float : " << r_float << endl;
  277. cout << "r_int : " << r_int << endl;
  278. cout << "--------------" << endl << endl;;
  279.  
  280. DU = UB - ch.bits[r_int];
  281. DL = ch.bits[r_int] - LB;
  282.  
  283. cout << "DU : " << DU << endl;
  284. cout << "DL : " << DL << endl;
  285.  
  286. if(r_float <= 0.5)
  287. {
  288. double r_int1,r_int2;
  289. r_int1 = RandomFloat(0.0,DU);
  290.  
  291. cout << "r_int1 : " << r_int1 << endl << endl;;
  292.  
  293. cout << "Bit : " << ch.bits[r_int] << endl;
  294. ch.bits[r_int] += r_int1;
  295.  
  296. cout << "Bit : " << ch.bits[r_int] << endl;
  297.  
  298. cout << "---------------22222" << endl;
  299.  
  300. return ch;
  301.  
  302. }
  303. else
  304. {
  305.  
  306. double r_int1,r_int2;
  307. r_int1 = RandomFloat(0.0,DL);
  308.  
  309. cout << "r_int1 : " << r_int1 << endl << endl;
  310.  
  311. cout << "Bit : " << ch.bits[r_int] << endl;
  312. ch.bits[r_int] -= r_int1;
  313. cout << "Bit : " << ch.bits[r_int] << endl;
  314.  
  315. cout << "---------------1111111" << endl;
  316.  
  317. return ch;
  318. }
  319.  
  320.  
  321.  
  322. }
  323. //-------------------------------------------------------//
  324. void printTable(vector<Chromosome> population, vector<float>cumTable)
  325. {
  326. for(int i = 0; i < population.size(); i++)
  327. {
  328. cout << "[" ;
  329. population[i].printBits();
  330. cout << "] " << population[i].MSE << "\t|" << "\t" << cumTable[i] << endl;
  331. }
  332. cout << endl;
  333.  
  334. //cout << "Random number: " << r << "\nExist in index " << index << endl;
  335. }
  336. void Compute(int popSize, int numOfIterations )
  337. {
  338. vector<Chromosome> population;
  339. Chromosome bestSoFar;
  340. for(int i = 0; i < popSize; i++)
  341. {
  342. Chromosome c;
  343. //Keep our best
  344. if(i == 0)
  345. bestSoFar=c;
  346. else if(bestSoFar.getMSE() < c.getMSE() )
  347. bestSoFar.setChromosome(c);
  348. population.push_back(c);
  349. }
  350. /*
  351. for(int i = 0 ; i < population.size(); i++)
  352. cout << population[i].MSE << endl;
  353. */
  354. vector<float> cumTable = createCumulativeTable(population);
  355. //printTable(population,cumTable);
  356.  
  357. int counter = 0;
  358. while(counter < numOfIterations)
  359. {
  360. for(int i = 0 ; i < popSize; i++)
  361. {
  362. Chromosome c1,c2;
  363. int index1,index2;
  364.  
  365. float r = RandomFloat(0,cumTable.back());
  366.  
  367. for(int k = cumTable.size()-1 ; k > 0; k--)
  368. if( r < cumTable[k] && r >= cumTable[k-1])
  369. {
  370. index1 = k;
  371. break;
  372. }
  373. c1.setChromosome(population[index1]);
  374.  
  375. float r2 = RandomFloat(0,cumTable.back());
  376. for(int k = cumTable.size()-1 ; k > 0; k--)
  377. if( r2 < cumTable[k] && r2 >= cumTable[k-1])
  378. {
  379. index2 = k;
  380. if( index1 == index2)
  381. {
  382. r2 = RandomFloat(0,cumTable.back());
  383. k = cumTable.size()-1;
  384. }
  385. else
  386. break;
  387. }
  388. c2.setChromosome(population[index2]);
  389. Chromosome newChild;
  390.  
  391. if(size >2)
  392. newChild = crossOver(c1 , c2);
  393.  
  394. //Send c1, c2, newChild to cross over which will make 2 children. Choose best of them and return it to mutation then replace it with it's index
  395. //in population vector.
  396. /*
  397. sort( indices.begin(), indices.end() );
  398. indices.erase( unique( indices.begin(), indices.end() ), indices.end() );
  399.  
  400. double r = (rand()%10 + 1.0)/10.0;
  401.  
  402. if(r >= 0.4 && r <= 0.7)
  403. crossOver(population,indices,maxWeight,bestSoFar,weights,benefits);
  404.  
  405.  
  406. */
  407. if(bestSoFar.MSE > newChild.MSE)
  408. bestSoFar.setChromosome(newChild);
  409. population[i].setChromosome(newChild);
  410. }
  411. counter++;
  412. }
  413. cout << endl << "And best solution the algorithm reached : ";
  414. bestSoFar.printFullChromosome();
  415. cout << endl;
  416. }
  417.  
  418. int main()
  419. {
  420. srand(time(0));
  421. cout << "Enter Number of points" << endl;
  422. int numOfPoints;
  423. cin >> numOfPoints;
  424.  
  425. cout << "Enter Requested degree" << endl;
  426. int requestedDegree;
  427. cin >> requestedDegree;
  428. size = requestedDegree+1;
  429.  
  430. for(int i = 0 ; i < numOfPoints; i++)
  431. {
  432. float tempX,tempY;
  433. cout << "Enter (X" << i << ",Y" << i << ")" << endl;
  434. cin >> tempX >> tempY;
  435. x.push_back(tempX);
  436. yActual.push_back(tempY);
  437. }
  438.  
  439. cout << "Enter population size " ;
  440. int popSize;
  441. cin >> popSize;
  442.  
  443. cout << "Enter number of iterations " ;
  444. int numOfIterations;
  445. cin >> numOfIterations;
  446.  
  447. Compute(popSize,numOfIterations);
  448. /*
  449. Chromosome c;
  450. c.printFullChromosome();
  451. */
  452. return 0;
  453. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement