-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathG5-test.cpp
More file actions
342 lines (305 loc) · 7.76 KB
/
G5-test.cpp
File metadata and controls
342 lines (305 loc) · 7.76 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
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
#include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
struct BNode{
std::string val;
BNode* left;
BNode* right;
};
struct oneandzero{
std::vector<std::string> one;
std::vector<std::string> zero;
};
BNode* bt_node(std::string e,BNode* l,BNode* r){
BNode* tmp = new BNode;
tmp -> val = e;
tmp -> left = l;
tmp -> right = r;
return tmp;
}
std::vector<std::string> shorthand(std::vector<std::string> r){
std::vector<std::string> tmp;
for(int y=r[0].size()-1; y>=0;y--){
int i=0;
while(i<r.size()){
int j = i+1,check = 1;
while((j < r.size())&&(check!=-1)){
int x =0;
check=-1;
while((x != r[0].size())&&(check == -1)){
if((r[i][x]!=r[j][x])&&(x!=y)){
check = 0;
}
x++;
}
if(check == -1){
r[i][y]='x';
r[j]="";
}
j++;
if(r[j]==""){
j++;
}
}
if(r[i]!=""){
tmp.push_back(r[i]);
}
i++;
if(r[i]==""){
i++;
}
}
r = tmp;
tmp.clear();
}
return r;
}
int order(std::vector<std::string> in){
int out = -1,min = -1,max;
if(in.size()==0){
return -2;
}
for(int i=0;i<in[0].size();i++){
int counter = 0,n1=0,n0=0;
if(in[0][i]!='s'){
for(int j=0;j<in.size();j++){
if(in[j][i]=='x'){
counter++;
}else if(in[j][i]=='1'){
n1++;
}else{
n0++;
}
}
if(n1 < n0){
n1 = n0;
}
if((counter<min)||(min==-1)){
min=counter;
out = i;
max = n1;
}else if((counter == min)&&(n1>max)){
min=counter;
out = i;
max = n1;
}
}
}
if(min==in.size()){
return -1;
}
return out;
}
oneandzero LRshrink(std::vector<std::string> r,int ord){
oneandzero out;
std::string tmp;
for(int i=0;i<r.size();i++){
tmp = r[i];
tmp[ord] = 's';
if(r[i][ord]=='1'){
out.one.push_back(tmp);
}else if(r[i][ord]=='0'){
out.zero.push_back(tmp);
}else{
out.one.push_back(tmp);
out.zero.push_back(tmp);
}
}
return out;
}
BNode* Build_tree(std::vector<std::string> direction,int ord){
std::string name;
if(ord==-1){
return bt_node("1",NULL,NULL);
}
if(ord==-2){
return bt_node("0",NULL,NULL);
}
oneandzero next = LRshrink(direction,ord);
name = "x" + std::to_string(ord + 1);
return bt_node(name,Build_tree(next.zero,order(next.zero)),Build_tree(next.one,order(next.one)));
}
BNode* build_bt(const std::vector<std::string>& fvalues){
std::vector<std::string> r ;
BNode* t = bt_node("0",NULL,NULL);
if(fvalues.size()!=0){
r = shorthand(fvalues);
t = Build_tree(r,order(r));
}
return t;
}
std::string one_above(std::string r){
int check = -1,k=r.size()-1,end =0;
while(check == -1){
if(k<0){
r="";
check = 0;
}else if(r[k]=='0'){
r[k] = '1';
check = 0;
}else{
r[k] = '0';
}
k--;
}
return r;
}
//creates random inputs
std::string random(std::string r){
int k=0;
while(k<r.size()){
if(((" %d ", rand())%2)==0){
r[k] = '1';
}else{
r[k] = '0';
}
k++;
}
return r;
}
//counts up through possible inputs
std::vector<std::string> counter(int const size){
std::vector<std::string> allon = {""};
for(int i = 0;i<size;i++){
allon[0].push_back('0');
}
int j =0;
while(allon.size()!=pow(2,size)){
int check = -1,k=size-1;
std::string r = allon[j];
r = one_above(r);
allon.push_back(r);
j++;
}
return allon;
}
oneandzero posibilities(std::string pos,std::vector<std::string> allone){
oneandzero out;
for(int j=0;j<pos.size();j++){
if(pos[j]=='1'){
out.one.push_back(allone[j]);
}
else{
out.zero.push_back(allone[j]);
}
}
return out;
}
// do not alter the following function
// this function converts e.g. std::string "x3" to int 2
int label_to_idx(const std::string& label){
std::string out;
for(int i = 1; i < label.size(); i++){
out.push_back(label[i]);
}
return std::stoi(out) - 1;
}
void destroy_tree(BNode* T){
if(T != NULL){
destroy_tree(T->left);
destroy_tree(T->right);
delete T;
}
}
// do not alter the following function
std::string eval_bt(BNode* bt, const std::string& input){
if( (bt->left == NULL) && (bt->right == NULL) ){
return bt->val;
}
else{
int idx = label_to_idx(bt->val);
std::string input_idx;
input_idx.push_back(input[idx]);
if(input_idx == "0"){
return eval_bt(bt->left, input);
}
else{
return eval_bt(bt->right, input);
}
}
}
// do not alter the following function
int n_nodes_bt(BNode* t){
if(t == NULL){
return 0;
}
else{
return 1 + n_nodes_bt(t->left) + n_nodes_bt(t->right);
}
}
class BoolTree{
public:
BoolTree(const std::vector<std::string>& fvalues){
t = build_bt(fvalues);
}
std::string eval(const std::string& s){
return eval_bt(t, s);
}
int n_nodes(){
return n_nodes_bt(t);
}
~BoolTree(){
destroy_tree(t);
}
private:
BNode* t;
};
// the main provided below must work correctly
// with your implementation for the code above
// however you can change it as you wish for your own testing
// and your changes won't be considered for the assessment
// (in your submission you can also have an empty main or no main at all)
int main(){
int treesize;
srand(time(0));
std::cout<<"How many different variables do you want to test?"<<std::endl;
oneandzero test;
std::cin>>treesize;
std::vector<std::string> fv,allone = counter(treesize);
std::string ran,pos = {""};
int end = 0;
for(int i = 0;i<pow(2,treesize);i++){
pos.push_back('0');
}
double nodes=0,roll=0;
int check =0;
int i=0;
while((pos!="")&&(i!=1000000)){
//ran =random(pos);
//std::cout<<ran<<" ";
test=posibilities(pos,allone);
fv=test.one;
BoolTree ft(fv);
for(int j=0;j<test.one.size();j++){
if(ft.eval(test.one[j])!="1"){
check++;
std::cout<<"??"<<i<<"??";
}
}
for(int j=0;j<test.zero.size();j++){
if(ft.eval(test.zero[j])!="0"){
check++;
std::cout<<"!!"<<i<<"!!";
}
}
if(i%10000==0){
//std::cout<<"rolling average: "<<nodes/i<<std::endl;
//nodes=0;
//i=0;
srand(time(0));
}
nodes = nodes + ft.n_nodes();
pos = one_above(pos);
i++;
}
std::cout<<std::endl<<"The average number of nodes is:"<<nodes/i<<" The normal average is:"<<pow(2,treesize+1)-1<<" ran:"<<i<<" times"<<std::endl;
if(check==0){
std::cout<<"Check passed"<<std::endl;
}else{
std::cout<<"Check Failed:"<<check<<" times"<<std::endl;
}
}