#include "storm/modelchecker/prctl/helper/rewardbounded/EpochModel.h" #include "storm/modelchecker/prctl/helper/rewardbounded/MultiDimensionalRewardUnfolding.h" #include "storm/exceptions/UncheckedRequirementException.h" namespace storm { namespace modelchecker { namespace helper { namespace rewardbounded { template std::vector analyzeTrivialDtmcEpochModel(EpochModel& epochModel) { std::vector epochResult; epochResult.reserve(epochModel.epochInStates.getNumberOfSetBits()); auto stepSolutionIt = epochModel.stepSolutions.begin(); auto stepChoiceIt = epochModel.stepChoices.begin(); for (auto const& state : epochModel.epochInStates) { while (*stepChoiceIt < state) { ++stepChoiceIt; ++stepSolutionIt; } if (epochModel.objectiveRewardFilter.front().get(state)) { if (*stepChoiceIt == state) { epochResult.push_back(epochModel.objectiveRewards.front()[state] + *stepSolutionIt); } else { epochResult.push_back(epochModel.objectiveRewards.front()[state]); } } else { if (*stepChoiceIt == state) { epochResult.push_back(*stepSolutionIt); } else { epochResult.push_back(storm::utility::zero()); } } } return epochResult; } template std::vector analyzeNonTrivialDtmcEpochModel(Environment const& env, EpochModel& epochModel, std::vector& x, std::vector& b, std::unique_ptr>& linEqSolver, boost::optional const& lowerBound, boost::optional const& upperBound) { // Update some data for the case that the Matrix has changed if (epochModel.epochMatrixChanged) { x.assign(epochModel.epochMatrix.getRowGroupCount(), storm::utility::zero()); storm::solver::GeneralLinearEquationSolverFactory linearEquationSolverFactory; linEqSolver = linearEquationSolverFactory.create(env, epochModel.epochMatrix); linEqSolver->setCachingEnabled(true); auto req = linEqSolver->getRequirements(env); if (lowerBound) { linEqSolver->setLowerBound(lowerBound.get()); req.clearLowerBounds(); } if (upperBound) { linEqSolver->setUpperBound(upperBound.get()); req.clearUpperBounds(); } STORM_LOG_THROW(!req.hasEnabledCriticalRequirement(), storm::exceptions::UncheckedRequirementException, "Solver requirements " + req.getEnabledRequirementsAsString() + " not checked."); } // Prepare the right hand side of the equation system b.assign(epochModel.epochMatrix.getRowCount(), storm::utility::zero()); std::vector const& objectiveValues = epochModel.objectiveRewards.front(); for (auto const& choice : epochModel.objectiveRewardFilter.front()) { b[choice] = objectiveValues[choice]; } auto stepSolutionIt = epochModel.stepSolutions.begin(); for (auto const& choice : epochModel.stepChoices) { b[choice] += *stepSolutionIt; ++stepSolutionIt; } assert(stepSolutionIt == epochModel.stepSolutions.end()); // Solve the minMax equation system linEqSolver->solveEquations(env, x, b); return storm::utility::vector::filterVector(x, epochModel.epochInStates); } template std::vector analyzeTrivialMdpEpochModel(OptimizationDirection dir, EpochModel& epochModel) { // Assert that the epoch model is indeed trivial assert(epochModel.epochMatrix.getEntryCount() == 0); std::vector epochResult; epochResult.reserve(epochModel.epochInStates.getNumberOfSetBits()); auto stepSolutionIt = epochModel.stepSolutions.begin(); auto stepChoiceIt = epochModel.stepChoices.begin(); for (auto const& state : epochModel.epochInStates) { // Obtain the best choice for this state ValueType bestValue; uint64_t lastChoice = epochModel.epochMatrix.getRowGroupIndices()[state + 1]; bool isFirstChoice = true; for (uint64_t choice = epochModel.epochMatrix.getRowGroupIndices()[state]; choice < lastChoice; ++choice) { while (*stepChoiceIt < choice) { ++stepChoiceIt; ++stepSolutionIt; } ValueType choiceValue = storm::utility::zero(); if (epochModel.objectiveRewardFilter.front().get(choice)) { choiceValue += epochModel.objectiveRewards.front()[choice]; } if (*stepChoiceIt == choice) { choiceValue += *stepSolutionIt; } if (isFirstChoice) { bestValue = std::move(choiceValue); isFirstChoice = false; } else { if (storm::solver::minimize(dir)) { if (choiceValue < bestValue) { bestValue = std::move(choiceValue); } } else { if (choiceValue > bestValue) { bestValue = std::move(choiceValue); } } } } // Insert the solution w.r.t. this choice epochResult.push_back(std::move(bestValue)); } return epochResult; } template std::vector analyzeNonTrivialMdpEpochModel(Environment const& env, OptimizationDirection dir, EpochModel& epochModel, std::vector& x, std::vector& b, std::unique_ptr>& minMaxSolver, boost::optional const& lowerBound, boost::optional const& upperBound) { // Update some data for the case that the Matrix has changed if (epochModel.epochMatrixChanged) { x.assign(epochModel.epochMatrix.getRowGroupCount(), storm::utility::zero()); storm::solver::GeneralMinMaxLinearEquationSolverFactory minMaxLinearEquationSolverFactory; minMaxSolver = minMaxLinearEquationSolverFactory.create(env, epochModel.epochMatrix); minMaxSolver->setHasUniqueSolution(); minMaxSolver->setHasNoEndComponents(); minMaxSolver->setOptimizationDirection(dir); minMaxSolver->setCachingEnabled(true); minMaxSolver->setTrackScheduler(true); auto req = minMaxSolver->getRequirements(env, dir, false); if (lowerBound) { minMaxSolver->setLowerBound(lowerBound.get()); req.clearLowerBounds(); } if (upperBound) { minMaxSolver->setUpperBound(upperBound.get()); req.clearUpperBounds(); } STORM_LOG_THROW(!req.hasEnabledCriticalRequirement(), storm::exceptions::UncheckedRequirementException, "Solver requirements " + req.getEnabledRequirementsAsString() + " not checked."); minMaxSolver->setRequirementsChecked(); } else { auto choicesTmp = minMaxSolver->getSchedulerChoices(); minMaxSolver->setInitialScheduler(std::move(choicesTmp)); } // Prepare the right hand side of the equation system b.assign(epochModel.epochMatrix.getRowCount(), storm::utility::zero()); std::vector const& objectiveValues = epochModel.objectiveRewards.front(); for (auto const& choice : epochModel.objectiveRewardFilter.front()) { b[choice] = objectiveValues[choice]; } auto stepSolutionIt = epochModel.stepSolutions.begin(); for (auto const& choice : epochModel.stepChoices) { b[choice] += *stepSolutionIt; ++stepSolutionIt; } assert(stepSolutionIt == epochModel.stepSolutions.end()); // Solve the minMax equation system minMaxSolver->solveEquations(env, x, b); return storm::utility::vector::filterVector(x, epochModel.epochInStates); } template<> std::vector EpochModel::analyzeSingleObjective( const storm::Environment &env, std::vector &x, std::vector &b, std::unique_ptr> &linEqSolver, const boost::optional &lowerBound, const boost::optional &upperBound) { STORM_LOG_ASSERT(epochMatrix.hasTrivialRowGrouping(), "This operation is only allowed if no nondeterminism is present."); STORM_LOG_ASSERT(equationSolverProblemFormat.is_initialized(), "Unknown equation problem format."); // If the epoch matrix is empty we do not need to solve a linear equation system bool convertToEquationSystem = (equationSolverProblemFormat == storm::solver::LinearEquationSolverProblemFormat::EquationSystem); if ((convertToEquationSystem && epochMatrix.isIdentityMatrix()) || (!convertToEquationSystem && epochMatrix.getEntryCount() == 0)) { return analyzeTrivialDtmcEpochModel(*this); } else { return analyzeNonTrivialDtmcEpochModel(env, *this, x, b, linEqSolver, lowerBound, upperBound); } } template<> std::vector EpochModel::analyzeSingleObjective( const storm::Environment &env, storm::OptimizationDirection dir, std::vector &x, std::vector &b, std::unique_ptr> &minMaxSolver, const boost::optional &lowerBound, const boost::optional &upperBound) { // If the epoch matrix is empty we do not need to solve a linear equation system if (epochMatrix.getEntryCount() == 0) { return analyzeTrivialMdpEpochModel(dir, *this); } else { return analyzeNonTrivialMdpEpochModel(env, dir, *this, x, b, minMaxSolver, lowerBound, upperBound); } } template<> std::vector EpochModel::analyzeSingleObjective( const storm::Environment &env, std::vector &x, std::vector &b, std::unique_ptr> &linEqSolver, const boost::optional &lowerBound, const boost::optional &upperBound) { STORM_LOG_ASSERT(epochMatrix.hasTrivialRowGrouping(), "This operation is only allowed if no nondeterminism is present."); STORM_LOG_ASSERT(equationSolverProblemFormat.is_initialized(), "Unknown equation problem format."); // If the epoch matrix is empty we do not need to solve a linear equation system bool convertToEquationSystem = (equationSolverProblemFormat == storm::solver::LinearEquationSolverProblemFormat::EquationSystem); if ((convertToEquationSystem && epochMatrix.isIdentityMatrix()) || (!convertToEquationSystem && epochMatrix.getEntryCount() == 0)) { return analyzeTrivialDtmcEpochModel(*this); } else { return analyzeNonTrivialDtmcEpochModel(env, *this, x, b, linEqSolver, lowerBound, upperBound); } } template<> std::vector EpochModel::analyzeSingleObjective( const storm::Environment &env, storm::OptimizationDirection dir, std::vector &x, std::vector &b, std::unique_ptr> &minMaxSolver, const boost::optional &lowerBound, const boost::optional &upperBound) { // If the epoch matrix is empty we do not need to solve a linear equation system if (epochMatrix.getEntryCount() == 0) { return analyzeTrivialMdpEpochModel(dir, *this); } else { return analyzeNonTrivialMdpEpochModel(env, dir, *this, x, b, minMaxSolver, lowerBound, upperBound); } } template struct EpochModel; template struct EpochModel; template struct EpochModel; template struct EpochModel; } } } }