mCRL2
Loading...
Searching...
No Matches
constelm.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//
11
12#ifndef MCRL2_LPS_CONSTELM_H
13#define MCRL2_LPS_CONSTELM_H
14
16
17namespace mcrl2
18{
19
20namespace lps
21{
22
24template <typename DataRewriter, typename Specification = specification>
26{
28
29 protected:
33
36
38 std::map<data::variable, std::size_t> m_index_of;
39
41 const DataRewriter& R;
42
43 void LOG_CONSTANT_PARAMETERS(const data::mutable_map_substitution<>& sigma, const std::string& constant_removed_msg = "", const std::string& nothing_removed_msg = "")
44 {
45 if (mCRL2logEnabled(log::verbose))
46 {
47 if (sigma.empty())
48 {
49 mCRL2log(log::verbose) << nothing_removed_msg;
50 }
51 else
52 {
53 mCRL2log(log::verbose) << constant_removed_msg;
54 for (const auto& i : sigma)
55 {
56 mCRL2log(log::verbose) << data::pp(i.first) << " := " << data::pp(i.second) << std::endl;
57 }
58 }
59 }
60 }
61
63 const data::data_expression& Rd_j,
64 const data::data_expression& Rg_ij,
66 const std::string& msg = ""
67 )
68 {
69 if (mCRL2logEnabled(log::debug))
70 {
71 mCRL2log(log::debug) << msg
72 << data::pp(d_j) << "\n"
73 << " value before: " << Rd_j << "\n"
74 << " value after: " << Rg_ij << "\n"
75 << " replacements: " << sigma << std::endl;
76 }
77 }
78
80 const data::data_expression& c_i,
82 const std::string& msg = ""
83 )
84
85 {
86 if (mCRL2logEnabled(log::debug))
87 {
88 mCRL2log(log::debug) << msg
89 << cond
90 << sigma
91 << " -> "
92 << c_i << std::endl;
93 }
94 }
95
96 // returns true if x contains free variables that are not in global_variables
97 bool is_constant(const data::data_expression& x, const std::set<data::variable>& global_variables) const
98 {
100
101 for (const data::variable& v: find_free_variables(x))
102 {
103 if (!contains(global_variables, v))
104 {
105 return false;
106 }
107 }
108 return true;
109 }
110
111 public:
112
114 constelm_algorithm(Specification& spec, const DataRewriter& R_)
115 : lps::detail::lps_algorithm<Specification>(spec),
117 m_ignore_conditions(false),
118 R(R_)
119 {}
120
125 data::mutable_map_substitution<> compute_constant_parameters(bool instantiate_global_variables = false, bool ignore_conditions = false)
126 {
128
130
131 m_instantiate_global_variables = instantiate_global_variables;
132 m_ignore_conditions = ignore_conditions;
133 data::data_expression_list initial_state = super::m_spec.initial_process().expressions();
134 data::data_expression_vector r(initial_state.begin(), initial_state.end());
135
136 // essential: rewrite the initial state vector r to normal form. Essential
137 // because this value is used in W below, and assigned to the right hand side of a substitution, which
138 // must be a normal form.
139 lps::rewrite(r, R);
140
141 auto& process = super::m_spec.process();
142 const std::set<data::variable>& global_variables = super::m_spec.global_variables();
143 const data::variable_list& d = process.process_parameters();
144 const data::data_expression_list& e = super::m_spec.initial_process().expressions();
145
146 // initialize m_index_of
147 unsigned index = 0;
148 for (const data::variable& v: d)
149 {
150 m_index_of[v] = index++;
151 }
152
153 std::set<data::variable> G(d.begin(), d.end());
154 std::set<data::variable> dG;
155 auto di = d.begin();
156 auto ei = e.begin();
157 for (; di != d.end(); ++di, ++ei)
158 {
159 // The rewriter requires that the right hand sides of a substitution are in normal form.
160 data::data_expression rhs = R(*ei);
161 if (is_constant(rhs, global_variables))
162 {
163 sigma[*di] = rhs;
164 }
165 else
166 {
167 G.erase(*di);
168 }
169 }
170
171 // undo contains undo information of instantiations of free variables
172 std::map<data::variable, std::set<data::variable> > undo;
173
174 do
175 {
176 dG.clear();
177 for (const auto& summand: process.action_summands())
178 {
179 const data::data_expression& c_i = summand.condition();
181 {
182 for (const data::variable& j: G)
183 {
184 if (dG.find(j) != dG.end())
185 {
186 continue;
187 }
188 std::size_t index_j = m_index_of[j];
189 const data::variable& d_j = j;
190 data::data_expression g_ij = super::next_state(summand, d_j);
191
192 if (R(g_ij, sigma) != R(d_j, sigma))
193 {
194 LOG_PARAMETER_CHANGE(d_j, R(d_j, sigma), R(g_ij, sigma), sigma, "POSSIBLE CHANGE FOR PARAMETER ");
195 data::data_expression z = R(g_ij, sigma);
196 if (is_variable(z) && contains(global_variables, atermpp::down_cast<data::variable>(z)))
197 {
198 sigma[atermpp::down_cast<data::variable>(z)] = r[index_j];
199 undo[d_j].insert(atermpp::down_cast<data::variable>(z));
200 }
201 else
202 {
203 dG.insert(d_j);
204 sigma[d_j] = d_j; // erase d_j
205 for (const data::variable& w: undo[d_j])
206 {
207 sigma[w] = w; // erase *w
208 }
209 undo[d_j].clear();
210 }
211 }
212 else
213 {
214 LOG_PARAMETER_CHANGE(d_j, R(d_j, sigma), R(g_ij, sigma), sigma, "NO CHANGE FOR PARAMETER ");
215 }
216 }
217 }
218 else
219 {
220 LOG_CONDITION(summand.condition(), R(c_i, sigma), sigma, "CONDITION IS FALSE: ");
221 }
222 }
223 for (const data::variable& v: dG)
224 {
225 G.erase(v);
226 }
227 }
228 while (!dG.empty());
229
230 return sigma;
231 }
232
235 {
236 LOG_CONSTANT_PARAMETERS(sigma, "Removing the following constant parameters:\n", "No constant parameters are removed.\n");
237
238 // N.B. The order of removing constant parameters and rewriting has been reversed
239 // as requested by Jan Friso Groote. This may lead to some gain in performance (13%
240 // in the case of 6x6 othello). Note that due to this change the intermediate result
241 // after removing parameters may not be a valid LPS.
242
243 // remove the constant parameters from the specification spec
244 std::set<data::variable> constant_parameters;
245 for (const auto& i: sigma)
246 {
247 constant_parameters.insert(i.first);
248 }
249 lps::remove_parameters(super::m_spec, constant_parameters);
250
251 // rewrite the specification with substitution sigma
253 }
254
259 void run(bool instantiate_global_variables = false, bool ignore_conditions = false)
260 {
261 data::mutable_map_substitution<> sigma = compute_constant_parameters(instantiate_global_variables, ignore_conditions);
263 };
264};
265
270template <typename DataRewriter, typename Specification>
271void constelm(Specification& spec, const DataRewriter& R, bool instantiate_global_variables = false)
272{
274 algorithm.run(instantiate_global_variables);
275}
276
277} // namespace lps
278
279} // namespace mcrl2
280
281#endif // MCRL2_LPS_CONSTELM_H
const_iterator end() const
Returns a const_iterator pointing to the end of the term_list.
Definition aterm_list.h:282
const_iterator begin() const
Returns a const_iterator pointing to the beginning of the term_list.
Definition aterm_list.h:275
Generic substitution function. The substitution is stored as a mapping of variables to expressions....
\brief A data variable
Definition variable.h:28
Algorithm class for elimination of constant parameters.
Definition constelm.h:26
void LOG_PARAMETER_CHANGE(const data::data_expression &d_j, const data::data_expression &Rd_j, const data::data_expression &Rg_ij, const data::mutable_map_substitution<> &sigma, const std::string &msg="")
Definition constelm.h:62
lps::detail::lps_algorithm< Specification > super
Definition constelm.h:27
bool is_constant(const data::data_expression &x, const std::set< data::variable > &global_variables) const
Definition constelm.h:97
void remove_parameters(data::mutable_map_substitution<> &sigma)
Applies the substitution computed by compute_constant_parameters.
Definition constelm.h:234
const DataRewriter & R
The rewriter used by the constelm algorithm.
Definition constelm.h:41
void LOG_CONSTANT_PARAMETERS(const data::mutable_map_substitution<> &sigma, const std::string &constant_removed_msg="", const std::string &nothing_removed_msg="")
Definition constelm.h:43
bool m_ignore_conditions
If true, conditions are not evaluated and assumed to be true.
Definition constelm.h:35
constelm_algorithm(Specification &spec, const DataRewriter &R_)
Constructor.
Definition constelm.h:114
void LOG_CONDITION(const data::data_expression &cond, const data::data_expression &c_i, const data::mutable_map_substitution<> &sigma, const std::string &msg="")
Definition constelm.h:79
void run(bool instantiate_global_variables=false, bool ignore_conditions=false)
Runs the constelm algorithm.
Definition constelm.h:259
std::map< data::variable, std::size_t > m_index_of
Maps process parameters to their index.
Definition constelm.h:38
data::mutable_map_substitution compute_constant_parameters(bool instantiate_global_variables=false, bool ignore_conditions=false)
Computes constant parameters.
Definition constelm.h:125
bool m_instantiate_global_variables
If true, then the algorithm is allowed to instantiate free variables as a side effect.
Definition constelm.h:32
Algorithm class for algorithms on linear process specifications. It can be instantiated with lps::spe...
data::data_expression next_state(const action_summand &s, const data::variable &v) const
Applies the next state substitution to the variable v.
lps_algorithm(Specification &spec)
Constructor.
Specification & m_spec
The specification that is processed by the algorithm.
static RewriterCompilingJitty::substitution_type & sigma(RewriterCompilingJitty *this_rewriter)
#define mCRL2log(LEVEL)
mCRL2log(LEVEL) provides the stream used to log.
Definition logger.h:391
Add your file description here.
const function_symbol & false_()
Constructor for function symbol false.
Definition bool.h:109
std::vector< data_expression > data_expression_vector
\brief vector of data_expressions
std::string pp(const abstraction &x)
Definition data.cpp:39
@ verbose
Definition logger.h:37
void remove_parameters(Object &x, const std::set< data::variable > &to_be_removed)
Rewrites an LPS data type.
Definition remove.h:190
void constelm(Specification &spec, const DataRewriter &R, bool instantiate_global_variables=false)
Removes zero or more constant parameters from the specification spec.
Definition constelm.h:271
std::set< data::variable > find_free_variables(const lps::deadlock &x)
Definition lps.cpp:54
void rewrite(T &x, Rewriter R, typename std::enable_if<!std::is_base_of< atermpp::aterm, T >::value >::type *=nullptr)
Definition rewrite.h:27
bool contains(const atermpp::indexed_set< Key, ThreadSafe, Hash, Equals, Allocator, KeyTable > &c, const typename atermpp::indexed_set< Key, ThreadSafe, Hash, Equals, Allocator, KeyTable >::key_type &v, const std::size_t thread_index=0)
Definition indexed_set.h:86
A class that takes a linear process specification and checks all tau-summands of that LPS for conflue...
Definition indexed_set.h:72