-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_enum_solver.m
More file actions
436 lines (392 loc) · 15 KB
/
test_enum_solver.m
File metadata and controls
436 lines (392 loc) · 15 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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
function test_enum_solver()
%% Clear
clc;
clear all;
%% close all opened files
close all;
%% set a random seed
seed=0;
rng(seed);
%% Intitializations
% tspan = 1:10;
% READ IN DATA
% good_traces = readmatrix('generated_hapt_data.csv');
% good_traces = readmatrix('good_hapt_data.csv');
% bad_traces = readmatrix('bad_hapt_data.csv');
% good_traces = readmatrix('test_good_data.csv');
% bad_traces = readmatrix('test_bad_data.csv');
% good_traces = readmatrix('good_uav_dists.csv');
% bad_traces = readmatrix('bad_uav_dists.csv');
good_traces = readmatrix('good_pitch_data.csv');
bad_traces = readmatrix('bad_pitch_data.csv');
time_len = size(good_traces);
time_len = time_len(2);
tspan = 1:time_len;
min_all = round(min(min(good_traces, [], 'all'), min(bad_traces, [], 'all')));
max_all = round(max(max(good_traces, [], 'all'), max(bad_traces, [], 'all')));
param_range = [min_all, max_all];
MCR_thresh = 0.4; % higher mcr_thresh during GAN_training
% MCR_thresh = 0.1;
% figure;
% hold all;
%
% plot(good_traces','g');
% plot(bad_traces','r');
%% APPLYING ENUMERATIVE SOLVER
cd ('/Users/nicole/OSU/GAN_TL/learningSTL/EnumerativeSolver');
traceTimeBegin = 1;
traceTimeHorizon = time_len;
timeRange = [traceTimeBegin, traceTimeHorizon];
s1 = struct('name', 'x', ...
'ops', {{'<','>'}}, ...
'params', {{'valx'}}, ...
'timeRange', timeRange, ...
'range', param_range);
signals = {s1};
Traces0 = makeBreachTraceSystem(signals);%anomalous
Traces1 = makeBreachTraceSystem(signals);%normal
Traces0_test = makeBreachTraceSystem(signals);%anomalous
Traces1_test = makeBreachTraceSystem(signals);%normal
% what are these parameters?
options.numSignatureTraces = 3;
numTraces = 10;
% numTraces = 3;
yapp = YAP(signals, numTraces, options);
%% train traces
for jj = 1:50
x=awgn(good_traces(jj,:),25,'measured');
t=1:time_len;
trace = [t' x'];
Traces1.AddTrace(trace);
yapp.addTrace(jj,trace);
x=awgn(bad_traces(jj,:),25,'measured');
trace = [t' x'];
Traces0.AddTrace(trace);
end
%% test traces
for jj = 51:100
x=awgn(good_traces(jj,:),25,'measured');
t=1:time_len;
trace = [t' x'];
Traces1_test.AddTrace(trace);
x=awgn(bad_traces(jj,:),25,'measured');
trace = [t' x'];
Traces0_test.AddTrace(trace);
end
% figure;
% hold all;
%
% plot(good_traces(1:50,:)','g');
% plot(bad_traces(1:50,:)','r');
%% learning STL classifier with signature
clc
fprintf('running with optimization...\n')
j=1;
done=0;
% f = formulaIterator(10, signals);
f = formulaIterator(3, signals); % restricting max length to 3
tic
while (1)
fprintf('getting next formula \n')
fprintf('%d \n', j)
formula = f.nextFormula();
yapp.addTimeParams(formula);
% check the equivalence of formulas
fprintf('check the equivalence of formulas \n')
if (yapp.isNew(formula))
fprintf('found new formula \n')
% set parameter ranges
params = fieldnames(get_params(formula));
numparam=length(params);
paramranges = zeros(numparam,2);
for i=1:numparam
if string(params(i,1)) == "valx"
paramranges (i,:)= param_range;
elseif string(params(i,1)) == "tau_1"
paramranges (i,:)=[1,time_len];
elseif string(params(i,1)) == "tau_2"
paramranges (i,:)=[1,time_len];
elseif string(params(i,1)) == "tau_3"
paramranges (i,:)=[1,time_len];
end
end
% Monotonic_Bipartition function parameters
uncertainty=10e-3;
num_steps = 3;
n= numparam;
% monotonicity direction for enumerated formulas, will
% automate this part in next version
switch j
case 1
monoDir1=0;
case 2
monoDir1=1;
case 3
monoDir1=1;
case 4
monoDir1=0;
case 5
monoDir1=[0,0];
case 6
monoDir1=[1,0];
case 7
monoDir1=[0,1];
case 8
monoDir1=[1,1];
case 9
monoDir1=[1,0];
case 10
monoDir1=[0,0];
case 11
monoDir1=[0,1,0];
case 12
monoDir1=[1,1,0];
case 13
monoDir1=[1,1];
case 14
monoDir1=[0,1];
case 15
monoDir1=[0,0,1];
case 16
monoDir1=[1,0,1];
otherwise
monoDir1=-1;
end
fprintf('begin calculating robustness \n')
if (all(monoDir1)>=0)
% obtain validity domain boundary
fprintf('obtaining validity domain boundary \n')
fprintf(disp(formula));
mono=Monotonic_Bipartition (formula,paramranges,num_steps,uncertainty,Traces1,monoDir1);
c1=reshape(mono.boundry_points,numparam,size(mono.boundry_points,2)/numparam)';
% check points on validity domain boundary and choose
% the point with MCR = 0
fprintf('checking points on validity domain boundary \n')
for i = 1:size (c1,1)
formula = set_params(formula,params, c1(i,:));
fprintf('calculating robustness values of point \n')
robustness1(i,:)=Traces1.CheckSpec(formula);
robustness2(i,:)=Traces0.CheckSpec(formula);
if and(all(robustness1(i,:)> 0),all(robustness2(i,:)<0))
fprintf('\n\n');
fprintf('The learned STL formula is:\n');
fprintf('\n');
fprintf(disp(formula));
fprintf('\n\n');
fprintf('The values of parameters are:\n');
for n = 1:size(params,1)
params(n)
c1(i,n)
fprintf('\n');
end
fprintf('train MCR=0\n')
fprintf('Elapsed time with signature based optimization:\n')
toc
done=1;
break;
else
TruePos=size(find (robustness1(i,:)> 0 == 1),2);
FalsePos=size(robustness1,2)-TruePos;
TrueNeg=size(find (robustness2(i,:) < 0 == 1),2);
FalseNeg=size(robustness2,2)-TrueNeg;
MCR = (FalsePos + FalseNeg)/(size(robustness1,2)+size(robustness2,2));
% check points on validity domain boundary and choose
% the point with MCR < 0.1
if MCR < MCR_thresh
fprintf('\n\n');
fprintf('The learned STL formula is:\n');
fprintf('\n');
fprintf(disp(formula));
fprintf('\n\n');
fprintf('The values of parameters are:\n');
for n = 1:size(params,1)
params(n)
c1(i,n)
fprintf('\n');
end
fprintf('train MCR = %f\n',MCR);
fprintf('Elapsed time with signature based optimization:\n')
toc
done=1;
break;
end
end
end
if done==1 || j > 100
break;
end
end
end
j=j+1;
end
% % learning STL classifier without signature
% % fprintf('***************************************************\n');
% % fprintf('running without optimization...\n')
% % j=1;
% % done=0;
% % %f = formulaIterator(10, signals);
% % f = formulaIterator(2, signals); % restricting max length of formulae
% % tic
% % while (1)
% %
% % formula = f.nextFormula();
% % %set parameter ranges
% % params = fieldnames(get_params(formula));
% % numparam=length(params);
% % paramranges = zeros(numparam,2);
% %
% %
% % for i=1:numparam
% % if string(params(i,1)) == "valx"
% % paramranges (i,:)= param_range;
% % elseif string(params(i,1)) == "tau_1"
% % paramranges (i,:)=[1,10];
% % elseif string(params(i,1)) == "tau_2"
% % paramranges (i,:)=[1,10];
% % elseif string(params(i,1)) == "tau_3"
% % paramranges (i,:)=[1,10];
% % end
% % end
% % % Monotonic_Bipartition function parameters
% % uncertainty=10e-3;
% % num_steps = 3;
% % n= numparam;
% %
% % % monotonicity direction for enumerated formulas, will
% % % automate this part in next version
% % switch j
% % case 1
% % monoDir1=0;
% % case 2
% % monoDir1=1;
% % case 3
% % monoDir1=1;
% % case 4
% % monoDir1=0;
% % case 5
% % monoDir1=[0,0];
% % case 6
% % monoDir1=[1,0];
% % case 7
% % monoDir1=[0,1];
% % case 8
% % monoDir1=[1,1];
% % case 9
% % monoDir1=[1,0];
% % case 10
% % monoDir1=[0,0];
% % case 11
% % monoDir1=[0,1,0];
% % case 12
% % monoDir1=[1,1,0];
% % case 13
% % monoDir1=[1,1];
% % case 14
% % monoDir1=[0,1];
% % case 15
% % monoDir1=[0,0,1];
% % case 16
% % monoDir1=[1,0,1];
% % otherwise
% % monoDir1=-1;
% % end
% %
% % if (all(monoDir1)>=0)
% %
% % % obtain validity domain boundary
% % mono=Monotonic_Bipartition (formula,paramranges,num_steps,uncertainty,Traces1,monoDir1);
% % c1=reshape(mono.boundry_points,numparam,size(mono.boundry_points,2)/numparam)';
% %
% % % check points on validity domain boundary and choose
% % % the point with MCR = 0
% % for i = 1:size (c1,1)
% % formula = set_params(formula,params, c1(i,:));
% % robustness1(i,:)=Traces1.CheckSpec(formula);
% % robustness2(i,:)=Traces0.CheckSpec(formula);
% %
% % if and(all(robustness1(i,:)> 0),all(robustness2(i,:)<0))
% % fprintf('\n\n');
% % fprintf('The learned STL formula is:\n');
% % fprintf('\n');
% % fprintf(disp(formula));
% % fprintf('\n\n');
% % fprintf('The values of parameters are:\n');
% % for n = 1:size(params,1)
% % params(n)
% % c1(i,n)
% % fprintf('\n');
% % end
% % fprintf('train MCR=0\n')
% % fprintf('Elapsed time without signature based optimization:\n')
% % toc
% % %plot the learned thresholds by our tool
% % %y=[];
% % %y(1,1:size(tspan,2))=c1(i,1);
% % %plot(tspan,y,'b--','LineWidth',2);
% % %xlabel('t(s)');
% % %ylabel('v(m/s)');
% % done=1;
% % break;
% % else
% %
% % TruePos=size(find (robustness1(i,:)> 0 == 1),2);
% % FalsePos=size(robustness1,2)-TruePos;
% %
% % TrueNeg=size(find (robustness2(i,:) < 0 == 1),2);
% % FalseNeg=size(robustness2,2)-TrueNeg;
% %
% % MCR = (FalsePos + FalseNeg)/(size(robustness1,2)+size(robustness2,2));
% %
% % % check points on validity domain boundary and choose
% % % the point with MCR = 0.1
% % if MCR < 0.1
% % fprintf('\n\n');
% % fprintf('The learned STL formula is:\n');
% % fprintf('\n');
% % fprintf(disp(formula));
% % fprintf('\n\n');
% % fprintf('The values of parameters are:\n');
% % for n = 1:size(params,1)
% % params(n)
% % c1(i,n)
% % fprintf('\n');
% % end
% % fprintf('train MCR = %f\n',MCR);
% % fprintf('Elapsed time without signature based optimization:\n')
% % toc
% % %plot the learned thresholds by our tool
% % %y=[];
% % %y(1,1:size(tspan,2))=c1(i,1);
% % %plot(tspan,y,'b--','LineWidth',2);
% % %xlabel('t(s)');
% % %ylabel('v(m/s)');
% % %done=1;
% % break;
% % end
% % end
% %
% % end
% % if done==1
% % break;
% % end
% % end
% %
% % j=j+1;
% % end
%
% Test MCR
pos = Traces1_test.CheckSpec(formula);
neg = Traces0_test.CheckSpec(formula);
% mcr_test = (size(find (pos < 0),1) + size(find (neg > 0),1))/100;
mcr_test = (length(find (pos < 0)) + length(find (neg > 0)))/100;
fprintf('test MCR = %f\n',mcr_test);
% TODO: append loss (mcr) to file
% Save Formula to File
fid = fopen('~/OSU/GAN_TL/formula.txt', 'wt');
fprintf(fid, disp(formula));
fclose(fid);
fid = fopen('~/OSU/GAN_TL/formula_params.txt', 'wt');
fprintf(fid, params{1});
fprintf(fid, ',');
fprintf(fid, '%d\n', round(c1(1), 2));
fclose(fid);