-
Notifications
You must be signed in to change notification settings - Fork 0
/
project.cpp
376 lines (326 loc) · 11.9 KB
/
project.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#include <iostream>
#include <pthread.h>
#include <vector>
#include <string>
#include <regex>
#include <memory>
#include <map>
#include <chrono>
#include <csignal>
#include <chrono>
#include <sys/time.h>
#include "minisat/core/SolverTypes.h"
#include "minisat/core/Solver.h"
using namespace std;
using namespace Minisat;
struct ThreadArg{
int v;
vector < int > edges;
string results;
std::chrono::time_point<std::chrono::high_resolution_clock> start_time;
std::chrono::time_point<std::chrono::high_resolution_clock> end_time;
std::chrono::microseconds duration_time;
};
long threadCalc(ThreadArg* args){
auto start = std::chrono::time_point_cast<std::chrono::milliseconds>(args->start_time).time_since_epoch().count();
auto end = std::chrono::time_point_cast<std::chrono::milliseconds>(args->end_time).time_since_epoch().count();
long time = end - start;
return time;
}
int vertex;
vector < int > edges;
int get_v(string input) {
regex pattern("^[V]\\s(\\d+)(\\s)*$");
smatch matches;
return (regex_match(input, matches, pattern)) ? stoi(matches[1]) : -1;
}
vector < int > get_e(string input) {
vector < int > temp_edge;
regex pattern("\\d+");
smatch matches;
while (regex_search(input, matches, pattern)) {
for (string match: matches) {
temp_edge.push_back(stoi(match));
}
input = matches.suffix().str();
}
return temp_edge;
}
// CNF-SAT
void* CNFSAT(void* args) {
//cout << "CNFSAT Thread started" << endl;
ThreadArg* inputArgs = static_cast<ThreadArg*>(args);
vertex = inputArgs->v;
edges = inputArgs->edges;
// start timer for runtime calculation
inputArgs->start_time = std::chrono::high_resolution_clock::now();
auto start_time = std::chrono::high_resolution_clock::now();
std::chrono::time_point<std::chrono::high_resolution_clock> timer_start = std::chrono::high_resolution_clock::now();
// time out setting in us (60,000,000)us = 1 minute
int timeout = 500000;
std::unique_ptr < Solver > solver(new Solver());
for (int counter = 1; counter < vertex + 1; counter++) {
// every loop check time
auto timer_now = std::chrono::high_resolution_clock::now();
auto elapsed_seconds = std::chrono::duration_cast<std::chrono::microseconds>(timer_now - timer_start);
if (elapsed_seconds.count() >= timeout) {
//cout << "timeout set for : " << timeout << "seconds and current timer is running for " << elapsed_seconds.count() << endl;
inputArgs->results = "CNF-SAT-VC: timeout";
pthread_exit(nullptr);
}
vec < Lit > temp;
// Crete new variables for all
for (int i = 0; i < counter; i++) {
for (int j = 0; j < vertex; j++) {
temp.push(mkLit(solver -> newVar()));
}
}
// C1. At least one vertex is the ith vertex in the vertex cover
for (int i = 0; i < counter; i++) {
vec < Lit > c1;
for (int j = 0; j < vertex; j++) {
c1.push(temp[(i * vertex) + j]);
}
solver -> addClause(c1);
c1.clear();
}
// C2. No one vertex can appear twice in a vertex cover.
for (int i = 0; i < vertex; i++) {
for (int j = 0; j < counter - 1; j++) {
for (int l = j + 1; l < counter; l++) {
solver -> addClause(~temp[(j * vertex) + i], ~temp[(l * vertex) + i]);
}
}
}
// C3. No more than one vertex appears in the mth position of the vertex cover
for (int i = 0; i < counter; i++) {
for (int j = 0; j < vertex - 1; j++) {
for (int l = j + 1; l < vertex; l++) {
solver -> addClause(~temp[(i * vertex) + j], ~temp[(i * vertex) + l]);
}
}
}
// C4. Every edge is incident to at least one vertex in the vertex cover
for (size_t i = 1; i < edges.size(); i += 2) {
vec < Lit > c4;
for (int j = 0; j < counter; j++) {
c4.push(temp[(j * vertex) + (edges[i - 1] - 1)]);
c4.push(temp[(j * vertex) + (edges[i] - 1)]);
}
solver -> addClause(c4);
c4.clear();
}
/* Attempted optimization -- not successful
// C5. Avoid reciprocal entries into vertex cover (full order relationship)
for (int i = 0; i < vertex; i++){
for (int j = 0; j < counter -1; j++){
for (int x = i+1; x < vertex; x++){
for (int y = 0; y < j; y++){
//a[i][j] v ~ a[x][y]
printf("a[%d][%d]v~a[%d][%d] || ",i,j,x,y);
cout << endl;
solver -> addClause(temp[(i * vertex) + j], ~temp[(x * vertex) + y]); // did i access a[i][j] v ~a[x][y] correctly?
// a[x][y] is supposed to be a[{i+1 to vertex}][{0 to j-1}]
}
}
}
}
*/
// Result
vector < int > result;
if (solver -> solve()) {
for (int i = 0; i < counter; i++) {
for (int j = 0; j < vertex; j++) {
if (!toInt(solver -> modelValue(temp[(i * vertex) + j]))) {
result.push_back(j + 1);
}
}
}
sort(result.begin(), result.end());
//copy(result.begin(), result.end() - 1, ostream_iterator < int > (cout, " "));
//cout << result.back() << endl;
string resultString;
resultString += "CNF-SAT-VC: ";
for (size_t i = 0; i < result.size(); ++i) {
resultString += std::to_string(result[i]);
// Add space if not the last element
if (i < result.size() - 1) {
resultString += ",";
}
}
inputArgs->results = resultString;
// stop timer for runtime calculation
inputArgs->end_time = std::chrono::high_resolution_clock::now();
auto end_time = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time);
inputArgs->duration_time = duration;
pthread_exit(nullptr); //required for threads, instead of return 0
}
solver.reset(new Solver());
}
return nullptr;
}
// APPROX-VC1
void* VC1(void* args) {
ThreadArg* inputArgs = static_cast<ThreadArg*>(args);
vector<int> edgesVC1 = inputArgs->edges;
vector<int> edgeCopy = edgesVC1;
vector<int> VC1;
map<int, vector<int> > edgeMap;
// start timer for runtime calculation
auto start_time = std::chrono::high_resolution_clock::now();
inputArgs->start_time = std::chrono::high_resolution_clock::now();
for (size_t i = 0; i < edgeCopy.size() - 1; i+=2) {
edgeMap[edgeCopy[i]].push_back(edgeCopy[i + 1]);
edgeMap[edgeCopy[i + 1]].push_back(edgeCopy[i]);
}
// find highest edge, add to vector VC
while (!edgeMap.empty()){
size_t maxSize = 0;
int maxKey = 0;
vector<int> maxVal;
for (auto it = edgeMap.begin(); it != edgeMap.end(); ++it){
int key = it->first;
vector<int> valVC1 = it->second;
if (valVC1.size() > maxSize){
maxSize = valVC1.size();
maxKey = key;
maxVal = valVC1;
}
}
VC1.push_back(maxKey);
// loop through edges attached to highest incident edge and remove from map
for(auto& u: maxVal){
auto& edgesList = edgeMap[u];
edgesList.erase(std::remove(edgesList.begin(), edgesList.end(), maxKey), edgesList.end());
// If the incident edge list becomes empty, remove it from the map
if (edgesList.empty()) {
edgeMap.erase(u);
}
}
edgeMap.erase(maxKey);
}
// organize VC and return string output
std::sort(VC1.begin(), VC1.end());
std::string result;
result += "APPROX-VC-1: ";
for (size_t i = 0; i < VC1.size(); ++i) {
result += std::to_string(VC1[i]);
if (i < VC1.size() - 1) {
result += ",";
}
}
inputArgs->results = result;
// stop timer for runtime calculation
inputArgs->end_time = std::chrono::high_resolution_clock::now();
auto end_time = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time);
inputArgs->duration_time = duration;
pthread_exit(nullptr);
}
// APPROX-VC2
void* VC2(void* args) {
//cout << "VC2 Thread started" << endl;
ThreadArg* inputArgs = static_cast<ThreadArg*>(args);
vector<int> edgesVC2 = inputArgs->edges;
vector<int> VC2;
// start timer for runtime calculation
inputArgs->start_time = std::chrono::high_resolution_clock::now();
auto start_time = std::chrono::high_resolution_clock::now();
while (!edgesVC2.empty()){
// store values of edge <x,y>
int vec1 = edgesVC2[0];
int vec2 = edgesVC2[1];
// add x and y to VC
VC2.push_back(edgesVC2[0]);
VC2.push_back(edgesVC2[1]);
// remove x and y
edgesVC2.erase(edgesVC2.begin());
edgesVC2.erase(edgesVC2.begin());
vector<int> eraseVec;
for(size_t i = 0; i < edgesVC2.size(); i++){
if(edgesVC2[i] == vec1 || edgesVC2[i] == vec2){
if(i == 0){
eraseVec.push_back(i);
eraseVec.push_back(i+1);
} else if(i == 1){
eraseVec.push_back(i);
eraseVec.push_back(i-1);
} else if (i >= 2 && i%2 == 0){
eraseVec.push_back(i);
eraseVec.push_back(i + 1);
} else if (i >= 2 && i%2 == 1){
eraseVec.push_back(i);
eraseVec.push_back(i-1);
}
}
}
std::sort(eraseVec.begin(), eraseVec.end());
for (auto it = eraseVec.rbegin(); it != eraseVec.rend(); ++it) {
edgesVC2.erase(edgesVC2.begin() + *it);
}
}
// organize VC and return string output
std::sort(VC2.begin(), VC2.end());
std::string result;
result += "APPROX-VC-2: ";
for (size_t i = 0; i < VC2.size(); ++i) {
result += std::to_string(VC2[i]);
if (i < VC2.size() - 1) {
result += ",";
}
}
inputArgs->results = result;
// stop timer for runtime calculation
inputArgs->end_time = std::chrono::high_resolution_clock::now();
auto end_time = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time);
inputArgs->duration_time = duration;
pthread_exit(nullptr);
}
void* processInput(string userInput) {
switch (userInput[0]) {
case 'V':
vertex = get_v(userInput);
break;
case 'E':
edges = get_e(userInput);
pthread_t CNFSAT_Thread, VC1_Thread, VC2_Thread;
ThreadArg CNFSAT_Args = {vertex, edges, "CNF-SAT-VC:"};
ThreadArg VC1_Args = {vertex, edges, "APPROX-VC-1:"};
ThreadArg VC2_Args = {vertex, edges, "APPROX-VC-2:"};
// create thread for each algorithm
pthread_create(&CNFSAT_Thread, nullptr, CNFSAT, &CNFSAT_Args);
pthread_create(&VC1_Thread, nullptr, VC1, &VC1_Args);
pthread_create(&VC2_Thread, nullptr, VC2, &VC2_Args);
struct timespec ts = {0,0};
//add time to timeout
ts.tv_sec += 120;
// wait for threads to finish
pthread_join(VC1_Thread, nullptr);
pthread_join(VC2_Thread, nullptr);
pthread_join(CNFSAT_Thread, nullptr);
cout << CNFSAT_Args.results << endl;
//cout << "CNF-SAT-VC runtime: " << CNFSAT_Args.duration_time.count() << "us" << endl;
cout << VC1_Args.results << endl;
//cout << "APPROX-VC-1 runtime: " << VC1_Args.duration_time.count() << "us" << endl;
cout << VC2_Args.results << endl;
//cout << "APPROX-VC-2 runtime: " << VC2_Args.duration_time.count() << "us" << endl;
break;
}
return nullptr;
}
void* IO(void* args) {
while (!cin.eof()) {
string userInput;
getline(cin, userInput);
processInput(userInput);
}
return nullptr;
}
int main(int argc, char ** argv) {
pthread_t io_Thread;
pthread_create(&io_Thread, nullptr, IO, nullptr);
pthread_join(io_Thread, nullptr);
return 0;
}