Storm 1.14.0.1
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
SparseExplorationModelChecker.cpp
Go to the documentation of this file.
2
3#include <sstream>
4
9
11
13
16
18
20
22
26
29
31#include "storm/utility/graph.h"
33
37
38namespace storm {
39namespace modelchecker {
40
41template<typename ModelType, typename StateType>
43 : program(program.substituteConstantsFormulas()),
44 randomGenerator(std::chrono::system_clock::now().time_since_epoch().count()),
45 comparator(storm::settings::getModule<storm::settings::modules::ExplorationSettings>().getPrecision()) {
46 // Intentionally left empty.
47}
48
49template<typename ModelType, typename StateType>
55
56template<typename ModelType, typename StateType>
60
61template<typename ModelType, typename StateType>
64 storm::logic::UntilFormula const& untilFormula = checkTask.getFormula();
65 storm::logic::Formula const& conditionFormula = untilFormula.getLeftSubformula();
66 storm::logic::Formula const& targetFormula = untilFormula.getRightSubformula();
67 STORM_LOG_THROW(program.isDeterministicModel() || checkTask.isOptimizationDirectionSet(), storm::exceptions::InvalidPropertyException,
68 "For nondeterministic systems, an optimization direction (min/max) must be given in the property.");
69
71 : storm::OptimizationDirection::Maximize);
72
73 // The first row group starts at action 0.
74 explorationInformation.newRowGroup(0);
75
76 std::map<std::string, storm::expressions::Expression> labelToExpressionMapping = program.getLabelToExpressionMapping();
77 StateGeneration<StateType, ValueType> stateGeneration(program, explorationInformation,
78 conditionFormula.toExpression(program.getManager(), labelToExpressionMapping),
79 targetFormula.toExpression(program.getManager(), labelToExpressionMapping));
80
81 // Compute and return result.
82 std::tuple<StateType, ValueType, ValueType> boundsForInitialState = performExploration(stateGeneration, explorationInformation);
83 return std::make_unique<ExplicitQuantitativeCheckResult<ValueType>>(std::get<0>(boundsForInitialState), std::get<1>(boundsForInitialState));
84}
85
86template<typename ModelType, typename StateType>
87std::tuple<StateType, typename ModelType::ValueType, typename ModelType::ValueType> SparseExplorationModelChecker<ModelType, StateType>::performExploration(
89 // Generate the initial state so we know where to start the simulation.
90 stateGeneration.computeInitialStates();
91 STORM_LOG_THROW(stateGeneration.getNumberOfInitialStates() == 1, storm::exceptions::NotSupportedException,
92 "Currently only models with one initial state are supported by the exploration engine.");
93 StateType initialStateIndex = stateGeneration.getFirstInitialState();
94
95 // Create a structure that holds the bounds for the states and actions.
97
98 // Create a stack that is used to track the path we sampled.
99 StateActionStack stack;
100
101 // Now perform the actual sampling.
103 bool convergenceCriterionMet = false;
104 while (!convergenceCriterionMet) {
105 bool result = samplePathFromInitialState(stateGeneration, explorationInformation, stack, bounds, stats);
106
107 stats.sampledPath();
108 stats.updateMaxPathLength(stack.size());
109
110 // If a terminal state was found, we update the probabilities along the path contained in the stack.
111 if (result) {
112 // Update the bounds along the path to the terminal state.
113 STORM_LOG_TRACE("Found terminal state, updating probabilities along path.");
114 updateProbabilityBoundsAlongSampledPath(stack, explorationInformation, bounds);
115 } else {
116 // If not terminal state was found, the search aborted, possibly because of an EC-detection. In this
117 // case, we cannot update the probabilities.
118 STORM_LOG_TRACE("Did not find terminal state.");
119 }
120
121 STORM_LOG_DEBUG("Discovered states: " << explorationInformation.getNumberOfDiscoveredStates() << " (" << stats.numberOfExploredStates << " explored, "
122 << explorationInformation.getNumberOfUnexploredStates() << " unexplored).");
123 STORM_LOG_DEBUG("Value of initial state is in [" << bounds.getLowerBoundForState(initialStateIndex, explorationInformation) << ", "
124 << bounds.getUpperBoundForState(initialStateIndex, explorationInformation) << "].");
125 ValueType difference = bounds.getDifferenceOfStateBounds(initialStateIndex, explorationInformation);
126 STORM_LOG_DEBUG("Difference after iteration " << stats.pathsSampled << " is " << difference << ".");
127 convergenceCriterionMet = comparator.isZero(difference);
128
129 // If the number of sampled paths exceeds a certain threshold, do a precomputation.
130 if (!convergenceCriterionMet && explorationInformation.performPrecomputationExcessiveSampledPaths(stats.pathsSampledSinceLastPrecomputation)) {
131 performPrecomputation(stack, explorationInformation, bounds, stats);
132 }
133 }
134
135 std::stringstream statsStream;
136 stats.printToStream(statsStream, explorationInformation);
137 STORM_LOG_STATISTICS(statsStream.str());
138
139 return std::make_tuple(initialStateIndex, bounds.getLowerBoundForState(initialStateIndex, explorationInformation),
140 bounds.getUpperBoundForState(initialStateIndex, explorationInformation));
141}
142
143template<typename ModelType, typename StateType>
144bool SparseExplorationModelChecker<ModelType, StateType>::samplePathFromInitialState(StateGeneration<StateType, ValueType>& stateGeneration,
145 ExplorationInformation<StateType, ValueType>& explorationInformation,
146 StateActionStack& stack, Bounds<StateType, ValueType>& bounds,
148 // Start the search from the initial state.
149 stack.push_back(std::make_pair(stateGeneration.getFirstInitialState(), 0));
150
151 // As long as we didn't find a terminal (accepting or rejecting) state in the search, sample a new successor.
152 bool foundTerminalState = false;
153 while (!foundTerminalState) {
154 StateType const& currentStateId = stack.back().first;
155 STORM_LOG_TRACE("State on top of stack is: " << currentStateId << ".");
156
157 // If the state is not yet explored, we need to retrieve its behaviors.
158 auto unexploredIt = explorationInformation.findUnexploredState(currentStateId);
159 if (unexploredIt != explorationInformation.unexploredStatesEnd()) {
160 STORM_LOG_TRACE("State was not yet explored.");
161
162 // Explore the previously unexplored state.
163 storm::generator::CompressedState const& compressedState = unexploredIt->second;
164 foundTerminalState = exploreState(stateGeneration, currentStateId, compressedState, explorationInformation, bounds, stats);
165 if (foundTerminalState) {
166 STORM_LOG_TRACE("Aborting sampling of path, because a terminal state was reached.");
167 }
168 explorationInformation.removeUnexploredState(unexploredIt);
169 } else {
170 // If the state was already explored, we check whether it is a terminal state or not.
171 if (explorationInformation.isTerminal(currentStateId)) {
172 STORM_LOG_TRACE("Found already explored terminal state: " << currentStateId << ".");
173 foundTerminalState = true;
174 }
175 }
176
177 // Notify the stats about the performed exploration step.
178 stats.explorationStep();
179
180 // If the state was not a terminal state, we continue the path search and sample the next state.
181 if (!foundTerminalState) {
182 // At this point, we can be sure that the state was expanded and that we can sample according to the
183 // probabilities in the matrix.
184 uint32_t chosenAction = sampleActionOfState(currentStateId, explorationInformation, bounds);
185 stack.back().second = chosenAction;
186 STORM_LOG_TRACE("Sampled action " << chosenAction << " in state " << currentStateId << ".");
187
188 StateType successor = sampleSuccessorFromAction(chosenAction, explorationInformation, bounds);
189 STORM_LOG_TRACE("Sampled successor " << successor << " according to action " << chosenAction << " of state " << currentStateId << ".");
190
191 // Put the successor state and a dummy action on top of the stack.
192 stack.emplace_back(successor, 0);
193
194 // If the number of exploration steps exceeds a certain threshold, do a precomputation.
195 if (explorationInformation.performPrecomputationExcessiveExplorationSteps(stats.explorationStepsSinceLastPrecomputation)) {
196 performPrecomputation(stack, explorationInformation, bounds, stats);
197
198 STORM_LOG_TRACE("Aborting the search after precomputation.");
199 stack.clear();
200 break;
201 }
202 }
203 }
204
205 return foundTerminalState;
206}
207
208template<typename ModelType, typename StateType>
209bool SparseExplorationModelChecker<ModelType, StateType>::exploreState(StateGeneration<StateType, ValueType>& stateGeneration, StateType const& currentStateId,
210 storm::generator::CompressedState const& currentState,
211 ExplorationInformation<StateType, ValueType>& explorationInformation,
213 bool isTerminalState = false;
214 bool isTargetState = false;
215
216 ++stats.numberOfExploredStates;
217
218 // Finally, map the unexplored state to the row group.
219 explorationInformation.assignStateToNextRowGroup(currentStateId);
220 STORM_LOG_TRACE("Assigning row group " << explorationInformation.getRowGroup(currentStateId) << " to state " << currentStateId << ".");
221
222 // Initialize the bounds, because some of the following computations depend on the values to be available for
223 // all states that have been assigned to a row-group.
224 bounds.initializeBoundsForNextState();
225
226 // Before generating the behavior of the state, we need to determine whether it's a target state that
227 // does not need to be expanded.
228 stateGeneration.load(currentState);
229 if (stateGeneration.isTargetState()) {
230 ++stats.numberOfTargetStates;
231 isTargetState = true;
232 isTerminalState = true;
233 } else if (stateGeneration.isConditionState()) {
234 STORM_LOG_TRACE("Exploring state.");
235
236 // If it needs to be expanded, we use the generator to retrieve the behavior of the new state.
237 storm::generator::StateBehavior<ValueType, StateType> behavior = stateGeneration.expand();
238 STORM_LOG_TRACE("State has " << behavior.getNumberOfChoices() << " choices.");
239
240 // Clumsily check whether we have found a state that forms a trivial BMEC.
241 bool otherSuccessor = false;
242 for (auto const& choice : behavior) {
243 for (auto const& entry : choice) {
244 if (entry.first != currentStateId) {
245 otherSuccessor = true;
246 break;
247 }
248 }
249 }
250 isTerminalState = !otherSuccessor;
251
252 // If the state was neither a trivial (non-accepting) terminal state nor a target state, we
253 // need to store its behavior.
254 if (!isTerminalState) {
255 // Next, we insert the behavior into our matrix structure.
256 StateType startAction = explorationInformation.getActionCount();
257 explorationInformation.addActionsToMatrix(behavior.getNumberOfChoices());
258
259 ActionType localAction = 0;
260
261 // Retrieve the lowest state bounds (wrt. to the current optimization direction).
262 std::pair<ValueType, ValueType> stateBounds = getLowestBounds(explorationInformation.getOptimizationDirection());
263
264 for (auto const& choice : behavior) {
265 for (auto const& entry : choice) {
266 explorationInformation.getRowOfMatrix(startAction + localAction).emplace_back(entry.first, entry.second);
267 STORM_LOG_TRACE("Found transition " << currentStateId << "-[" << (startAction + localAction) << ", " << entry.second << "]-> "
268 << entry.first << ".");
269 }
270
271 std::pair<ValueType, ValueType> actionBounds = computeBoundsOfAction(startAction + localAction, explorationInformation, bounds);
272 bounds.initializeBoundsForNextAction(actionBounds);
273 stateBounds = combineBounds(explorationInformation.getOptimizationDirection(), stateBounds, actionBounds);
274
275 STORM_LOG_TRACE("Initializing bounds of action " << (startAction + localAction) << " to "
276 << bounds.getLowerBoundForAction(startAction + localAction) << " and "
277 << bounds.getUpperBoundForAction(startAction + localAction) << ".");
278
279 ++localAction;
280 }
281
282 // Terminate the row group.
283 explorationInformation.terminateCurrentRowGroup();
284
285 bounds.setBoundsForState(currentStateId, explorationInformation, stateBounds);
286 STORM_LOG_TRACE("Initializing bounds of state " << currentStateId << " to " << bounds.getLowerBoundForState(currentStateId, explorationInformation)
287 << " and " << bounds.getUpperBoundForState(currentStateId, explorationInformation) << ".");
288 }
289 } else {
290 // In this case, the state is neither a target state nor a condition state and therefore a rejecting
291 // terminal state.
292 isTerminalState = true;
293 }
294
295 if (isTerminalState) {
296 STORM_LOG_TRACE("State does not need to be explored, because it is " << (isTargetState ? "a target state" : "a rejecting terminal state") << ".");
297 explorationInformation.addTerminalState(currentStateId);
298
299 if (isTargetState) {
300 bounds.setBoundsForState(currentStateId, explorationInformation,
302 bounds.initializeBoundsForNextAction(std::make_pair(storm::utility::one<ValueType>(), storm::utility::one<ValueType>()));
303 } else {
304 bounds.setBoundsForState(currentStateId, explorationInformation,
306 bounds.initializeBoundsForNextAction(std::make_pair(storm::utility::zero<ValueType>(), storm::utility::zero<ValueType>()));
307 }
308
309 // Increase the size of the matrix, but leave the row empty.
310 explorationInformation.addActionsToMatrix(1);
311
312 // Terminate the row group.
313 explorationInformation.newRowGroup();
314 }
315
316 return isTerminalState;
317}
318
319template<typename ModelType, typename StateType>
320typename SparseExplorationModelChecker<ModelType, StateType>::ActionType SparseExplorationModelChecker<ModelType, StateType>::sampleActionOfState(
321 StateType const& currentStateId, ExplorationInformation<StateType, ValueType> const& explorationInformation, Bounds<StateType, ValueType>& bounds) const {
322 // Determine the values of all available actions.
323 std::vector<std::pair<ActionType, ValueType>> actionValues;
324 StateType rowGroup = explorationInformation.getRowGroup(currentStateId);
325
326 // Check for cases in which we do not need to perform more work.
327 if (explorationInformation.onlyOneActionAvailable(rowGroup)) {
328 return explorationInformation.getStartRowOfGroup(rowGroup);
329 }
330
331 // If there are more choices to consider, start by gathering the values of relevant actions.
332 STORM_LOG_TRACE("Sampling from actions leaving the state.");
333
334 for (uint32_t row = explorationInformation.getStartRowOfGroup(rowGroup); row < explorationInformation.getStartRowOfGroup(rowGroup + 1); ++row) {
335 actionValues.push_back(std::make_pair(row, bounds.getBoundForAction(explorationInformation.getOptimizationDirection(), row)));
336 }
337
338 STORM_LOG_ASSERT(!actionValues.empty(), "Values for actions must not be empty.");
339
340 // Sort the actions wrt. to the optimization direction.
341 if (explorationInformation.maximize()) {
342 std::sort(actionValues.begin(), actionValues.end(),
343 [](std::pair<ActionType, ValueType> const& a, std::pair<ActionType, ValueType> const& b) { return a.second > b.second; });
344 } else {
345 std::sort(actionValues.begin(), actionValues.end(),
346 [](std::pair<ActionType, ValueType> const& a, std::pair<ActionType, ValueType> const& b) { return a.second < b.second; });
347 }
348
349 // Determine the first elements of the sorted range that agree on their value.
350 auto end = ++actionValues.begin();
351 while (end != actionValues.end() && comparator.isEqual(actionValues.begin()->second, end->second)) {
352 ++end;
353 }
354
355 // Now sample from all maximizing actions.
356 std::uniform_int_distribution<ActionType> distribution(0, std::distance(actionValues.begin(), end) - 1);
357 return actionValues[distribution(randomGenerator)].first;
358}
359
360template<typename ModelType, typename StateType>
361StateType SparseExplorationModelChecker<ModelType, StateType>::sampleSuccessorFromAction(
362 ActionType const& chosenAction, ExplorationInformation<StateType, ValueType> const& explorationInformation,
363 Bounds<StateType, ValueType> const& bounds) const {
364 std::vector<storm::storage::MatrixEntry<StateType, ValueType>> const& row = explorationInformation.getRowOfMatrix(chosenAction);
365 if (row.size() == 1) {
366 return row.front().getColumn();
367 }
368
369 // Depending on the selected next-state heuristic, we give the states other likelihoods of getting chosen.
370 if (explorationInformation.useDifferenceProbabilitySumHeuristic() || explorationInformation.useProbabilityHeuristic()) {
371 std::vector<ValueType> probabilities(row.size());
372 if (explorationInformation.useDifferenceProbabilitySumHeuristic()) {
373 std::transform(row.begin(), row.end(), probabilities.begin(),
374 [&bounds, &explorationInformation](storm::storage::MatrixEntry<StateType, ValueType> const& entry) {
375 return entry.getValue() + bounds.getDifferenceOfStateBounds(entry.getColumn(), explorationInformation);
376 });
377 } else if (explorationInformation.useProbabilityHeuristic()) {
378 std::transform(row.begin(), row.end(), probabilities.begin(),
379 [](storm::storage::MatrixEntry<StateType, ValueType> const& entry) { return entry.getValue(); });
380 }
381
382 // Now sample according to the probabilities.
383 std::discrete_distribution<StateType> distribution(probabilities.begin(), probabilities.end());
384 return row[distribution(randomGenerator)].getColumn();
385 } else {
386 STORM_LOG_ASSERT(explorationInformation.useUniformHeuristic(), "Illegal next-state heuristic.");
387 std::uniform_int_distribution<ActionType> distribution(0, row.size() - 1);
388 return row[distribution(randomGenerator)].getColumn();
389 }
390}
391
392template<typename ModelType, typename StateType>
393bool SparseExplorationModelChecker<ModelType, StateType>::performPrecomputation(StateActionStack const& stack,
394 ExplorationInformation<StateType, ValueType>& explorationInformation,
397 ++stats.numberOfPrecomputations;
398
399 // Outline:
400 // 1. construct a sparse transition matrix of the relevant part of the state space.
401 // 2. use this matrix to compute states with probability 0/1 and an MEC decomposition (in the max case).
402 // 3. use MEC decomposition to collapse MECs.
403 STORM_LOG_TRACE("Starting " << (explorationInformation.useLocalPrecomputation() ? "local" : "global") << " precomputation.");
404
405 // Construct the matrix that represents the fragment of the system contained in the currently sampled path.
406 storm::storage::SparseMatrixBuilder<ValueType> builder(0, 0, 0, false, true, 0);
407
408 // Determine the set of states that was expanded.
409 std::vector<StateType> relevantStates;
410 if (explorationInformation.useLocalPrecomputation()) {
411 for (auto const& stateActionPair : stack) {
412 if (explorationInformation.maximize() || !storm::utility::isOne(bounds.getLowerBoundForState(stateActionPair.first, explorationInformation))) {
413 relevantStates.push_back(stateActionPair.first);
414 }
415 }
416 std::sort(relevantStates.begin(), relevantStates.end());
417 auto newEnd = std::unique(relevantStates.begin(), relevantStates.end());
418 relevantStates.resize(std::distance(relevantStates.begin(), newEnd));
419 } else {
420 for (StateType state = 0; state < explorationInformation.getNumberOfDiscoveredStates(); ++state) {
421 // Add the state to the relevant states if they are not unexplored.
422 if (!explorationInformation.isUnexplored(state)) {
423 relevantStates.push_back(state);
424 }
425 }
426 }
427 StateType sink = relevantStates.size();
428
429 // Create a mapping for faster look-up during the translation of flexible matrix to the real sparse matrix.
430 // While doing so, record all target states.
431 std::unordered_map<StateType, StateType> relevantStateToNewRowGroupMapping;
432 storm::storage::BitVector targetStates(sink + 1);
433 for (StateType index = 0; index < relevantStates.size(); ++index) {
434 relevantStateToNewRowGroupMapping.emplace(relevantStates[index], index);
435 if (storm::utility::isOne(bounds.getLowerBoundForState(relevantStates[index], explorationInformation))) {
436 targetStates.set(index);
437 }
438 }
439
440 // Do the actual translation.
441 StateType currentRow = 0;
442 for (auto const& state : relevantStates) {
443 builder.newRowGroup(currentRow);
444 StateType rowGroup = explorationInformation.getRowGroup(state);
445 for (auto row = explorationInformation.getStartRowOfGroup(rowGroup); row < explorationInformation.getStartRowOfGroup(rowGroup + 1); ++row) {
446 ValueType unexpandedProbability = storm::utility::zero<ValueType>();
447 for (auto const& entry : explorationInformation.getRowOfMatrix(row)) {
448 auto it = relevantStateToNewRowGroupMapping.find(entry.getColumn());
449 if (it != relevantStateToNewRowGroupMapping.end()) {
450 // If the entry is a relevant state, we copy it over (and compensate for the offset change).
451 builder.addNextValue(currentRow, it->second, entry.getValue());
452 } else {
453 // If the entry is an unexpanded state, we gather the probability to later redirect it to an unexpanded sink.
454 unexpandedProbability += entry.getValue();
455 }
456 }
457 if (unexpandedProbability != storm::utility::zero<ValueType>()) {
458 builder.addNextValue(currentRow, sink, unexpandedProbability);
459 }
460 ++currentRow;
461 }
462 }
463 // Then, make the unexpanded state absorbing.
464 builder.newRowGroup(currentRow);
465 builder.addNextValue(currentRow, sink, storm::utility::one<ValueType>());
466 storm::storage::SparseMatrix<ValueType> relevantStatesMatrix = builder.build();
467 storm::storage::SparseMatrix<ValueType> transposedMatrix = relevantStatesMatrix.transpose(true);
468 STORM_LOG_TRACE("Successfully built matrix for precomputation.");
469
470 storm::storage::BitVector allStates(sink + 1, true);
471 storm::storage::BitVector statesWithProbability0;
472 storm::storage::BitVector statesWithProbability1;
473 if (explorationInformation.maximize()) {
474 // If we are computing maximal probabilities, we first perform a detection of states that have
475 // probability 01 and then additionally perform an MEC decomposition. The reason for this somewhat
476 // duplicate work is the following. Optimally, we would only do the MEC decomposition, because we need
477 // it anyway. However, when only detecting (accepting) MECs, we do not infer which of the other states
478 // (not contained in MECs) also have probability 0/1.
479 targetStates.set(sink, true);
480 statesWithProbability0 = storm::utility::graph::performProb0A(transposedMatrix, allStates, targetStates);
481 targetStates.set(sink, false);
482 statesWithProbability1 =
483 storm::utility::graph::performProb1E(relevantStatesMatrix, relevantStatesMatrix.getRowGroupIndices(), transposedMatrix, allStates, targetStates);
484
485 storm::storage::MaximalEndComponentDecomposition<ValueType> mecDecomposition(relevantStatesMatrix, relevantStatesMatrix.transpose(true));
486 ++stats.ecDetections;
487 STORM_LOG_TRACE("Successfully computed MEC decomposition. Found " << (mecDecomposition.size() > 1 ? (mecDecomposition.size() - 1) : 0) << " MEC(s).");
488
489 // If the decomposition contains only the MEC consisting of the sink state, we count it as 'failed'.
490 STORM_LOG_ASSERT(mecDecomposition.size() > 0, "Expected at least one MEC (the trivial sink MEC).");
491 if (mecDecomposition.size() == 1) {
492 ++stats.failedEcDetections;
493 } else {
494 stats.totalNumberOfEcDetected += mecDecomposition.size() - 1;
495
496 // 3. Analyze the MEC decomposition.
497 for (auto const& mec : mecDecomposition) {
498 // Ignore the (expected) MEC of the sink state.
499 if (mec.containsState(sink)) {
500 continue;
501 }
502
503 collapseMec(mec, relevantStates, relevantStatesMatrix, explorationInformation, bounds);
504 }
505 }
506 } else {
507 // If we are computing minimal probabilities, we do not need to perform an EC-detection. We rather
508 // compute all states (of the considered fragment) that have probability 0/1. For states with
509 // probability 0, we have to mark the sink as being a target. For states with probability 1, however,
510 // we must treat the sink as being rejecting.
511 targetStates.set(sink, true);
512 statesWithProbability0 =
513 storm::utility::graph::performProb0E(relevantStatesMatrix, relevantStatesMatrix.getRowGroupIndices(), transposedMatrix, allStates, targetStates);
514 targetStates.set(sink, false);
515 statesWithProbability1 =
516 storm::utility::graph::performProb1A(relevantStatesMatrix, relevantStatesMatrix.getRowGroupIndices(), transposedMatrix, allStates, targetStates);
517 }
518
519 // Set the bounds of the identified states.
520 STORM_LOG_ASSERT((statesWithProbability0 & statesWithProbability1).empty(), "States with probability 0 and 1 overlap.");
521 for (uint64_t state : statesWithProbability0) {
522 // Skip the sink state as it is not contained in the original system.
523 if (state == sink) {
524 continue;
525 }
526
527 StateType originalState = relevantStates[state];
528 bounds.setUpperBoundForState(originalState, explorationInformation, storm::utility::zero<ValueType>());
529 explorationInformation.addTerminalState(originalState);
530 }
531 for (uint64_t state : statesWithProbability1) {
532 // Skip the sink state as it is not contained in the original system.
533 if (state == sink) {
534 continue;
535 }
536
537 StateType originalState = relevantStates[state];
538 bounds.setLowerBoundForState(originalState, explorationInformation, storm::utility::one<ValueType>());
539 explorationInformation.addTerminalState(originalState);
540 }
541 return true;
542}
543
544template<typename ModelType, typename StateType>
545void SparseExplorationModelChecker<ModelType, StateType>::collapseMec(storm::storage::MaximalEndComponent const& mec,
546 std::vector<StateType> const& relevantStates,
547 storm::storage::SparseMatrix<ValueType> const& relevantStatesMatrix,
548 ExplorationInformation<StateType, ValueType>& explorationInformation,
549 Bounds<StateType, ValueType>& bounds) const {
550 bool containsTargetState = false;
551
552 // Now we record all actions leaving the EC.
553 std::vector<ActionType> leavingActions;
554 for (auto const& stateAndChoices : mec) {
555 // Compute the state of the original model that corresponds to the current state.
556 StateType originalState = relevantStates[stateAndChoices.first];
557 StateType originalRowGroup = explorationInformation.getRowGroup(originalState);
558
559 // Check whether a target state is contained in the MEC.
560 if (!containsTargetState && storm::utility::isOne(bounds.getLowerBoundForRowGroup(originalRowGroup))) {
561 containsTargetState = true;
562 }
563
564 // For each state, compute the actions that leave the MEC.
565 auto includedChoicesIt = stateAndChoices.second.begin();
566 auto includedChoicesIte = stateAndChoices.second.end();
567 for (auto action = explorationInformation.getStartRowOfGroup(originalRowGroup);
568 action < explorationInformation.getStartRowOfGroup(originalRowGroup + 1); ++action) {
569 if (includedChoicesIt != includedChoicesIte) {
570 STORM_LOG_TRACE("Next (local) choice contained in MEC is "
571 << (*includedChoicesIt - relevantStatesMatrix.getRowGroupIndices()[stateAndChoices.first]));
572 STORM_LOG_TRACE("Current (local) choice iterated is " << (action - explorationInformation.getStartRowOfGroup(originalRowGroup)));
573 if (action - explorationInformation.getStartRowOfGroup(originalRowGroup) !=
574 *includedChoicesIt - relevantStatesMatrix.getRowGroupIndices()[stateAndChoices.first]) {
575 STORM_LOG_TRACE("Choice leaves the EC.");
576 leavingActions.push_back(action);
577 } else {
578 STORM_LOG_TRACE("Choice stays in the EC.");
579 ++includedChoicesIt;
580 }
581 } else {
582 STORM_LOG_TRACE("Choice leaves the EC, because there is no more choice staying in the EC.");
583 leavingActions.push_back(action);
584 }
585 }
586 }
587
588 // Now, we need to collapse the EC only if it does not contain a target state and the leaving actions are
589 // non-empty, because only then have the states a (potentially) non-zero, non-one probability.
590 if (!containsTargetState && !leavingActions.empty()) {
591 // In this case, no target state is contained in the MEC, but there are actions leaving the MEC. To
592 // prevent the simulation getting stuck in this MEC again, we replace all states in the MEC by a new
593 // state whose outgoing actions are the ones leaving the MEC. We do this, by assigning all states in the
594 // MEC to a new row group, which will then hold all the outgoing choices.
595
596 // Remap all contained states to the new row group.
597 StateType nextRowGroup = explorationInformation.getNextRowGroup();
598 for (auto const& stateAndChoices : mec) {
599 StateType originalState = relevantStates[stateAndChoices.first];
600 explorationInformation.assignStateToRowGroup(originalState, nextRowGroup);
601 }
602
603 bounds.initializeBoundsForNextState();
604
605 // Add to the new row group all leaving actions of contained states and set the appropriate bounds for
606 // the actions and the new state.
607 std::pair<ValueType, ValueType> stateBounds = getLowestBounds(explorationInformation.getOptimizationDirection());
608 for (auto const& action : leavingActions) {
609 explorationInformation.moveActionToBackOfMatrix(action);
610 std::pair<ValueType, ValueType> actionBounds = bounds.getBoundsForAction(action);
611 bounds.initializeBoundsForNextAction(actionBounds);
612 stateBounds = combineBounds(explorationInformation.getOptimizationDirection(), stateBounds, actionBounds);
613 }
614 bounds.setBoundsForRowGroup(nextRowGroup, stateBounds);
615
616 // Terminate the row group of the newly introduced state.
617 explorationInformation.terminateCurrentRowGroup();
618 }
619}
620
621template<typename ModelType, typename StateType>
622typename ModelType::ValueType SparseExplorationModelChecker<ModelType, StateType>::computeLowerBoundOfAction(
623 ActionType const& action, ExplorationInformation<StateType, ValueType> const& explorationInformation, Bounds<StateType, ValueType> const& bounds) const {
625 for (auto const& element : explorationInformation.getRowOfMatrix(action)) {
626 result += element.getValue() * bounds.getLowerBoundForState(element.getColumn(), explorationInformation);
627 }
628 return result;
629}
630
631template<typename ModelType, typename StateType>
632typename ModelType::ValueType SparseExplorationModelChecker<ModelType, StateType>::computeUpperBoundOfAction(
633 ActionType const& action, ExplorationInformation<StateType, ValueType> const& explorationInformation, Bounds<StateType, ValueType> const& bounds) const {
635 for (auto const& element : explorationInformation.getRowOfMatrix(action)) {
636 result += element.getValue() * bounds.getUpperBoundForState(element.getColumn(), explorationInformation);
637 }
638 return result;
639}
640
641template<typename ModelType, typename StateType>
642std::pair<typename ModelType::ValueType, typename ModelType::ValueType> SparseExplorationModelChecker<ModelType, StateType>::computeBoundsOfAction(
643 ActionType const& action, ExplorationInformation<StateType, ValueType> const& explorationInformation, Bounds<StateType, ValueType> const& bounds) const {
644 // TODO: take into account self-loops?
645 std::pair<ValueType, ValueType> result = std::make_pair(storm::utility::zero<ValueType>(), storm::utility::zero<ValueType>());
646 for (auto const& element : explorationInformation.getRowOfMatrix(action)) {
647 result.first += element.getValue() * bounds.getLowerBoundForState(element.getColumn(), explorationInformation);
648 result.second += element.getValue() * bounds.getUpperBoundForState(element.getColumn(), explorationInformation);
649 }
650 return result;
651}
652
653template<typename ModelType, typename StateType>
654std::pair<typename ModelType::ValueType, typename ModelType::ValueType> SparseExplorationModelChecker<ModelType, StateType>::computeBoundsOfState(
655 StateType const& currentStateId, ExplorationInformation<StateType, ValueType> const& explorationInformation,
656 Bounds<StateType, ValueType> const& bounds) const {
657 StateType group = explorationInformation.getRowGroup(currentStateId);
658 std::pair<ValueType, ValueType> result = getLowestBounds(explorationInformation.getOptimizationDirection());
659 for (ActionType action = explorationInformation.getStartRowOfGroup(group); action < explorationInformation.getStartRowOfGroup(group + 1); ++action) {
660 std::pair<ValueType, ValueType> actionValues = computeBoundsOfAction(action, explorationInformation, bounds);
661 result = combineBounds(explorationInformation.getOptimizationDirection(), result, actionValues);
662 }
663 return result;
664}
665
666template<typename ModelType, typename StateType>
667void SparseExplorationModelChecker<ModelType, StateType>::updateProbabilityBoundsAlongSampledPath(
668 StateActionStack& stack, ExplorationInformation<StateType, ValueType> const& explorationInformation, Bounds<StateType, ValueType>& bounds) const {
669 stack.pop_back();
670 while (!stack.empty()) {
671 updateProbabilityOfAction(stack.back().first, stack.back().second, explorationInformation, bounds);
672 stack.pop_back();
673 }
674}
675
676template<typename ModelType, typename StateType>
677void SparseExplorationModelChecker<ModelType, StateType>::updateProbabilityOfAction(StateType const& state, ActionType const& action,
678 ExplorationInformation<StateType, ValueType> const& explorationInformation,
679 Bounds<StateType, ValueType>& bounds) const {
680 // Compute the new lower/upper values of the action.
681 std::pair<ValueType, ValueType> newBoundsForAction = computeBoundsOfAction(action, explorationInformation, bounds);
682
683 // And set them as the current value.
684 bounds.setBoundsForAction(action, newBoundsForAction);
685
686 // Check if we need to update the values for the states.
687 if (explorationInformation.maximize()) {
688 bounds.setLowerBoundOfStateIfGreaterThanOld(state, explorationInformation, newBoundsForAction.first);
689
690 StateType rowGroup = explorationInformation.getRowGroup(state);
691 if (newBoundsForAction.second < bounds.getUpperBoundForRowGroup(rowGroup)) {
692 if (explorationInformation.getRowGroupSize(rowGroup) > 1) {
693 newBoundsForAction.second = std::max(newBoundsForAction.second, computeBoundOverAllOtherActions(storm::OptimizationDirection::Maximize, state,
694 action, explorationInformation, bounds));
695 }
696
697 bounds.setUpperBoundForRowGroup(rowGroup, newBoundsForAction.second);
698 }
699 } else {
700 bounds.setUpperBoundOfStateIfLessThanOld(state, explorationInformation, newBoundsForAction.second);
701
702 StateType rowGroup = explorationInformation.getRowGroup(state);
703 if (bounds.getLowerBoundForRowGroup(rowGroup) < newBoundsForAction.first) {
704 if (explorationInformation.getRowGroupSize(rowGroup) > 1) {
705 ValueType min = computeBoundOverAllOtherActions(storm::OptimizationDirection::Minimize, state, action, explorationInformation, bounds);
706 newBoundsForAction.first = std::min(newBoundsForAction.first, min);
707 }
708
709 bounds.setLowerBoundForRowGroup(rowGroup, newBoundsForAction.first);
710 }
711 }
712}
713
714template<typename ModelType, typename StateType>
715typename ModelType::ValueType SparseExplorationModelChecker<ModelType, StateType>::computeBoundOverAllOtherActions(
716 storm::OptimizationDirection const& direction, StateType const& state, ActionType const& action,
717 ExplorationInformation<StateType, ValueType> const& explorationInformation, Bounds<StateType, ValueType> const& bounds) const {
718 ValueType bound = getLowestBound(explorationInformation.getOptimizationDirection());
719
720 ActionType group = explorationInformation.getRowGroup(state);
721 for (auto currentAction = explorationInformation.getStartRowOfGroup(group); currentAction < explorationInformation.getStartRowOfGroup(group + 1);
722 ++currentAction) {
723 if (currentAction == action) {
724 continue;
725 }
726
727 if (direction == storm::OptimizationDirection::Maximize) {
728 bound = std::max(bound, computeUpperBoundOfAction(currentAction, explorationInformation, bounds));
729 } else {
730 bound = std::min(bound, computeLowerBoundOfAction(currentAction, explorationInformation, bounds));
731 }
732 }
733 return bound;
734}
735
736template<typename ModelType, typename StateType>
737std::pair<typename ModelType::ValueType, typename ModelType::ValueType> SparseExplorationModelChecker<ModelType, StateType>::getLowestBounds(
738 storm::OptimizationDirection const& direction) const {
739 ValueType val = getLowestBound(direction);
740 return std::make_pair(val, val);
741}
742
743template<typename ModelType, typename StateType>
744typename ModelType::ValueType SparseExplorationModelChecker<ModelType, StateType>::getLowestBound(storm::OptimizationDirection const& direction) const {
745 if (direction == storm::OptimizationDirection::Maximize) {
747 } else {
749 }
750}
751
752template<typename ModelType, typename StateType>
753std::pair<typename ModelType::ValueType, typename ModelType::ValueType> SparseExplorationModelChecker<ModelType, StateType>::combineBounds(
754 storm::OptimizationDirection const& direction, std::pair<ValueType, ValueType> const& bounds1, std::pair<ValueType, ValueType> const& bounds2) const {
755 if (direction == storm::OptimizationDirection::Maximize) {
756 return std::make_pair(std::max(bounds1.first, bounds2.first), std::max(bounds1.second, bounds2.second));
757 } else {
758 return std::make_pair(std::min(bounds1.first, bounds2.first), std::min(bounds1.second, bounds2.second));
759 }
760}
761
764} // namespace modelchecker
765} // namespace storm
std::size_t getNumberOfChoices() const
Retrieves the number of choices in the behavior.
Formula const & getRightSubformula() const
Formula const & getLeftSubformula() const
storm::expressions::Expression toExpression(storm::expressions::ExpressionManager const &manager, std::map< std::string, storm::expressions::Expression > const &labelToExpressionMapping={}) const
Takes the formula and converts it to an equivalent expression.
Definition Formula.cpp:561
bool isInFragment(FragmentSpecification const &fragment) const
Definition Formula.cpp:204
bool isOptimizationDirectionSet() const
Retrieves whether an optimization direction was set.
Definition CheckTask.h:148
FormulaType const & getFormula() const
Retrieves the formula from this task.
Definition CheckTask.h:141
storm::OptimizationDirection const & getOptimizationDirection() const
Retrieves the optimization direction (if set).
Definition CheckTask.h:155
bool isOnlyInitialStatesRelevantSet() const
Retrieves whether only the initial states are relevant in the computation.
Definition CheckTask.h:205
virtual std::unique_ptr< CheckResult > computeUntilProbabilities(Environment const &env, CheckTask< storm::logic::UntilFormula, ValueType > const &checkTask) override
static bool canHandleStatic(CheckTask< storm::logic::Formula, ValueType > const &checkTask)
SparseExplorationModelChecker(storm::prism::Program const &program)
virtual bool canHandle(CheckTask< storm::logic::Formula, ValueType > const &checkTask) const override
ValueType getDifferenceOfStateBounds(StateType const &state, ExplorationInformation< StateType, ValueType > const &explorationInformation) const
Definition Bounds.cpp:77
ValueType getLowerBoundForState(StateType const &state, ExplorationInformation< StateType, ValueType > const &explorationInformation) const
Definition Bounds.cpp:21
ValueType getUpperBoundForState(StateType const &state, ExplorationInformation< StateType, ValueType > const &explorationInformation) const
Definition Bounds.cpp:37
bool performPrecomputationExcessiveSampledPaths(std::size_t &numberOfSampledPathsSinceLastPrecomputation) const
std::vector< index_type > const & getRowGroupIndices() const
Returns the grouping of rows of this matrix.
storm::storage::SparseMatrix< value_type > transpose(bool joinGroups=false, bool keepZeros=false) const
Transposes the matrix.
#define STORM_LOG_DEBUG(message)
Definition logging.h:21
#define STORM_LOG_STATISTICS(message)
Definition logging.h:41
#define STORM_LOG_TRACE(message)
Definition logging.h:15
#define STORM_LOG_ASSERT(cond, message)
Definition macros.h:9
#define STORM_LOG_THROW(cond, exception, message)
Definition macros.h:28
SFTBDDChecker::ValueType ValueType
storm::storage::BitVector CompressedState
FragmentSpecification reachability()
storm::storage::BitVector performProb1A(storm::models::sparse::NondeterministicModel< T, RM > const &model, storm::storage::SparseMatrix< T > const &backwardTransitions, storm::storage::BitVector const &phiStates, storm::storage::BitVector const &psiStates)
Computes the sets of states that have probability 1 of satisfying phi until psi under all possible re...
Definition graph.cpp:981
storm::storage::BitVector performProb0A(storm::storage::SparseMatrix< T > const &backwardTransitions, storm::storage::BitVector const &phiStates, storm::storage::BitVector const &psiStates)
Definition graph.cpp:733
storm::storage::BitVector performProb0E(storm::models::sparse::NondeterministicModel< T, RM > const &model, storm::storage::SparseMatrix< T > const &backwardTransitions, storm::storage::BitVector const &phiStates, storm::storage::BitVector const &psiStates)
Computes the sets of states that have probability 0 of satisfying phi until psi under at least one po...
Definition graph.cpp:960
storm::storage::BitVector performProb1E(storm::storage::SparseMatrix< T > const &transitionMatrix, std::vector< uint_fast64_t > const &nondeterministicChoiceIndices, storm::storage::SparseMatrix< T > const &backwardTransitions, storm::storage::BitVector const &phiStates, storm::storage::BitVector const &psiStates, boost::optional< storm::storage::BitVector > const &choiceConstraint)
Computes the sets of states that have probability 1 of satisfying phi until psi under at least one po...
Definition graph.cpp:741
bool isOne(ValueType const &a)
Definition constants.cpp:37
ValueType min(ValueType const &first, ValueType const &second)
ValueType zero()
Definition constants.cpp:24
ValueType one()
Definition constants.cpp:19
solver::OptimizationDirection OptimizationDirection
void printToStream(std::ostream &out, ExplorationInformation< StateType, ValueType > const &explorationInformation) const
void updateMaxPathLength(std::size_t const &currentPathLength)