-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluator.cc
239 lines (207 loc) · 7.93 KB
/
evaluator.cc
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
/*
evaluator.cc contains function:
1. formula_eval : evaluates the truth of a formula
2. guard_checker : checks truth of a guard, true -> enabled
3. check_locality : checks whether a variable in guard is local w.r.t the module
4. find_conac : finds a corresponding action from "normal" agent w.r.t a variable
*/
#include "heading.h"
extern vector<string> rpn;
extern stack<bool> bvr;
extern vector<string> rpn_ac;
extern struct env_ag env_ag;
extern vector<agent> ag;
extern vector<string> environment_vars;
bool formula_eval(vector<string>& rpn)
{
bool v1, v2;
while(!rpn.empty())
{
if(rpn.back()!="and" && rpn.back()!="or" && rpn.back()!="implies" && rpn.back()!="iff" && rpn.back()!="not")
{
if(rpn.back()=="true")
bvr.push(true);
else
bvr.push(false);
rpn.pop_back();
}
else if(rpn.back()=="not")
{
v1 = bvr.top(); bvr.pop();
bvr.push(!v1);
rpn.pop_back();
}
else
{
v1 = bvr.top(); bvr.pop();
v2 = bvr.top(); bvr.pop();
if(rpn.back()=="and")
{
bvr.push(v1 && v2);
}
else if(rpn.back()=="or")
bvr.push(v1 || v2);
else if(rpn.back()=="implies")
bvr.push(v1 || !v2);
else
bvr.push((!v1 || v2) && (!v2 || v1));
rpn.pop_back();
}
}
v1 = bvr.top(); bvr.pop();
return v1;
}
void guard_checker(vector<agent>& ag, int ag_count, int var_count)
{
bool z,as_check;
for(unsigned int i=0;i<ag[ag_count].agPro.size();i++)
{
for (unsigned int j=0;j<ag[ag_count].ag_up.size()-1;j++)
{
vector<string> vnext_t, vnext_f;
rpn.clear();rpn_ac.clear();
for(unsigned int k=0;k<ag[ag_count].ag_up[j].guard_formula.size();k++)
{
if(find(ag[ag_count].agPro[i].t_pro.begin(),ag[ag_count].agPro[i].t_pro.end(), ag[ag_count].ag_up[j].guard_formula[k])!=ag[ag_count].agPro[i].t_pro.end())
{
rpn.push_back("true");
}
else if(find(ag[ag_count].agPro[i].f_pro.begin(),ag[ag_count].agPro[i].f_pro.end(), ag[ag_count].ag_up[j].guard_formula[k])!=ag[ag_count].agPro[i].f_pro.end())
{
rpn.push_back("false");
}
else
{
if(ag[ag_count].ag_up[j].guard_formula[k]=="and" || ag[ag_count].ag_up[j].guard_formula[k]=="or" || ag[ag_count].ag_up[j].guard_formula[k]=="implies" || ag[ag_count].ag_up[j].guard_formula[k]=="iff" || ag[ag_count].ag_up[j].guard_formula[k]=="not" )
rpn.push_back(ag[ag_count].ag_up[j].guard_formula[k]);
else
rpn.push_back("true");
}
}
reverse(rpn.begin(),rpn.end());
z = formula_eval(rpn);
if(z)
{
for(unsigned int r=0; r<ag[ag_count].ag_up[j].act_formula.size()-1;r++)
{
for(unsigned int s=0;s<ag[ag_count].ag_up[j].act_formula[r].assign_formula.size();s++)
{
if(find(ag[ag_count].agPro[i].t_pro.begin(),ag[ag_count].agPro[i].t_pro.end(), ag[ag_count].ag_up[j].act_formula[r].assign_formula[s])!=ag[ag_count].agPro[i].t_pro.end())
{
rpn_ac.push_back("true");
}
else if(find(ag[ag_count].agPro[i].f_pro.begin(),ag[ag_count].agPro[i].f_pro.end(), ag[ag_count].ag_up[j].act_formula[r].assign_formula[s])!=ag[ag_count].agPro[i].f_pro.end())
{
rpn_ac.push_back("false");
}
else
{
rpn_ac.push_back(ag[ag_count].ag_up[j].act_formula[r].assign_formula[s]);
}
}
reverse(rpn_ac.begin(),rpn_ac.end());
as_check = formula_eval(rpn_ac);
if(as_check)
vnext_t.push_back(ag[ag_count].ag_up[j].act_formula[r].next_var);
else
vnext_f.push_back(ag[ag_count].ag_up[j].act_formula[r].next_var);
}
for(unsigned int g=0;g<ag[ag_count].ac.size();g++)
{
unsigned int counter = 0;
for(unsigned int l=0;l<vnext_t.size();l++)
{
if(find(ag[ag_count].ac[g].t_ac.begin(),ag[ag_count].ac[g].t_ac.end(),vnext_t[l])!=ag[ag_count].ac[g].t_ac.end())
{
counter++;
}
}
for(unsigned int l=0;l<vnext_f.size();l++)
{
if(find(ag[ag_count].ac[g].f_ac.begin(),ag[ag_count].ac[g].f_ac.end(),vnext_f[l])!=ag[ag_count].ac[g].f_ac.end())
{
counter++;
}
}
if(counter==(vnext_t.size()+vnext_f.size()))
{
if(!(find(ag[ag_count].agPro[i].ac_pro.begin(),ag[ag_count].agPro[i].ac_pro.end(),ag[ag_count].ac[g].acName)!=ag[ag_count].agPro[i].ac_pro.end()))
ag[ag_count].agPro[i].ac_pro.push_back(ag[ag_count].ac[g].acName);
}
}
}
}
}
}
int check_locality(vector<agent>& ag, int ag_count)
{
for(unsigned int i=0;i<ag[ag_count].ag_up.size()-1;i++)
{
for(unsigned int j=0;j<ag[ag_count].ag_up[i].guard_formula.size();j++)
{
if(ag[ag_count].ag_up[i].guard_formula[j]!="and" && ag[ag_count].ag_up[i].guard_formula[j]!="or" && ag[ag_count].ag_up[i].guard_formula[j]!="not" && ag[ag_count].ag_up[i].guard_formula[j]!="implies" && ag[ag_count].ag_up[i].guard_formula[j]!="iff" && ag[ag_count].ag_up[i].guard_formula[j]!="true" && ag[ag_count].ag_up[i].guard_formula[j]!="false")
{
if(!(find(ag[ag_count].varpro.begin(),ag[ag_count].varpro.end(),ag[ag_count].ag_up[i].guard_formula[j])!=ag[ag_count].varpro.end()))
{
ag[ag_count].varpro.push_back(ag[ag_count].ag_up[i].guard_formula[j]);
if((find(environment_vars.begin(),environment_vars.end(),ag[ag_count].ag_up[i].guard_formula[j])==environment_vars.end()))
if((!(find(env_ag.obs_idx.begin(),env_ag.obs_idx.end(),ag[ag_count].ag_up[i].guard_formula[j])!=env_ag.obs_idx.end())))
{
env_ag.obs_idx.push_back(ag[ag_count].ag_up[i].guard_formula[j]);
env_ag.seed.push_back(env_evo());
env_ag.seed[env_ag.seed.size()-1].obsvar = ag[ag_count].ag_up[i].guard_formula[j];
}
}
}
}
for(unsigned int q=0;q<ag[ag_count].ag_up[i].act_formula.size();q++)
{
for(unsigned int w=0;w<ag[ag_count].ag_up[i].act_formula[q].assign_formula.size();w++)
{
if(ag[ag_count].ag_up[i].act_formula[q].assign_formula[w]!="and" && ag[ag_count].ag_up[i].act_formula[q].assign_formula[w]!="or" && ag[ag_count].ag_up[i].act_formula[q].assign_formula[w]!="not" && ag[ag_count].ag_up[i].act_formula[q].assign_formula[w]!="if" && ag[ag_count].ag_up[i].act_formula[q].assign_formula[w]!="iff" && ag[ag_count].ag_up[i].act_formula[q].assign_formula[w]!="true" && ag[ag_count].ag_up[i].act_formula[q].assign_formula[w]!="false")
{
if(!(find(ag[ag_count].varpro.begin(),ag[ag_count].varpro.end(),ag[ag_count].ag_up[i].act_formula[q].assign_formula[w])!=ag[ag_count].varpro.end()))
{
//add observable var to varpro
ag[ag_count].varpro.push_back(ag[ag_count].ag_up[i].act_formula[q].assign_formula[w]);
if(!(find(env_ag.obs_idx.begin(),env_ag.obs_idx.end(),ag[ag_count].ag_up[i].act_formula[q].assign_formula[w])!=env_ag.obs_idx.end()))
{
env_ag.obs_idx.push_back(ag[ag_count].ag_up[i].act_formula[q].assign_formula[w]);
env_ag.seed.push_back(env_evo());
env_ag.seed[env_ag.seed.size()-1].obsvar = ag[ag_count].ag_up[i].act_formula[q].assign_formula[w];
}
}
}
}
}
}
return 0;
}
//adding controlling actions in Environment agent
void find_conac()
{
for(unsigned int i=0;i<env_ag.seed.size();i++)
{
//for(unsigned int j=0;env_ag.seed[i].obsvar.size();j++)
{
for(unsigned int k=0;k<ag.size();k++)
{
if((find(ag[k].varName.begin(),ag[k].varName.end(),env_ag.seed[i].obsvar)!=ag[k].varName.end()))
{
env_ag.seed[i].con_agent = ag[k].agName;
for(unsigned int l=0;l<ag[k].ac.size();l++)
{
if(find(ag[k].ac[l].t_ac.begin(),ag[k].ac[l].t_ac.end(),env_ag.seed[i].obsvar)!=ag[k].ac[l].t_ac.end())
{
env_ag.seed[i].t_conac.push_back(ag[k].ac[l].acName);
}
else
{
env_ag.seed[i].f_conac.push_back(ag[k].ac[l].acName);
}
}
}
}
}
}
}