forked from nicholasRenninger/dfasat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstream.cpp
168 lines (133 loc) · 5.49 KB
/
stream.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
#include <stdlib.h>
#include <popt.h>
#include "random_greedy.h"
#include "state_merger.h"
#include "evaluate.h"
#include "dfasat.h"
#include <sstream>
#include <iostream>
#include "evaluation_factory.h"
#include <string>
#include "searcher.h"
#include <vector>
#include <cmath>
#include "parameters.h"
int STREAM_COUNT = 0;
int stream_mode(state_merger* merger, parameters* param, ifstream& input_stream) {
// first line has alphabet size and
std::string line;
//std::getline(input_stream, line);
merger->init_apta(string("10 1000"));
// line by line processing
// add items
int i = 0;
int solution = 0;
merger->reset();
std::getline(input_stream, line);
merger->advance_apta(line);
int samplecount = 1;
// hoeffding countfor sufficient stats
int hoeffding_count = (double)(RANGE*RANGE*log2(1/param->delta)) / (double)(2*param->epsilon*param->epsilon);
cout << (RANGE*RANGE*log2(1/param->delta)) << " ";
cout << (2*param->epsilon*param->epsilon);
cout << " therefore, relevant Hoeffding count for " << (double) param->delta << " and " << (float) param->epsilon << " delta/epsilon is " << hoeffding_count << endl;
STREAM_COUNT = hoeffding_count;
refinement_list* all_refs = new refinement_list();
merger->eval->initialize(merger);
int batch = 0;
while (std::getline(input_stream, line)) {
merger->advance_apta(line);
samplecount++;
batch++;
//merger.update_red_blue();
if(samplecount % param->batchsize == 0) {
while( true ) {
// output each micro-batch, keeping at host 10 files
merger->todot();
std::ostringstream oss2;
oss2 << param->dot_file <<"stream_pre_" << samplecount++ % 10 << ".dot";
ofstream output(oss2.str().c_str());
//if(output == NULL) cerr << "Cannot open " << oss2.str().c_str() << " to write intermediate dot output file" << endl;
output << merger->dot_output;
output.close();
refinement_set* refs = merger->get_possible_refinements();
if(refs->empty()){
// we just need more data
// cerr << "no more possible merges" << endl;
break;
}
// the following flags are mostly important for the sat solver
if(merger->red_states.size() > CLIQUE_BOUND){
cerr << "too many red states" << endl;
break;
}
// FIXME
if(merger->get_final_apta_size() <= APTA_BOUND){
cerr << "APTA too small" << endl;
break;
}
// top refinements to compare
refinement* best_ref;
bool found = true;
if(refs->size() > 1) {
std::set<refinement*, score_compare>::iterator top_ref_it = refs->begin();
std::set<refinement*, score_compare>::iterator runner_up_ref_it = refs->begin();
refinement* top_ref = *top_ref_it;
refinement* runner_up_ref = *( ++(runner_up_ref_it));
// enough evidence take best score and compare
while( ! (top_ref_it == refs->end()) ) {
// where's the match;
for(runner_up_ref_it = std::next(top_ref_it); runner_up_ref_it != refs->end() && top_ref->score - runner_up_ref->score < param->epsilon; ++runner_up_ref_it) {
if(runner_up_ref->right != top_ref->right) {
continue;
} else {
if(top_ref->score - runner_up_ref->score > param->epsilon) {
found = true;
break;
} else {
cout << " [" << top_ref->score << ":" << runner_up_ref->score << "] ";
// we are not cnofident in top merge
found = false;
}
}
runner_up_ref = *runner_up_ref_it;
}
// current top score is hoeffding match for this blue state
best_ref = top_ref;
if(found) break;
// check if next best score is hoeffding match
top_ref = *(++top_ref_it);
} // end find matching
} else {
// execute if enough evidence?
best_ref = *(refs->begin());
} // end only one ref
// what if we have no confident in any proposed refinement?
if(found==false) {
best_ref = *(refs->begin());
//cerr << "no option" << endl;
}
if(batch > 1) {
cerr << "b" << batch << " ";
}
best_ref->print_short();
cerr << " ";
best_ref->doref(merger);
all_refs->push_front(best_ref);
batch = 0;
for(refinement_set::iterator it = refs->begin(); it != refs->end(); ++it){
if(*it != best_ref) delete *it;
}
delete refs;
}
} // if batchsize
} // while input
// do one last step of batch?
// remaining input not used
cerr << "b" << batch << " ";
cout << endl;
int size = merger->get_final_apta_size();
int red_size = merger->red_states.size();
cout << endl << "found intermediate solution with " << size << " and " << red_size << " red states" << endl;
return 0;
}