18template<
typename PomdpType>
22template<
typename PomdpType>
28template<
typename PomdpType>
35template<
typename PomdpType>
36std::vector<typename PreprocessingPomdpValueBoundsModelChecker<PomdpType>::PomdpValueType>
37PreprocessingPomdpValueBoundsModelChecker<PomdpType>::getChoiceValues(std::vector<PomdpValueType>
const& stateValues,
38 std::vector<PomdpValueType>* actionBasedRewards) {
39 std::vector<PomdpValueType> choiceValues((
pomdp.getNumberOfChoices()));
40 pomdp.getTransitionMatrix().multiplyWithVector(stateValues, choiceValues, actionBasedRewards);
44template<
typename PomdpType>
45std::pair<std::vector<typename PreprocessingPomdpValueBoundsModelChecker<PomdpType>::PomdpValueType>,
47PreprocessingPomdpValueBoundsModelChecker<PomdpType>::computeValuesForGuessedScheduler(
48 storm::Environment const& env, std::vector<PomdpValueType>
const& stateValues, std::vector<PomdpValueType>* actionBasedRewards,
54 auto choiceValues = getChoiceValues(stateValues, actionBasedRewards);
55 auto const& choiceIndices =
pomdp.getTransitionMatrix().getRowGroupIndices();
56 std::vector<storm::storage::Distribution<PomdpValueType, uint_fast64_t>> choiceDistributions(
pomdp.getNrObservations());
57 for (uint64_t state = 0; state <
pomdp.getNumberOfStates(); ++state) {
58 auto& choiceDistribution = choiceDistributions[
pomdp.getObservation(state)];
59 PomdpValueType
const& stateValue = stateValues[state];
61 for (
auto choice = choiceIndices[state]; choice < choiceIndices[state + 1]; ++choice) {
62 PomdpValueType
const& choiceValue = choiceValues[choice];
69 choiceDistribution.addProbability(choice - choiceIndices[state], scoreThreshold);
71 PomdpValueType choiceScore = info.
minimize() ? (choiceValue - stateValue) : (stateValue - choiceValue);
79 if (choiceScore >= scoreThreshold) {
80 choiceDistribution.addProbability(choice - choiceIndices[state], choiceScore);
85 if (choiceDistribution.size() == 0) {
86 for (
auto choice = choiceIndices[state]; choice < choiceIndices[state + 1]; ++choice) {
87 choiceDistribution.addProbability(choice - choiceIndices[state], scoreThreshold);
90 STORM_LOG_ASSERT(choiceDistribution.size() > 0,
"Empty choice distribution.");
93 for (
auto& choiceDistribution : choiceDistributions) {
94 choiceDistribution.normalize();
97 for (uint64_t state = 0; state < pomdp.getNumberOfStates(); ++state) {
98 pomdpScheduler.setChoice(choiceDistributions[pomdp.getObservation(state)], state);
100 STORM_LOG_ASSERT(!pomdpScheduler.isPartialScheduler(),
"Expected a fully defined scheduler.");
101 auto scheduledModel = underlyingMdp->applyScheduler(pomdpScheduler,
false);
105 STORM_LOG_THROW(resultPtr, storm::exceptions::UnexpectedException,
"No check result obtained.");
106 STORM_LOG_THROW(resultPtr->isExplicitQuantitativeCheckResult(), storm::exceptions::UnexpectedException,
"Unexpected Check result Type.");
107 std::vector<PomdpValueType> pomdpSchedulerResult = resultPtr->template asExplicitQuantitativeCheckResult<PomdpValueType>().getSentinelValueVector();
108 return std::make_pair(pomdpSchedulerResult, pomdpScheduler);
111template<
typename PomdpType>
112std::pair<std::vector<typename PreprocessingPomdpValueBoundsModelChecker<PomdpType>::PomdpValueType>,
113 storm::storage::Scheduler<typename PreprocessingPomdpValueBoundsModelChecker<PomdpType>::PomdpValueType>>
114PreprocessingPomdpValueBoundsModelChecker<PomdpType>::computeValuesForRandomFMPolicy(storm::Environment
const& env, storm::logic::Formula
const& formula,
115 storm::pomdp::analysis::FormulaInformation
const& info,
116 uint64_t memoryBound) {
118 storm::storage::Scheduler<PomdpValueType> pomdpScheduler(pomdp.getNumberOfStates() * memoryBound);
120 STORM_LOG_DEBUG(
"Computing the unfolding for memory bound " << memoryBound);
122 storm::transformer::PomdpMemoryUnfolder<PomdpValueType> memoryUnfolder(pomdp, memory);
124 auto memPomdp = memoryUnfolder.transform(
false);
127 std::vector<uint64_t> obsChoiceVector(memPomdp->getNrObservations());
128 std::random_device rd;
129 auto engine = std::mt19937(rd());
130 for (uint64_t obs = 0; obs < memPomdp->getNrObservations(); ++obs) {
131 uint64_t nrChoices = memPomdp->getNumberOfChoices(memPomdp->getStatesWithObservation(obs).front());
132 std::uniform_int_distribution<uint64_t> uniform_dist(0, nrChoices - 1);
133 obsChoiceVector[obs] = uniform_dist(engine);
135 for (uint64_t state = 0; state < memPomdp->getNumberOfStates(); ++state) {
136 pomdpScheduler.setChoice(obsChoiceVector[memPomdp->getObservation(state)], state);
140 auto underlyingMdp = std::make_shared<storm::models::sparse::Mdp<PomdpValueType>>(memPomdp->getTransitionMatrix(), memPomdp->getStateLabeling(),
141 memPomdp->getRewardModels());
142 auto scheduledModel = underlyingMdp->applyScheduler(pomdpScheduler,
false);
145 STORM_LOG_THROW(resultPtr, storm::exceptions::UnexpectedException,
"No check result obtained.");
146 STORM_LOG_THROW(resultPtr->isExplicitQuantitativeCheckResult(), storm::exceptions::UnexpectedException,
"Unexpected Check result Type.");
147 std::vector<PomdpValueType> pomdpSchedulerResult = resultPtr->template asExplicitQuantitativeCheckResult<PomdpValueType>().getSentinelValueVector();
151 storm::storage::BitVector hasValue(pomdp.getNumberOfStates(),
false);
152 for (uint64_t memPomdpState = 0; memPomdpState < pomdpSchedulerResult.size(); ++memPomdpState) {
153 uint64_t modelState = memPomdpState / memoryBound;
154 if (!hasValue.get(modelState) || (info.
minimize() && pomdpSchedulerResult[memPomdpState] < res[modelState]) ||
155 (!info.
minimize() && pomdpSchedulerResult[memPomdpState] > res[modelState])) {
156 res[modelState] = pomdpSchedulerResult[memPomdpState];
157 hasValue.set(modelState);
160 return std::make_pair(res, pomdpScheduler);
163template<
typename PomdpType>
164[[maybe_unused]] std::pair<std::vector<typename PreprocessingPomdpValueBoundsModelChecker<PomdpType>::PomdpValueType>,
165 storm::storage::Scheduler<typename PreprocessingPomdpValueBoundsModelChecker<PomdpType>::PomdpValueType>>
166PreprocessingPomdpValueBoundsModelChecker<PomdpType>::computeValuesForRandomMemorylessPolicy(
167 storm::Environment
const& env, storm::logic::Formula
const& formula, storm::pomdp::analysis::FormulaInformation
const& info,
168 std::shared_ptr<storm::models::sparse::Mdp<PomdpValueType>> underlyingMdp) {
169 storm::storage::Scheduler<PomdpValueType> pomdpScheduler(pomdp.getNumberOfStates());
170 std::vector<uint64_t> obsChoiceVector(pomdp.getNrObservations());
172 std::random_device rd;
173 auto engine = std::mt19937(rd());
174 for (uint64_t obs = 0; obs < pomdp.getNrObservations(); ++obs) {
175 uint64_t nrChoices = pomdp.getNumberOfChoices(pomdp.getStatesWithObservation(obs).front());
176 std::uniform_int_distribution<uint64_t> uniform_dist(0, nrChoices - 1);
177 obsChoiceVector[obs] = uniform_dist(engine);
180 for (uint64_t state = 0; state < pomdp.getNumberOfStates(); ++state) {
181 STORM_LOG_DEBUG(
"State " << state <<
" -- Random Choice " << obsChoiceVector[pomdp.getObservation(state)]);
182 pomdpScheduler.setChoice(obsChoiceVector[pomdp.getObservation(state)], state);
185 auto scheduledModel = underlyingMdp->applyScheduler(pomdpScheduler,
false);
189 STORM_LOG_THROW(resultPtr, storm::exceptions::UnexpectedException,
"No check result obtained.");
190 STORM_LOG_THROW(resultPtr->isExplicitQuantitativeCheckResult(), storm::exceptions::UnexpectedException,
"Unexpected Check result Type.");
191 std::vector<PomdpValueType> pomdpSchedulerResult = resultPtr->template asExplicitQuantitativeCheckResult<PomdpValueType>().getSentinelValueVector();
193 STORM_LOG_DEBUG(
"Initial Value for guessed Policy: " << pomdpSchedulerResult[pomdp.getInitialStates().getNextSetIndex(0)]);
195 return std::make_pair(pomdpSchedulerResult, pomdpScheduler);
198template<
typename PomdpType>
203 "The threshold in '" << formula <<
"' is not supported by quantitative POMDP analysis. Use a Pmin=?/Pmax=? or Rmin=?/Rmax=? query instead.");
205 "The property type is not supported for this analysis.");
211 std::make_shared<storm::models::sparse::Mdp<PomdpValueType>>(pomdp.getTransitionMatrix(), pomdp.getStateLabeling(), pomdp.getRewardModels());
214 STORM_LOG_THROW(resultPtr, storm::exceptions::UnexpectedException,
"No check result obtained.");
215 STORM_LOG_THROW(resultPtr->isExplicitQuantitativeCheckResult(), storm::exceptions::UnexpectedException,
"Unexpected Check result Type.");
216 std::vector<PomdpValueType> fullyObservableResult = resultPtr->template asExplicitQuantitativeCheckResult<PomdpValueType>().getSentinelValueVector();
218 std::vector<PomdpValueType> actionBasedRewards;
219 std::vector<PomdpValueType>* actionBasedRewardsPtr =
nullptr;
221 actionBasedRewards = pomdp.getRewardModel(info.
getRewardModelName()).getTotalRewardVector(pomdp.getTransitionMatrix());
222 actionBasedRewardsPtr = &actionBasedRewards;
224 std::vector<std::vector<PomdpValueType>> guessedSchedulerValues;
225 std::vector<storm::storage::Scheduler<PomdpValueType>> guessedSchedulers;
227 std::vector<std::pair<double, bool>> guessParameters({{0.875,
false}, {0.875,
true}, {0.75,
false}, {0.75,
true}});
228 for (
auto const&
pars : guessParameters) {
230 computeValuesForGuessedScheduler(env, fullyObservableResult, actionBasedRewardsPtr, formula, info, underlyingMdp,
232 guessedSchedulerValues.push_back(guessedSchedulerPair->first);
233 guessedSchedulers.push_back(guessedSchedulerPair->second);
237 uint64_t bestGuess = 0;
240 for (uint64_t guess = 1; guess < guessedSchedulerValues.size(); ++guess) {
243 if ((info.
minimize() && guessSum < bestGuessSum) || (info.
maximize() && guessSum > bestGuessSum)) {
245 bestGuessSum = guessSum;
249 computeValuesForGuessedScheduler(env, guessedSchedulerValues[bestGuess], actionBasedRewardsPtr, formula, info, underlyingMdp,
251 guessedSchedulerValues.push_back(guessedSchedulerPair->first);
252 guessedSchedulers.push_back(guessedSchedulerPair->second);
254 computeValuesForGuessedScheduler(env, guessedSchedulerValues.back(), actionBasedRewardsPtr, formula, info, underlyingMdp,
256 guessedSchedulerValues.push_back(guessedSchedulerPair->first);
257 guessedSchedulers.push_back(guessedSchedulerPair->second);
259 computeValuesForGuessedScheduler(env, guessedSchedulerValues.back(), actionBasedRewardsPtr, formula, info, underlyingMdp,
261 guessedSchedulerValues.push_back(guessedSchedulerPair->first);
262 guessedSchedulers.push_back(guessedSchedulerPair->second);
267 for (uint64_t i = 0; i < guessedSchedulerValues.size() - 1; ++i) {
268 if (!keptGuesses.
get(i)) {
271 for (uint64_t j = i + 1; j < guessedSchedulerValues.size(); ++j) {
272 if (!keptGuesses.
get(j)) {
278 keptGuesses.
set(j,
false);
281 keptGuesses.
set(i,
false);
286 keptGuesses.
set(i,
false);
289 keptGuesses.
set(j,
false);
296 std::vector<storm::storage::Scheduler<PomdpValueType>> filteredSchedulers;
297 for (uint64_t i = 0; i < guessedSchedulers.size(); ++i) {
298 if (keptGuesses[i]) {
299 filteredSchedulers.push_back(guessedSchedulers[i]);
306 result.
lower.push_back(std::move(fullyObservableResult));
307 result.
upper = std::move(guessedSchedulerValues);
310 result.
lower = std::move(guessedSchedulerValues);
311 result.
upper.push_back(std::move(fullyObservableResult));
315 bool boundsValid =
true;
317 for (
auto const& lower : result.
lower) {
318 for (
auto const& upper : result.
upper) {
319 for (uint64_t state = 0; state < pomdp.getNumberOfStates(); ++state) {
323 STORM_LOG_TRACE(
"Lower bound " << lower.at(state) <<
" at state " << state <<
" is larger than upper bound " << upper.at(state));
331 <<
"). This might be due to floating point imprecisions. Enable TRACE output for more details.");
337template<
typename PomdpType>
344template<
typename PomdpType>
350template<
typename PomdpType>
354 "The threshold in '" << formula <<
"' is not supported by quantitative POMDP analysis. Use an Rmin=? or Rmax=? query instead.");
366 auto formulaPtr = std::make_shared<storm::logic::RewardOperatorFormula>(newFormula);
368 std::make_shared<storm::models::sparse::Mdp<PomdpValueType>>(pomdp.getTransitionMatrix(), pomdp.getStateLabeling(), pomdp.getRewardModels());
370 STORM_LOG_THROW(resultPtr, storm::exceptions::UnexpectedException,
"No check result obtained.");
371 STORM_LOG_THROW(resultPtr->isExplicitQuantitativeCheckResult(), storm::exceptions::UnexpectedException,
"Unexpected Check result Type.");
372 std::vector<PomdpValueType> resultVec = resultPtr->template asExplicitQuantitativeCheckResult<PomdpValueType>().getSentinelValueVector();
380 res.
values = std::move(resultVec);