Advertisement
migonne

kuba ts

Mar 7th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.96 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <algorithm>
  4. #include <vector>
  5. #include <cstdlib>
  6. #include <ctime>
  7. #define Population 10000
  8. #define Mutation 300
  9. #define Population_Childs 40// always Population_Childs is Constant^2
  10. #define Generation 500
  11. #define Difference 10
  12. #define MAX_REPEAT 40
  13.  
  14. using namespace std;
  15.  
  16. vector <int> Tasks;
  17. int Processor_Number, Task_Number, Time_Suma = 0, Number = 0;//Number is a number of childs
  18.  
  19. struct Time{
  20. int Value;
  21. int Index;
  22. };
  23.  
  24. Time Time_Tab[Population_Childs * Population_Childs];
  25.  
  26. bool sortByTime(Time A, Time B){
  27. return A.Value < B.Value;
  28. }
  29.  
  30. void Parents_produce(int ** Parents_Tab){
  31. for(int i = 0; i < Population; i++){
  32. for(int j = 0; j < Task_Number; j++){
  33. Parents_Tab[i][j] = rand() % Processor_Number;//which processor takes j-th task in i-th solution
  34. }
  35. }
  36. }
  37.  
  38. void Childs_produce(int ** Parents_Tab, int ** Childs_Tab){
  39. Number = 0;
  40. for(int i = 0; i < Population_Childs; i++){
  41. for(int j = 0; j < Population_Childs; j++){
  42. if(i != j){
  43. int Border = rand() % Task_Number;
  44. for(int l = 0; l < Task_Number; l++){
  45. if(rand() % Mutation == 2){
  46. Childs_Tab[Number][l] = rand() % Processor_Number;
  47. }
  48. else{
  49. if(l < Border)//outbreeding parents, what if i is equal to j?
  50. Childs_Tab[Number][l] = Parents_Tab[i][l];
  51. else
  52. Childs_Tab[Number][l] = Parents_Tab[j][l];
  53. }
  54. }
  55. Number++;
  56. }
  57. }
  58. }
  59. }
  60.  
  61. void Processing_Time(int ** Childs_Tab) {
  62. int Maks;
  63.  
  64. int Time_Task[Processor_Number];// set it to 0
  65. for(int i = 0; i < Number; i++){
  66. for(int j = 0; j < Processor_Number; j++)
  67. Time_Task[j] = 0;
  68.  
  69. for(int j = 0; j < Task_Number; j++){
  70. Time_Task[Childs_Tab[i][j]] += Tasks[j];// estimate time of each processor
  71. }
  72.  
  73. Maks = Time_Task[0];
  74. for(int j = 1; j < Processor_Number; j++){
  75. if(Time_Task[j] > Maks)
  76. Maks = Time_Task[j];
  77. }
  78. Time_Tab[i].Value = Maks;//estimate maximum from all processors, which is time of computation
  79. Time_Tab[i].Index = i;
  80. }
  81. sort(Time_Tab, Time_Tab + Number, sortByTime);// does it sort properly? Yes it does.
  82. }
  83.  
  84. void Parents_choose_new(int ** Parents_Tab, int ** Childs_Tab){
  85. int Maks_Parent, Counter = 0;
  86.  
  87. int Time_Task[Processor_Number];// set it to 0
  88. for(int j = 0; j < Processor_Number; j++)
  89. Time_Task[j] = 0;
  90.  
  91. for(int j = 0; j < Task_Number; j++){
  92. Time_Task[Parents_Tab[0][j]] += Tasks[j];// estimate time of each processor for parent, to find difference
  93. }
  94.  
  95. Maks_Parent = Time_Task[0];
  96. for(int j = 1; j < Processor_Number; j++){
  97. if(Time_Task[j] > Maks_Parent)
  98. Maks_Parent = Time_Task[j];
  99. }
  100.  
  101. Processing_Time(Childs_Tab);
  102.  
  103. while(Maks_Parent - Time_Tab[0].Value < Difference && Counter < MAX_REPEAT){
  104. Childs_produce(Parents_Tab, Childs_Tab);
  105. Processing_Time(Childs_Tab);
  106. Counter++;
  107. }
  108. /*cout << "hahaha" << Counter << endl;*/
  109. for(int i = 0; i < Population_Childs; i++){
  110. for(int j = 0; j < Task_Number; j++)
  111. Parents_Tab[i][j] = Childs_Tab[Time_Tab[i].Index][j];
  112. }
  113.  
  114. for(int i = 0; i < 4; i++)
  115. cout << Time_Tab[i].Index << " " << Time_Tab[i].Value << endl;
  116. cout << endl;
  117. }
  118.  
  119. int main(){
  120. srand(time(NULL));
  121.  
  122. ifstream plik;
  123. ofstream plik_1;
  124. plik.open("scheduling-input5");
  125. plik_1.open("wykres", ios::app);
  126.  
  127. plik >> Processor_Number >> Task_Number;
  128.  
  129. int ** Parents_Tab = new int * [Population];
  130. for(int i = 0; i < Population; i++)
  131. Parents_Tab[i] = new int [Task_Number];
  132.  
  133. int ** Childs_Tab = new int * [Population_Childs * Population_Childs];
  134. for(int i = 0; i < Population_Childs * Population_Childs; i++)
  135. Childs_Tab[i] = new int [Task_Number];
  136.  
  137. for(int i = 0; i < Task_Number; i++){
  138. int Task;
  139. plik >> Task;
  140. Time_Suma += Task;
  141. Tasks.push_back(Task);
  142. }
  143.  
  144. cout << Time_Suma / Processor_Number << endl;
  145.  
  146. Parents_produce(Parents_Tab);
  147. for(int k = 0; k < Generation; k++){
  148. Childs_produce(Parents_Tab, Childs_Tab);
  149. Parents_choose_new(Parents_Tab, Childs_Tab);
  150. }
  151. plik_1 << /*Population << " " << Number << " " <<*/ Time_Tab[0].Value /*<< " " << Time_Suma / Processor_Number*/ << endl;
  152.  
  153. for(int i = 0; i < Population; i++)
  154. delete [] Parents_Tab[i];
  155. delete Parents_Tab;
  156.  
  157. for(int i = 0; i < Population_Childs * Population_Childs; i++)
  158. delete [] Childs_Tab[i];
  159. delete Childs_Tab;
  160. plik.close();
  161. plik_1.close();
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement