mCRL2
Loading...
Searching...
No Matches
stochastic_state.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/stochastic_state.h
10/// \brief The class stochastic_state, a probability distribution over states.
11
12#ifndef MCRL2_LPS_STOCHASTIC_STATE_H
13#define MCRL2_LPS_STOCHASTIC_STATE_H
14
15#include "mcrl2/data/print.h"
16#include "mcrl2/data/rewriter.h"
17#include "mcrl2/data/real_utilities.h"
18#include "mcrl2/lps/state.h"
19
20namespace mcrl2::lps
21{
22
23// invariant: the elements of states must be unique
24// invariant: the elements of probabilities must be >= 0
25// invariant: the elements of probabilities must sum up to 1
26// invariant: |probabilities| = |states|
28{
29 // TODO: use a more efficient representation
32
33 stochastic_state() = default;
34
35 explicit stochastic_state(const state& s)
37 {}
38
39 void push_back(const data::data_expression& probability, const state& s)
40 {
41 probabilities.push_back(probability);
42 states.push_back(s);
43 }
44
45 void clear()
46 {
47 probabilities.clear();
48 states.clear();
49 }
50
51 std::size_t size() const
52 {
53 assert(states.size()==probabilities.size());
54 return states.size();
55 }
56};
57
58inline
60{
61 const auto& x_ = atermpp::down_cast<data::application>(x);
62 return data::pp(x_[0]) + '/' + data::pp(x_[1]);
63}
64
65inline
66void check_probability(const data::data_expression& x, const data::rewriter& rewr)
67{
68 const auto& x_ = atermpp::down_cast<data::application>(x);
69 if (x_.head().size() != 3)
70 {
71 throw mcrl2::runtime_error("The probability " + data::pp(x) + " is not a proper rational number.");
72 }
73 if (x_.head() != data::sort_real::creal())
74 {
75 throw mcrl2::runtime_error("Probability is not a closed expression with a proper enumerator and denominator: " + data::pp(x) + ".");
76 }
78 {
79 throw mcrl2::runtime_error("Probability is smaller than zero: " + data::pp(x) + ".");
80 }
82 {
83 throw mcrl2::runtime_error("Probability is greater than one: " + data::pp(x) + ".");
84 }
85}
86
87inline
89{
91 for (const data::data_expression& prob: s.probabilities)
92 {
93 check_probability(prob, rewr);
94 probability = data::sort_real::plus(probability, prob);
95 }
96 probability = rewr(probability);
97 if (probability != data::sort_real::real_one())
98 {
99 std::vector<std::string> v;
100 for (const data::data_expression& prob: s.probabilities)
101 {
102 v.push_back(print_probability(prob));
103 }
104 throw mcrl2::runtime_error("The probabilities " + core::detail::print_list(v) + " do not add up to one, but to " + pp(probability) + "." );
105 }
106}
107
108} // namespace mcrl2::lps
109
110#endif // MCRL2_LPS_STOCHASTIC_STATE_H
data_expression & operator=(data_expression &&) noexcept=default
Rewriter that operates on data expressions.
Definition rewriter.h:84
data_expression operator()(const data_expression &d) const
Rewrites a data expression.
Definition rewriter.h:161
Simulation process.
Definition simulation.h:22
void randomly_select_state()
Randomly choose one state among a sequence of probabilistic states.
bool m_auto_select_probabilistic_state
Definition simulation.h:85
void select_state(std::size_t state_number)
Choose one state among a sequence of probabilistic states.
simulation(const stochastic_specification &specification, data::rewrite_strategy strategy=data::rewrite_strategy())
Constructor.
std::vector< transition_type > transitions(const lps::state &source_state)
void truncate(std::size_t state_number, bool probabilistic)
Remove states from the end of the simulation, making state_number the last state.
#define mCRL2log(LEVEL)
mCRL2log(LEVEL) provides the stream used to log.
Definition logger.h:435
Namespace for system defined sort bool_.
Definition bool.h:29
const function_symbol & true_()
Constructor for function symbol true.
Definition bool.h:74
Namespace for system defined sort real_.
data_expression & real_one()
data_expression & real_zero()
application greater_equal(const data_expression &arg0, const data_expression &arg1)
Application of function symbol >=.
Definition standard.h:374
The main namespace for the LPS library.
Definition constelm.h:18
void check_stochastic_state(const stochastic_state &s, const data::rewriter &rewr)
void check_probability(const data::data_expression &x, const data::rewriter &rewr)
std::string print_probability(const data::data_expression &x)
void push_back(const data::data_expression &probability, const state &s)
std::vector< data::data_expression > probabilities
std::vector< state > states