Advertisement
Guest User

gfhgfh

a guest
Jan 19th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.07 KB | None | 0 0
  1. #ifndef GENETIC_H
  2. #define GENETIC_H
  3. #include <ctime>
  4. #include "container.h"
  5. #include <string.h>
  6. class Genetic
  7. {
  8. private:
  9. double capacity;
  10. vector< vector<Container> > primaryPopulation;
  11. Container *currentContainer;
  12. vector<Container> containers;
  13. vector<double> missedElements;
  14. vector<int> indexBinsWithMissedElements;
  15. int indexPopulationBestCostFunction;
  16. double getCostFunction(vector<Container> vv) {
  17. double s = 0;
  18. cout << "------------- " << endl;
  19. for(int j = 0; j < vv.size(); j++) {
  20. s = s + (vv[j].getWorkload() / capacity)*(vv[j].getWorkload() / capacity);
  21. }
  22. return s / vv.size();
  23. }
  24.  
  25. public:
  26. Genetic(vector<double> initialObj, double binCapacity) {
  27. capacity = binCapacity;
  28. srand(time(0));
  29.  
  30. //genera init population
  31. for(int i = 0; i < 10; i++) {
  32. generateInitPopulation(initialObj);
  33. containers.clear();
  34. }
  35. for(int j = 0; j < 9; j++) {
  36. Crossover(primaryPopulation[j], primaryPopulation[j+1]);
  37. missedElements.clear();
  38. indexBinsWithMissedElements.clear();
  39. }
  40. // cout << endl << "after crossover " << endl;
  41. // cout << " size of new population = " << primaryPopulation.size() << endl;
  42. findBestCostFunction();
  43. }
  44. void findBestCostFunction() {
  45. double valueCostFunction = getCostFunction(primaryPopulation[0]);
  46. indexPopulationBestCostFunction = 0;
  47. for(int i = 1; i < primaryPopulation.size(); i++) {
  48. double currentCostF = getCostFunction(primaryPopulation[i]);
  49. if(currentCostF > valueCostFunction) {
  50. indexPopulationBestCostFunction = i;
  51. valueCostFunction = currentCostF;
  52. }
  53. }
  54. // showContainers(100000, primaryPopulation[indexPopulationBestCostFunction]);
  55. }
  56. int getBestCountOfContainers(){
  57. return primaryPopulation[indexPopulationBestCostFunction].size();
  58. }
  59.  
  60. void someFunction(vector<Container> vv, vector<Container> vv2) {
  61. cout << "some function " << endl;
  62. showContainers(9, vv);
  63. showContainers(11, vv);
  64. }
  65. void Crossover(vector<Container> parent1, vector<Container> parent2) {
  66.  
  67. int point1_1 = rand() % (parent1.size()-1) + 1;
  68. int point1_2 = rand() % (parent1.size()-1) + 1;
  69.  
  70. int point2_1 = rand() % (parent2.size()-1) + 1;
  71. int point2_2 = rand() % (parent2.size()-1) + 1;
  72. if(point1_1 > point1_2) swap(point1_1, point1_2);
  73. if(point2_1 > point2_2) swap(point2_1, point2_2);
  74.  
  75. cout << endl;
  76. cout << "point1_1: " << point1_1 << " point1_2: " << point1_2 << endl;
  77. cout << "point2_1: " << point2_1 << " point2_2: " << point2_2 << endl;
  78.  
  79. //Push random bins into chromose1
  80. int previousSize = parent1.size();
  81. for(int i = 0; i < parent2.size(); i++) {
  82. if(i >= point2_1 && i <= point2_2) {
  83. parent1.push_back(parent2[i]);
  84. }
  85. }
  86. for(int i = previousSize; i < parent1.size(); i++) {
  87. for(int k = 0; k < parent1[i].getCountOfElements(); k++){
  88. Item itemFromNewBin(parent1[i].getElementByID(k));
  89. for(int j = 0; j < previousSize; j++){
  90. //цикл по элементам
  91. for(int m = 0; m < parent1[j].getCountOfElements(); m++) {
  92. Item item2(parent1[j].getElementByID(m));
  93. if(itemFromNewBin.getWeight() == item2.getWeight()) {
  94. parent1[j].DeleteElementFromContainer(item2);
  95. indexBinsWithMissedElements.push_back(j);
  96. //move to next element in part2
  97. goto metka;
  98. }
  99. }
  100. }
  101. metka:
  102. cout << endl;
  103. }
  104. }
  105. sort( indexBinsWithMissedElements.begin(), indexBinsWithMissedElements.end() );
  106. indexBinsWithMissedElements.erase( unique( indexBinsWithMissedElements.begin(), indexBinsWithMissedElements.end() ), indexBinsWithMissedElements.end() );
  107.  
  108. for(int z = 0; z < indexBinsWithMissedElements.size(); z++) {
  109. cout << indexBinsWithMissedElements[z] << " ";
  110. }
  111. cout << endl << "---------------------------------------------- " << endl;
  112.  
  113. //Получить упущенные элементы
  114. for(int i = 0; i < indexBinsWithMissedElements.size(); i++) {
  115. for(int k = 0; k < parent1[indexBinsWithMissedElements[i]].getCountOfElements(); k++)
  116. {
  117. //Add to new array
  118. Item item(parent1[indexBinsWithMissedElements[i]].getElementByID(k));
  119. missedElements.push_back(item.getWeight());
  120. }
  121. }
  122. cout << "missed elements " << endl;
  123. for(int i = 0; i < missedElements.size(); i++) {
  124. cout << missedElements[i] << " ";
  125. }
  126. cout << endl << "---------------------------------------------- " << endl;
  127. //Delete empty containers
  128. for(int i = 0; i < indexBinsWithMissedElements.size(); i++) {
  129. parent1.erase(parent1.begin() + indexBinsWithMissedElements[i]);
  130. for(int j = 0; j < indexBinsWithMissedElements.size(); j++) {
  131. indexBinsWithMissedElements[j] -= 1;
  132. }
  133. }
  134. cout << endl << "before FFD (empty containers has been deleted" << endl;
  135. showContainers(5, parent1);
  136.  
  137. bool execute;
  138. while(missedElements.size() > 0) {
  139. execute = false;
  140. m2:
  141. for(int i = 0; i < missedElements.size(); i++) {
  142. Item item(missedElements[i]);
  143. for(int j = 0; j < parent1.size(); j++) {
  144. if(parent1[j].IsEnoughSpace(item)) {
  145. parent1[j].PutElementInContainer(item);
  146. missedElements.erase(missedElements.begin() + i);
  147. cout << "move " << endl;
  148. execute = true;
  149. goto m2;
  150. } else {
  151. //swap elements
  152. for(int m = 0; m < parent1[j].getCountOfElements(); m++) {
  153. Item item2( parent1[j].getElementByID(m));
  154. if(item.getWeight() > item2.getWeight()) {
  155. if(PossibleToExchange(parent1[j], item2.getWeight(), item.getWeight())) {
  156. parent1[j].DeleteElementFromContainer(item2);
  157. parent1[j].PutElementInContainer(item);
  158. missedElements.erase(missedElements.begin() + i);
  159. missedElements.push_back(item2.getWeight());
  160. cout << "swap " << endl;
  161. execute = true;
  162. goto m2;
  163. }
  164. }
  165. }
  166. }
  167. }
  168. }
  169. if(execute == false) break;
  170. }
  171. cout << endl << "after FFD " << endl;
  172. showContainers(7, parent1);
  173. cout << "missed elements " << endl;
  174. for(int i = 0; i < missedElements.size(); i++) {
  175. cout << missedElements[i] << " ";
  176. }
  177. //add missed element to parent1
  178. if(missedElements.size() > 0) {
  179. cout << "missed elements exist!!!!!" << endl;
  180. currentContainer = new Container(capacity);
  181. for(int i = 0; i < missedElements.size(); i++) {
  182. Item item(missedElements[i]);
  183. if(currentContainer->IsEnoughSpace(item)) {
  184. currentContainer->PutElementInContainer(item);
  185. } else {
  186. //save current container
  187. parent1.push_back(*currentContainer);
  188. currentContainer = new Container(capacity);
  189. }
  190. }
  191. if (currentContainer->getWorkload() > 0) {
  192. parent1.push_back(*currentContainer);
  193. }
  194. }
  195. showContainers(9, parent1);
  196. primaryPopulation.push_back(parent1);
  197.  
  198. DeleteWorstSetOfBins();
  199. }
  200.  
  201. bool PossibleToExchange(Container c, Item itemInContainer, Item item2) {
  202. c.DeleteElementFromContainer(itemInContainer);
  203. if(c.PutElementInContainer(item2) == true) {
  204. return true;
  205. } else {
  206. return false;
  207. }
  208. }
  209.  
  210. template <typename T>
  211. void swap(T &a, T &b) {
  212. T temp = a;
  213. a = b;
  214. b = temp;
  215. }
  216. void DeleteWorstSetOfBins() {
  217. double costF = getCostFunction(primaryPopulation[0]);
  218. int index = 0;
  219. for(int i = 1; i < primaryPopulation.size(); i++) {
  220. double cf = getCostFunction(primaryPopulation[i]);
  221. if(cf < costF) {
  222. index = i;
  223. }
  224. }
  225. primaryPopulation.erase(primaryPopulation.begin() + index);
  226. }
  227.  
  228. void generateInitPopulation(vector<double> initialObj) {
  229. int index;
  230. currentContainer = new Container(capacity);
  231. while (initialObj.size() > 0) {
  232. if (initialObj.size() > 1)
  233. index = rand() % (initialObj.size() - 1);
  234. else index = 0;
  235. Item item(initialObj[index]);
  236. if (currentContainer->IsEnoughSpace(item)) {
  237. currentContainer->PutElementInContainer(item);
  238. initialObj.erase(initialObj.begin() + index);
  239. }
  240. else {
  241. //save current container
  242. containers.push_back(*currentContainer);
  243. //add new container
  244. currentContainer = new Container(capacity);
  245. }
  246. }
  247. if (currentContainer->getWorkload() > 0) {
  248. containers.push_back(*currentContainer);
  249. }
  250. primaryPopulation.push_back(containers);
  251. // showContainers(primaryPopulation.size()-1, containers);
  252. }
  253.  
  254. void showContainers(int nCont, vector<Container> v) {
  255. cout << "container# " << nCont << endl;
  256. for (int i = 0; i < v.size(); i++) {
  257. cout << v[i].getAmountOfFreeSpace() << " "; v[i].showObjects(); cout << endl;
  258. }
  259. cout << "Count of containers " << v.size() << endl;
  260. cout << "Cost function " << getCostFunction(v) << endl;
  261. }
  262. void showR() {
  263. showResult(primaryPopulation[indexPopulationBestCostFunction]);
  264. }
  265.  
  266. void showResult(vector<Container> v) {
  267. cout << endl;
  268. for(int i = 0; i < v.size(); i++) {
  269. v[i].showObjects();
  270. }
  271. }
  272. };
  273.  
  274. #endif // GENETIC_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement