Browse Source

sampling from a distribution and from a choice

tempestpy_adaptions
Sebastian Junges 4 years ago
parent
commit
aa6a3d2142
  1. 5
      src/storm/generator/Choice.cpp
  2. 11
      src/storm/generator/Choice.h
  3. 14
      src/storm/storage/Distribution.cpp
  4. 11
      src/storm/storage/Distribution.h

5
src/storm/generator/Choice.cpp

@ -44,6 +44,11 @@ namespace storm {
this->addOriginData(other.originData.get());
}
}
template<typename ValueType, typename StateType>
StateType Choice<ValueType, StateType>::sampleFromDistribution(ValueType const& quantile) const {
return distribution.sampleFromDistribution(quantile);
}
template<typename ValueType, typename StateType>
typename storm::storage::Distribution<ValueType, StateType>::iterator Choice<ValueType, StateType>::begin() {

11
src/storm/generator/Choice.h

@ -28,6 +28,17 @@ namespace storm {
* Adds the given choice to the current one.
*/
void add(Choice const& other);
/**
* Given a value q, find the event in the ordered distribution that corresponds to this prob.
* Example: Given a (sub)distribution { x -> 0.4, y -> 0.3, z -> 0.2 },
* A value q in [0,0.4] yields x, q in [0.4, 0.7] yields y, and q in [0.7, 0.9] yields z.
* Any other value for q yields undefined behavior.
*
* @param quantile q, a value in the CDF.
* @return A state
*/
StateType sampleFromDistribution(ValueType const& quantile) const;
/*!
* Returns an iterator to the distribution associated with this choice.

14
src/storm/storage/Distribution.cpp

@ -176,6 +176,20 @@ namespace storm {
entry.second /= sum;
}
}
template<typename ValueType, typename StateType>
StateType Distribution<ValueType, StateType>::sampleFromDistribution(const ValueType &quantile) const {
ValueType sum = storm::utility::zero<ValueType>();
storm::utility::ConstantsComparator<ValueType> comp;
for (auto const& entry: distribution) {
sum += entry.second;
if (comp.isLess(quantile,sum)) {
return entry.first;
}
}
STORM_LOG_ASSERT(false,"This point should not be reached.");
return 0;
}
template class Distribution<double>;

11
src/storm/storage/Distribution.h

@ -148,6 +148,17 @@ namespace storm {
* Normalizes the distribution such that the values sum up to one.
*/
void normalize();
/**
* Given a value q, find the event in the ordered distribution that corresponds to this prob.
* Example: Given a (sub)distribution { x -> 0.4, y -> 0.3, z -> 0.2 },
* A value q in [0,0.4] yields x, q in [0.4, 0.7] yields y, and q in [0.7, 0.9] yields z.
* Any other value for q yields undefined behavior.
*
* @param quantile q, a value in the CDF.
* @return A state
*/
StateType sampleFromDistribution(ValueType const& quantile) const;
private:
// A list of states and the probabilities that are assigned to them.

Loading…
Cancel
Save