-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathncsTransitionRelation.hh
877 lines (669 loc) · 25.2 KB
/
ncsTransitionRelation.hh
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
/*
* ncsTransitionRelation.hh
*
* created on: 07.11.2016
* author: M.Khaled
*/
#ifndef NCSTRANSITIONRELATION_HH_
#define NCSTRANSITIONRELATION_HH_
#include <vector>
#include "cuddObj.hh"
#include "SymbolicSetInterface.hh"
#include "ncsState.hh"
#include "ncsFixPoint.hh"
#include "ncsController.hh"
#include "Misc.hh"
#define NCS_TR_REMARKS_TEXT "NCS Transition Relation"
using namespace std;
class ncsTransitionRelation{
protected:
Cudd* pCuddManager; // CUDD Manager
size_t SS_DIM; // State space dimension, got from the user in command line
size_t IS_DIM; // Input space dimension, got from the user in command line
size_t NSCMIN;
size_t NSCMAX; // Max delay in the Sensor-Controller Channel, got from the user in command line
size_t NCAMIN;
size_t NCAMAX; // Max delay in the Controller-Actuator Channel, got from the user in command line
/*
* BDD, details and bddVars of the original transition relation
*/
BDD f_org_rel;
vector<size_t> org_x_bddVars, org_u_bddVars, org_xd_bddVars;
vector<size_t> ssVarsCount, isVarsCount;
vector<double> ssEta, ssLb, ssUb, isEta, isLb, isUb;
/* 1] BddVars for the new transition relation.
* Note the the new system transition relation will have:
* (x1, x2 ... xn, u1, u2 ... um, u, xd1, xd2 ... xdn, ud1, ud2 ... udm)
*
* - where n = NscMax and m= NcaMax
* - x1, um and xd1 are directly from the old transition relation (org_x, org_u, org_xd)
* - xd2 to xdn are synchronized with x1 to xn-1 (FIFO style shifting)
* - xn is dont-care (can be any allowed value as it will be kicked out of the channel)
* - u is dont-care (can be any allowed value as it will the input to the system)
* - ud1 is synchronized with u
* - ud2 to udm are synchronized with u1 to um-1
*
* x1 = org_x
* xd1 = org_xd
* um = org_u
*
* synchronization means having the same value whatever the combination of other vars. Please also
* notice that all of these values might require more than one bddVar (in case the number of
* possibilities of the values are more than 2).
*/
vector<size_t> q1qn, qd1qdn, ui;
vector<vector<size_t>> x1xn, xd1xdn, u1um, ud1udm;
/* same vars but as X_U_Xd*/
vector<size_t> preVars_noq , postVars_noq;
vector<size_t> qBddVars;
// BDD variables for the new reation and the IC mask
BDD f_new_rel;
BDD normalTransRules, noqTransitions;
BDD qTransRules, qTransitions;
string Remarks;
bool isExpanded = false;
double expandTime = 0;
double ComputeContrTime = 0;
/* Asks the Cudd BDD-Manager to give bddVars for the new elements in the expanded relation
* returns the index of last created bddVar */
size_t RequestNewBddVars(){
if(org_x_bddVars.size() != org_xd_bddVars.size())
throw runtime_error("Error::ReArrangeBDDVars:: Size mismatch between the BddVars of X and Xd!");
if(org_x_bddVars[0] != 0)
throw runtime_error("Error::ExpandBDD:: First BddVar of the original relation should be BddVar of index 0.");
if((pCuddManager->ReadSize()-1) != (int)org_xd_bddVars[org_xd_bddVars.size()-1])
throw runtime_error("Error::ExpandBDD:: The manager should only have BddVars from the original relation.");
// Counting current and new vars that will be requested
size_t nBddVarsX = org_x_bddVars.size();
size_t nBddVarsU = org_u_bddVars.size();
size_t nBddVars = 2*NSCMAX*nBddVarsX + // for x1xn and xd1xdn
2*NCAMAX*nBddVarsU + // for u1um and ud1udm
nBddVarsU + // for the single ui
2*NSCMAX; // for q1qn and qd1qdm
// exclude already existing BddVars from the original relation
size_t nBddVarsToRquest = nBddVars-(2*nBddVarsX + nBddVarsU);
// requesting for extra BddVars
for(size_t i=0; i<nBddVarsToRquest; i++)
pCuddManager->bddVar();
// arranging/assigning the current/new BddVars
int c=0;
// q1|x1, q2|x2 .... qn|xn
for(size_t i=0; i<NSCMAX; i++){
q1qn.push_back(c++);
vector<size_t> tmp;
for(size_t j=0; j<nBddVarsX; j++)
tmp.push_back(c++);
x1xn.push_back(tmp);
}
// u1, u2 .. um
for(size_t i=0; i<NCAMAX; i++){
vector<size_t> tmp;
for(size_t j=0; j<nBddVarsU; j++)
tmp.push_back(c++);
u1um.push_back(tmp);
}
for(size_t j=0; j<nBddVarsU; j++)
ui.push_back(c++);
// qd1|xd1, qd2|xd2 .... qdn|xdn
for(size_t i=0; i<NSCMAX; i++){
qd1qdn.push_back(c++);
vector<size_t> tmp;
for(size_t j=0; j<nBddVarsX; j++)
tmp.push_back(c++);
xd1xdn.push_back(tmp);
}
// ud1, ud2 .. udm
for(size_t i=0; i<NCAMAX; i++){
vector<size_t> tmp;
for(size_t j=0; j<nBddVarsU; j++)
tmp.push_back(c++);
ud1udm.push_back(tmp);
}
if(c != pCuddManager->ReadSize())
throw runtime_error("Error::ReArrangeBddVars:: Some error happened, the assigned BddVars seems to miss some.");
#ifdef VERBOSEBASIC
cout << "ReArranged BddVars:" << endl;
cout << " q1qn: "; VectorManager::PrintVector(q1qn);
cout << " x1xn: "; for(size_t i=0; i<NSCMAX;i++) {VectorManager::PrintVector(x1xn[i], ' ', false); cout<<", ";} cout<<endl;
cout << " u1um: "; for(size_t i=0; i<NCAMAX;i++) {VectorManager::PrintVector(u1um[i], ' ', false); cout<<", ";} cout<<endl;
cout << " ui: "; VectorManager::PrintVector(ui, ' ');
cout << "qd1qdn: "; VectorManager::PrintVector(qd1qdn);
cout << "xd1xdn: "; for(size_t i=0; i<NSCMAX;i++) {VectorManager::PrintVector(xd1xdn[i], ' ', false); cout<<", ";} cout<<endl;
cout << "ud1udm: "; for(size_t i=0; i<NCAMAX;i++) {VectorManager::PrintVector(ud1udm[i], ' ', false); cout<<", ";} cout<<endl;
cout << endl;
#endif
return (c-1);
}
/* Permutes a given BDD from original transition relation to the new order of transition relation*/
BDD PermutreBddToNewVars(BDD& f_to_permute){
size_t nBddVarsX = org_x_bddVars.size();
size_t nBddVarsU = org_u_bddVars.size();
// Permute f_org_rel to reflect the new BddVars
vector<int> permute = BDDUtils::GetReadyPermuteMap(*pCuddManager);
for(size_t i=0; i<nBddVarsX; i++){
permute[org_x_bddVars[i]] = x1xn[0][i];
permute[org_xd_bddVars[i]] = xd1xdn[0][i];
}
for(size_t i=0; i<nBddVarsU; i++){
permute[org_u_bddVars[i]] = u1um[NCAMAX-1][i];
}
BDD f_permuted = f_to_permute.Permute(permute.data());
return f_permuted;
}
BDD ExpandStateBasedBDD(BDD& f_org_ts){
BDD f_org_ts_permuted = PermutreBddToNewVars(f_org_ts);
#ifdef VERBOSE
BDDUtils::PrintBDD("f_org_ts before permutation", f_org_ts);
BDDUtils::PrintBDD("f_org_ts after permutation", f_org_ts_permuted);
#endif
BDD f_new_ts = f_org_ts_permuted*noqTransitions;
#ifdef VERBOSE
BDDUtils::PrintBDD("original ts", f_org_ts);
cout << "support vars of permuted ts (before projection): "; VectorManager::PrintVector(f_new_ts.SupportIndices());
BDDUtils::PrintBDD("new permuted ts (before projection)", f_new_ts);
#endif
vector<size_t> sourceStateVars;
VectorManager::AppendVector(sourceStateVars, q1qn);
VectorManager::AppendVectors(sourceStateVars, x1xn);
VectorManager::AppendVectors(sourceStateVars, u1um);
f_new_ts = BDDUtils::ProjectBDD(*pCuddManager, f_new_ts, sourceStateVars);
#ifdef VERBOSE
cout << "support vars of ts: "; VectorManager::PrintVector(f_new_ts.SupportIndices());
BDDUtils::PrintBDD("new ts", f_new_ts);
#endif
return f_new_ts;
}
/* Virtual unctions needed by any NCS-TR type */
virtual ncsState* getSourceStateTemplate() = 0;
virtual BDD ConstructQTransitionRules() = 0;
virtual BDD ConstructNormalTransRules(const BDD& f_org_permuted, const BDD& f_org_projected) = 0;
public:
// Constructors
/* reads an NBDD File holding a transition relation */
ncsTransitionRelation(Cudd& cuddManager_, const char* NBDD_INPUT_FILE){
pCuddManager = &cuddManager_;
string Remarks;
BDDUtils::readFromFile(NBDD_INPUT_FILE, cuddManager_,
f_new_rel,
SS_DIM, IS_DIM,
ssVarsCount, isVarsCount,
ssEta, ssLb, ssUb, isEta, isLb, isUb,
NSCMIN, NSCMAX, NCAMIN, NCAMAX,
preVars_noq, ui, postVars_noq,
qBddVars, Remarks);
size_t good_remarks = Remarks.find(NCS_TR_REMARKS_TEXT);
if (good_remarks==std::string::npos)
throw(runtime_error("Error::ncsTransitionRelation:: This is not a valid NCS Transition Relation File !!"));
string time = "Time:";
size_t time_found = Remarks.find(time);
if (time_found!=std::string::npos){
stringstream ss;
ss << Remarks.substr(time_found+time.length());
ss >> expandTime;
}
isExpanded = true;
// TO DO :: fill the other original BDD and BddVars
size_t nBddVarsX = 0, nBddVarsU = 0;
for(size_t i=0, k=0; i<SS_DIM; i++)
for(size_t j=0; j<ssVarsCount[i]; j++, k++){
org_x_bddVars.push_back(preVars_noq[k]);
org_xd_bddVars.push_back(postVars_noq[k]);
nBddVarsX++;
}
for(size_t i=0; i<IS_DIM; i++)
for(size_t j=0; j<isVarsCount[i]; j++)
nBddVarsU++;
for(size_t i=0, k=0; i<IS_DIM; i++)
for(size_t j=0; j<isVarsCount[i]; j++, k++)
org_u_bddVars.push_back(preVars_noq[NSCMAX*nBddVarsX + (NCAMAX-1)*nBddVarsU + k]);
size_t c=0;
for(size_t i=0; i<NSCMAX; i++){
vector<size_t> tmp1,tmp2;
for(size_t j=0; j<SS_DIM; j++){
for(size_t k=0; k<ssVarsCount[j]; k++){
tmp1.push_back(preVars_noq[c]);
tmp2.push_back(postVars_noq[c]);
c++;
}
}
x1xn.push_back(tmp1);
xd1xdn.push_back(tmp2);
}
for(size_t i=0; i<NCAMAX; i++){
vector<size_t> tmp1,tmp2;
for(size_t j=0; j<IS_DIM; j++){
for(size_t k=0; k<isVarsCount[j]; k++){
tmp1.push_back(preVars_noq[c]);
tmp2.push_back(postVars_noq[c]);
c++;
}
}
u1um.push_back(tmp1);
ud1udm.push_back(tmp2);
}
size_t nQsOverTwo = qBddVars.size()/2;
for(size_t i=0; i<nQsOverTwo; i++){
q1qn.push_back(qBddVars[i]);
qd1qdn.push_back(qBddVars[nQsOverTwo+i]);
}
BDD NoQsCube = BDDUtils::ConstructZerosAnd(*pCuddManager, qBddVars);
f_org_rel = f_new_rel*NoQsCube;
vector<size_t> projVars;
VectorManager::AppendVector(projVars, org_x_bddVars);
VectorManager::AppendVector(projVars, org_u_bddVars);
VectorManager::AppendVector(projVars, org_xd_bddVars);
f_org_rel = BDDUtils::ProjectBDD(*pCuddManager, f_org_rel, projVars);
}
/* Reads a BDD file and expnds the relation*/
ncsTransitionRelation(Cudd& cuddManager_,
const char* BDD_INPUT_FILE,
const size_t SS_DIM_, const size_t IS_DIM_,
const size_t NSCMIN_, const size_t NSCMAX_, const size_t NCAMIN_, const size_t NCAMAX_){
pCuddManager = &cuddManager_;
SS_DIM = SS_DIM_;
IS_DIM = IS_DIM_;
NSCMIN = NSCMIN_;
NSCMAX = NSCMAX_;
NCAMIN = NCAMIN_;
NCAMAX = NCAMAX_;
// Reading original transition relation and target/avoid states from the file as a symbolic set
if(!IOUtils::file_exists(BDD_INPUT_FILE)){
throw runtime_error("Error::ncsTransitionRelation:: The input file ( trans_rel ) does not exist.");
}
SymbolicSet ss_org;
ss_org.LoadFromFile(*pCuddManager, BDD_INPUT_FILE);
f_org_rel = ss_org.getSymbolicSet();
// Checking dimensions in the file with the provided ones
if(ss_org.getDimension() != (2*SS_DIM + IS_DIM)){
throw runtime_error("Error::ncsTransitionRelation:: input provided dimensions in input-file do not match the given dimensions");
}
ssVarsCount = VectorManager::ArrayToVector(ss_org.getNofBddVars(),SS_DIM);
isVarsCount = VectorManager::ArrayToVector(ss_org.getNofBddVars()+SS_DIM, IS_DIM);
ssEta = VectorManager::ArrayToVector(ss_org.getEta(),SS_DIM);
ssLb = VectorManager::ArrayToVector(ss_org.getFirstGridPoint(),SS_DIM);
ssUb = VectorManager::ArrayToVector(ss_org.getLastGridPoint(),SS_DIM);
isEta = VectorManager::ArrayToVector(ss_org.getEta()+SS_DIM, IS_DIM);
isLb = VectorManager::ArrayToVector(ss_org.getFirstGridPoint()+SS_DIM, IS_DIM);
isUb = VectorManager::ArrayToVector(ss_org.getLastGridPoint()+SS_DIM, IS_DIM);
SymbolicSetInterface::ExtractBddVars(ss_org, org_x_bddVars, org_u_bddVars, org_xd_bddVars, SS_DIM, IS_DIM);
/* 3] Requesting extra bddVars for the expanded system and re arrange BddVars
*/
size_t last_var = RequestNewBddVars();
if((last_var-1)>(sizeof(size_t)*8)){
cout << "Warning::ncsTransitionRelation:: Your bddVars exceeds the bound of (size_t). You will have to do more job if you like to iterate on the transitions !" << endl;
}
VectorManager::AppendVectors(preVars_noq, x1xn);
VectorManager::AppendVectors(preVars_noq, u1um);
VectorManager::AppendVectors(postVars_noq, xd1xdn);
VectorManager::AppendVectors(postVars_noq, ud1udm);
VectorManager::AppendVector(qBddVars, q1qn);
VectorManager::AppendVector(qBddVars, qd1qdn);
}
virtual ~ncsTransitionRelation(){
}
void ExpandBDD(){
if(isExpanded)
throw runtime_error("Error::Expand:: You are trying to expand already expanded NCS transition relation !");
StopWatch::Start();
// 0] Permute and Project the original relation
BDD f_org_rel_permuted, f_org_rel_projected;
f_org_rel_permuted = PermutreBddToNewVars(f_org_rel);
vector<size_t> projVars;
VectorManager::AppendVector(projVars, x1xn[0]);
VectorManager::AppendVector(projVars, xd1xdn[0]);
f_org_rel_projected = BDDUtils::ProjectBDD(*pCuddManager, f_org_rel_permuted, projVars);
#ifdef VERBOSE
BDDUtils::PrintBDD("f_org_rel before permutation", f_org_rel);
BDDUtils::PrintBDD("f_org_rel after permutation", f_org_rel_permuted);
BDDUtils::PrintBDD("f_org_rel after projection", f_org_rel_projected);
#endif
// 1] Normal Transitions
// But first, All Qs are zeros
vector<size_t> AllQsBddVars;
VectorManager::AppendVector(AllQsBddVars, q1qn);
VectorManager::AppendVector(AllQsBddVars, qd1qdn);
BDD f_AllQsAreZeros = BDDUtils::ConstructZerosAnd(*pCuddManager, AllQsBddVars);
#ifdef VERBOSE
BDDUtils::PrintBDD("f_AllQsAreZeros", f_AllQsAreZeros);
#endif
normalTransRules = ConstructNormalTransRules(f_org_rel_permuted, f_org_rel_projected);
noqTransitions = normalTransRules*f_AllQsAreZeros;
#ifdef VERBOSE
BDDUtils::PrintBDD("noqTransitions", noqTransitions);
#endif
// 2] Q-based Transitions
qTransRules = ConstructQTransitionRules();
qTransitions = qTransRules*/*f_org_rel_permuted*/normalTransRules/*f_org_rel_projected*/;
#ifdef VERBOSE
BDDUtils::PrintBDD("qTransitions", qTransitions);
#endif
// 3] The new expanded relation
f_new_rel = qTransitions+noqTransitions;
expandTime = StopWatch::Stop(false);
#ifdef VERBOSE
BDDUtils::PrintBDD("new rel", f_new_rel);
#endif
#ifdef VERBOSEBASIC
cout << "The size of the new expanded relation is: " << f_new_rel.CountMinterm(pCuddManager->ReadSize()) << endl;
#endif
}
size_t getStateVarsCount(){
return getSourceStateTemplate()->getVarsCount();
}
size_t getInpVarsCount(){
return ui.size();
}
size_t getVarsCount(){
return 2*getStateVarsCount() + getInpVarsCount();
}
vector<size_t> getSsVarsCount(){
return ssVarsCount;
}
vector<size_t> getIsVarsCount(){
return isVarsCount;
}
size_t getNscMax(){
return NSCMAX;
}
size_t getNcaMax(){
return NCAMAX;
}
BDD getOriginalRelation(){
return f_org_rel;
}
double getExpandTime(){
return expandTime;
}
double getComputeContrTime(){
return ComputeContrTime;
}
vector<size_t> getPreVars(){
vector<size_t> preVars;
for(size_t i=0; i<NSCMAX; i++){
preVars.push_back(q1qn[i]);
VectorManager::AppendVector(preVars, x1xn[i]);
}
VectorManager::AppendVectors(preVars, u1um);
return preVars;
}
vector<size_t> getPostVars(){
vector<size_t> postVars;
for(size_t i=0; i<NSCMAX; i++){
postVars.push_back(qd1qdn[i]);
VectorManager::AppendVector(postVars, xd1xdn[i]);
}
VectorManager::AppendVectors(postVars, ud1udm);
return postVars;
}
vector<size_t> getOrginalVars(){
vector<size_t> orgVars;
VectorManager::AppendVector(orgVars, org_x_bddVars);
VectorManager::AppendVector(orgVars, org_u_bddVars);
VectorManager::AppendVector(orgVars, org_xd_bddVars);
return orgVars;
}
BDD getTransitionRelation(){
return f_new_rel;
}
BDD getXICMask(){
throw(runtime_error("Not Implemented !!"));
}
ncsController ComputeReachController(const char* BDD_FILE_TARGET){
SymbolicSet ss_ts;
ss_ts.LoadFromFile(*pCuddManager, BDD_FILE_TARGET);
return ComputeReachController(ss_ts.getSymbolicSet());
}
ncsController ComputeReachController(BDD f_org_ts){
vector<size_t> preVars = getPreVars();
vector<size_t> postVars = getPostVars();
ncsFixPoint fp(pCuddManager, f_new_rel, preVars, ui, postVars);
BDD f_ts = ExpandStateBasedBDD(f_org_ts);
StopWatch::Start();
BDD C = fp.reach(f_ts,1);
ComputeContrTime = StopWatch::Stop();
#ifdef VERBOSE
BDDUtils::PrintBDD("The Controller:", C);
#endif
ncsController contr(*pCuddManager, C, getSourceStateTemplate(), ui, ComputeContrTime);
return contr;
}
ncsController ComputeSafetyController(const char* BDD_FILE_SAFESET){
SymbolicSet ss_safe;
ss_safe.LoadFromFile(*pCuddManager, BDD_FILE_SAFESET);
return ComputeSafetyController(ss_safe.getSymbolicSet());
}
ncsController ComputeSafetyController(BDD f_org_safe){
vector<size_t> preVars = getPreVars();
vector<size_t> postVars = getPostVars();
ncsFixPoint fp(pCuddManager, f_new_rel, preVars, ui, postVars);
BDD f_safe = ExpandStateBasedBDD(f_org_safe);
StopWatch::Start();
BDD C = fp.safe(f_safe, 1);
ComputeContrTime = StopWatch::Stop();
#ifdef VERBOSE
BDDUtils::PrintBDD("The Controller:", C);
#endif
ncsController contr(*pCuddManager, C, getSourceStateTemplate(), ui, ComputeContrTime);
return contr;
}
ncsController ComputePersistanceController(const char* BDD_FILE_TARGET){
SymbolicSet ss_ts;
ss_ts.LoadFromFile(*pCuddManager, BDD_FILE_TARGET);
return ComputePersistanceController(ss_ts.getSymbolicSet());
}
ncsController ComputePersistanceController(BDD f_org_ts){
vector<size_t> preVars = getPreVars();
vector<size_t> postVars = getPostVars();
ncsFixPoint fp(pCuddManager, f_new_rel, preVars, ui, postVars);
BDD f_ts = ExpandStateBasedBDD(f_org_ts);
StopWatch::Start();
BDD C = fp.persistance(f_ts,1);
ComputeContrTime = StopWatch::Stop();
#ifdef VERBOSE
BDDUtils::PrintBDD("The Controller:", C);
#endif
ncsController contr(*pCuddManager, C, getSourceStateTemplate(), ui, ComputeContrTime);
return contr;
}
ncsController ComputeRecurrenceController(const char* BDD_FILE_TARGET){
SymbolicSet ss_ts;
ss_ts.LoadFromFile(*pCuddManager, BDD_FILE_TARGET);
return ComputeRecurrenceController(ss_ts.getSymbolicSet());
}
ncsController ComputeRecurrenceController(BDD f_org_ts){
vector<size_t> preVars = getPreVars();
vector<size_t> postVars = getPostVars();
ncsFixPoint fp(pCuddManager, f_new_rel, preVars, ui, postVars);
BDD f_ts = ExpandStateBasedBDD(f_org_ts);
StopWatch::Start();
BDD C = fp.recurrence(f_ts,1);
ComputeContrTime = StopWatch::Stop();
#ifdef VERBOSE
BDDUtils::PrintBDD("The Controller:", C);
#endif
ncsController contr(*pCuddManager, C, getSourceStateTemplate(), ui, ComputeContrTime);
return contr;
}
vector<ncsController> ComputeRecurrenceConjController(vector<const char*> vBDD_FILE_TARGET){
vector<SymbolicSet> ss_targets(vBDD_FILE_TARGET.size());
vector<BDD> targetBDDs(vBDD_FILE_TARGET.size());
for(size_t i=0; i<vBDD_FILE_TARGET.size(); i++){
ss_targets[i].LoadFromFile(*pCuddManager, vBDD_FILE_TARGET[i]);
targetBDDs[i] = ss_targets[i].getSymbolicSet();
}
return ComputeRecurrenceConjController(targetBDDs);
}
vector<ncsController> ComputeRecurrenceConjController(vector<BDD> f_org_targets){
vector<size_t> preVars = getPreVars();
vector<size_t> postVars = getPostVars();
vector<BDD> f_targets(f_org_targets.size());
ncsFixPoint fp(pCuddManager, f_new_rel, preVars, ui, postVars);
for(size_t i=0; i<f_org_targets.size(); i++)
f_targets[i] = ExpandStateBasedBDD(f_org_targets[i]);
StopWatch::Start();
vector<BDD> vC = fp.recurrence_conj(f_targets,1);
ComputeContrTime = StopWatch::Stop();
vector<ncsController> vContr;
for(size_t i=0; i<f_org_targets.size(); i++){
ncsController contr(*pCuddManager, vC[i], getSourceStateTemplate(), ui, ComputeContrTime);
vContr.push_back(contr);
}
return vContr;
}
void WriteToFile(string outRelationFile){
stringstream ss;
ss << NCS_TR_REMARKS_TEXT << " | Time:" << expandTime;
BDDUtils::writeToFile(
f_new_rel,
SS_DIM, IS_DIM,
ssVarsCount.data(), isVarsCount.data(),
ssEta.data(), ssLb.data() , ssUb.data(), isEta.data(), isLb.data(), isUb.data(),
NSCMAX, NSCMAX, NCAMAX, NCAMAX,
preVars_noq, ui, postVars_noq,
qBddVars,
ss.str().c_str(),
outRelationFile.c_str());
}
}; /* class ncsModel */
class ncsFIFOTransitionRelation : public ncsTransitionRelation {
private:
ncsState* sourceState;
void Init(){
sourceState = new ncsFifoState(NSCMAX, NCAMAX,
SS_DIM, IS_DIM,
ssEta, ssLb, ssUb, isEta, isLb, isUb,
ssVarsCount, isVarsCount,
x1xn, u1um, q1qn);
}
public:
// Constructors
ncsFIFOTransitionRelation(Cudd& cuddManager_, const char* NBDD_INPUT_FILE) :
ncsTransitionRelation(cuddManager_, NBDD_INPUT_FILE){
Init();
}
ncsFIFOTransitionRelation(Cudd& cuddManager_,
const char* BDD_INPUT_FILE,
const size_t SS_DIM_, const size_t IS_DIM_,
const size_t NSCMAX_, const size_t NCAMAX_) :
ncsTransitionRelation(cuddManager_, BDD_INPUT_FILE, SS_DIM_, IS_DIM_, NSCMAX_, NSCMAX_, NCAMAX_, NCAMAX_){
Init();
}
~ncsFIFOTransitionRelation(){
delete sourceState;
}
ncsState* getSourceStateTemplate(){
return sourceState;
}
// Interface methods enforced by the abstract class
/* Construct rules of the Q-based Transitions as a BDD */
BDD ConstructQTransitionRules(){
BDD qTransRules = pCuddManager->bddOne();
// 1] qd1 is always zero
BDD R1_qd1_zero = !pCuddManager->bddVar(qd1qdn[0]);
#ifdef VERBOSE
BDDUtils::PrintBDD("R1_qd1_zero", R1_qd1_zero);
#endif
// 2] any q=1 ==> its x=0
BDD R2_qOne_implies_xZero = pCuddManager->bddOne();
for(size_t i=0; i<NSCMAX; i++){
BDD qEqOne;
BDD xEqZeros;
qEqOne = pCuddManager->bddVar(q1qn[i]);
xEqZeros = BDDUtils::ConstructZerosAnd(*pCuddManager, x1xn[i]);
R2_qOne_implies_xZero *= qEqOne.Ite(xEqZeros, pCuddManager->bddOne());
qEqOne = pCuddManager->bddVar(qd1qdn[i]);
xEqZeros = BDDUtils::ConstructZerosAnd(*pCuddManager, xd1xdn[i]);
R2_qOne_implies_xZero *= qEqOne.Ite(xEqZeros, pCuddManager->bddOne());
}
#ifdef VERBOSE
BDDUtils::PrintBDD("R2_qOne_implies_xZero", R2_qOne_implies_xZero);
#endif
// 3] Allowed configurations of Qs (Pyramid-Upsidedown)
BDD R3_Allowed_Q_Configs = pCuddManager->bddZero();
size_t skip_q =0, skip_qd = 1; // this allows for the (all-q-in-source case)
for(size_t i=0; i<NSCMAX; i++){
BDD subR3 = pCuddManager->bddOne();
for(size_t j=0; j<q1qn.size(); j++){
if(j >= skip_q)
subR3 *= pCuddManager->bddVar(q1qn[j]);
else
subR3 *= !pCuddManager->bddVar(q1qn[j]);
}
for(size_t j=0; j<qd1qdn.size(); j++){
if(j >= skip_qd)
subR3 *= pCuddManager->bddVar(qd1qdn[j]);
else
subR3 *= !pCuddManager->bddVar(qd1qdn[j]);
}
skip_q++;
skip_qd++;
R3_Allowed_Q_Configs += subR3;
}
#ifdef VERBOSE
BDDUtils::PrintBDD("R3_Allowed_Q_Configs", R3_Allowed_Q_Configs);
#endif
// 4] All us are the same
BDD R4_AllUsTheSame = pCuddManager->bddOne();
for(size_t i=0; i<NCAMAX; i++){
R4_AllUsTheSame *= BDDUtils::BddSyncValues(*pCuddManager, ui, u1um[i]);
R4_AllUsTheSame *= BDDUtils::BddSyncValues(*pCuddManager, ui, ud1udm[i]);
}
#ifdef VERBOSE
BDDUtils::PrintBDD("R4_AllUsTheSame", R4_AllUsTheSame);
#endif
// Collect Rules
qTransRules = R1_qd1_zero * R2_qOne_implies_xZero * R3_Allowed_Q_Configs*R4_AllUsTheSame;
#ifdef VERBOSE
BDDUtils::PrintBDD("qTransRules", qTransRules);
#endif
return qTransRules;
}
/* Construct rules of the Non-Q Transitions as a BDD */
BDD ConstructNormalTransRules(const BDD& f_org_permuted, const BDD& f_org_projected){
size_t nBddVarsX = x1xn[0].size();
// 1] R1: follow the original transition
BDD R1_OrgTrans_Permuted = f_org_permuted;
// 2] R2: X-Coherency (xs come from Xs not arbitrary)
BDD R2_XCoherency = pCuddManager->bddOne();
for(size_t i=0; i<NSCMAX-1; i++){
// x[i+1] --> x[i]
vector<int> permute = BDDUtils::GetReadyPermuteMap(*pCuddManager);
for(size_t j=0; j<nBddVarsX; j++){
permute[x1xn[0][j]] = x1xn[i+1][j]; // the pre
permute[xd1xdn[0][j]] = x1xn[i][j]; // the post
}
R2_XCoherency *= f_org_projected.Permute(permute.data());
#ifdef VERBOSE
BDDUtils::PrintBDD("R2_XCoherency", R2_XCoherency);
#endif
}
// 3] R3: Xs are shifted from pre to post (synchronization)
BDD R3_XShifting = pCuddManager->bddOne();
for(size_t i=0; i<NSCMAX-1; i++){
R3_XShifting *= BDDUtils::BddSyncValues(*pCuddManager, x1xn[i], xd1xdn[i+1]);
}
#ifdef VERBOSE
BDDUtils::PrintBDD("R3_XShifting", R3_XShifting);
#endif
// 4] R4: Input ui goes to first Input in post (syncronization)
BDD R4_ui_ud1_sync = BDDUtils::BddSyncValues(*pCuddManager, ui, ud1udm[0]);
#ifdef VERBOSE
BDDUtils::PrintBDD("R4_ui_ud1_sync", R4_ui_ud1_sync);
#endif
// 5] R5 Us are shifted from pre to post
BDD R5_UShifting = pCuddManager->bddOne();
for(size_t i=0; i<NCAMAX-1; i++){
R5_UShifting *= BDDUtils::BddSyncValues(*pCuddManager, u1um[i], ud1udm[i+1]);
}
#ifdef VERBOSE
BDDUtils::PrintBDD("R5_UShifting", R5_UShifting);
#endif
// Combine all
BDD normalTransRules = R1_OrgTrans_Permuted*R2_XCoherency*R3_XShifting*R4_ui_ud1_sync*R5_UShifting;
#ifdef VERBOSE
BDDUtils::PrintBDD("normalTransRules", normalTransRules);
#endif
return normalTransRules;
}
};
#endif /* NCSTRANSITIONRELATION_HH_ */