-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathomegaControlProblem.cpp
More file actions
353 lines (266 loc) · 9.52 KB
/
omegaControlProblem.cpp
File metadata and controls
353 lines (266 loc) · 9.52 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
/*
* omegaControlProblem.cpp
*
* date : 10.09.2020
* author : M. Khaled
* details : a class for symbolic control problems.
*
*/
#include "omegaControlProblem.h"
namespace pFacesOmegaKernels{
// ------------------------------------------
// class SymState
// ------------------------------------------
SymState::SymState()
:type(SYM_STATE_TYPE::DUMMY_STATE),value(0){
}
SymState::SymState(SYM_STATE_TYPE _type)
:type(_type),value(0){
}
SymState::SymState(SYM_STATE_TYPE _type, symbolic_t _value)
:type(_type),value(_value){
}
bool SymState::operator==(const SymState& anotherSymState) const {
return (type == anotherSymState.type)&&(value == anotherSymState.value);
}
// ------------------------------------------
// class SymModel
// ------------------------------------------
template<class F>
SymModel<F>::SymModel(const size_t _n_sym_states, const size_t _n_sym_controls, const std::vector<symbolic_t>& sym_inital_states, F& post_func)
:n_sym_states(_n_sym_states), n_sym_controls(_n_sym_controls),
overflow_state(SymState(SymState::SYM_STATE_TYPE::OVERFLOW_STATE, _n_sym_states)),
dummy_state(SymState(SymState::SYM_STATE_TYPE::DUMMY_STATE, _n_sym_states+1)),
get_sym_posts(post_func){
for (symbolic_t sym_inital_state : sym_inital_states){
if(!is_valid_sym_state(sym_inital_state))
throw std::runtime_error("SymModel::SymModel: one of the supplied initial states is not valid!");
SymState init_state = SymState(SymState::SYM_STATE_TYPE::NORMAL_STATE, sym_inital_state);
initial_states.push_back(init_state);
}
}
template<class F>
bool SymModel<F>::is_valid_sym_state(symbolic_t state) const {
return (state < n_sym_states);
}
template<class F>
bool SymModel<F>::is_valid_sym_control(symbolic_t control) const {
return (control < n_sym_controls);
}
template<class F>
bool SymModel<F>::is_overflow_state(const SymState& state) const {
if(state.type == SymState::SYM_STATE_TYPE::OVERFLOW_STATE)
return true;
return false;
}
template<class F>
bool SymModel<F>::is_dummy_state(const SymState& state) const {
if(state.type == SymState::SYM_STATE_TYPE::DUMMY_STATE)
return true;
return false;
}
template<class F>
std::vector<SymState> SymModel<F>::get_initial_states() const {
return initial_states;
}
template<class F>
SymState SymModel<F>::get_dummy_state() const {
return dummy_state;
}
template<class F>
SymState SymModel<F>::get_overflow_state() const {
return overflow_state;
}
template<class F>
size_t SymModel<F>::get_n_states(){
return n_sym_states;
}
template<class F>
size_t SymModel<F>::get_n_controls(){
return n_sym_controls;
}
template<class F>
std::vector<SymState> SymModel<F>::get_posts(const SymState& state, const symbolic_t control) const {
static std::vector<SymState> ret_overflow = {overflow_state};
// is dummy ?
if(is_dummy_state(state))
throw std::runtime_error("SymModel::get_posts: no posts for DUMMY states.");
// is overflow ?
if(is_overflow_state(state))
return ret_overflow;
// it is a normal state
if(is_valid_sym_state(state.value) && is_valid_sym_control(control)){
std::vector<symbolic_t> sym_post_states = get_sym_posts(state.value, control);
std::vector<SymState> ret;
for (symbolic_t sym_state : sym_post_states){
if(!is_valid_sym_state(sym_state))
return ret_overflow;
ret.push_back(SymState(SymState::SYM_STATE_TYPE::NORMAL_STATE, sym_state));
}
return ret;
}
else
throw std::runtime_error("SymModel::get_posts: Invalid input state or control.");
}
template<class F>
SymState SymModel<F>::construct_state(const symbolic_t val){
if(val == dummy_state.value)
return get_dummy_state();
if(val == overflow_state.value)
return get_overflow_state();
return SymState(SymState::SYM_STATE_TYPE::NORMAL_STATE, val);
}
template<class F>
void SymModel<F>::print_info(){
std::cout << "Num states: " << n_sym_states << std::endl;
std::cout << "Num controls: " << n_sym_controls << std::endl;
std::cout << "Initial states: " << std::endl;
for (SymState initial_state : initial_states)
std::cout << "x_" << initial_state.value << " ";
std::cout << std::endl;
std::cout << "Transitions: " << std::endl;
for(size_t x=0; x<n_sym_states; x++){
for(size_t u=0; u<n_sym_controls; u++){
auto posts = get_sym_posts(x, u);
for(size_t xx_i=0; xx_i<posts.size(); xx_i++){
std::cout << "(" << x << "," << u << "," << posts[xx_i] << ") ";
}
}
}
std::cout << std::endl;
}
/* force generating the class implementation for function of type post_func_t */
template class SymModel<post_func_t>;
// ------------------------------------------
// class SymSpec
// ------------------------------------------
template<class L1, class L2>
bool SymSpec<L1, L2>::is_valid_AP(std::string AP){
// first letter must be alpha
if(!std::isalpha(AP[0]))
return false;
// all letters must be lower alpha or numeric
for(char letter : AP)
if(!(
(std::islower(letter) && std::isalpha(letter))
||
std::isdigit(letter)
))
return false;
return true;
}
template<class L1, class L2>
size_t SymSpec<L1, L2>::count_all_APs() const {
return state_APs.size() + control_APs.size();
}
template<class L1, class L2>
size_t SymSpec<L1, L2>::count_state_APs() const {
return state_APs.size();
}
template<class L1, class L2>
size_t SymSpec<L1, L2>::count_control_APs() const {
return control_APs.size();
}
template<class L1, class L2>
size_t SymSpec<L1, L2>::count_DPA_states() const {
return dpa.getStatesCount();
}
template<class L1, class L2>
SymSpec<L1, L2>::SymSpec(
const std::vector<std::string>& _state_APs,
const std::vector<std::string>& _control_APs,
const std::string& _ltl_formula,
L1& _L_x,
L2& _L_u):
L_x(_L_x),
L_u(_L_u),
dpa(TotalDPA(_state_APs, _control_APs, _ltl_formula)){
// fill local data
add_ltl_formula(_ltl_formula);
add_state_APs(_state_APs);
add_control_APs(_control_APs);
}
template<class L1, class L2>
SymSpec<L1, L2>::SymSpec(
const std::string& _dpa_file,
L1& _L_x,
L2& _L_u):
L_x(_L_x),
L_u(_L_u),
dpa(TotalDPA(_dpa_file)){
// fill local data
add_ltl_formula(dpa.getLtlFormula());
add_state_APs(dpa.getInVars());
add_control_APs(dpa.getOutVars());
}
template<class L1, class L2>
void SymSpec<L1, L2>::add_ltl_formula(std::string _ltl_spec){
ltl_spec = _ltl_spec;
}
template<class L1, class L2>
void SymSpec<L1, L2>::add_state_APs(const std::vector<std::string>& _state_APs){
if(_state_APs.size() >= max_possible_state_APs)
throw std::runtime_error("SymSpec::add_state_APs: Maximum number of possible state APs reached !");
for(auto state_AP : _state_APs){
if(pfacesUtils::isVectorElement(control_APs, state_AP))
throw std::runtime_error("SymSpec::add_state_AP: One provided state AP is already used as a control AP.");
if(!is_valid_AP(state_AP))
throw std::runtime_error(
std::string("SymSpec::add_state_AP: Invalid state AP: an AP must be ") + valid_AP_descr
);
}
state_APs = _state_APs;
}
template<class L1, class L2>
void SymSpec<L1, L2>::add_control_APs(const std::vector<std::string>& _control_APs){
if(_control_APs.size() >= max_possible_control_APs)
throw std::runtime_error("SymSpec::add_control_AP: Maximum number of possible contorl APs reached !");
for(auto control_AP : _control_APs){
if(pfacesUtils::isVectorElement(state_APs, control_AP))
throw std::runtime_error("SymSpec::add_control_AP: The provided control AP is already used as a state AP.");
if(!is_valid_AP(control_AP))
throw std::runtime_error(
std::string("SymSpec::add_control_AP: Invalid control AP: an AP must be ") + valid_AP_descr
);
}
control_APs = _control_APs;
}
template<class L1, class L2>
std::vector<std::string> SymSpec<L1, L2>::get_state_APs(){
return state_APs;
}
template<class L1, class L2>
std::vector<std::string> SymSpec<L1, L2>::get_control_APs(){
return control_APs;
}
template<class L1, class L2>
std::string SymSpec<L1, L2>::get_ltl_formula(){
return ltl_spec;
}
template<class L1, class L2>
strix_aut::letter_t SymSpec<L1, L2>::get_complete_clause(const SymState& symState, const symbolic_t control) const {
// bits : ... 1 0
// vars : control_APs state_APs
// start with empty clause
strix_aut::letter_t clause = 0;
// add state APs having 'symState'
if(symState.type == SymState::SYM_STATE_TYPE::NORMAL_STATE && count_state_APs() > 0){
symbolic_t state = symState.value;
strix_aut::letter_t state_APs_clause = L_x(state);
clause = clause | state_APs_clause;
}
// add control APs having 'control'
if(count_control_APs() > 0){
strix_aut::letter_t control_APs_clause = L_u(control);
control_APs_clause = control_APs_clause << count_state_APs();
clause = clause | control_APs_clause;
}
return clause;
}
template<class L1, class L2>
void SymSpec<L1, L2>::print_info(){
dpa.printInfo();
}
/* force generating the class implementation for functions of types: L_x_func_t and L_u_func_t */
template class SymSpec<L_x_func_t, L_u_func_t>;
}