Advertisement
Guest User

Untitled

a guest
Dec 16th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <fstream>
  4. #include <vector>
  5. #include <string>
  6. using namespace std;
  7.  
  8. void print_vec(vector<int> &v)
  9. {
  10. for (int i = 0; i < v.size(); i++)
  11. cout << v[i] << " ";
  12. }
  13.  
  14. void set_up_costs(vector<vector <double> > &costs)
  15. {
  16. ifstream file;
  17. file.open("matrice.txt.txt");
  18. if (!file.is_open())
  19. {
  20. cout << "nu s-a deschis fisierul \n";
  21. return;
  22. }
  23. int N;
  24. file >> N;
  25. double cost;
  26. for (int i = 0; i < N; i++)
  27. {
  28. vector <double> cost_lin;
  29. for (int j = 0; j < N; j++)
  30. {
  31. file >> cost;
  32. cost_lin.push_back(cost);
  33. }
  34. costs.push_back(cost_lin);
  35. }
  36. file.close();
  37. }
  38.  
  39. vector<int> get_random_sol(vector<vector <double> > &costs)
  40. {
  41. vector <int> sol;
  42. vector<int> used;
  43. for (int i = 0; i < costs.size(); i++)
  44. {
  45. used.push_back(0);
  46. }
  47. for (int i = costs.size() - 1; i >= 0; i--)
  48. {
  49. int poz = (rand() + 1) % (i + 1);
  50. /*int nr_fol = 0;
  51. for (int j = 0; j <= poz; j++)
  52. if (used[j] == 1) nr_fol++;
  53. poz += nr_fol;
  54. while (used[poz] == 1)
  55. poz++;
  56. sol.push_back(poz);
  57. used[poz] = 1;*/
  58. sol.push_back(poz);
  59. }
  60. return sol;
  61. }
  62.  
  63. double get_sol_val(vector<int> &sol, vector<vector <double> > &costs)
  64. {
  65. int val = 0;
  66. vector<int>perm;
  67. vector<int> used;
  68. for (int i = 0; i < costs.size(); i++)
  69. {
  70. used.push_back(0);
  71. }
  72. for (int i =0;i<sol.size();i++)
  73. {
  74. int poz = sol[i];
  75. int nr_fol = 0;
  76. for (int j = 0; j <= poz; j++)
  77. if (used[j] == 1) nr_fol++;
  78. poz += nr_fol;
  79. while (used[poz] == 1)
  80. poz++;
  81. perm.push_back(poz);
  82. used[poz] = 1;
  83. }
  84. for (int i = 0; i < sol.size() - 1; i++)
  85. val += costs[perm[i]][perm[i + 1]];
  86. return val;
  87. }
  88.  
  89. double genetic(vector<vector <double> > &costs)
  90. {
  91. vector<vector<int> >pop;
  92. for (int i = 0; i < 100; i++)//100 indivizi
  93. {
  94. vector<int> rand_sol = get_random_sol(costs);
  95. pop.push_back(rand_sol);
  96. }
  97.  
  98. for (int i = 0; i < pop.size(); i++)
  99. {
  100. print_vec(pop[i]);
  101. cout << " <- "<<get_sol_val(pop[i],costs)<<endl;
  102. }
  103. return 0;
  104. }
  105.  
  106. int main()
  107. {
  108.  
  109. vector <vector<double> > costs;
  110. set_up_costs(costs);
  111. int N = costs.size();
  112. cout << "matricea are " << N << " lin/col \n";
  113.  
  114. for (int i = 0; i < costs.size(); i++)
  115. {
  116. for (int j = 0; j < costs[i].size(); j++)
  117. cout << costs[i][j] << " ";
  118. cout << endl;
  119. }
  120. cout << endl;
  121. genetic(costs);
  122.  
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement