-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
executable file
·100 lines (87 loc) · 2.98 KB
/
main.cpp
File metadata and controls
executable file
·100 lines (87 loc) · 2.98 KB
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
#include <iostream>
#include <fstream>
#include <memory>
#include <vector>
#include <atomic>
#include <chrono>
#include "formula.h"
#include "read.h"
using namespace std;
// Test if a formula satisfies all examples
bool testFormula(const Formula& formula, const vector<Example>& examples) {
for (const Example& example : examples) {
uint32_t result = formula.evaluate(example.inputs);
if (result != example.output) {
return false;
}
}
return true;
}
// Generate formulas until size n
void generateFormulas(int n, const vector<Example>& examples) {
vector<vector<shared_ptr<Formula>>> formulas(n + 1);
// Initialization
for (int i = 0; i < numVar; ++i) {
std::shared_ptr<Formula> formula = make_shared<Formula>(Variable(i));
formulas[1].push_back(formula);
if (testFormula(*formula, examples)) {
formula->printFormula();
return;
}
}
// Generation
atomic<bool> found(false);
for (int size = 2; size <= n; ++size) {
if (found) break;
cout << "Starting generation of formulas of size " << size << endl;
// Unary operators
#pragma omp parallel for
for (const std::shared_ptr<Formula>& formula : formulas[size - 1]) {
if (found) continue;
vector<UnOp> ops = { UnOp::Not };
for (UnOp op : ops) {
std::shared_ptr<Formula> newFormula = make_shared<Formula>(UnaryOperation(op, formula));
#pragma omp critical
formulas[size].push_back(newFormula);
if (testFormula(*newFormula, examples)) {
#pragma omp critical
newFormula->printFormula();
found = true;
#pragma omp flush(found)
}
}
}
// Binary operators
for (int k = 1; k <= size - 2; ++k) {
#pragma omp parallel for collapse(2)
for (const std::shared_ptr<Formula>& left : formulas[k]) {
for (const std::shared_ptr<Formula>& right : formulas[size - k - 1]) {
if (found) continue;
vector<BinOp> ops = { BinOp::Plus, BinOp::Minus, BinOp::Mult, BinOp::And, BinOp::Or, BinOp::Xor };
for (BinOp op : ops) {
std::shared_ptr<Formula> newFormula = make_shared<Formula>(BinaryOperation(op, left, right));
#pragma omp critical
formulas[size].push_back(newFormula);
if (testFormula(*newFormula, examples)) {
#pragma omp critical
newFormula->printFormula();
found = true;
#pragma omp flush(found)
}
}
}
}
}
}
return;
}
int main() {
const string filename = "../benchmarks/test.json";
vector<Example> examples = readExamples(filename);
auto start = std::chrono::high_resolution_clock::now();
generateFormulas(11, examples);
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> duration = end - start;
cout << "Time elapsed: " << duration.count() << " seconds" << endl;
return 0;
}