Advertisement
Guest User

Untitled

a guest
Feb 6th, 2016
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.94 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <ga.h>
  5. #include <cmath>
  6. #include <stdexcept>
  7. #include <algorithm>
  8.  
  9. # define M_PI 3.141592653589793238462643383279502884
  10. # define ZERO 0.001
  11.  
  12. using namespace std;
  13.  
  14. typedef int (*CrossoverMethod)(const GAGenome&, const GAGenome&, GAGenome*, GAGenome*);
  15.  
  16. namespace {
  17. vector<float> cables;
  18. int populationSize = 1000;
  19. int numberOfGenerations = 1000;
  20. vector<float> mutation = {0.01, 0.015, 0.02}; //, 0.1, 0.15, 0.2};
  21. vector<float> crossover = {0.5, 0.6, 0.7}; //0.5, 0.7};
  22. vector<GABin2DecGenome> bestGenomes;
  23. }
  24.  
  25. vector<float> readDataFromFile(const char* fileName) {
  26. ifstream dataFile(fileName);
  27. float oneLineValue = 0.0;
  28. vector<float> output;
  29.  
  30. while(dataFile >> oneLineValue)
  31. output.push_back(oneLineValue);
  32. dataFile.close();
  33.  
  34. cout << output.size() << endl;
  35.  
  36. return output;
  37. }
  38.  
  39. void writeSolutionToFile(const char* fileName, GABin2DecGenome bestGenome) {
  40. ofstream resultsFile, scoresAB;
  41. vector<float> radius, x, y;
  42. int numberOfUsedCables = 0;
  43. float A = 0.0, B = 1.0, phi = 0.0, d;
  44.  
  45. scoresAB.open("scores.txt", std::ios_base::app);
  46. resultsFile.open(fileName);
  47.  
  48. resultsFile << cables.size() << endl;
  49.  
  50. for (int i = 0; i < cables.size(); i++) {
  51. if (bestGenome.phenotype(i * 3) != 0) {
  52. numberOfUsedCables++;
  53. phi += (M_PI * cables.at(i) * cables.at(i));
  54. }
  55. }
  56.  
  57. resultsFile << numberOfUsedCables << endl;
  58. resultsFile << phi << endl;
  59.  
  60. for (int i = 0; i < cables.size(); i++) {
  61. if (bestGenome.phenotype(i * 3) != 0) {
  62. A++;
  63. radius.push_back(cables.at(i));
  64. x.push_back(bestGenome.phenotype((i * 3) + 1));
  65. y.push_back(bestGenome.phenotype((i * 3) + 2));
  66. resultsFile << cables.at(i) << " "
  67. << bestGenome.phenotype((i * 3) + 1)<< " "
  68. << bestGenome.phenotype((i * 3) + 2) << endl;
  69. }
  70. }
  71.  
  72. // sprawdzenie warunkow zadania
  73. for (int j = 0; j < radius.size(); j++) {
  74. for (int i = j + 1; i < radius.size(); i++) {
  75. d = sqrt(pow((x.at(i) - x.at(j)), 2.0) + pow((y.at(i) - y.at(j)), 2.0));
  76. if (d < radius.at(i) + radius.at(j)) {
  77. cout << "Kola zachodza na siebie" << endl;
  78. }
  79. }
  80. }
  81.  
  82. for (int i = 0; i < radius.size(); i++) {
  83. if (x[i] + radius[i] > 1 ||
  84. x[i] - radius[i] < 0 ||
  85. y[i] + radius[i] > 1 ||
  86. y[i] - radius[i] < 0) { cout << "Kolo wychodzi za pole" << endl; }
  87. }
  88.  
  89. if (radius.size() < ceil(0.2 * cables.size())) {
  90. cout << "za malo uzytych kabli" << endl;
  91. }
  92.  
  93. A /= cables.size();
  94.  
  95. scoresAB << A << " " << (B - phi) << endl;
  96.  
  97. scoresAB.close();
  98. resultsFile.close();
  99. }
  100.  
  101. vector<float> calculateA() {
  102. vector<float> output;
  103.  
  104. for (auto genome : bestGenomes) {
  105. float A = 0;
  106. for (int i = 0; i < cables.size(); i++) {
  107. if (genome.phenotype(i * 3) != 0) {
  108. A += 1.0;
  109. }
  110. }
  111. output.push_back(A /= cables.size());
  112. }
  113.  
  114. return output;
  115. }
  116.  
  117. vector<float> calculateB() {
  118. vector<float> output;
  119.  
  120. for (auto genome : bestGenomes) {
  121. float phi = 0;
  122. for (int i = 0; i < cables.size(); i++) {
  123. if (genome.phenotype(i * 3) != 0) {
  124. phi += (M_PI * cables.at(i) * cables.at(i));
  125. }
  126. }
  127. output.push_back(1.0 - phi);
  128. }
  129.  
  130. return output;
  131. }
  132.  
  133. int findGenomeFromFirstParetoFrontIndex(vector<float> allA, vector<float> allB) {
  134. float bestA = -100.0, bestB = -100.0;
  135. int bestIndex = 0;
  136.  
  137. for (int i = 0; i < allB.size(); i++) {//
  138. if (allB.at(i) > bestB && allA.at(i) >= 0.2) {
  139. bestB = allB.at(i);
  140. bestA = allA.at(i);
  141. bestIndex = i;
  142. }
  143. else if (allB.at(i) == bestB && allA.at(i) >= 0.2) {
  144. if (allA.at(i) > bestA) {
  145. bestB = allB.at(i);
  146. bestA = allA.at(i);
  147. bestIndex = i;
  148. }
  149. }
  150. }
  151.  
  152. return bestIndex;
  153. }
  154.  
  155. void findBestSolution() {
  156. vector<float> allA = calculateA();
  157. vector<float> allB = calculateB();
  158.  
  159. for (auto a : allA) {
  160. cout << a << ", ";
  161. }
  162. cout << endl << allA.size() << endl;
  163.  
  164. for (auto b : allB) {
  165. cout << b << ", ";
  166. }
  167. cout << endl << allB.size() << endl;
  168.  
  169. int index = findGenomeFromFirstParetoFrontIndex(allA, allB);
  170.  
  171. cout << "BEST: " << allA.at(index) << " " << allB.at(index) << " " << index << endl;
  172. cout << "MAX B: " << *std::max_element(allB.begin(),allB.end()) << endl;
  173.  
  174. writeSolutionToFile("output.txt", bestGenomes.at(index));
  175. }
  176.  
  177. float objective(GAGenome& c)
  178. {
  179. vector<float> radius, x, y;
  180. GABin2DecGenome& genome = (GABin2DecGenome &)c;
  181. float A = 0.0, B = 1.0, phi = 0.0, output = 0.0, d = 0.0;
  182. int usedKables = 0;
  183.  
  184. for (int i = 0; i < cables.size(); i++) {
  185. if (genome.phenotype(i * 3) != 0) {
  186. usedKables++;
  187. A += 1.0;
  188. phi += (M_PI * cables.at(i) * cables.at(i));
  189. radius.push_back(cables.at(i));
  190. x.push_back(genome.phenotype((i * 3) + 1));
  191. y.push_back(genome.phenotype((i * 3) + 2));
  192. }
  193. }
  194.  
  195. A /= static_cast<float>(cables.size());
  196. B -= phi;
  197. output += (5 * A) + (10 * B);
  198.  
  199. // warunek konieczny zadania
  200. if (phi >= 1.0)
  201. return ZERO;
  202.  
  203. if (usedKables < ceil(0.2 * cables.size())) {
  204. output *= (radius.size() / (1.8 * (ceil(0.2 * cables.size()))));
  205. }
  206.  
  207. // czy kable na siebie nachodza
  208. for (int j = 0; j < radius.size(); j++) {
  209. for (int i = j + 1; i < radius.size(); i++) {
  210. d = sqrt(pow((x.at(i) - x.at(j)), 2.0) + pow((y.at(i) - y.at(j)), 2.0));
  211. if (d < radius.at(i) + radius.at(j))
  212. output *= (d / (radius.at(i) + radius.at(j)));
  213. }
  214. }
  215.  
  216. // czy kable mieszcza sie w polu
  217. for (int i = 0; i < radius.size(); i++) {
  218. if (x[i] + radius[i] > 1) { output *= abs(x[i] + radius[i] - 1.0);}
  219. if (x[i] - radius[i] < 0) { output *= abs(x[i] - radius[i]);}
  220. if (y[i] + radius[i] > 1) { output *= abs(y[i] + radius[i] - 1.0);}
  221. if (y[i] - radius[i] < 0) { output *= abs(y[i] - radius[i]);}
  222. }
  223.  
  224. return output;
  225. }
  226.  
  227. GABin2DecGenome run(float mutationProbability, float crossoverProbability, CrossoverMethod crossoverMethod) {
  228. GABin2DecPhenotype phenotypeMap;
  229.  
  230. for (int i = 0; i < cables.size(); i++) {
  231. phenotypeMap.add(1, 0, 1);
  232. phenotypeMap.add(10, 0, 1);
  233. phenotypeMap.add(10, 0, 1);
  234. }
  235.  
  236. GABin2DecGenome genome(phenotypeMap, objective);
  237. genome.mutator(GA1DBinaryStringGenome::FlipMutator);
  238. genome.crossover(crossoverMethod);
  239.  
  240. GATournamentSelector selector;
  241.  
  242. GASimpleGA ga(genome);
  243. ga.maximize();
  244. ga.populationSize(populationSize);
  245. ga.nGenerations(numberOfGenerations);
  246. ga.pMutation(mutationProbability);
  247. ga.pCrossover(crossoverProbability);
  248. ga.selector(selector);
  249.  
  250. GASigmaTruncationScaling scaling;
  251. ga.scaling(scaling);
  252.  
  253. ga.initialize();
  254.  
  255. while (!ga.done()) { ga.step();}
  256.  
  257. GABin2DecGenome best_genome(phenotypeMap, objective);
  258. best_genome = ga.statistics().bestIndividual();
  259.  
  260. return best_genome;
  261. }
  262.  
  263. int main(int argc, char * argv[]) {
  264. cables = readDataFromFile("kable.ini");
  265.  
  266. vector<CrossoverMethod> crossoverMethods = {&GA1DBinaryStringGenome::OnePointCrossover};
  267.  
  268. for (auto crossoverMethod : crossoverMethods) {
  269. for (float m: mutation) {
  270. for (float c: crossover) {
  271. GABin2DecGenome bestGenome = run(m, c, crossoverMethod);
  272. bestGenomes.push_back(bestGenome);
  273. }
  274. }
  275. }
  276.  
  277. findBestSolution();
  278. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement