Advertisement
ASabry

Untitled

Feb 25th, 2020
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.99 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cstdlib>
  4. #include <ctime>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9. //Prototypes
  10. void printTable(vector<int> benefits,vector<int> table,int r,int index);
  11.  
  12. class Chromosome
  13. {
  14.  
  15. public:
  16. vector<int> bits;
  17. int benefit;
  18. int weight;
  19. int size;
  20.  
  21. Chromosome(){}
  22. Chromosome(int length)
  23. {
  24. size = length;
  25. }
  26. int getBenefit()
  27. {
  28. return benefit;
  29. }
  30. void setBenefit(int b)
  31. {
  32. benefit = b;
  33. }
  34. void fill()
  35. {
  36. cout << "Enter Bits Of Chromosome" << endl;
  37. for(int i = 0; i < size; i++)
  38. {
  39. int num;
  40. cin >> num;
  41. bits.push_back(num);
  42. }
  43. }
  44. void randomFill()
  45. {
  46. for(int i = 0; i < size; i++)
  47. bits.push_back(rand()%2);
  48.  
  49. }
  50. void reRandomFill()
  51. {
  52. for(int i = 0; i < size; i++)
  53. bits[i]=(rand()%2);
  54. }
  55. void printBits()
  56. {
  57. for(int i = 0; i < bits.size(); i++)
  58. cout << bits[i];
  59. }
  60. void printFullChromosome()
  61. {
  62. for(int i = 0; i < bits.size(); i++)
  63. cout << bits[i];
  64. cout << endl;
  65. cout << "Weight " << weight << endl;
  66. cout << "Benefit " << benefit << endl;
  67. }
  68. bool checkIfValid(vector<int> weights, vector<int> benefits, int maxWeight)
  69. {
  70. int totalWeight =0, totalBenefit =0;
  71.  
  72. for(int i = 0 ; i < size ; i++)
  73. {
  74. if(bits[i] == 1)
  75. {
  76. totalWeight+= weights[i];
  77. totalBenefit+= benefits[i];
  78. }
  79. }
  80. if(maxWeight < totalWeight)
  81. return false;
  82. //
  83. weight = totalWeight;
  84. benefit = totalBenefit;
  85. return true;
  86. }
  87. };
  88. //--------------------------------------------------------------------//
  89. vector<int> createCumulativeTable(vector<int> benefits, vector<Chromosome> population)
  90. {
  91. vector<int> CumBenefits;
  92. int temp;
  93.  
  94. CumBenefits.push_back(population[0].getBenefit());
  95.  
  96. temp = CumBenefits[0];
  97.  
  98. for(int i=1; i < population.size(); i++)
  99. {
  100. CumBenefits.push_back(population[i].getBenefit() + temp);
  101.  
  102. temp = CumBenefits[i];
  103.  
  104. }
  105. cout<<"return"<<endl;
  106. return CumBenefits;
  107. }
  108. //--------------------------------------------------------------------//
  109. void printTable(vector<Chromosome> population,vector<int> cumTable)
  110. {
  111. system("CLS");
  112. for(int i = 0; i < population.size(); i++)
  113. {
  114. cout << "[" ;
  115. population[i].printBits();
  116. cout << "] " << population[i].getBenefit() << "\t|" << "\t" << cumTable[i] << endl;
  117. }
  118. cout << endl;
  119.  
  120. //cout << "Random number: " << r << "\nExist in index " << index << endl;
  121. }
  122. //--------------------------------------------------------------------//
  123. void mutation (vector <Chromosome> ch,vector<int> weights, vector<int> benefits)
  124. {
  125. double pm= 0.001,r;
  126.  
  127. for (int i=0; i<weights.size()&&i<benefits.size(); i++)
  128. {
  129. for (int j=0; j<ch.size(); j++)
  130. {
  131. r = (rand()%10 + 1.0)/10.0;
  132.  
  133. if (r<=pm)
  134. ch[i].bits[j]=!ch[i].bits[j];
  135. }
  136. }
  137.  
  138. }
  139. //--------------------------------------------------------------------//
  140. void crossOver(vector<Chromosome> ch,vector<int> Choosen,int maxWeight,
  141. Chromosome bestSoFar,vector<int> weights, vector<int> benefits)
  142. {
  143. Chromosome ch1,ch2;
  144. vector<int> ChoosenVector;
  145. int S = ch[0].size;
  146.  
  147. int splitSize = rand()%ch[0].size;
  148.  
  149. while(splitSize == 0)
  150. splitSize = rand()%ch[0].size;
  151.  
  152. for(int i = 0; i < Choosen.size(); i++)
  153. {
  154. ChoosenVector.push_back(Choosen[i]);
  155. }
  156.  
  157. cout << "splitSize : " << splitSize << endl;
  158.  
  159. for(int i = ChoosenVector.size()/2; i > 0 && !ChoosenVector.empty(); i--)
  160. {
  161. ch1.bits.clear();
  162. ch2.bits.clear();
  163.  
  164. int r = rand()%ChoosenVector.size();
  165. int r2 = rand()%ChoosenVector.size();
  166.  
  167. while(r2 == r)
  168. r2 = rand()%ChoosenVector.size();
  169.  
  170.  
  171. for(int k = 0; k < splitSize; k++)
  172. {
  173. ch1.bits.push_back(ch[r].bits[k]) ;
  174. ch2.bits.push_back(ch[r2].bits[k]) ;
  175. }
  176.  
  177. for(int k = splitSize; k < ch[0].size; k++)
  178. {
  179. ch1.bits.push_back(ch[r2].bits[k]);
  180. ch2.bits.push_back(ch[r].bits[k]);
  181. }
  182.  
  183. ch1.size = ch[0].size;
  184.  
  185. cout << "-------------------------" << endl;
  186. cout << "r : " << r <<endl;
  187. cout << "r2 : " << r2 << endl;
  188.  
  189. ch[r].printFullChromosome();
  190. ch[r2].printFullChromosome();
  191.  
  192. if(ch1.checkIfValid(weights,benefits,maxWeight))
  193. {
  194.  
  195. if( bestSoFar.getBenefit() < ch1.getBenefit())
  196. {
  197. ch[r].bits.clear();
  198.  
  199. for(int k = 0; k < S; k++)
  200. ch[r].bits.push_back(ch1.bits[k]);
  201.  
  202. ch[r].weight = ch1.weight;
  203. ch[r].benefit = ch1.benefit;
  204.  
  205. bestSoFar.setBenefit(ch1.getBenefit());
  206.  
  207. }
  208.  
  209. }
  210.  
  211. if(ch2.checkIfValid(weights,benefits,maxWeight))
  212. {
  213. if( bestSoFar.getBenefit() < ch2.getBenefit())
  214. {
  215. ch[r2].bits.clear();
  216.  
  217. for(int k = 0; k < S; k++)
  218. ch[r2].bits.push_back(ch2.bits[k]);
  219.  
  220. ch[r2].weight = ch2.weight;
  221. ch[r2].benefit = ch2.benefit;
  222.  
  223.  
  224. bestSoFar.setBenefit(ch2.getBenefit());
  225.  
  226. }
  227. }
  228.  
  229. ch[r].printFullChromosome();
  230. ch[r2].printFullChromosome();
  231.  
  232. // for(int q = 0; q < ChoosenVector.size(); q++)
  233. // {
  234. // cout << ChoosenVector[q] << ",";
  235. // }
  236.  
  237. // cout << endl;
  238. // cout << "r : "<< r <<endl;
  239. // cout << "r2 : "<< r2 <<endl;
  240. // cout << "size : "<< ChoosenVector.size() <<endl;
  241.  
  242. // int q1 = ChoosenVector.begin() + r ;
  243. // int q2 = ChoosenVector.begin() + r2 ;
  244. // cout << ChoosenVector.begin() + r <<endl;
  245. // cout << ChoosenVector.begin() + r2 <<endl;
  246. // ChoosenVector.erase(ChoosenVector.begin() + r);
  247. // ChoosenVector.erase(ChoosenVector.begin() + r2);
  248. //
  249. // for(int q = 0; q < ChoosenVector.size(); q++)
  250. // {
  251. // cout << ChoosenVector[q] << "," ;
  252. // }
  253. // cout << endl;
  254.  
  255. }
  256.  
  257.  
  258. }
  259. //--------------------------------------------------------------------//
  260. Chromosome compute(int popSize, int chSize,
  261. vector<int> weights, vector<int> benefits, int maxWeight,
  262. int numOfIterations, Chromosome bestSoln)
  263. {
  264. //First we generate random solutions of size m
  265. vector<Chromosome> population;
  266. Chromosome bestSoFar(chSize);
  267. for(int i = 0; i < popSize; i++)
  268. {
  269. Chromosome c(chSize);
  270. c.randomFill();
  271. //Making sure of no invalid solution
  272. while( !c.checkIfValid(weights,benefits,maxWeight) )
  273. c.reRandomFill();
  274. //Keep our best
  275. if(i == 0)
  276. bestSoFar=c;
  277. else if(bestSoFar.getBenefit() < c.getBenefit() )
  278. bestSoFar.setBenefit(c.getBenefit());
  279. //
  280. population.push_back(c);
  281. }
  282. vector<int> cumTable = createCumulativeTable(benefits,population);
  283.  
  284. //Using Elitism Algorithm, we choose only some fraction of the population to change(not all the population).
  285. //Let it be 40% of the population
  286. int chosenChromosomes = (popSize*40)/100;
  287. vector<int> indices;
  288.  
  289.  
  290. int counter = 0;
  291. while(counter < numOfIterations)
  292. {
  293. for(int i = 0; i < chosenChromosomes; i++)
  294. {
  295. int r = rand()%cumTable.back();
  296.  
  297. int index = 0;
  298. for(int k = cumTable.size()-1 ; k > 0; k--)
  299. if( r < cumTable[k] && r >= cumTable[k-1])
  300. {
  301. index = k;
  302. break;
  303. }
  304.  
  305. indices.push_back(index);
  306. }
  307.  
  308. sort( indices.begin(), indices.end() );
  309. indices.erase( unique( indices.begin(), indices.end() ), indices.end() );
  310.  
  311. double r = (rand()%10 + 1.0)/10.0;
  312.  
  313. if(r >= 0.4 && r <= 0.7)
  314. crossOver(population,indices,maxWeight,bestSoFar,weights,benefits);
  315.  
  316. if(bestSoFar.getBenefit() >= bestSoln.getBenefit())
  317. break;
  318. counter++;
  319. }
  320. cout << "Computation finished at iteration : " << counter+1 << endl;
  321. cout << "With best solution entered : ";
  322. bestSoln.printFullChromosome();
  323. cout << endl << "And best solution the algorithm reached : ";
  324. bestSoFar.printFullChromosome();
  325. cout << endl;
  326. return bestSoFar;
  327. }
  328. //--------------------------------------------------------------------//
  329. int main()
  330. {
  331. srand(time(0));
  332.  
  333. int chSize;
  334. cout << "Enter Size of the chromosome : ";
  335. cin >> chSize;
  336.  
  337. int popSize;
  338. cout << "Enter Size of the population : " ;
  339. cin >> popSize;
  340.  
  341.  
  342. int maxWeight;
  343. cout << "Enter Knapsack Maximum Weight of the problem : " ;
  344. cin >> maxWeight;
  345.  
  346. int numOfIterations;
  347. cout << "Enter Knapsack Maximum number of iterations of the problem : ";
  348. cin >> numOfIterations;
  349. cout << endl;
  350.  
  351. vector<int> benefits;
  352. cout << "Enter Benefits" << endl;
  353. for(int i = 0; i < chSize; i++)
  354. {
  355. int num;
  356. cin >> num;
  357. benefits.push_back(num);
  358. }
  359. vector<int> weights;
  360. cout << "Enter Weights" << endl;
  361. for(int i = 0; i < chSize; i++)
  362. {
  363. int num;
  364. cin >> num;
  365. weights.push_back(num);
  366. }
  367.  
  368. Chromosome bestSoln(chSize);
  369. bestSoln.fill();
  370. while( !bestSoln.checkIfValid(weights,benefits,maxWeight) )
  371. {
  372. cout << "Invalid Solution.." ;
  373.  
  374. bestSoln.fill();
  375. }
  376. //bestSoln.printBits();
  377.  
  378.  
  379. compute(popSize,chSize,weights,benefits,maxWeight,numOfIterations,bestSoln);
  380. return 0;
  381. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement