From 5fa667b847f9daaf99431d4e612b815139c89504 Mon Sep 17 00:00:00 2001 From: Sebastian Junges Date: Mon, 24 Aug 2020 21:37:46 -0700 Subject: [PATCH] add random step functionality to simulator --- src/storm/simulator/DiscreteTimeSparseModelSimulator.cpp | 9 +++++++++ src/storm/simulator/DiscreteTimeSparseModelSimulator.h | 1 + src/storm/utility/random.cpp | 4 ++++ src/storm/utility/random.h | 2 ++ 4 files changed, 16 insertions(+) diff --git a/src/storm/simulator/DiscreteTimeSparseModelSimulator.cpp b/src/storm/simulator/DiscreteTimeSparseModelSimulator.cpp index d6669516d..ba6ad778b 100644 --- a/src/storm/simulator/DiscreteTimeSparseModelSimulator.cpp +++ b/src/storm/simulator/DiscreteTimeSparseModelSimulator.cpp @@ -21,6 +21,15 @@ namespace storm { generator = storm::utility::RandomProbabilityGenerator(seed); } + template + bool DiscreteTimeSparseModelSimulator::randomStep() { + // TODO random_uint is slow + if (model.getTransitionMatrix().getRowGroupSize(currentState) == 0) { + return false; + } + return step(generator.random_uint(0, model.getTransitionMatrix().getRowGroupSize(currentState) - 1)); + } + template bool DiscreteTimeSparseModelSimulator::step(uint64_t action) { // TODO lots of optimization potential. diff --git a/src/storm/simulator/DiscreteTimeSparseModelSimulator.h b/src/storm/simulator/DiscreteTimeSparseModelSimulator.h index a0dcf4e3a..617a2c9c8 100644 --- a/src/storm/simulator/DiscreteTimeSparseModelSimulator.h +++ b/src/storm/simulator/DiscreteTimeSparseModelSimulator.h @@ -19,6 +19,7 @@ namespace storm { DiscreteTimeSparseModelSimulator(storm::models::sparse::Model const& model); void setSeed(uint64_t); bool step(uint64_t action); + bool randomStep(); std::vector const& getLastRewards() const; uint64_t getCurrentState() const; bool resetToInitial(); diff --git a/src/storm/utility/random.cpp b/src/storm/utility/random.cpp index ad0afecb0..f13d403cd 100644 --- a/src/storm/utility/random.cpp +++ b/src/storm/utility/random.cpp @@ -19,6 +19,10 @@ namespace storm { return distribution(engine); } + uint64_t RandomProbabilityGenerator::random_uint(uint64_t min, uint64_t max) { + return std::uniform_int_distribution(min, max)(engine); + } + } } diff --git a/src/storm/utility/random.h b/src/storm/utility/random.h index ef5b359df..93bfad20c 100644 --- a/src/storm/utility/random.h +++ b/src/storm/utility/random.h @@ -8,6 +8,7 @@ namespace storm { RandomProbabilityGenerator(); RandomProbabilityGenerator(uint64_t seed); ValueType random() const; + uint64_t random_uint(uint64_t min, uint64_t max); }; @@ -17,6 +18,7 @@ namespace storm { RandomProbabilityGenerator(); RandomProbabilityGenerator(uint64_t seed); double random(); + uint64_t random_uint(uint64_t min, uint64_t max); private: std::uniform_real_distribution distribution; std::mt19937 engine;