mCRL2
Loading...
Searching...
No Matches
parelm.h
Go to the documentation of this file.
1// Author(s): Wieger Wesselink
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 mcrl2/lps/parelm.h
10/// \brief The parelm algorithm.
11
12#ifndef MCRL2_LPS_PARELM_H
13#define MCRL2_LPS_PARELM_H
14
15#include "mcrl2/lps/detail/lps_algorithm.h"
16#include "mcrl2/utilities/detail/iota.h"
17#include "mcrl2/utilities/reachable_nodes.h"
18#include <boost/integer.hpp>
19
20namespace mcrl2::lps
21{
22
23namespace detail
24{
25 inline
26 void collect_transition_variables(const action_summand& s, std::set<data::variable>& result)
27 {
28 std::set<data::variable> tmp;
29
30 tmp = data::find_all_variables(s.condition());
31 result.insert(tmp.begin(), tmp.end());
32
33 tmp = lps::find_all_variables(s.multi_action());
34 result.insert(tmp.begin(), tmp.end());
35 }
36
37 inline
38 void collect_transition_variables(const stochastic_action_summand& s, std::set<data::variable>& result)
39 {
40 collect_transition_variables(static_cast<const action_summand&>(s), result);
41 std::set<data::variable> tmp = lps::find_all_variables(s.distribution());
42 result.insert(tmp.begin(), tmp.end());
43 }
44
45 inline
46 void collect_transition_variables(const deadlock_summand& s, std::set<data::variable>& result)
47 {
48 std::set<data::variable> tmp;
49
50 tmp = data::find_all_variables(s.condition());
51 result.insert(tmp.begin(), tmp.end());
52
53 tmp = lps::find_all_variables(s.deadlock());
54 result.insert(tmp.begin(), tmp.end());
55 }
56
57} // namespace detail
58
59/// \brief Algorithm class for elimination of unused parameters from a linear
60/// process specification
61template <typename Specification>
62class parelm_algorithm: public lps::detail::lps_algorithm<Specification>
63{
64 using super = typename lps::detail::lps_algorithm<Specification>;
65 using super::m_spec;
66 using super::verbose;
67
68 protected:
69
70 /// \brief Returns the data variables that are considered in the parelm algorithm.
71 /// \return The data variables that appear in the condition, action or time of the summands
73 {
74 std::set<data::variable> result;
75
76 auto const& action_summands = m_spec.process().action_summands();
77 for (auto i = action_summands.begin(); i != action_summands.end(); ++i)
78 {
79 detail::collect_transition_variables(*i, result);
80 }
81
82 auto const& deadlock_summands = m_spec.process().deadlock_summands();
83 for (auto i = deadlock_summands.begin(); i != deadlock_summands.end(); ++i)
84 {
85 detail::collect_transition_variables(*i, result);
86 }
87 return result;
88 }
89
90 void report_results(const std::set<data::variable>& to_be_removed) const
91 {
92 if (verbose())
93 {
94 std::clog << "parelm removed " << to_be_removed.size() << " process parameters: " <<std::endl;
95 for (const data::variable& v: to_be_removed)
96 {
97 std::clog << v << ":" << v.sort() << std::endl;
98 }
99 }
100 }
101
102 /// \brief First version of parelm1
103 void parelm1()
104 {
105 const data::variable_list& pars = m_spec.process().process_parameters();
106 std::set<data::variable> process_parameters(pars.begin(),pars.end());
107
108 // significant variables may not be removed by parelm
109 std::set<data::variable> significant_variables = transition_variables();
110
111 // recursively extend the set of significant variables
112 std::set<data::variable> todo = significant_variables;
113 while (!todo.empty())
114 {
115 data::variable x = *todo.begin();
116 todo.erase(todo.begin());
117
118 for (auto i = m_spec.process().action_summands().begin(); i != m_spec.process().action_summands().end(); ++i)
119 {
120 const data::assignment_list& assignments = i->assignments();
121 auto j = std::find_if(assignments.begin(), assignments.end(), [&](const data::assignment& a) { return a.lhs() == x; });
122 if (j != assignments.end())
123 {
124 std::set<data::variable> vars;
125 data::find_all_variables(j->rhs(), std::inserter(vars, vars.end()));
126 std::set<data::variable> new_variables = utilities::detail::set_difference(vars, significant_variables);
127 todo.insert(new_variables.begin(), new_variables.end());
128 significant_variables.insert(new_variables.begin(), new_variables.end());
129 }
130 }
131 }
132 std::set<data::variable> to_be_removed = utilities::detail::set_difference(process_parameters, significant_variables);
133 report_results(to_be_removed);
134 lps::remove_parameters(m_spec, to_be_removed);
135 assert(check_well_typedness(m_spec));
136 }
137
138 /// \brief Second version of parelm that builds a dependency graph
139 void parelm2()
140 {
141 const data::variable_list& pars = m_spec.process().process_parameters();
142 std::set<data::variable> process_parameters(pars.begin(),pars.end());
143
144 // create a mapping m from process parameters to integers
145 std::map<data::variable, std::size_t> m;
146 int index = 0;
147 for (const data::variable& process_parameter: process_parameters)
148 {
149 m[process_parameter] = index++;
150 }
151
152 // compute the initial set v of significant variables
153 std::set<data::variable> significant_variables = transition_variables();
154 std::vector<std::size_t> v;
155 for (const data::variable& significant_variable: significant_variables)
156 {
157 v.push_back(m[significant_variable]);
158 }
159
160 // compute the dependency graph G
161 using graph = boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS>;
162 graph G(process_parameters.size());
163 for (auto i = m_spec.process().action_summands().begin(); i != m_spec.process().action_summands().end(); ++i)
164 {
165 for (const data::assignment& a: i->assignments())
166 {
167 std::size_t j0 = m[a.lhs()];
168 std::set<data::variable> vars;
169 data::find_all_variables(a.rhs(), std::inserter(vars, vars.end()));
170 for (const data::variable& var: vars)
171 {
172 std::size_t k0 = m[var];
173 boost::add_edge(j0, k0, G);
174 }
175 }
176 }
177
178 // compute the reachable nodes (i.e. the significant parameters)
179 std::vector<std::size_t> r1 = mcrl2::utilities::reachable_nodes(G, v.begin(), v.end());
180 std::set<std::size_t> r2(r1.begin(), r1.end());
181 std::set<data::variable> to_be_removed;
182 for (const data::variable& process_parameter: process_parameters)
183 {
184 if (r2.find(m[process_parameter]) == r2.end())
185 {
186 to_be_removed.insert(process_parameter);
187 }
188 }
189 report_results(to_be_removed);
190 lps::remove_parameters(m_spec, to_be_removed);
191 assert(check_well_typedness(m_spec));
192 }
193
194 public:
195
196 /// \brief Constructor
197 parelm_algorithm(Specification& spec )
198 : lps::detail::lps_algorithm<Specification>(spec)
199 {}
200
201 /// \brief Runs the parelm algorithm
202 void run(bool variant1 = true)
203 {
204 if (variant1)
205 {
206 parelm1();
207 }
208 else
209 {
210 parelm2();
211 }
212 }
213};
214
215/// \brief Removes unused parameters from a linear process specification.
216/// \param spec A linear process specification
217/// \param variant1 If true the default variant of parelm is used, otherwise an
218/// alternative implementation is chosen.
219template <typename Specification>
220void parelm(Specification& spec, bool variant1 = true)
221{
222 parelm_algorithm<Specification> algorithm(spec);
223 algorithm.run(variant1);
224}
225
226} // namespace mcrl2::lps
227
228
229
230#endif // MCRL2_LPS_PARELM_H
\brief Assignment of a data expression to a variable
Definition assignment.h:88
LPS summand containing a multi-action.
LPS summand containing a deadlock.
Algorithm class for algorithms on linear process specifications. It can be instantiated with lps::spe...
Algorithm class for elimination of unused parameters from a linear process specification.
Definition parelm.h:63
parelm_algorithm(Specification &spec)
Constructor.
Definition parelm.h:197
std::set< data::variable > transition_variables()
Returns the data variables that are considered in the parelm algorithm.
Definition parelm.h:72
void parelm2()
Second version of parelm that builds a dependency graph.
Definition parelm.h:139
void report_results(const std::set< data::variable > &to_be_removed) const
Definition parelm.h:90
void run(bool variant1=true)
Runs the parelm algorithm.
Definition parelm.h:202
void parelm1()
First version of parelm1.
Definition parelm.h:103
LPS summand containing a multi-action.
A class that takes a linear process specification and checks all tau-summands of that LPS for conflue...
void collect_transition_variables(const stochastic_action_summand &s, std::set< data::variable > &result)
Definition parelm.h:38
void collect_transition_variables(const deadlock_summand &s, std::set< data::variable > &result)
Definition parelm.h:46
void collect_transition_variables(const action_summand &s, std::set< data::variable > &result)
Definition parelm.h:26
The main namespace for the LPS library.
Definition constelm.h:18
void parelm(Specification &spec, bool variant1=true)
Removes unused parameters from a linear process specification.
Definition parelm.h:220