Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // PV_energy.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <iostream>
- #include <stdio.h>
- #include <vector>
- #include <stdlib.h>
- #include <time.h>
- #include <thread>
- #include <ctime>
- #include <string>
- #include <mutex>
- using namespace std;
- mutex mtx;
- static int bestSeq;
- static int bestPro;
- vector<int> calcCi(vector<int> c, int i, int di) {
- vector<int> ci;
- // cout << c[0] << " " << c[1] << " " << c[2] << " i: " << i << endl;
- if (i < 1) {
- ci = { 0,0,0 };
- }
- else {
- ci = c;
- switch (di) {
- case 'L':
- ci[0] -= 1;
- break;
- case 'R':
- ci[0] += 1;
- break;
- case 'D':
- ci[1] -= 1;
- break;
- case 'U':
- ci[1] += 1;
- break;
- case 'B':
- ci[2] -= 1;
- break;
- case 'F':
- ci[2] += 1;
- break;
- default:
- cout << "D[i] out of bounds." << endl;
- exit(1);
- }
- }
- return ci;
- }
- bool adjacent(vector<int> a, vector<int> b) {
- if ((a[0] == b[0] && a[1] == b[1]) && (a[2] == b[2] - 1 || a[2] == b[2] + 1) ||
- (a[0] == b[0] && a[2] == b[2]) && (a[1] == b[1] - 1 || a[1] == b[1] + 1) ||
- (a[1] == b[1] && a[2] == b[2]) && (a[0] == b[0] - 1 || a[0] == b[0] + 1)) {
- return true;
- }
- return false;
- }
- int proteinCubicEnergy(vector<int> s, vector<int> d, vector<vector<int>> c) {
- int sum = 0, tmp_sum;
- for (int i = 0; i < s.size() - 2; i++) {
- tmp_sum = 0;
- for (int j = i + 2; j < s.size(); j++) {
- vector<int> ci = calcCi(c[i], i, d[i]);
- vector<int> cj = c[j];
- if (adjacent(ci, cj) && s[i] == s[j] && s[i] == 'H') {
- tmp_sum += 1;
- }
- }
- sum += tmp_sum;
- }
- for (int i = 0; i < c.size() - 1; i++) {
- for (int j = i + 1; j < c.size(); j++) {
- if (c[i] == c[j]) {
- sum += 1000;
- break;
- }
- }
- }
- return -1 * sum;
- }
- int sequentialEnergy(vector<int> arr) {
- int tmp_sum, sum = 0;
- for (int k = 0; k < arr.size() - 1; k++) {
- tmp_sum = 0;
- for (int i = 0; i < arr.size() - (k + 1); i++) {
- tmp_sum += arr[i] * arr[i + (k + 1)];
- }
- sum += tmp_sum*tmp_sum;
- }
- return sum;
- }
- void threadSeq(int seqSize, double t, int threadSpeed) {
- srand(time(NULL));
- vector<int> seq;
- for (int i = 0; i < seqSize; i++) {
- int s = rand() % 2 == 0 ? -1 : 1;
- seq.push_back(s);
- }
- int nrg = sequentialEnergy(seq);
- if (nrg < bestSeq) {
- mtx.lock();
- bestSeq = nrg;
- cout << "Cas: " << t << "s\t"
- << "Hitrost: " << threadSpeed << "\t"
- << "Rezultat: " << nrg << endl
- << "Sekvenca: { ";
- for (int i = 0; i < seqSize; i++) {
- cout << seq[i] << " ";
- }
- cout << "}" << endl;
- mtx.unlock();
- }
- }
- void threadProtein(vector<int> seq, double t, int threadSpeed) {
- srand(time(NULL));
- vector<int> d;
- vector<vector<int>> c;
- c.push_back({ 0,0,0 });
- for (int i = 0; i < seq.size() - 1; i++) {
- vector<int> tmp_pos = c[i];
- switch (rand() % 6) { // 0-F 1-B 2-U 3-D 4-L 5-D
- case 0:
- d.push_back('F');
- c.push_back({ c[i][0], c[i][1], c[i][2] + 1 });
- break;
- case 1:
- d.push_back('B');
- c.push_back({ c[i][0], c[i][1], c[i][2] - 1 });
- break;
- case 2:
- d.push_back('U');
- c.push_back({ c[i][0], c[i][1] + 1, c[i][2] });
- break;
- case 3:
- d.push_back('D');
- c.push_back({ c[i][0], c[i][1] - 1, c[i][2] });
- break;
- case 4:
- d.push_back('L');
- c.push_back({ c[i][0] - 1, c[i][1], c[i][2] });
- break;
- case 5:
- d.push_back('R');
- c.push_back({ c[i][0] + 1, c[i][1], c[i][2] });
- break;
- }
- }
- int nrg = proteinCubicEnergy(seq, d, c);
- if (nrg < bestPro) {
- mtx.lock();
- bestPro = nrg;
- cout << "Cas: " << t << "s\t"
- << "Hitrost: " << threadSpeed << "\t"
- << "Rezultat: " << nrg << endl
- << "Sekvenca: { ";
- for (int i = 0; i < seq.size(); i++) {
- cout << (char)seq[i] << " ";
- }
- cout << "}" << endl;
- mtx.unlock();
- }
- }
- double completionTime(clock_t cl) {
- return (double)(clock() - cl) / CLOCKS_PER_SEC;
- }
- int main(int argc, char** argv) {
- if (argc != 5) {
- vector<int> s = { 'H', 'P', 'H', 'H', 'P', 'H', 'P', 'H', 'H' };
- vector<int> d = { 'B', 'R', 'D', 'F', 'U', 'R', 'B', 'D' };
- 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 } };
- cout << "Invalid number of arguments" << endl << "DEMO: " << endl;
- cout << "SEQ: " << sequentialEnergy({1,1,1,1,-1,-1,1,-1}) << endl;
- cout << "PRO: " << proteinCubicEnergy(s, d, c) << endl;
- }
- else if (strcmp(argv[1], "seq") && strcmp(argv[1], "protein")) {
- cout << "Invalid arguments" << endl;
- }
- else if (!strcmp(argv[1], "seq")) {
- cout << "SEQ" << endl;
- bestSeq = INT_MAX;
- int seqSize = atoi(argv[2]);
- double time = atoi(argv[3]);
- int threadNum = atoi(argv[4]);
- int threadSpeed = 0;
- thread *threads = new thread[threadNum];
- clock_t cl = clock();
- while (completionTime(cl) < time) {
- for (int i = 0; i < threadNum; i++) {
- threads[i] = thread(threadSeq, seqSize, completionTime(cl), threadSpeed);
- }
- for (int i = 0; i < threadNum; i++) {
- threads[i].join();
- }
- if (completionTime(cl) < 1.0) {
- threadSpeed += 1;
- }
- }
- }
- else {
- cout << "PROTEIN" << endl;
- bestPro = INT_MAX;
- vector<int> seq;
- for (int i = 0; argv[2][i] != '\0'; i++) {
- seq.push_back(argv[2][i]);
- }
- double time = atoi(argv[3]);
- int threadNum = atoi(argv[4]);
- int threadSpeed = 0;
- thread *threads = new thread[threadNum];
- clock_t cl = clock();
- while (completionTime(cl) < time) {
- for (int i = 0; i < threadNum; i++) {
- threads[i] = thread(threadProtein, seq, completionTime(cl), threadSpeed);
- }
- for (int i = 0; i < threadNum; i++) {
- threads[i].join();
- }
- if (completionTime(cl) < 1.0) {
- threadSpeed += 1;
- }
- }
- }
- system("PAUSE");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement