forked from nicholasRenninger/dfasat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluate.cpp
315 lines (232 loc) · 8.39 KB
/
evaluate.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
#include "state_merger.h"
#include "evaluate.h"
#include "evaluation_factory.h"
#include <stdlib.h>
#include <math.h>
#include <vector>
#include <map>
#include <stdio.h>
#include <gsl/gsl_cdf.h>
#include <string>
#include <sstream>
#include "parameters.h"
evaluation_data::evaluation_data(){
node_type = -1;
undo_pointer = 0;
}
void evaluation_data::read_from(int seq_nr, int index){
read_from( inputdata::get_type(seq_nr),
index,
inputdata::get_length(seq_nr),
inputdata::get_symbol(seq_nr, index),
inputdata::get_data(seq_nr, index) );
};
void evaluation_data::read_to(int seq_nr, int index){
read_to( inputdata::get_type(seq_nr),
index,
inputdata::get_length(seq_nr),
inputdata::get_symbol(seq_nr, index),
inputdata::get_data(seq_nr, index) );
};
void evaluation_data::read_from(int type, int index, int length, int symbol, string data){
};
void evaluation_data::read_to(int type, int index, int length, int symbol, string data){
if(length == index+1){
node_type = type;
}
};
void evaluation_data::update(evaluation_data* right){
//cerr << "read " << endl;
if(node_type == -1){
node_type = right->node_type;
undo_pointer = right;
}
};
void evaluation_data::undo(evaluation_data* right){
if(right == undo_pointer){
node_type = -1;
undo_pointer = 0;
}
};
// DOT output functions
void evaluation_data::print_state_label(iostream& output, apta* aptacontext){
};
void evaluation_data::print_state_style(iostream& output, apta* aptacontext){
};
void evaluation_data::print_transition_label(iostream& output, int symbol, apta* aptacontext){
};
// JSON output functions
void evaluation_data::print_state_properties(iostream& output, apta* aptacontext) {
};
void evaluation_data::print_transition_properties(iostream& output, int symbol, apta* aptacontext) {
};
// this should have a pair, set<pair<int, eval_data*>>
void evaluation_data::print_transition_style(iostream& output, set<int> symbols, apta* aptacontext){
};
int evaluation_data::sink_type(apta_node* node){
return -1;
};
bool evaluation_data::sink_consistent(int type){
return true;
};
/* defa */
evaluation_function::evaluation_function() {
};
void evaluation_function::set_params(string params) {
this->evalpar = params;
};
void evaluation_function::set_safetydfa(SafetyDFA* safetyDFA) {
this->safetyDFA = safetyDFA;
}
void evaluation_function::set_checkNodeTypeConsistent(int checkNodeTypeConsistent){
this->checkNodeTypeConsistent = checkNodeTypeConsistent;
}
/* default evaluation, count number of performed merges */
bool evaluation_function::consistency_check(evaluation_data* l, evaluation_data* r){
if(l->node_type != -1 && r->node_type != -1 && l->node_type != r->node_type) return false;
return true;
};
bool evaluation_function::consistent(state_merger *merger, apta_node* left, apta_node* right){
if(inconsistency_found) return false;
if(checkNodeTypeConsistent && right->type != right->type){ inconsistency_found = true; return false; }
if(left->data->node_type != -1 && right->data->node_type != -1 && left->data->node_type != right->data->node_type){ inconsistency_found = true; return false; }
if (safetyDFA->preCheckSafety(merger->aut, left, right) == false) { inconsistency_found = true; return false; }
return true;
};
void evaluation_function::update_score(state_merger *merger, apta_node* left, apta_node* right){
num_merges += 1;
merged_left_states.insert(left);
};
void evaluation_function::undo_update(state_merger *merger, apta_node* left, apta_node* right){
};
bool evaluation_function::compute_consistency(state_merger *merger, apta_node* left, apta_node* right){
if (inconsistency_found) return false;
return satisfy_safety(merger, left, right);
};
bool evaluation_function::satisfy_safety(state_merger *merger, apta_node* left, apta_node* right){
return safetyDFA->postCheckSafety(merger->aut, left, right);
};
int evaluation_function::compute_score(state_merger *merger, apta_node* left, apta_node* right){
return num_merges;
};
void evaluation_function::reset(state_merger *merger){
inconsistency_found = false;
num_merges = 0;
merged_left_states.clear();
};
void evaluation_function::update(state_merger *merger){
};
void evaluation_function::initialize(state_merger *merger){
safetyDFA->initializeAPTA(merger->aut);
};
/* When is an APTA node a sink state?
* sink states are not considered merge candidates
*
* accepting sink = only accept, accept now, accept afterwards
* rejecting sink = only reject, reject now, reject afterwards
* low count sink = frequency smaller than STATE_COUNT
* stream sink = frequency smaller than hoeffding says
**** */
bool is_low_count_sink(apta_node* node){
node = node->find();
return node->size < SINK_COUNT;
}
bool is_stream_sink(apta_node* node){
node = node->find();
cerr << "stream sink check ";
return node->size < STREAM_COUNT;
}
/*int evaluation_data::sink_type(apta_node* node){
if(!USE_SINKS) return -1;
if (is_low_count_sink(node)) return 0;
if (is_stream_sink(node)) return 3;
//if (is_accepting_sink(node)) return 1;
//if (is_rejecting_sink(node)) return 2;
return -1;
};*/
bool evaluation_data::sink_consistent(apta_node* node, int type){
if(!USE_SINKS) return true;
if(type == 0) return is_low_count_sink(node);
if(type == 3) return is_stream_sink(node);
//if(type == 1) return is_accepting_sink(node);
//if(type == 2) return is_rejecting_sink(node);
return true;
};
int evaluation_data::num_sink_types(){
if(!USE_SINKS) return 0;
// accepting, rejecting, and low count (old)
// low count and stream
return 2;
};
/* read functions*/
void evaluation_function::init(string data, state_merger* merger) {
// alphabet size is global but no longer used
// once iterators are fully miplemented it's not needed
// anymore.
std::stringstream lineStream;
lineStream.str(data);
int samples;
lineStream >> samples >> alphabet_size;
apta* aut = merger->aut;
merger->node_number = 1;
aut->root->depth = 0;
}
void evaluation_function::add_sample(string data, state_merger* merger) {
// set up segmentation of sample line
std::stringstream lineStream;
lineStream.str(data);
// header of line
int label;
int length;
lineStream >> label >> length;
apta* aut = merger->aut;
apta_node* node = aut->root;
// run over symbol/data of sample
int depth=0;
int num_alph=merger->seen.size();
// leading BLANK between string and label length
string adv;
std::getline(lineStream, adv, ' ');
// init with current length of seen
for (int index=0; index < length; index++) {
depth++;
string symbol;
string tuple;
std::getline(lineStream,tuple,' ');
string dat;
std::stringstream elements;
elements.str(tuple);
std::getline(elements,symbol,'/');
std::getline(elements,dat);
//cout << "length: " << length << ". label: " << label << ". index" << index << ". symbol: " << symbol << ". data: " << dat << "." << endl;
if(merger->seen.find(symbol) == merger->seen.end()){
aut->alphabet[num_alph] = symbol;
merger->seen[symbol] = num_alph;
num_alph++;
}
int c = merger->seen[symbol];
// set node age
node->age = merger->node_number;
// if root is red, mark child blue
if(node->child(c) == 0){
apta_node* next_node = new apta_node(aut);
node->set_child(c,next_node);
next_node->source = node;
next_node->label = c;
next_node->number = merger->node_number++;
next_node->depth = depth;
// move this down
if(merger->red_states.find(node) != merger->red_states.end()) { merger->blue_states.insert(node->child(c)); }
}
node->size = node->size + 1;
node->data->read_from(label, index, length, c, dat);
node = node->child(c);
node->data->read_to(label, index, length, c, dat);
node->age= merger->node_number;
// update union-find structure
// next_node->find()
}
if(depth > aut->max_depth) aut->max_depth = depth;
node->size = node->size + 1;
node->type = label;
};