#include "BoundedGloballyGameViHelper.h" #include "storm/environment/Environment.h" #include "storm/environment/solver/SolverEnvironment.h" #include "storm/environment/solver/GameSolverEnvironment.h" #include "storm/utility/SignalHandler.h" #include "storm/utility/vector.h" namespace storm { namespace modelchecker { namespace helper { namespace internal { template BoundedGloballyGameViHelper::BoundedGloballyGameViHelper(storm::storage::SparseMatrix const& transitionMatrix, storm::storage::BitVector statesOfCoalition) : _transitionMatrix(transitionMatrix), _statesOfCoalition(statesOfCoalition) { } template void BoundedGloballyGameViHelper::prepareSolversAndMultipliersReachability(const Environment& env) { _multiplier = storm::solver::MultiplierFactory().create(env, _transitionMatrix); /* _x1IsCurrent = false; */ } template void BoundedGloballyGameViHelper::performValueIteration(Environment const& env, std::vector& x, std::vector b, storm::solver::OptimizationDirection const dir, uint64_t upperBound) { prepareSolversAndMultipliersReachability(env); _b = b; /* _x1.assign(_transitionMatrix.getRowGroupCount(), storm::utility::zero()); */ _x1 = x; _x2 = _x1; /* STORM_LOG_DEBUG("_b = " << _b); STORM_LOG_DEBUG("_x1 = " << _x1); STORM_LOG_DEBUG("_x2 = " << _x2);*/ if (this->isProduceSchedulerSet()) { if (!this->_producedOptimalChoices.is_initialized()) { this->_producedOptimalChoices.emplace(); } this->_producedOptimalChoices->resize(this->_transitionMatrix.getRowGroupCount()); } for (uint64_t iter = 0; iter < upperBound; iter++) { performIterationStep(env, dir); /* STORM_LOG_DEBUG("After iteration " << iter << ":"); STORM_LOG_DEBUG("_x1 = " << _x1); STORM_LOG_DEBUG("_x2 = " << _x2);*/ /* if (storm::utility::resources::isTerminate()) { break; }*/ } /* x = xNew(); */ x = _x1; /* if (isProduceSchedulerSet()) { // We will be doing one more iteration step and track scheduler choices this time. performIterationStep(env, dir, &_producedOptimalChoices.get()); }*/ } template void BoundedGloballyGameViHelper::performIterationStep(Environment const& env, storm::solver::OptimizationDirection const dir, std::vector* choices) { if (!_multiplier) { prepareSolversAndMultipliersReachability(env); } /* _x1IsCurrent = !_x1IsCurrent;*/ // multiplyandreducegaussseidel // Ax + b if (choices == nullptr) { _multiplier->multiplyAndReduce(env, dir, _x1, &_b, _x1, nullptr, &_statesOfCoalition); } else { // Also keep track of the choices made. _multiplier->multiplyAndReduce(env, dir, _x1, &_b, _x1, choices, &_statesOfCoalition); } /* // compare applyPointwise storm::utility::vector::applyPointwise(xOld(), xNew(), xNew(), [&dir] (ValueType const& a, ValueType const& b) -> ValueType { if(storm::solver::maximize(dir)) { if(a > b) return a; else return b; } else { if(a > b) return a; else return b; } });*/ } template void BoundedGloballyGameViHelper::fillResultVector(std::vector& result, storm::storage::BitVector psiStates) { std::vector filledVector = std::vector(psiStates.size(), storm::utility::zero()); uint bitIndex = 0; for(uint i = 0; i < filledVector.size(); i++) { if (psiStates.get(i)) { filledVector.at(i) = result.at(bitIndex); bitIndex++; } } result = filledVector; } template void BoundedGloballyGameViHelper::setProduceScheduler(bool value) { _produceScheduler = value; } template bool BoundedGloballyGameViHelper::isProduceSchedulerSet() const { return _produceScheduler; } template std::vector const& BoundedGloballyGameViHelper::getProducedOptimalChoices() const { STORM_LOG_ASSERT(this->isProduceSchedulerSet(), "Trying to get the produced optimal choices although no scheduler was requested."); STORM_LOG_ASSERT(this->_producedOptimalChoices.is_initialized(), "Trying to get the produced optimal choices but none were available. Was there a computation call before?"); return this->_producedOptimalChoices.get(); } template std::vector& BoundedGloballyGameViHelper::getProducedOptimalChoices() { STORM_LOG_ASSERT(this->isProduceSchedulerSet(), "Trying to get the produced optimal choices although no scheduler was requested."); STORM_LOG_ASSERT(this->_producedOptimalChoices.is_initialized(), "Trying to get the produced optimal choices but none were available. Was there a computation call before?"); return this->_producedOptimalChoices.get(); } template storm::storage::Scheduler BoundedGloballyGameViHelper::extractScheduler() const{ auto const& optimalChoices = getProducedOptimalChoices(); storm::storage::Scheduler scheduler(optimalChoices.size()); for (uint64_t state = 0; state < optimalChoices.size(); ++state) { scheduler.setChoice(optimalChoices[state], state); } return scheduler; } /* template std::vector& BoundedGloballyGameViHelper::xNew() { return _x1IsCurrent ? _x1 : _x2; } template std::vector const& BoundedGloballyGameViHelper::xNew() const { return _x1IsCurrent ? _x1 : _x2; } template std::vector& BoundedGloballyGameViHelper::xOld() { return _x1IsCurrent ? _x2 : _x1; } template std::vector const& BoundedGloballyGameViHelper::xOld() const { return _x1IsCurrent ? _x2 : _x1; }*/ template class BoundedGloballyGameViHelper; #ifdef STORM_HAVE_CARL template class BoundedGloballyGameViHelper; #endif } } } }