Advertisement
Guest User

Untitled

a guest
May 24th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.74 KB | None | 0 0
  1. // PV_energy.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <stdio.h>
  7. #include <vector>
  8. #include <stdlib.h>
  9. #include <time.h>
  10. #include <thread>
  11. #include <ctime>
  12. #include <string>
  13. #include <mutex>
  14. using namespace std;
  15.  
  16. mutex mtx;
  17. static int bestSeq;
  18. static int bestPro;
  19.  
  20.  
  21. vector<int> calcCi(vector<int> c, int i, int di) {
  22. vector<int> ci;
  23. // cout << c[0] << " " << c[1] << " " << c[2] << " i: " << i << endl;
  24. if (i < 1) {
  25. ci = { 0,0,0 };
  26. }
  27. else {
  28. ci = c;
  29. switch (di) {
  30. case 'L':
  31. ci[0] -= 1;
  32. break;
  33. case 'R':
  34. ci[0] += 1;
  35. break;
  36. case 'D':
  37. ci[1] -= 1;
  38. break;
  39. case 'U':
  40. ci[1] += 1;
  41. break;
  42. case 'B':
  43. ci[2] -= 1;
  44. break;
  45. case 'F':
  46. ci[2] += 1;
  47. break;
  48. default:
  49. cout << "D[i] out of bounds." << endl;
  50. exit(1);
  51. }
  52. }
  53.  
  54. return ci;
  55. }
  56.  
  57. bool adjacent(vector<int> a, vector<int> b) {
  58. if ((a[0] == b[0] && a[1] == b[1]) && (a[2] == b[2] - 1 || a[2] == b[2] + 1) ||
  59. (a[0] == b[0] && a[2] == b[2]) && (a[1] == b[1] - 1 || a[1] == b[1] + 1) ||
  60. (a[1] == b[1] && a[2] == b[2]) && (a[0] == b[0] - 1 || a[0] == b[0] + 1)) {
  61. return true;
  62. }
  63.  
  64. return false;
  65. }
  66.  
  67. int proteinCubicEnergy(vector<int> s, vector<int> d, vector<vector<int>> c) {
  68. int sum = 0, tmp_sum;
  69. for (int i = 0; i < s.size() - 2; i++) {
  70. tmp_sum = 0;
  71. for (int j = i + 2; j < s.size(); j++) {
  72. vector<int> ci = calcCi(c[i], i, d[i]);
  73. vector<int> cj = c[j];
  74.  
  75. if (adjacent(ci, cj) && s[i] == s[j] && s[i] == 'H') {
  76. tmp_sum += 1;
  77. }
  78. }
  79. sum += tmp_sum;
  80. }
  81. for (int i = 0; i < c.size() - 1; i++) {
  82. for (int j = i + 1; j < c.size(); j++) {
  83. if (c[i] == c[j]) {
  84. sum += 1000;
  85. break;
  86. }
  87. }
  88. }
  89. return -1 * sum;
  90. }
  91.  
  92. int sequentialEnergy(vector<int> arr) {
  93. int tmp_sum, sum = 0;
  94. for (int k = 0; k < arr.size() - 1; k++) {
  95. tmp_sum = 0;
  96. for (int i = 0; i < arr.size() - (k + 1); i++) {
  97. tmp_sum += arr[i] * arr[i + (k + 1)];
  98. }
  99. sum += tmp_sum*tmp_sum;
  100. }
  101. return sum;
  102. }
  103.  
  104. void threadSeq(int seqSize, double t, int threadSpeed) {
  105. srand(time(NULL));
  106. vector<int> seq;
  107. for (int i = 0; i < seqSize; i++) {
  108. int s = rand() % 2 == 0 ? -1 : 1;
  109. seq.push_back(s);
  110. }
  111.  
  112. int nrg = sequentialEnergy(seq);
  113. if (nrg < bestSeq) {
  114. mtx.lock();
  115. bestSeq = nrg;
  116. cout << "Cas: " << t << "s\t"
  117. << "Hitrost: " << threadSpeed << "\t"
  118. << "Rezultat: " << nrg << endl
  119. << "Sekvenca: { ";
  120. for (int i = 0; i < seqSize; i++) {
  121. cout << seq[i] << " ";
  122. }
  123. cout << "}" << endl;
  124. mtx.unlock();
  125. }
  126. }
  127.  
  128. void threadProtein(vector<int> seq, double t, int threadSpeed) {
  129. srand(time(NULL));
  130. vector<int> d;
  131. vector<vector<int>> c;
  132. c.push_back({ 0,0,0 });
  133. for (int i = 0; i < seq.size() - 1; i++) {
  134. vector<int> tmp_pos = c[i];
  135. switch (rand() % 6) { // 0-F 1-B 2-U 3-D 4-L 5-D
  136. case 0:
  137. d.push_back('F');
  138. c.push_back({ c[i][0], c[i][1], c[i][2] + 1 });
  139. break;
  140. case 1:
  141. d.push_back('B');
  142. c.push_back({ c[i][0], c[i][1], c[i][2] - 1 });
  143. break;
  144. case 2:
  145. d.push_back('U');
  146. c.push_back({ c[i][0], c[i][1] + 1, c[i][2] });
  147. break;
  148. case 3:
  149. d.push_back('D');
  150. c.push_back({ c[i][0], c[i][1] - 1, c[i][2] });
  151. break;
  152. case 4:
  153. d.push_back('L');
  154. c.push_back({ c[i][0] - 1, c[i][1], c[i][2] });
  155. break;
  156. case 5:
  157. d.push_back('R');
  158. c.push_back({ c[i][0] + 1, c[i][1], c[i][2] });
  159. break;
  160. }
  161. }
  162.  
  163. int nrg = proteinCubicEnergy(seq, d, c);
  164. if (nrg < bestPro) {
  165. mtx.lock();
  166. bestPro = nrg;
  167. cout << "Cas: " << t << "s\t"
  168. << "Hitrost: " << threadSpeed << "\t"
  169. << "Rezultat: " << nrg << endl
  170. << "Sekvenca: { ";
  171. for (int i = 0; i < seq.size(); i++) {
  172. cout << (char)seq[i] << " ";
  173. }
  174. cout << "}" << endl;
  175. mtx.unlock();
  176. }
  177. }
  178.  
  179. double completionTime(clock_t cl) {
  180. return (double)(clock() - cl) / CLOCKS_PER_SEC;
  181. }
  182.  
  183. int main(int argc, char** argv) {
  184. if (argc != 5) {
  185. vector<int> s = { 'H', 'P', 'H', 'H', 'P', 'H', 'P', 'H', 'H' };
  186. vector<int> d = { 'B', 'R', 'D', 'F', 'U', 'R', 'B', 'D' };
  187. vector<vector<int>> c = { { 0,0,0 },{ 0,0,-1 },{ 1,0,-1 },{ 1,-1,-1 },{ 1,-1,0 },{ 1,0,0 },{ 2,0,0 },{ 2,0,-1 },{ 2,-1,-1 } };
  188. cout << "Invalid number of arguments" << endl << "DEMO: " << endl;
  189. cout << "SEQ: " << sequentialEnergy({1,1,1,1,-1,-1,1,-1}) << endl;
  190. cout << "PRO: " << proteinCubicEnergy(s, d, c) << endl;
  191. }
  192. else if (strcmp(argv[1], "seq") && strcmp(argv[1], "protein")) {
  193. cout << "Invalid arguments" << endl;
  194. }
  195. else if (!strcmp(argv[1], "seq")) {
  196. cout << "SEQ" << endl;
  197. bestSeq = INT_MAX;
  198.  
  199. int seqSize = atoi(argv[2]);
  200. double time = atoi(argv[3]);
  201. int threadNum = atoi(argv[4]);
  202. int threadSpeed = 0;
  203. thread *threads = new thread[threadNum];
  204. clock_t cl = clock();
  205.  
  206. while (completionTime(cl) < time) {
  207. for (int i = 0; i < threadNum; i++) {
  208. threads[i] = thread(threadSeq, seqSize, completionTime(cl), threadSpeed);
  209. }
  210. for (int i = 0; i < threadNum; i++) {
  211. threads[i].join();
  212. }
  213. if (completionTime(cl) < 1.0) {
  214. threadSpeed += 1;
  215. }
  216. }
  217. }
  218. else {
  219. cout << "PROTEIN" << endl;
  220. bestPro = INT_MAX;
  221.  
  222. vector<int> seq;
  223. for (int i = 0; argv[2][i] != '\0'; i++) {
  224. seq.push_back(argv[2][i]);
  225. }
  226. double time = atoi(argv[3]);
  227. int threadNum = atoi(argv[4]);
  228. int threadSpeed = 0;
  229. thread *threads = new thread[threadNum];
  230. clock_t cl = clock();
  231.  
  232. while (completionTime(cl) < time) {
  233. for (int i = 0; i < threadNum; i++) {
  234. threads[i] = thread(threadProtein, seq, completionTime(cl), threadSpeed);
  235. }
  236. for (int i = 0; i < threadNum; i++) {
  237. threads[i].join();
  238. }
  239. if (completionTime(cl) < 1.0) {
  240. threadSpeed += 1;
  241. }
  242. }
  243. }
  244.  
  245. system("PAUSE");
  246. return 0;
  247. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement