mCRL2
Loading...
Searching...
No Matches
binary.h
Go to the documentation of this file.
1// Author(s): Jeroen Keiren
2// Copyright: see the accompanying file COPYING or copy at
3// https://github.com/mCRL2org/mCRL2/blob/master/COPYING
4//
5// Distributed under the Boost Software License, Version 1.0.
6// (See accompanying file LICENSE_1_0.txt or copy at
7// http://www.boost.org/LICENSE_1_0.txt)
8//
9/// \file binary.h
10/// \brief The binary algorithm.
11
12#ifndef MCRL2_LPS_BINARY_H
13#define MCRL2_LPS_BINARY_H
14
15#include "mcrl2/data/enumerator.h"
16#include "mcrl2/lps/detail/lps_algorithm.h"
17#include "mcrl2/lps/detail/parameter_selection.h"
18
19namespace mcrl2::lps
20{
21
22// Compute the number of booleans needed to represent a set of size n.
23inline
25{
26 if(n == 1)
27 {
28 return 1;
29 }
30 else
31 {
32 return utilities::ceil_log2(n-1);
33 }
34}
35
36/// \brief Algorithm class that can be used to apply the binary algorithm
37///
38/// All parameters of finite data types are replaced with a vector of
39/// booleans.
40template<typename DataRewriter, typename Specification>
41class binary_algorithm: public detail::lps_algorithm<Specification>
42{
44 using super = typename detail::lps_algorithm<Specification>;
45 using process_type = typename Specification::process_type;
46 using action_summand_type = typename process_type::action_summand_type;
47 using super::m_spec;
48
49 protected:
50 /// Rewriter
51 DataRewriter m_rewriter;
52
54
55 /// Mapping of finite variables to boolean vectors
57
58 /// Mapping of variables to all values they can be assigned
60
61 /// Mapping of variables to corresponding if-tree
63
64 /// Contains the names of variables appearing in rhs of m_if_trees
66
67 /// Identifier generator for the enumerator
69
70 /// \brief Build an if-then-else tree of enumerated elements in terms
71 /// of new parameters.
72 /// \pre enumerated_elements.size() <= 2^new_parameters.size()
73 /// \return if then else tree from enumerated_elements in terms of new_parameters
74 data::data_expression make_if_tree(data::variable_vector new_parameters,
75 const data::data_expression_vector& enumerated_elements)
76 {
77 data::data_expression result;
78
79 if (new_parameters.empty())
80 {
81 result = enumerated_elements.front();
82 }
83 else
84 {
85 std::size_t n = enumerated_elements.size();
86 std::size_t m = static_cast<std::size_t>(1) << (new_parameters.size() - 1); //m == 2^(new_parameters.size() - 1)
87
88 if (m > n)
89 {
90 m = n;
91 }
92
93 data::data_expression_vector left_list(enumerated_elements.begin(), enumerated_elements.begin() + static_cast<std::ptrdiff_t>(m));
94 data::data_expression_vector right_list;
95 if (m == n)
96 {
97 right_list = data::data_expression_vector(enumerated_elements.begin() + static_cast<std::ptrdiff_t>(m) - 1, enumerated_elements.end());
98 }
99 else
100 {
101 right_list = data::data_expression_vector(enumerated_elements.begin() + static_cast<std::ptrdiff_t>(m), enumerated_elements.end());
102 }
103
104 data::data_expression condition = new_parameters.back();
105 new_parameters.pop_back();
106 result = if_(condition,
107 make_if_tree(new_parameters, right_list),
108 make_if_tree(new_parameters, left_list));
109 }
110
111 return result;
112 }
113
114 /// \brief Determine which variables should be replaced, based on parameter_selection
115 /// \return A subset of the process parameters, with a finite sort that is not Bool
117 {
118 const data::variable_list& process_parameters = m_spec.process().process_parameters();
119 bool use_selection = !parameter_selection.empty();
120
121 std::list<data::variable> selected_params;
122 if (use_selection)
123 {
124 const data::variable_vector sel =
125 detail::parse_lps_parameter_selection(process_parameters, m_spec.data(), parameter_selection);
126 selected_params = std::list<data::variable>(sel.begin(), sel.end());
127 }
128 else
129 {
130 selected_params = std::list<data::variable>(process_parameters.begin(), process_parameters.end());
131 }
132 selected_params.remove_if([&](const data::variable& v)
133 {
134 bool cannot_replace = v.sort() == data::sort_bool::bool_() || !m_spec.data().is_certainly_finite(v.sort());
135 if (cannot_replace && use_selection)
136 {
137 mCRL2log(log::log_level_t::info) << "Not selecting " << v << ":" << v.sort() << " since it is already Bool, or its type is not finite." << std::endl;
138 }
139 return cannot_replace;
140 }
141 );
142
143 if (use_selection && selected_params.empty())
144 {
145 mCRL2log(log::log_level_t::info) << "No parameters were selected to be replaced." << std::endl;
146 }
147
148 return std::set<data::variable>(selected_params.begin(), selected_params.end());
149 }
150
151 /// \brief Take a specification and calculate a vector of boolean variables for each process
152 /// parameter in selected_params. A mapping variable -> vector of booleans is stored in new_parameters_table
153 /// a mapping variable -> enumerated elements is stored in enumerated_elements_table
154 /// \pre all elements in selected_params should not be of type Bool and should have a finite type
155 /// \return data variable list with the new process parameters (i.e. with all variables of a
156 /// finite type != bool replaced by a vector of boolean variables.
157 void replace_enumerated_parameters(const std::set<data::variable>& selected_params)
158 {
159 data::variable_list process_parameters = m_spec.process().process_parameters();
160 data::variable_vector new_parameters;
161
162 mCRL2log(log::log_level_t::debug) << "Original process parameters: " << data::pp(process_parameters) << std::endl;
163
165 generator.add_identifiers(lps::find_identifiers(m_spec));
166 generator.add_identifiers(data::function_and_mapping_identifiers(m_spec.data()));
167 bool accept_solutions_with_variables = false;
168 data::enumerator_algorithm<> enumerator(m_rewriter, m_spec.data(), m_rewriter, m_id_generator, accept_solutions_with_variables);
169
170 // Transpose all process parameters, and replace those that are finite, and not bool with boolean variables.
171 for (const data::variable& par: process_parameters)
172 {
173 if (selected_params.find(par) != selected_params.end())
174 {
175 //Get all constructors for par
176 data::data_expression_vector enumerated_elements; // List to store enumerated elements of a parameter
177
178 data::mutable_indexed_substitution<> local_sigma;
179 const data::variable_list vl{ par };
180 enumerator.enumerate(enumerator_element(vl, data::sort_bool::true_()),
181 local_sigma,
182 [&](const enumerator_element& p)
183 {
184 p.add_assignments(vl, local_sigma, m_rewriter);
185 enumerated_elements.push_back(local_sigma(par));
186 return false;
187 }
188 );
189 m_enumerated_elements[par] = enumerated_elements;
190
191 //Calculate the number of booleans needed to encode par
192 std::size_t n = nr_of_booleans_for_elements(enumerated_elements.size());
193
194 //Set hint for fresh variable names
195 std::string par_name = par.name();
196
197 // Temp list for storage
198 data::variable_vector new_pars;
199 //Create new parameters and add them to the parameter list.
200 for (std::size_t i = 0; i<n; ++i)
201 {
202 data::variable v(generator(par_name), data::sort_bool::bool_());
203 new_parameters.push_back(v);
204 new_pars.push_back(v);
205 }
206 // n = new_pars.size() && new_pars.size() = ceil(log_2(j)) && new_pars.size() = ceil(log_2(enumerated_elements.size()))
207
208 mCRL2log(log::log_level_t::verbose) << "Parameter " << data::pp(par) << ":" << data::pp(par.sort()) << " has been replaced by " << new_pars.size() << " parameter(s) " << data::pp(new_pars) << " of sort Bool" << std::endl;
209
210 //Store new parameters in a hastable
211 m_new_parameters[par]=new_pars;
212
213 m_if_trees[par]=make_if_tree(new_pars,
214 enumerated_elements);
215 }
216 else
217 {
218 new_parameters.push_back(par);
219 }
220 }
221
222 mCRL2log(log::log_level_t::debug) << "New process parameter(s): " << data::pp(new_parameters) << std::endl;
223
224 m_spec.process().process_parameters() = data::variable_list(new_parameters.begin(),new_parameters.end());
225 for (const data::variable& v: data::substitution_variables(m_if_trees))
226 {
227 m_if_trees_generator.add_identifier(v.name());
228 }
229 }
230
231 /// \brief Replace expressions in v that are of a finite sort with a
232 /// vector of assignments to Boolean variables.
234 const data::variable_list& vl,
235 const data::data_expression_list& el)
236 {
237 // We use replace_variables, to make sure that the binding variables of assignments are ignored.
238 // Note that this operation is safe because the generated fresh variables can not clash with other
239 // binding variables.
240 const data::data_expression_list el_ = data::replace_variables(el, m_if_trees);
241
242 data::data_expression_vector result;
243 data::variable_list::const_iterator i=vl.begin();
244 for (const data::data_expression& a: el_)
245 {
246 const data::variable par= *i;
247 i++;
248 if (m_new_parameters.find(par) == m_new_parameters.end()) // This parameter is not replaced by a boolean parameters.
249 {
250 result.push_back(a);
251 }
252 else
253 {
254 data::variable_vector new_parameters = m_new_parameters[par];
255 data::data_expression_vector elements = m_enumerated_elements[par];
256
257 mCRL2log(log::log_level_t::debug) << "Found " << new_parameters.size() << " new parameter(s) for parameter " << data::pp(par) << std::endl;
258
259 for (std::size_t j = 0; j < new_parameters.size(); ++j)
260 {
261 data::data_expression_vector disjuncts;
262
263 data::data_expression_vector::iterator k = elements.begin();
264 while (k != elements.end())
265 {
266 // Elements that get boolean value false
267 std::ptrdiff_t count(static_cast<std::ptrdiff_t>(1) << j);
268 if (std::distance(k, elements.end()) < count)
269 {
270 k = elements.end();
271 }
272 else
273 {
274 std::advance(k, count);
275 }
276
277 // Elements that get value true
278 for (std::ptrdiff_t l = 0; l < count && k != elements.end(); ++l)
279 {
280 disjuncts.push_back(data::equal_to(a, *k++));
281 }
282 }
283 result.push_back(data::lazy::join_or(disjuncts.begin(), disjuncts.end()));
284 }
285 }
286 }
287
288 mCRL2log(log::log_level_t::debug) << "Replaced expression(s) " << data::pp(el_) << " in the initial state with expression(s) " << data::pp(result) << std::endl;
289
290 return data::data_expression_list(result.begin(),result.end());
291 }
292
293 /// \brief Replace assignments in v that are of a finite sort with a
294 /// vector of assignments to Boolean variables.
295 data::assignment_list replace_enumerated_parameters_in_assignments(data::assignment_list v)
296 {
297 // We use replace_variables, to make sure that the binding variables of assignments are ignored.
298 // Note that this operation is safe because the generated fresh variables can not clash with other
299 // binding variables.
300 v = data::replace_variables(v, m_if_trees);
301
302 data::assignment_vector result;
303 for (const data::assignment& a: v)
304 {
305 if (m_new_parameters.find(a.lhs()) == m_new_parameters.end())
306 {
307 result.push_back(a);
308 }
309 else
310 {
311 data::variable_vector new_parameters = m_new_parameters[a.lhs()];
312 data::data_expression_vector elements = m_enumerated_elements[a.lhs()];
313
314 mCRL2log(log::log_level_t::debug) << "Found " << new_parameters.size() << " new parameter(s) for parameter " << data::pp(a.lhs()) << std::endl;
315
316 for (std::size_t j = 0; j < new_parameters.size(); ++j)
317 {
318 data::data_expression_vector disjuncts;
319
320 data::data_expression_vector::iterator k = elements.begin();
321 while (k != elements.end())
322 {
323 // Elements that get boolean value false
324 std::ptrdiff_t count(static_cast<std::ptrdiff_t>(1) << j);
325 if (std::distance(k, elements.end()) < count)
326 {
327 k = elements.end();
328 }
329 else
330 {
331 std::advance(k, count);
332 }
333
334 // Elements that get value true
335 for (std::ptrdiff_t l = 0; l < count && k != elements.end(); ++l)
336 {
337 disjuncts.push_back(data::equal_to(a.rhs(), *k++));
338 }
339 }
340 result.emplace_back(new_parameters[j], data::lazy::join_or(disjuncts.begin(), disjuncts.end()));
341 }
342
343 }
344 }
345
346 mCRL2log(log::log_level_t::debug) << "Replaced assignment(s) " << data::pp(v) << " with assignment(s) " << data::pp(result) << std::endl;
347
348 return data::assignment_list(result.begin(),result.end());
349 }
350
351 /// \brief Update an action summand with the new Boolean parameters
353 {
354 s.condition() = data::replace_variables_capture_avoiding(s.condition(), m_if_trees, m_if_trees_generator);
355 s.multi_action()=lps::replace_variables_capture_avoiding(s.multi_action(), m_if_trees, m_if_trees_generator);
356 s.assignments() = replace_enumerated_parameters_in_assignments(s.assignments());
357 }
358
359 /// \brief Update an action summand with the new Boolean parameters
361 {
362 update_action_summand(static_cast<action_summand&>(s));
363 s.distribution() = lps::replace_variables_capture_avoiding(s.distribution(), m_if_trees, m_if_trees_generator);
364 }
365
366 /// \brief Update a deadlock summand with the new Boolean parameters
368 {
369 s.condition() = data::replace_variables_capture_avoiding(s.condition(), m_if_trees, m_if_trees_generator);
370 lps::replace_variables_capture_avoiding(s.deadlock(), m_if_trees, m_if_trees_generator);
371 }
372
373 process_initializer update_initial_process(const data::variable_list& parameters, const process_initializer& init)
374 {
375 return process_initializer(replace_enumerated_parameters_in_initial_expressions(parameters, init.expressions()));
376 }
377
379 {
380 /* return stochastic_process_initializer(replace_enumerated_parameters_in_initial_expressions(parameters, init.expressions()),
381 lps::replace_variables_capture_avoiding(init.distribution(), m_if_trees, m_if_trees_generator)
382 ); */
383 data::data_expression_list d = replace_enumerated_parameters_in_initial_expressions(parameters, init.expressions());
384 const stochastic_distribution dist = lps::replace_variables_capture_avoiding(init.distribution(), d, m_if_trees, m_if_trees_generator);
385 return stochastic_process_initializer(d, dist);
386 }
387
388 public:
389 /// \brief Constructor for binary algorithm
390 /// \param spec Specification to which the algorithm should be applied
391 /// \param r a rewriter for data
392 binary_algorithm(Specification& spec,
393 DataRewriter& r,
394 const std::string parameter_selection = "")
395 : detail::lps_algorithm<Specification>(spec),
396 m_rewriter(r),
398 {}
399
400 /// \brief Apply the algorithm to the specification passed in the
401 /// constructor
402 void run()
403 {
404 data::variable_list old_parameters = m_spec.process().process_parameters();
405 const std::set<data::variable> to_replace = select_parameters(m_parameter_selection);
406 replace_enumerated_parameters(to_replace);
407
408 // Initial process
409 mCRL2log(log::log_level_t::debug) << "Updating process initializer" << std::endl;
410 m_spec.initial_process() = update_initial_process(old_parameters, m_spec.initial_process());
411
412 // Summands
413 mCRL2log(log::log_level_t::debug) << "Updating summands" << std::endl;
414
415 // Use auto& so that for a stochastic_specification the concrete
416 // stochastic_action_summand type is deduced and the right overload is
417 // selected.
418 //
419 // TODO: The stochastic_action_summand path was never tested and does not
420 // work.
421 for (action_summand& a: m_spec.process().action_summands())
422 {
423 update_action_summand(a);
424 }
425
426 for (deadlock_summand& d: m_spec.process().deadlock_summands())
427 {
429 }
430 }
431};
432
433} // namespace mcrl2::lps
434
435#endif // MCRL2_LPS_BINARY_H
Identifier generator that stores the identifiers of the context in a set. Using the operator()() and ...
LPS summand containing a multi-action.
Algorithm class that can be used to apply the binary algorithm.
Definition binary.h:42
data::assignment_list replace_enumerated_parameters_in_assignments(data::assignment_list v)
Replace assignments in v that are of a finite sort with a vector of assignments to Boolean variables.
Definition binary.h:295
data::data_expression make_if_tree(data::variable_vector new_parameters, const data::data_expression_vector &enumerated_elements)
Build an if-then-else tree of enumerated elements in terms of new parameters.
Definition binary.h:74
binary_algorithm(Specification &spec, DataRewriter &r, const std::string parameter_selection="")
Constructor for binary algorithm.
Definition binary.h:392
void update_deadlock_summand(deadlock_summand &s)
Update a deadlock summand with the new Boolean parameters.
Definition binary.h:367
stochastic_process_initializer update_initial_process(const data::variable_list &parameters, const stochastic_process_initializer &init)
Definition binary.h:378
data::set_identifier_generator m_if_trees_generator
Contains the names of variables appearing in rhs of m_if_trees.
Definition binary.h:65
const std::string m_parameter_selection
Definition binary.h:53
void replace_enumerated_parameters(const std::set< data::variable > &selected_params)
Take a specification and calculate a vector of boolean variables for each process parameter in select...
Definition binary.h:157
data::data_expression_list replace_enumerated_parameters_in_initial_expressions(const data::variable_list &vl, const data::data_expression_list &el)
Replace expressions in v that are of a finite sort with a vector of assignments to Boolean variables.
Definition binary.h:233
void update_action_summand(action_summand &s)
Update an action summand with the new Boolean parameters.
Definition binary.h:352
std::set< data::variable > select_parameters(const std::string parameter_selection) const
Determine which variables should be replaced, based on parameter_selection.
Definition binary.h:116
void run()
Apply the algorithm to the specification passed in the constructor.
Definition binary.h:402
data::mutable_map_substitution m_if_trees
Mapping of variables to corresponding if-tree.
Definition binary.h:62
process_initializer update_initial_process(const data::variable_list &parameters, const process_initializer &init)
Definition binary.h:373
DataRewriter m_rewriter
Rewriter.
Definition binary.h:51
void update_action_summand(stochastic_action_summand &s)
Update an action summand with the new Boolean parameters.
Definition binary.h:360
data::enumerator_identifier_generator m_id_generator
Identifier generator for the enumerator.
Definition binary.h:68
LPS summand containing a deadlock.
Algorithm class for algorithms on linear process specifications. It can be instantiated with lps::spe...
LPS summand containing a multi-action.
\brief A stochastic distribution
#define mCRL2log(LEVEL)
mCRL2log(LEVEL) provides the stream used to log.
Definition logger.h:435
A class that takes a linear process specification and checks all tau-summands of that LPS for conflue...
The main namespace for the LPS library.
Definition constelm.h:18
std::size_t nr_of_booleans_for_elements(std::size_t n)
Definition binary.h:24