Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.90 KB | None | 0 0
  1. #include <iostream>
  2. #include <bitset>
  3. #include <limits>
  4. #include <cmath>
  5. #include <ctime>
  6. #include <algorithm>
  7.  
  8. using namespace std;
  9.  
  10. const int NUMBITS = 500;
  11. const double PESOMAX = 25;
  12. const double penalidade = 20;
  13. /*const int NUMBITS = 5;
  14. const double PESOMAX = 13;
  15. const double penalidade = 20;*/
  16.  
  17. double pesos[NUMBITS];
  18. double utilidades[NUMBITS];
  19. /*double pesos [] = {1, 6, 4, 3, 3};
  20. double utilidades [] = {6, 10, 9, 4, 2};*/
  21.  
  22. void gerarAleatoriamente(){
  23. for (int i = 0; i < NUMBITS; i++) {
  24. utilidades[i] = rand()%20;
  25. pesos[i] = rand()%20;
  26. }
  27. }
  28.  
  29. /* Function to print an array */
  30. void printArray(double arr[], int size)
  31. {
  32. int i;
  33. for (i=0; i < size; i++)
  34. printf("%2.f ", arr[i]);
  35. printf("\n");
  36. }
  37.  
  38. // Funcao q realiza as trocas
  39. void swap(double *xp, double *yp)
  40. {
  41. int temp = *xp;
  42. *xp = *yp;
  43. *yp = temp;
  44. }
  45.  
  46. void bubbleSort(double arr[], int n, double util[])
  47. {
  48. int i, j;
  49. for (i = 0; i < n-1; i++)
  50. {
  51. for (j = 0; j < n-i-1; j++)
  52. {
  53. if (util[j] < util[j+1])
  54. {
  55. swap(&util[j], &util[j+1]);
  56. swap(&arr[j], &arr[j+1]);
  57. }
  58. }
  59. }
  60. }
  61.  
  62. double fObj(bitset<NUMBITS> solucao) {
  63. double somautil=0, somapeso=0;
  64. for(int i=0; i<NUMBITS; i++) {
  65. somapeso += pesos[i] * solucao[i];
  66. somautil += utilidades[i] * solucao[i];
  67. }
  68. if ( somapeso > PESOMAX ) {
  69. somautil -= (somapeso-PESOMAX) * penalidade;
  70. }
  71. return somautil;
  72. }
  73.  
  74. bitset<NUMBITS> buscaLocal(bitset<NUMBITS> sIni) {
  75.  
  76. bitset<NUMBITS> solucao, solucaoAux;
  77.  
  78. solucao = sIni ;
  79. int cont=0;
  80. bool melhorou;
  81. do {
  82. melhorou = false;
  83. for(int i=0; i<NUMBITS; i++) {
  84. solucaoAux = solucao;
  85. solucaoAux[i] = ~solucaoAux[i];
  86. double fsolucao, fsolucaoAux;
  87. fsolucao = fObj(solucao);
  88. fsolucaoAux = fObj(solucaoAux);
  89.  
  90. if ( fsolucaoAux > fsolucao ) {
  91. solucao = solucaoAux;
  92. melhorou = true;
  93. }
  94. //cout << ++cont << "\t" << fsolucao << endl;
  95. cout << ++cont << "\t" << fsolucao << "\t" << fsolucaoAux << endl;
  96. }
  97. } while (melhorou);
  98. return solucao;
  99. }
  100.  
  101. bitset<NUMBITS> obtemSolucaoInicial() {
  102. int sIni [] = {0,1,0,1,1};
  103. bitset<NUMBITS> solInicial;
  104.  
  105. for(int i=0; i<NUMBITS; i++) {
  106. solInicial[i] = rand()%2; // solução aletória
  107. //solInicial[i] = sIni[i]; // solução a partir do vetor sIni [] = {0,1,0,1,1}
  108. }
  109. return solInicial;
  110. }
  111.  
  112. bitset<NUMBITS> obtemSolucaoInicialGuloso(double peso[]) {
  113. bitset<NUMBITS> solInicial;
  114. double pesoFinal = 0;
  115. double vetor[NUMBITS];
  116. for(int i = 0; i < NUMBITS; i++) {
  117. if(pesoFinal + peso[i] <= PESOMAX) {
  118. pesoFinal += peso[i];
  119. vetor[i] = 1;
  120. } else {
  121. vetor[i] = 0;
  122. }
  123. }
  124. printArray(vetor, NUMBITS);
  125.  
  126. for(int i=0; i<NUMBITS; i++) {
  127. solInicial[i] = vetor[i];
  128. }
  129. return solInicial;
  130. }
  131.  
  132. int main() {
  133. srand(time(NULL));
  134. gerarAleatoriamente();
  135.  
  136. printArray(utilidades, NUMBITS);
  137. printArray(pesos, NUMBITS);
  138. cout<<"\n";
  139.  
  140. bubbleSort(pesos, NUMBITS, utilidades);
  141.  
  142. bitset<NUMBITS> solInicial = obtemSolucaoInicialGuloso(pesos);
  143. bitset<NUMBITS> s = buscaLocal(solInicial);
  144.  
  145. // código para inverter o bitset (little-endian => big-endian)
  146. string s_ini_str=solInicial.to_string();
  147. reverse(s_ini_str.begin(), s_ini_str.end());
  148. string s_fin_str=s.to_string();
  149. reverse(s_fin_str.begin(), s_fin_str.end());
  150.  
  151. cout << "fObj inicial =\t" << fObj(solInicial) << " para s =\t" << s_fin_str << endl;
  152. cout << "fObj máximo =\t" << fObj(s) << " para s =\t" << s_ini_str << endl;
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement