Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for has_cycle() on sparse models #103

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions src/storage/model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@
#include "storm/models/symbolic/Ctmc.h"
#include "storm/models/symbolic/MarkovAutomaton.h"
#include "storm/models/symbolic/StandardRewardModel.h"
#include "storm/utility/dd.h"

#include "storm/storage/dd/DdManager.h"

#include "storm/storage/Scheduler.h"
#include "storm/utility/dd.h"
#include "storm/utility/graph.h"

#include <functional>
#include <string>
Expand Down Expand Up @@ -90,6 +89,26 @@ storm::storage::BitVector statesWithParameter(SparseModel<RationalFunction> cons
return result;
}

template<typename ValueType>
bool hasCycle(SparseModel<ValueType> const& model) {
// Find all states with absorbing self-loops and exclude them
auto const& matrix = model.getTransitionMatrix();
storm::storage::BitVector subStates(matrix.getColumnCount(), true);
for (auto group = 0; group < matrix.getRowGroupCount(); ++group) {
for (auto& entry : matrix.getRowGroup(group)) {
if (entry.getColumn() == group) {
if (storm::utility::isOne<ValueType>(entry.getValue())) {
// Found self-loop
subStates.set(entry.getColumn(), false);
}
}
if (entry.getColumn() >= group) {
continue;
}
}
}
return storm::utility::graph::hasCycle(matrix, subStates);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is problematic for MDPs, when we have one self-loop row first and then one non-selfloop row which induces a cycle with other states, right?

I think the "hasCycle" would need to have a "subChoices" input to make this work?
Alternatively, this method could also build an SCC decomposition and we then check if each SCC has size 1.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, this is indeed problematic.
I am open for other suggestion. The SCC decomposition might be a good idea.
I actually just wanted to have a simple function to check for acyclity. Unfortunately, hasCycle is not exactly giving me what I want. Maybe it makes sense to implement a better function in Storm directly.

}

std::string getModelInfoPrinter(ModelBase const& model) {
std::stringstream ss;
Expand Down Expand Up @@ -221,6 +240,7 @@ void define_sparse_model(py::module& m, std::string const& vtSuffix) {
.def_property_readonly("state_valuations", [](SparseModel<ValueType> const& model) {return model.getStateValuations();}, "state valuations")
.def("reduce_to_state_based_rewards", &SparseModel<ValueType>::reduceToStateBasedRewards)
.def("is_sink_state", &SparseModel<ValueType>::isSinkState, py::arg("state"))
.def("has_cycle", &hasCycle<ValueType>, "Whether the model contains a cycle")
.def("__str__", &getModelInfoPrinter)
.def("to_dot", [](SparseModel<ValueType>& model) { std::stringstream ss; model.writeDotToStream(ss); return ss.str(); }, "Write dot to a string")
;
Expand Down
15 changes: 15 additions & 0 deletions tests/storage/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,21 @@ def test_choice_origins(self):
model = stormpy.build_sparse_model_with_options(program, options)
a = model.choice_origins.get_edge_index_set(3)

def test_cycles(self):
program = stormpy.parse_prism_program(get_example_path("dtmc", "crowds-5-5.pm"))
model = stormpy.build_model(program)
assert model.nr_states == 8607
assert model.nr_transitions == 15113
assert model.has_cycle()
program = stormpy.parse_prism_program(get_example_path("dtmc", "brp-16-2.pm"))
model = stormpy.build_model(program)
assert model.nr_states == 677
assert model.nr_transitions == 867
assert not model.has_cycle()
program = stormpy.parse_prism_program(get_example_path("ma", "simple.ma"))
model = stormpy.build_model(program)
assert model.has_cycle()

class TestSymbolicSylvanModel:
def test_build_dtmc_from_prism_program(self):
program = stormpy.parse_prism_program(get_example_path("dtmc", "die.pm"))
Expand Down