-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain2.c
312 lines (252 loc) · 7.75 KB
/
main2.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <regex.h>
#include <math.h>
#include "library.h"
#include <time.h>
int main() {
/* code */
srand(time(NULL));
int T1[] = {1, 1,1,0,0,0,0,1,0,1,1,1,0,0,0,0,0,0,1,1,1,1,0,0,1,1,0,0,0,1,1,1,1,0,0,0,0,0,0,1,1,1,0,0,1,0,0,0,1,1,1,1,0,1,0,0,1,0,0,1,1,1,1,1,0,1,0,0,0,1,1,0,0,0,0,0,1,1,1,0,1};
matrix * Tadj = newMatrix(9,9);
for(int i = 0; i < 9;i++){
for(int j = 0; j < 9; j++){
ELEM(Tadj,i+1,j+1) = T1[i*9 + j];
}
}
printMatrix(Tadj);
int TQ1[] = {1,2,3,4,5,6,7,8,9};
int Tx1[] = {0, 0, 4, 6, 8, 8, 8, 4, 10};
int Ty1[] = {0, 4, 4, 4, 4, 2, 0, 0, 2};
node * TQ = NULL;
node * Tx = NULL;
node * Ty = NULL;
callback disp = display;
TQ = AppendList(TQ,TQ1,9);
Tx = AppendList(Tx, Tx1, 9);
Ty = AppendList(Ty, Ty1, 9);
traverse(TQ,disp);
printf("\n");
traverse(Tx,disp);
printf("\n");
traverse(Ty,disp);
int maxNum= 9;
int BigNum = 1000000;
matrix * dist = DistanceMatrix(Tadj, Tx, Ty, BigNum);
printMatrix(dist);
int N=9;
int x01[] = {1,2,3,4,5,6,7,8,1};
node * x0 = AppendList(x0,x01,9);
int d=2*N;
//--------Buchi Automaton--------
int N_p = 7;
Buchi * B1;
FILE *stream;
char BUCH_AUT[100000];
char* STATE_NAMES[50];
int sn_count = getStateNamesNew(stream, BUCH_AUT, STATE_NAMES);
B1 = Buchi_Struct_New(7);
matrix * neig = newMatrix(sn_count,sn_count);
for(int j1 = 1; j1 <= sn_count; j1++){
for(int j2 = 1; j2 <= sn_count; j2++){
if(CELL(B1->Trans,j1,j2)){
setElement(neig,j1,j2,1);
}
}
}
printf("\n\n\n\n");
printCellMatrix(B1->Trans);
printf("\n\n\n\n");
traverse(B1->S0,disp);
printf("\n\n\n\n");
traverse(B1->S,disp);
printf("\n\n\n\n");
traverse(B1->F,disp);
//--------end of Buchi automation--------
//-------------Atomic Propositions--------
cell_matrix * AP = newCellMatrix(N_p,N+2);
int num = -100;
//AP(:,:)={num}; initialize all of the cells to a number
for(int i = 1; i <= N_p;i++ ){
SetCell(AP,i,N+1,pow(2,i-1));
}
SetCell(AP,1,1,5);
SetCell(AP,1,2,5);
SetCell(AP,1,3,5);
SetCell(AP,2,2,1);
SetCell(AP,2,3,1);
SetCell(AP,2,4,1);
SetCell(AP,3,4,7);
SetCell(AP,3,5,7);
SetCell(AP,3,6,7);
SetCell(AP,4,6,8);
SetCell(AP,4,7,8);
SetCell(AP,5,7,4);
SetCell(AP,5,8,4);
SetCell(AP,6,8,3);
SetCell(AP,6,9,3);
SetCell(AP,7,1,7);
for(int i = 1; i <= AP->rows; i++){
for(int j = 1; j <= AP->cols - 2; j++){
if(getCell(AP,i,j)!=NULL){
AppendToCell(AP,i,N+2,j);
}
else{
SetCell(AP,i,j,num);
}
}
}
printCellMatrix(AP);
//AP(7,N+2)={6};AP(8,N+2)={2};%AP(8,N+2)={[6,7]};%indices of rbts involved in satisfaction of an AP
//-------------------------------------
int nMaxPre=400;
int n = ListLength(B1->S)*(maxNum)*N;
matrix * parent=newMatrix(nMaxPre*10,1);
matrix * Qpba = newMatrix(nMaxPre*10,N+1);
//initializing Qpba
node * temp = x0;
for(int i = 1; i <= ListLength(x0); i++){
setElement(Qpba,1,i,temp->data);
temp = temp->next;
}
setElement(Qpba,1,Qpba->cols,B1->S0->data);
printMatrix(Qpba);
node * path;
float epsilon=0.1;
int ind = 2;
int stop=0;
matrix * CostNode= newMatrix(nMaxPre*10,1);
matrix * SizeTree= newMatrix(nMaxPre*10,1);
//==================Build Prefix part========================================
int goal = B1->F->data;
node * F;
matrix * reject = newMatrix(1,nMaxPre*10);
int sample=0;
matrix * rejectExist= newMatrix(1,nMaxPre*10);
matrix * rejectInf= newMatrix(1,nMaxPre*10);
int f = 1;
node * candParents = NULL;
node * xNew = NULL;
node * sat = NULL;
node * indices = NULL;
node * qPrev;
matrix * QNext;
int qBnext;
int qBPrev;
matrix * QPTS;
matrix * QB;
int prevQPBAindex;
int newCost;
for(int n = 1; n <= nMaxPre-3; n++){
dispose(xNew);
xNew = NULL;
dispose(sat);
sat = NULL;
xNew = sampleReachablePTSpointTree(Qpba, N, Tadj, Tx, Ty,TQ,ind); //FIGURE THESE OUT TOMORROW
sat = observeInDiscreteEnvironment(N,N_p,AP,xNew,epsilon);
printf("SAT\n");
traverse(xNew,disp);
setElement(reject,1,n,0);
for(int q2 = 1; q2 <= ListLength(B1->S); q2++){
qBnext = getLinkedElement(B1->S, q2);
printf("\nQQQNEE%i\n", qBnext);
dispose(candParents);
candParents = NULL;
candParents = findBuchiNeighbors(qBnext,neig);
traverse(candParents,disp);
dispose(indices);
indices = NULL;
for(int jj = 1; jj <= ListLength(candParents); jj++){
qBPrev = getLinkedElement(candParents,jj);
for(int i = 1; i <= ind; i++){
if(qBPrev == ELEM(Qpba,i,N+1)){
indices = append(indices,i);
}
}
}
if(indices){
printf("DAVID\n");
traverse(indices,disp);
printf("DAVID\n");
deleteMatrix(QPTS);
QPTS = NULL;
deleteMatrix(QB);
QB = NULL;
dispose(qPrev);
qPrev = NULL;
QPTS = Qpts(Qpba,indices,N);
QB = Qb(Qpba,indices,N);
//printMatrix(QPTS);
//printMatrix(QB);
prevQPBAindex = findPrevPTSpoint(QPTS,xNew,dist,BigNum,CostNode,indices);
//printMatrix(QPTS);
printf("prevQPBAindex%i\n", prevQPBAindex);
if(prevQPBAindex){
qPrev = BuildQPREV(QPTS,QB,prevQPBAindex);
printf("QPREV\n");
traverse(qPrev,disp);
printf("QPRV\n");
deleteMatrix(QNext);
QNext = BuildQNEXT(xNew,qBnext,N);
printf("QNEXT\n");
//printMatrix(QNext);
printf("QNEX\n");
printf("ismem\n");
printf("break\n");
printf("ismem\n");
printf("break\n");
//printMatrix(Qpba);
printf("AMOUNT OF INDS\n%i", ind);
printf("AMOUNT OF INDS\n");
if(!(ismemberRows(QNext,Qpba))){
if(search(CELL(B1->Trans,getLinkedElement(qPrev,N+1),qBnext),LLSUM(sat))){
printf("gotit\n");
for(int d = 1; d<= N+1; d++){
ELEM(Qpba,ind,d) = ELEM(QNext,1,d);
}
ELEM(parent,ind,1) = ismemberRows(linkedToMatrix(qPrev),Qpba)->data;
ELEM(CostNode,ind,1) = ELEM(CostNode,ELEM(parent,ind,1),1) + costTree(linkedToMatrix(qPrev),QNext,dist);
ind++;
}
}
else{
printf("nogood\n");
}
}
}
// for jj=1:length(candParents)%for each possible qBprev
// qBPrev=candParents(jj);
// indices=[indices;find(Qpba(:,N+1)==qBPrev)];%indices of states in Qpba that point at states in Qba that reach the Buchi state qBnext
// end
// if ~isempty(indices)
// % Find the nearest node to xNew among all states in Qpba q=(?,qBprev):
// Qpts=Qpba(indices,1:N);
// Qb=Qpba(indices,N+1);
// %Given qNext=(xNew,qBnext), we have to determine: qPrev=(xPrev(?), qBPrev(?)):
// %[xPrev,indexQpts]=findPrevPTSpoint(Qpts,xNew,Dist,BigNum);%indexQpts points at the indexQpts-th state of subspace [Qpts,Qb]
// [xPrev,indexQpts]=findPrevPTSpoint_star_V2(Qpts,Qb,Qpba,xNew,qBnext,Dist,BigNum,parent,B1,AP,N_p,epsilon);
// qBPrev=Qb(indexQpts); %goal
// qPrev=[xPrev,qBPrev];%parent %goal
}
}
// printMatrix(Qpba);
// printMatrix(parent);
// printMatrix(CostNode);
// printMatrix(neig);
// printMatrix(CostNode);
printCellMatrix(B1->Trans);
xNew = NULL;
xNew = append(xNew,1);
xNew = append(xNew,1);
xNew = append(xNew,1);
xNew = append(xNew,1);
xNew = append(xNew,3);
xNew = append(xNew,7);
xNew = append(xNew,6);
xNew = append(xNew,7);
xNew = append(xNew,2);
traverse(observeInDiscreteEnvironment(N,N_p,AP,xNew,epsilon),disp);
subprintMatrix(Qpba,ind);
return 0;
}