@ -66,33 +66,33 @@ namespace storm {
storm : : storage : : SparseMatrixBuilder < ValueType > transitionRewardMatrixBuilder ;
} ;
template < typename ValueType , typename RewardModelType , typename Index Type>
ExplicitPrismModelBuilder < ValueType , RewardModelType , Index Type> : : StateInformation : : StateInformation ( uint_fast64_t numberOfStates ) : valuations ( numberOfStates ) {
template < typename ValueType , typename RewardModelType , typename State Type>
ExplicitPrismModelBuilder < ValueType , RewardModelType , State Type> : : StateInformation : : StateInformation ( uint_fast64_t numberOfStates ) : valuations ( numberOfStates ) {
// Intentionally left empty.
}
template < typename ValueType , typename RewardModelType , typename Index Type>
ExplicitPrismModelBuilder < ValueType , RewardModelType , Index Type> : : InternalStateInformation : : InternalStateInformation ( uint64_t bitsPerState ) : stateStorage ( bitsPerState , 10000000 ) , bitsPerState ( bitsPerState ) , reachable States( ) {
template < typename ValueType , typename RewardModelType , typename State Type>
ExplicitPrismModelBuilder < ValueType , RewardModelType , State Type> : : InternalStateInformation : : InternalStateInformation ( uint64_t bitsPerState ) : stateStorage ( bitsPerState , 10000000 ) , bitsPerState ( bitsPerState ) , numberOf States( ) {
// Intentionally left empty.
}
template < typename ValueType , typename RewardModelType , typename Index Type>
ExplicitPrismModelBuilder < ValueType , RewardModelType , Index Type> : : ModelComponents : : ModelComponents ( ) : transitionMatrix ( ) , stateLabeling ( ) , rewardModels ( ) , choiceLabeling ( ) {
template < typename ValueType , typename RewardModelType , typename State Type>
ExplicitPrismModelBuilder < ValueType , RewardModelType , State Type> : : ModelComponents : : ModelComponents ( ) : transitionMatrix ( ) , stateLabeling ( ) , rewardModels ( ) , choiceLabeling ( ) {
// Intentionally left empty.
}
template < typename ValueType , typename RewardModelType , typename Index Type>
ExplicitPrismModelBuilder < ValueType , RewardModelType , Index Type> : : Options : : Options ( ) : buildCommandLabels ( false ) , buildAllRewardModels ( true ) , buildStateInformation ( false ) , rewardModelsToBuild ( ) , constantDefinitions ( ) , buildAllLabels ( true ) , labelsToBuild ( ) , expressionLabels ( ) , terminalStates ( ) , negatedTerminalStates ( ) {
template < typename ValueType , typename RewardModelType , typename State Type>
ExplicitPrismModelBuilder < ValueType , RewardModelType , State Type> : : Options : : Options ( ) : buildCommandLabels ( false ) , buildAllRewardModels ( true ) , buildStateInformation ( false ) , rewardModelsToBuild ( ) , constantDefinitions ( ) , buildAllLabels ( true ) , labelsToBuild ( ) , expressionLabels ( ) , terminalStates ( ) , negatedTerminalStates ( ) {
// Intentionally left empty.
}
template < typename ValueType , typename RewardModelType , typename Index Type>
ExplicitPrismModelBuilder < ValueType , RewardModelType , Index Type> : : Options : : Options ( storm : : logic : : Formula const & formula ) : buildCommandLabels ( false ) , buildAllRewardModels ( false ) , buildStateInformation ( false ) , rewardModelsToBuild ( ) , constantDefinitions ( ) , buildAllLabels ( false ) , labelsToBuild ( std : : set < std : : string > ( ) ) , expressionLabels ( std : : vector < storm : : expressions : : Expression > ( ) ) , terminalStates ( ) , negatedTerminalStates ( ) {
template < typename ValueType , typename RewardModelType , typename State Type>
ExplicitPrismModelBuilder < ValueType , RewardModelType , State Type> : : Options : : Options ( storm : : logic : : Formula const & formula ) : buildCommandLabels ( false ) , buildAllRewardModels ( false ) , buildStateInformation ( false ) , rewardModelsToBuild ( ) , constantDefinitions ( ) , buildAllLabels ( false ) , labelsToBuild ( std : : set < std : : string > ( ) ) , expressionLabels ( std : : vector < storm : : expressions : : Expression > ( ) ) , terminalStates ( ) , negatedTerminalStates ( ) {
this - > preserveFormula ( formula ) ;
}
template < typename ValueType , typename RewardModelType , typename Index Type>
ExplicitPrismModelBuilder < ValueType , RewardModelType , Index Type> : : Options : : Options ( std : : vector < std : : shared_ptr < const storm : : logic : : Formula > > const & formulas ) : buildCommandLabels ( false ) , buildAllRewardModels ( false ) , buildStateInformation ( false ) , rewardModelsToBuild ( ) , constantDefinitions ( ) , buildAllLabels ( false ) , labelsToBuild ( ) , expressionLabels ( ) , terminalStates ( ) , negatedTerminalStates ( ) {
template < typename ValueType , typename RewardModelType , typename State Type>
ExplicitPrismModelBuilder < ValueType , RewardModelType , State Type> : : Options : : Options ( std : : vector < std : : shared_ptr < const storm : : logic : : Formula > > const & formulas ) : buildCommandLabels ( false ) , buildAllRewardModels ( false ) , buildStateInformation ( false ) , rewardModelsToBuild ( ) , constantDefinitions ( ) , buildAllLabels ( false ) , labelsToBuild ( ) , expressionLabels ( ) , terminalStates ( ) , negatedTerminalStates ( ) {
if ( formulas . empty ( ) ) {
this - > buildAllRewardModels = true ;
this - > buildAllLabels = true ;
@ -106,8 +106,8 @@ namespace storm {
}
}
template < typename ValueType , typename RewardModelType , typename Index Type>
void ExplicitPrismModelBuilder < ValueType , RewardModelType , Index Type> : : Options : : setTerminalStatesFromFormula ( storm : : logic : : Formula const & formula ) {
template < typename ValueType , typename RewardModelType , typename State Type>
void ExplicitPrismModelBuilder < ValueType , RewardModelType , State Type> : : Options : : setTerminalStatesFromFormula ( storm : : logic : : Formula const & formula ) {
if ( formula . isAtomicExpressionFormula ( ) ) {
terminalStates = formula . asAtomicExpressionFormula ( ) . getExpression ( ) ;
} else if ( formula . isAtomicLabelFormula ( ) ) {
@ -136,8 +136,8 @@ namespace storm {
}
}
template < typename ValueType , typename RewardModelType , typename Index Type>
void ExplicitPrismModelBuilder < ValueType , RewardModelType , Index Type> : : Options : : preserveFormula ( storm : : logic : : Formula const & formula ) {
template < typename ValueType , typename RewardModelType , typename State Type>
void ExplicitPrismModelBuilder < ValueType , RewardModelType , State Type> : : Options : : preserveFormula ( storm : : logic : : Formula const & formula ) {
// If we already had terminal states, we need to erase them.
if ( terminalStates ) {
terminalStates . reset ( ) ;
@ -174,40 +174,29 @@ namespace storm {
}
}
template < typename ValueType , typename RewardModelType , typename Index Type>
void ExplicitPrismModelBuilder < ValueType , RewardModelType , Index Type> : : Options : : addConstantDefinitionsFromString ( storm : : prism : : Program const & program , std : : string const & constantDefinitionString ) {
template < typename ValueType , typename RewardModelType , typename State Type>
void ExplicitPrismModelBuilder < ValueType , RewardModelType , State Type> : : Options : : addConstantDefinitionsFromString ( storm : : prism : : Program const & program , std : : string const & constantDefinitionString ) {
constantDefinitions = storm : : utility : : prism : : parseConstantDefinitionString ( program , constantDefinitionString ) ;
}
template < typename ValueType , typename RewardModelType , typename IndexType >
typename ExplicitPrismModelBuilder < ValueType , RewardModelType , IndexType > : : StateInformation const & ExplicitPrismModelBuilder < ValueType , RewardModelType , IndexType > : : getStateInformation ( ) const {
STORM_LOG_THROW ( static_cast < bool > ( stateInformation ) , storm : : exceptions : : InvalidOperationException , " The state information was not properly build. " ) ;
return stateInformation . get ( ) ;
}
template < typename ValueType , typename RewardModelType , typename IndexType >
storm : : prism : : Program const & ExplicitPrismModelBuilder < ValueType , RewardModelType , IndexType > : : getTranslatedProgram ( ) const {
return preparedProgram . get ( ) ;
}
template < typename ValueType , typename RewardModelType , typename IndexType >
std : : shared_ptr < storm : : models : : sparse : : Model < ValueType , RewardModelType > > ExplicitPrismModelBuilder < ValueType , RewardModelType , IndexType > : : translateProgram ( storm : : prism : : Program program , Options const & options ) {
template < typename ValueType , typename RewardModelType , typename StateType >
ExplicitPrismModelBuilder < ValueType , RewardModelType , StateType > : : ExplicitPrismModelBuilder ( storm : : prism : : Program const & program , Options const & options ) : options ( options ) , program ( program ) {
// Start by defining the undefined constants in the model.
if ( options . constantDefinitions ) {
preparedP rogram = program . defineUndefinedConstants ( options . constantDefinitions . get ( ) ) ;
this - > program = program . defineUndefinedConstants ( options . constantDefinitions . get ( ) ) ;
} else {
preparedP rogram = program ;
this - > program = program ;
}
// If the program still contains undefined constants and we are not in a parametric setting, assemble an appropriate error message.
# ifdef STORM_HAVE_CARL
// If the program either has undefined constants or we are building a parametric model, but the parameters
// not only appear in the probabilities, we re
if ( ! std : : is_same < ValueType , storm : : RationalFunction > : : value & & preparedProgram - > hasUndefinedConstants ( ) ) {
if ( ! std : : is_same < ValueType , storm : : RationalFunction > : : value & & this - > program . hasUndefinedConstants ( ) ) {
# else
if ( preparedP rogram- > hasUndefinedConstants ( ) ) {
if ( this - > p rogram- > hasUndefinedConstants ( ) ) {
# endif
std : : vector < std : : reference_wrapper < storm : : prism : : Constant const > > undefinedConstants = preparedProgram - > getUndefinedConstants ( ) ;
std : : vector < std : : reference_wrapper < storm : : prism : : Constant const > > undefinedConstants = this - > program . getUndefinedConstants ( ) ;
std : : stringstream stream ;
bool printComma = false ;
for ( auto const & constant : undefinedConstants ) {
@ -221,7 +210,7 @@ namespace storm {
stream < < " . " ;
STORM_LOG_THROW ( false , storm : : exceptions : : InvalidArgumentException , " Program still contains these undefined constants: " + stream . str ( ) ) ;
# ifdef STORM_HAVE_CARL
} else if ( std : : is_same < ValueType , storm : : RationalFunction > : : value & & ! preparedProgram - > hasUndefinedConstantsOnlyInUpdateProbabilitiesAndRewards ( ) ) {
} else if ( std : : is_same < ValueType , storm : : RationalFunction > : : value & & ! this - > program . hasUndefinedConstantsOnlyInUpdateProbabilitiesAndRewards ( ) ) {
STORM_LOG_THROW ( false , storm : : exceptions : : InvalidArgumentException , " The program contains undefined constants that appear in some places other than update probabilities and reward value expressions, which is not admitted. " ) ;
# endif
}
@ -229,50 +218,64 @@ namespace storm {
// If the set of labels we are supposed to built is restricted, we need to remove the other labels from the program.
if ( options . labelsToBuild ) {
if ( ! options . buildAllLabels ) {
preparedProgram - > filterLabels ( options . labelsToBuild . get ( ) ) ;
this - > program . filterLabels ( options . labelsToBuild . get ( ) ) ;
}
}
// If we need to build labels for expressions that may appear in some formula, we need to add appropriate
// labels to the program.
if ( options . expressionLabels ) {
std : : map < storm : : expressions : : Variable , storm : : expressions : : Expression > constantsSubstitution = preparedProgram - > getConstantsSubstitution ( ) ;
std : : map < storm : : expressions : : Variable , storm : : expressions : : Expression > constantsSubstitution = this - > program . getConstantsSubstitution ( ) ;
for ( auto const & expression : options . expressionLabels . get ( ) ) {
std : : stringstream stream ;
stream < < expression . substitute ( constantsSubstitution ) ;
std : : string name = stream . str ( ) ;
if ( ! preparedProgram - > hasLabel ( name ) ) {
preparedProgram - > addLabel ( name , expression ) ;
if ( ! this - > program . hasLabel ( name ) ) {
this - > program . addLabel ( name , expression ) ;
}
}
}
// Now that the program is fixed, we we need to substitute all constants with their concrete value.
preparedProgram = preparedProgram - > substituteConstants ( ) ;
this - > program = program . substituteConstants ( ) ;
}
STORM_LOG_DEBUG ( " Building representation of program: " < < std : : endl < < * preparedProgram < < std : : endl ) ;
template < typename ValueType , typename RewardModelType , typename StateType >
typename ExplicitPrismModelBuilder < ValueType , RewardModelType , StateType > : : StateInformation const & ExplicitPrismModelBuilder < ValueType , RewardModelType , StateType > : : getStateInformation ( ) const {
STORM_LOG_THROW ( static_cast < bool > ( stateInformation ) , storm : : exceptions : : InvalidOperationException , " The state information was not properly build. " ) ;
return stateInformation . get ( ) ;
}
template < typename ValueType , typename RewardModelType , typename StateType >
storm : : prism : : Program const & ExplicitPrismModelBuilder < ValueType , RewardModelType , StateType > : : getTranslatedProgram ( ) const {
return program ;
}
template < typename ValueType , typename RewardModelType , typename StateType >
std : : shared_ptr < storm : : models : : sparse : : Model < ValueType , RewardModelType > > ExplicitPrismModelBuilder < ValueType , RewardModelType , StateType > : : translate ( ) {
STORM_LOG_DEBUG ( " Building representation of program: " < < std : : endl < < * program < < std : : endl ) ;
// Select the appropriate reward models (after the constants have been substituted).
std : : vector < std : : reference_wrapper < storm : : prism : : RewardModel const > > selectedRewardModels ;
// First, we make sure that all selected reward models actually exist.
for ( auto const & rewardModelName : options . rewardModelsToBuild ) {
STORM_LOG_THROW ( rewardModelName . empty ( ) | | preparedProgram - > hasRewardModel ( rewardModelName ) , storm : : exceptions : : InvalidArgumentException , " Model does not possess a reward model with the name ' " < < rewardModelName < < " '. " ) ;
STORM_LOG_THROW ( rewardModelName . empty ( ) | | program . hasRewardModel ( rewardModelName ) , storm : : exceptions : : InvalidArgumentException , " Model does not possess a reward model with the name ' " < < rewardModelName < < " '. " ) ;
}
for ( auto const & rewardModel : preparedProgram - > getRewardModels ( ) ) {
for ( auto const & rewardModel : program . getRewardModels ( ) ) {
if ( options . buildAllRewardModels | | options . rewardModelsToBuild . find ( rewardModel . getName ( ) ) ! = options . rewardModelsToBuild . end ( ) ) {
selectedRewardModels . push_back ( rewardModel ) ;
}
}
// If no reward model was selected until now and a referenced reward model appears to be unique, we build
// the only existing reward model (given that no explicit name was given for the referenced reward model).
if ( selectedRewardModels . empty ( ) & & preparedProgram - > getNumberOfRewardModels ( ) = = 1 & & options . rewardModelsToBuild . size ( ) = = 1 & & * options . rewardModelsToBuild . begin ( ) = = " " ) {
selectedRewardModels . push_back ( preparedProgram - > getRewardModel ( 0 ) ) ;
if ( selectedRewardModels . empty ( ) & & program . getNumberOfRewardModels ( ) = = 1 & & options . rewardModelsToBuild . size ( ) = = 1 & & * options . rewardModelsToBuild . begin ( ) = = " " ) {
selectedRewardModels . push_back ( program . getRewardModel ( 0 ) ) ;
}
ModelComponents modelComponents = buildModelComponents ( * preparedPr ogram , selectedRewardModels , options ) ;
ModelComponents modelComponents = buildModelComponents ( * program , selectedRewardModels , options ) ;
std : : shared_ptr < storm : : models : : sparse : : Model < ValueType , RewardModelType > > result ;
switch ( program . getModelType ( ) ) {
@ -293,18 +296,8 @@ namespace storm {
return result ;
}
template < typename ValueType , typename RewardModelType , typename IndexType >
void ExplicitPrismModelBuilder < ValueType , RewardModelType , IndexType > : : unpackStateIntoEvaluator ( storm : : storage : : BitVector const & currentState , VariableInformation const & variableInformation , storm : : expressions : : ExpressionEvaluator < ValueType > & evaluator ) {
for ( auto const & booleanVariable : variableInformation . booleanVariables ) {
evaluator . setBooleanValue ( booleanVariable . variable , currentState . get ( booleanVariable . bitOffset ) ) ;
}
for ( auto const & integerVariable : variableInformation . integerVariables ) {
evaluator . setIntegerValue ( integerVariable . variable , currentState . getAsInt ( integerVariable . bitOffset , integerVariable . bitWidth ) + integerVariable . lowerBound ) ;
}
}
template < typename ValueType , typename RewardModelType , typename IndexType >
storm : : expressions : : SimpleValuation ExplicitPrismModelBuilder < ValueType , RewardModelType , IndexType > : : unpackStateIntoValuation ( storm : : storage : : BitVector const & currentState , VariableInformation const & variableInformation ) {
template < typename ValueType , typename RewardModelType , typename StateType >
storm : : expressions : : SimpleValuation ExplicitPrismModelBuilder < ValueType , RewardModelType , StateType > : : unpackStateIntoValuation ( storm : : storage : : BitVector const & currentState ) {
storm : : expressions : : SimpleValuation result ( variableInformation . manager . getSharedPointer ( ) ) ;
for ( auto const & booleanVariable : variableInformation . booleanVariables ) {
result . setBooleanValue ( booleanVariable . variable , currentState . get ( booleanVariable . bitOffset ) ) ;
@ -315,254 +308,26 @@ namespace storm {
return result ;
}
template < typename ValueType , typename RewardModelType , typename IndexType >
typename ExplicitPrismModelBuilder < ValueType , RewardModelType , IndexType > : : CompressedState ExplicitPrismModelBuilder < ValueType , RewardModelType , IndexType > : : applyUpdate ( VariableInformation const & variableInformation , CompressedState const & state , storm : : prism : : Update const & update , storm : : expressions : : ExpressionEvaluator < ValueType > const & evaluator ) {
return applyUpdate ( variableInformation , state , state , update , evaluator ) ;
}
template < typename ValueType , typename RewardModelType , typename IndexType >
typename ExplicitPrismModelBuilder < ValueType , RewardModelType , IndexType > : : CompressedState ExplicitPrismModelBuilder < ValueType , RewardModelType , IndexType > : : applyUpdate ( VariableInformation const & variableInformation , CompressedState const & state , CompressedState const & baseState , storm : : prism : : Update const & update , storm : : expressions : : ExpressionEvaluator < ValueType > const & evaluator ) {
CompressedState newState ( state ) ;
auto assignmentIt = update . getAssignments ( ) . begin ( ) ;
auto assignmentIte = update . getAssignments ( ) . end ( ) ;
// Iterate over all boolean assignments and carry them out.
auto boolIt = variableInformation . booleanVariables . begin ( ) ;
for ( ; assignmentIt ! = assignmentIte & & assignmentIt - > getExpression ( ) . hasBooleanType ( ) ; + + assignmentIt ) {
while ( assignmentIt - > getVariable ( ) ! = boolIt - > variable ) {
+ + boolIt ;
}
newState . set ( boolIt - > bitOffset , evaluator . asBool ( assignmentIt - > getExpression ( ) ) ) ;
}
// Iterate over all integer assignments and carry them out.
auto integerIt = variableInformation . integerVariables . begin ( ) ;
for ( ; assignmentIt ! = assignmentIte & & assignmentIt - > getExpression ( ) . hasIntegerType ( ) ; + + assignmentIt ) {
while ( assignmentIt - > getVariable ( ) ! = integerIt - > variable ) {
+ + integerIt ;
}
int_fast64_t assignedValue = evaluator . asInt ( assignmentIt - > getExpression ( ) ) ;
STORM_LOG_THROW ( assignedValue < = integerIt - > upperBound , storm : : exceptions : : WrongFormatException , " The update " < < update < < " leads to an out-of-bounds value ( " < < assignedValue < < " ) for the variable ' " < < assignmentIt - > getVariableName ( ) < < " '. " ) ;
newState . setFromInt ( integerIt - > bitOffset , integerIt - > bitWidth , assignedValue - integerIt - > lowerBound ) ;
STORM_LOG_ASSERT ( static_cast < int_fast64_t > ( newState . getAsInt ( integerIt - > bitOffset , integerIt - > bitWidth ) ) + integerIt - > lowerBound = = assignedValue , " Writing to the bit vector bucket failed (read " < < newState . getAsInt ( integerIt - > bitOffset , integerIt - > bitWidth ) < < " but wrote " < < assignedValue < < " ). " ) ;
}
// Check that we processed all assignments.
STORM_LOG_ASSERT ( assignmentIt = = assignmentIte , " Not all assignments were consumed. " ) ;
return newState ;
}
template < typename ValueType , typename RewardModelType , typename IndexType >
IndexType ExplicitPrismModelBuilder < ValueType , RewardModelType , IndexType > : : getOrAddStateIndex ( CompressedState const & state , InternalStateInformation & internalStateInformation , std : : queue < storm : : storage : : BitVector > & stateQueue ) {
template < typename ValueType , typename RewardModelType , typename StateType >
StateType ExplicitPrismModelBuilder < ValueType , RewardModelType , StateType > : : getOrAddStateIndex ( CompressedState const & state ) {
uint32_t newIndex = internalStateInformation . reachableStates . size ( ) ;
// Check, if the state was already registered.
std : : pair < uint32_t , std : : size_t > actualIndexBucketPair = internalStateInformation . stateStorage . findOrAddAndGetBucket ( state , newIndex ) ;
if ( actualIndexBucketPair . first = = newIndex ) {
stateQueu e . push ( state ) ;
internalStateInformation . reachableStates . push_back ( state ) ;
statesToExplore . push ( state ) ;
+ + internalStateInformation . numberOfStates ;
}
return actualIndexBucketPair . first ;
}
template < typename ValueType , typename RewardModelType , typename IndexType >
boost : : optional < std : : vector < std : : vector < std : : reference_wrapper < storm : : prism : : Command const > > > > ExplicitPrismModelBuilder < ValueType , RewardModelType , IndexType > : : getActiveCommandsByActionIndex ( storm : : prism : : Program const & program , storm : : expressions : : ExpressionEvaluator < ValueType > const & evaluator , uint_fast64_t const & actionIndex ) {
boost : : optional < std : : vector < std : : vector < std : : reference_wrapper < storm : : prism : : Command const > > > > result ( ( std : : vector < std : : vector < std : : reference_wrapper < storm : : prism : : Command const > > > ( ) ) ) ;
// Iterate over all modules.
for ( uint_fast64_t i = 0 ; i < program . getNumberOfModules ( ) ; + + i ) {
storm : : prism : : Module const & module = program . getModule ( i ) ;
// If the module has no command labeled with the given action, we can skip this module.
if ( ! module . hasActionIndex ( actionIndex ) ) {
continue ;
}
std : : set < uint_fast64_t > const & commandIndices = module . getCommandIndicesByActionIndex ( actionIndex ) ;
// If the module contains the action, but there is no command in the module that is labeled with
// this action, we don't have any feasible command combinations.
if ( commandIndices . empty ( ) ) {
return boost : : optional < std : : vector < std : : vector < std : : reference_wrapper < storm : : prism : : Command const > > > > ( ) ;
}
std : : vector < std : : reference_wrapper < storm : : prism : : Command const > > commands ;
// Look up commands by their indices and add them if the guard evaluates to true in the given state.
for ( uint_fast64_t commandIndex : commandIndices ) {
storm : : prism : : Command const & command = module . getCommand ( commandIndex ) ;
if ( evaluator . asBool ( command . getGuardExpression ( ) ) ) {
commands . push_back ( command ) ;
}
}
// If there was no enabled command although the module has some command with the required action label,
// we must not return anything.
if ( commands . size ( ) = = 0 ) {
return boost : : optional < std : : vector < std : : vector < std : : reference_wrapper < storm : : prism : : Command const > > > > ( ) ;
}
result . get ( ) . push_back ( std : : move ( commands ) ) ;
}
return result ;
}
template < typename ValueType , typename RewardModelType , typename IndexType >
std : : vector < Choice < ValueType > > ExplicitPrismModelBuilder < ValueType , RewardModelType , IndexType > : : getUnlabeledTransitions ( storm : : prism : : Program const & program , bool discreteTimeModel , InternalStateInformation & internalStateInformation , VariableInformation const & variableInformation , storm : : storage : : BitVector const & currentState , bool choiceLabeling , storm : : expressions : : ExpressionEvaluator < ValueType > const & evaluator , std : : queue < storm : : storage : : BitVector > & stateQueue , storm : : utility : : ConstantsComparator < ValueType > const & comparator ) {
std : : vector < Choice < ValueType > > result ;
// Iterate over all modules.
for ( uint_fast64_t i = 0 ; i < program . getNumberOfModules ( ) ; + + i ) {
storm : : prism : : Module const & module = program . getModule ( i ) ;
// Iterate over all commands.
for ( uint_fast64_t j = 0 ; j < module . getNumberOfCommands ( ) ; + + j ) {
storm : : prism : : Command const & command = module . getCommand ( j ) ;
// Only consider unlabeled commands.
if ( command . isLabeled ( ) ) continue ;
// Skip the command, if it is not enabled.
if ( ! evaluator . asBool ( command . getGuardExpression ( ) ) ) {
continue ;
}
result . push_back ( Choice < ValueType > ( 0 , choiceLabeling ) ) ;
Choice < ValueType > & choice = result . back ( ) ;
// Remember the command labels only if we were asked to.
if ( choiceLabeling ) {
choice . addChoiceLabel ( command . getGlobalIndex ( ) ) ;
}
// Iterate over all updates of the current command.
ValueType probabilitySum = storm : : utility : : zero < ValueType > ( ) ;
for ( uint_fast64_t k = 0 ; k < command . getNumberOfUpdates ( ) ; + + k ) {
storm : : prism : : Update const & update = command . getUpdate ( k ) ;
// Obtain target state index and add it to the list of known states. If it has not yet been
// seen, we also add it to the set of states that have yet to be explored.
uint32_t stateIndex = getOrAddStateIndex ( applyUpdate ( variableInformation , currentState , update , evaluator ) , internalStateInformation , stateQueue ) ;
// Update the choice by adding the probability/target state to it.
ValueType probability = evaluator . asRational ( update . getLikelihoodExpression ( ) ) ;
choice . addProbability ( stateIndex , probability ) ;
probabilitySum + = probability ;
}
// Check that the resulting distribution is in fact a distribution.
STORM_LOG_THROW ( ! discreteTimeModel | | comparator . isOne ( probabilitySum ) , storm : : exceptions : : WrongFormatException , " Probabilities do not sum to one for command ' " < < command < < " ' (actually sum to " < < probabilitySum < < " ). " ) ;
}
}
return result ;
}
template < typename ValueType , typename RewardModelType , typename IndexType >
std : : vector < Choice < ValueType > > ExplicitPrismModelBuilder < ValueType , RewardModelType , IndexType > : : getLabeledTransitions ( storm : : prism : : Program const & program , bool discreteTimeModel , InternalStateInformation & internalStateInformation , VariableInformation const & variableInformation , storm : : storage : : BitVector const & currentState , bool choiceLabeling , storm : : expressions : : ExpressionEvaluator < ValueType > const & evaluator , std : : queue < storm : : storage : : BitVector > & stateQueue , storm : : utility : : ConstantsComparator < ValueType > const & comparator ) {
std : : vector < Choice < ValueType > > result ;
for ( uint_fast64_t actionIndex : program . getSynchronizingActionIndices ( ) ) {
boost : : optional < std : : vector < std : : vector < std : : reference_wrapper < storm : : prism : : Command const > > > > optionalActiveCommandLists = getActiveCommandsByActionIndex ( program , evaluator , actionIndex ) ;
// Only process this action label, if there is at least one feasible solution.
if ( optionalActiveCommandLists ) {
std : : vector < std : : vector < std : : reference_wrapper < storm : : prism : : Command const > > > const & activeCommandList = optionalActiveCommandLists . get ( ) ;
std : : vector < std : : vector < std : : reference_wrapper < storm : : prism : : Command const > > : : const_iterator > iteratorList ( activeCommandList . size ( ) ) ;
// Initialize the list of iterators.
for ( size_t i = 0 ; i < activeCommandList . size ( ) ; + + i ) {
iteratorList [ i ] = activeCommandList [ i ] . cbegin ( ) ;
}
// As long as there is one feasible combination of commands, keep on expanding it.
bool done = false ;
while ( ! done ) {
std : : unordered_map < CompressedState , ValueType > * currentTargetStates = new std : : unordered_map < CompressedState , ValueType > ( ) ;
std : : unordered_map < CompressedState , ValueType > * newTargetStates = new std : : unordered_map < CompressedState , ValueType > ( ) ;
currentTargetStates - > emplace ( currentState , storm : : utility : : one < ValueType > ( ) ) ;
for ( uint_fast64_t i = 0 ; i < iteratorList . size ( ) ; + + i ) {
storm : : prism : : Command const & command = * iteratorList [ i ] ;
for ( uint_fast64_t j = 0 ; j < command . getNumberOfUpdates ( ) ; + + j ) {
storm : : prism : : Update const & update = command . getUpdate ( j ) ;
for ( auto const & stateProbabilityPair : * currentTargetStates ) {
// Compute the new state under the current update and add it to the set of new target states.
CompressedState newTargetState = applyUpdate ( variableInformation , stateProbabilityPair . first , currentState , update , evaluator ) ;
newTargetStates - > emplace ( newTargetState , stateProbabilityPair . second * evaluator . asRational ( update . getLikelihoodExpression ( ) ) ) ;
}
}
// If there is one more command to come, shift the target states one time step back.
if ( i < iteratorList . size ( ) - 1 ) {
delete currentTargetStates ;
currentTargetStates = newTargetStates ;
newTargetStates = new std : : unordered_map < CompressedState , ValueType > ( ) ;
}
}
// At this point, we applied all commands of the current command combination and newTargetStates
// contains all target states and their respective probabilities. That means we are now ready to
// add the choice to the list of transitions.
result . push_back ( Choice < ValueType > ( actionIndex , choiceLabeling ) ) ;
// Now create the actual distribution.
Choice < ValueType > & choice = result . back ( ) ;
// Remember the command labels only if we were asked to.
if ( choiceLabeling ) {
// Add the labels of all commands to this choice.
for ( uint_fast64_t i = 0 ; i < iteratorList . size ( ) ; + + i ) {
choice . addChoiceLabel ( iteratorList [ i ] - > get ( ) . getGlobalIndex ( ) ) ;
}
}
ValueType probabilitySum = storm : : utility : : zero < ValueType > ( ) ;
for ( auto const & stateProbabilityPair : * newTargetStates ) {
uint32_t actualIndex = getOrAddStateIndex ( stateProbabilityPair . first , internalStateInformation , stateQueue ) ;
choice . addProbability ( actualIndex , stateProbabilityPair . second ) ;
probabilitySum + = stateProbabilityPair . second ;
}
// Check that the resulting distribution is in fact a distribution.
STORM_LOG_THROW ( ! discreteTimeModel | | ! comparator . isConstant ( probabilitySum ) | | comparator . isOne ( probabilitySum ) , storm : : exceptions : : WrongFormatException , " Sum of update probabilities do not some to one for some command (actually sum to " < < probabilitySum < < " ). " ) ;
// Dispose of the temporary maps.
delete currentTargetStates ;
delete newTargetStates ;
// Now, check whether there is one more command combination to consider.
bool movedIterator = false ;
for ( int_fast64_t j = iteratorList . size ( ) - 1 ; j > = 0 ; - - j ) {
+ + iteratorList [ j ] ;
if ( iteratorList [ j ] ! = activeCommandList [ j ] . end ( ) ) {
movedIterator = true ;
} else {
// Reset the iterator to the beginning of the list.
iteratorList [ j ] = activeCommandList [ j ] . begin ( ) ;
}
}
done = ! movedIterator ;
}
}
}
return result ;
}
template < typename ValueType , typename RewardModelType , typename IndexType >
boost : : optional < std : : vector < boost : : container : : flat_set < uint_fast64_t > > > ExplicitPrismModelBuilder < ValueType , RewardModelType , IndexType > : : buildMatrices ( storm : : prism : : Program const & program , VariableInformation const & variableInformation , std : : vector < std : : reference_wrapper < storm : : prism : : RewardModel const > > const & selectedRewardModels , InternalStateInformation & internalStateInformation , bool commandLabels , bool deterministicModel , bool discreteTimeModel , storm : : storage : : SparseMatrixBuilder < ValueType > & transitionMatrixBuilder , std : : vector < RewardModelBuilder < typename RewardModelType : : ValueType > > & rewardModelBuilders , boost : : optional < storm : : expressions : : Expression > const & terminalExpression ) {
template < typename ValueType , typename RewardModelType , typename StateType >
boost : : optional < std : : vector < boost : : container : : flat_set < uint_fast64_t > > > ExplicitPrismModelBuilder < ValueType , RewardModelType , StateType > : : buildMatrices ( std : : vector < std : : reference_wrapper < storm : : prism : : RewardModel const > > const & selectedRewardModels , InternalStateInformation & internalStateInformation , storm : : storage : : SparseMatrixBuilder < ValueType > & transitionMatrixBuilder , std : : vector < RewardModelBuilder < typename RewardModelType : : ValueType > > & rewardModelBuilders , boost : : optional < storm : : expressions : : Expression > const & terminalExpression ) {
// Create choice labels, if requested,
boost : : optional < std : : vector < boost : : container : : flat_set < uint_fast64_t > > > choiceLabels ;
if ( c ommandLabels) {
if ( options . buildCommandLabels ) {
choiceLabels = std : : vector < boost : : container : : flat_set < uint_fast64_t > > ( ) ;
}
@ -606,7 +371,7 @@ namespace storm {
// Get the current state and unpack it.
storm : : storage : : BitVector currentState = stateQueue . front ( ) ;
stateQueue . pop ( ) ;
Index Type stateIndex = internalStateInformation . stateStorage . getValue ( currentState ) ;
State Type stateIndex = internalStateInformation . stateStorage . getValue ( currentState ) ;
STORM_LOG_TRACE ( " Exploring state with id " < < stateIndex < < " . " ) ;
unpackStateIntoEvaluator ( currentState , variableInformation , evaluator ) ;
@ -615,10 +380,6 @@ namespace storm {
std : : vector < Choice < ValueType > > allLabeledChoices ;
bool deadlockOnPurpose = false ;
if ( terminalExpression & & evaluator . asBool ( terminalExpression . get ( ) ) ) {
//std::cout << unpackStateIntoValuation(currentState, variableInformation).toString(true) << std::endl;
//allUnlabeledChoices = getUnlabeledTransitions(program, discreteTimeModel, internalStateInformation, variableInformation, currentState, commandLabels, evaluator, stateQueue, comparator);
//allLabeledChoices = getLabeledTransitions(program, discreteTimeModel, internalStateInformation, variableInformation, currentState, commandLabels, evaluator, stateQueue, comparator);
// Nothing to do in this case.
deadlockOnPurpose = true ;
} else {
@ -886,8 +647,8 @@ namespace storm {
return choiceLabels ;
}
template < typename ValueType , typename RewardModelType , typename Index Type>
typename ExplicitPrismModelBuilder < ValueType , RewardModelType , Index Type> : : ModelComponents ExplicitPrismModelBuilder < ValueType , RewardModelType , Index Type> : : buildModelComponents ( storm : : prism : : Program const & program , std : : vector < std : : reference_wrapper < storm : : prism : : RewardModel const > > const & selectedRewardModels , Options const & options ) {
template < typename ValueType , typename RewardModelType , typename State Type>
typename ExplicitPrismModelBuilder < ValueType , RewardModelType , State Type> : : ModelComponents ExplicitPrismModelBuilder < ValueType , RewardModelType , State Type> : : buildModelComponents ( storm : : prism : : Program const & program , std : : vector < std : : reference_wrapper < storm : : prism : : RewardModel const > > const & selectedRewardModels , Options const & options ) {
ModelComponents modelComponents ;
uint_fast64_t bitOffset = 0 ;
@ -991,8 +752,8 @@ namespace storm {
return modelComponents ;
}
template < typename ValueType , typename RewardModelType , typename Index Type>
storm : : models : : sparse : : StateLabeling ExplicitPrismModelBuilder < ValueType , RewardModelType , Index Type> : : buildStateLabeling ( storm : : prism : : Program const & program , VariableInformation const & variableInformation , InternalStateInformation const & internalStateInformation ) {
template < typename ValueType , typename RewardModelType , typename State Type>
storm : : models : : sparse : : StateLabeling ExplicitPrismModelBuilder < ValueType , RewardModelType , State Type> : : buildStateLabeling ( storm : : prism : : Program const & program , VariableInformation const & variableInformation , InternalStateInformation const & internalStateInformation ) {
storm : : expressions : : ExpressionEvaluator < ValueType > evaluator ( program . getManager ( ) ) ;
std : : vector < storm : : prism : : Label > const & labels = program . getLabels ( ) ;
xxxxxxxxxx