-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulator.cpp
190 lines (153 loc) · 6.11 KB
/
simulator.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
#include <iostream>
#include <fstream>
#include <unordered_map>
#include <regex>
#include <math.h>
#include "utils.cpp"
unsigned num_of_var, num_of_clauses;
std::vector<std::vector<std::string>> expressions;
std::vector<std::string> sim_var;
void parseMacFile(std::istream& input_file){
std::string line;
int rows;
std::unordered_map<int, std::string> row_var_map;
std::regex row_init_pattern(R"(\b(\d+)\s+(x\d+|-x\d+)\b)");
std::regex memristor_init_pattern(R"(\b(\d+)\s(True|False)\b)");
std::regex sim_pattern (R"(c(\s+)sim(\s+)((\d+)(\s*),?(\s*))+)");
std::vector<std::vector<bool>> memory;
// initialise all memristors to False
while(std::getline(input_file, line)){
if(line.empty()) continue;
if(line[0u] == 'c'){
if(std::regex_match(line,sim_pattern)){
std::cout << line;
std::smatch match;
// if (std::regex_search(line, match, std::regex("\\d+"))) {
// for (auto num : match) {
// sim_var.push_back(num);
// }
// }
std::regex digit_pattern("\\d+"); // Pattern to match one or more digits
// Search for all occurrences of digits in the string
std::sregex_iterator iter(line.begin(), line.end(), digit_pattern);
std::sregex_iterator end;
for (; iter != end; ++iter) {
sim_var.push_back('x'+(*iter)[0].str()); // Add matched digits to the list
}
//store it in a list
std::cout << "Extracted Numbers:" << std::endl;
for (const auto& num : sim_var) {
std::cout << num << std::endl;
}
}
continue;
}
if(line[0u] == 'h'){
// If needed add a header line
std::istringstream headerStream(line);
std::string p;
headerStream >> p >> num_of_var >> num_of_clauses;
rows = num_of_var * 2;
std::cout << "num of clauses " << num_of_clauses << std::endl;
memory.resize(rows, std::vector<bool>(num_of_clauses, false));
continue;
}
// std::vector<std::vector<bool>> memory(rows, std::vector<bool>(num_of_clauses, false));
// parse 1st line and create a row variable map
std::sregex_iterator iter(line.begin(), line.end(), row_init_pattern);
std::sregex_iterator end;
while(iter != end){
std::smatch match = *iter;
// std::cout << "Row init match: "<< match.str() <<std::endl;
row_var_map[std::stoi(match[1])] = match[2];
++iter;
}
//Meristor initialisation
std::sregex_iterator iter1(line.begin(), line.end(), memristor_init_pattern);
int row_num;
bool bypass = true;
while(iter1 != end){
std::smatch match = *iter1;
++iter1;
if(bypass){
row_num = std::stoi(match[1]);
bypass = false;
continue;
}
// std::cout << "memristor init: "<< row_num <<"x" << match[1] <<std::endl;
memory[row_num][std::stoi(match[1])] = true;
}
// for (const auto& row : memory) {
// for (int value : row) {
// std::cout << value << " ";
// }
// std::cout << std::endl;
// }
}
// Clause extraction
for(int col=0; col< num_of_clauses; ++col){
std::vector<std::string> expression;
for(size_t row=0;row< 2*num_of_var; ++row){
if(memory[row][col]){
expression.push_back(row_var_map[row]);
}
}
expressions.push_back(expression); // x1 x3 -x4
}
prettyPrintVector(expressions);
}
bool checkVar(std::string input){
std::regex pattern(R"((-?)x(\d+))");
std::smatch match;
if (std::regex_match(input, match, pattern)) {
bool isNegated = (match[1].str() == "-");
return !isNegated; // Return true if not negated, false if negated
}
return false;
}
void simulate(){
// generate input combinations for 2^n and assign it to variables
// Then evaluate the expression using or operator
std::map<std::string, std::vector<bool>> testData;
testData = generateTestData(num_of_var);
int num = pow(2, num_of_var);
std::cout << "num pow: " << num << std::endl ;
for (int i =0; i < num; i++){
std::cout<<"-------*Simulation "<< i << " -------"<< std::endl;
bool result = true;
// bool skip_iteration = true;
// for (size_t j = 1; j < sim_var.size(); ++j) {
// if (testData[sim_var[j]][i] != testData[sim_var[j + 1]][i]) {
// skip_iteration = false;
// break;
// }
// }
// if(skip_iteration){
// std::cout << "Skipping iteration: "<< i << std::endl;
// continue;
// }
for (std::vector<std::string> vect1D : expressions)
{
std::cout << "Clause ";
bool col_result = false;
for (std::string var : vect1D)
{
std::cout<<"var crazy "<<var;
std::cout<<"var crazyyy "<<testData[var][i];
bool value = checkVar(var) ? testData[var][i] : !testData[var.substr(1)][i] ;
// std::cout << var << " " << value << " ";
col_result = col_result || value;
}
result = result && col_result; // check this
// std::cout<< " clause end**";
// std::cout <<std::endl;
}
std::cout<< "result: " << result << " "<<std::endl;
if (result) {
std::cout << "Test Data for Simulation " << i << ":" << std::endl;
for (const auto& entry : testData) {
std::cout << entry.first << ": " << entry.second[i] << std::endl;
}
}
}
}