@ -5,6 +5,8 @@
# include <sstream>
# include <set>
# include <map>
# include <mutex>
# include "storm/adapters/CarlAdapter.h"
# include "sylvan_storm_rational_function.h"
@ -13,224 +15,267 @@
# include <sylvan_common.h>
# include <sylvan_mtbdd.h>
# undef DEBUG_STORM_FUNCTION_WRAPPER
# ifdef DEBUG_STORM_FUNCTION_WRAPPER
# define LOG_I(funcName) std::cout << "Entering function " << funcName << std::endl;
# define LOG_O(funcName) std::cout << "Leaving function " << funcName << std::endl;
# else
# define LOG_I(funcName)
# define LOG_O(funcName)
# endif
std : : mutex carlMutex ;
void storm_rational_function_init ( storm_rational_function_ptr * a ) {
LOG_I ( " init " )
# ifdef DEBUG_STORM_FUNCTION_WRAPPER
std : : cout < < " storm_rational_function_init - ptr of old = " < < * a < < " , value = " < < * ( ( storm : : RationalFunction * ) ( * a ) ) < < std : : endl ;
# endif
storm_rational_function_ptr srf_ptr = new storm : : RationalFunction ( * ( ( storm : : RationalFunction * ) ( * a ) ) ) ;
std : : lock_guard < std : : mutex > lock ( carlMutex ) ;
{
storm_rational_function_ptr srf_ptr = new storm : : RationalFunction ( * ( ( storm : : RationalFunction * ) ( * a ) ) ) ;
if ( srf_ptr = = nullptr ) {
std : : cerr < < " Could not allocate memory in storm_rational_function_init()! " < < std : : endl ;
return ;
}
if ( srf_ptr = = nullptr ) {
std : : cerr < < " Could not allocate memory in storm_rational_function_init()! " < < std : : endl ;
return ;
* a = srf_ptr ;
}
* a = srf_ptr ;
# ifdef DEBUG_STORM_FUNCTION_WRAPPER
std : : cout < < " storm_rational_function_init - ptr of new = " < < * a < < " , value = " < < * ( ( storm : : RationalFunction * ) ( * a ) ) < < std : : endl ;
# endif
LOG_O ( " init " )
}
void storm_rational_function_destroy ( storm_rational_function_ptr a ) {
LOG_I ( " destroy " )
delete ( storm : : RationalFunction * ) a ;
LOG_O ( " destroy " )
std : : lock_guard < std : : mutex > lock ( carlMutex ) ;
{
storm : : RationalFunction * srf = ( storm : : RationalFunction * ) a ;
delete srf ;
}
}
int storm_rational_function_equals ( storm_rational_function_ptr a , storm_rational_function_ptr b ) {
LOG_I ( " equals " )
storm : : RationalFunction & srf_a = * ( storm : : RationalFunction * ) a ;
storm : : RationalFunction & srf_b = * ( storm : : RationalFunction * ) b ;
LOG_O ( " equals " )
std : : lock_guard < std : : mutex > lock ( carlMutex ) ;
int result = 0 ;
if ( srf_a = = srf_b ) {
result = 1 ;
}
{
storm : : RationalFunction & srf_a = * ( storm : : RationalFunction * ) a ;
storm : : RationalFunction & srf_b = * ( storm : : RationalFunction * ) b ;
# ifdef DEBUG_STORM_FUNCTION_WRAPPER
std : : cout < < " storm_rational_function_equals called with ptr = " < < a < < " value a = " < < srf_a < < " and ptr = " < < b < < " value b = " < < srf_b < < " result = " < < result < < " . " < < std : : endl ;
# endif
if ( srf_a = = srf_b ) {
result = 1 ;
}
}
return result ;
}
storm_rational_function_ptr storm_rational_function_plus ( storm_rational_function_ptr a , storm_rational_function_ptr b ) {
LOG_I ( " plus " )
storm : : RationalFunction & srf_a = * ( storm : : RationalFunction * ) a ;
storm : : RationalFunction & srf_b = * ( storm : : RationalFunction * ) b ;
std : : lock_guard < std : : mutex > lock ( carlMutex ) ;
storm_rational_function_ptr result = ( storm_rational_function_ptr ) nullptr ;
storm : : RationalFunction * result_srf = new storm : : RationalFunction ( srf_a ) ;
if ( result_srf = = nullptr ) {
std : : cerr < < " Could not allocate memory in storm_rational_function_plus()! " < < std : : endl ;
return ( storm_rational_function_ptr ) nullptr ;
}
{
storm : : RationalFunction & srf_a = * ( storm : : RationalFunction * ) a ;
storm : : RationalFunction & srf_b = * ( storm : : RationalFunction * ) b ;
* result_srf + = srf_b ;
storm_rational_function_ptr result = ( storm_rational_function_ptr ) result_srf ;
storm : : RationalFunction * result_srf = new storm : : RationalFunction ( srf_a ) ;
if ( result_srf = = nullptr ) {
std : : cerr < < " Could not allocate memory in storm_rational_function_plus()! " < < std : : endl ;
return result ;
}
* result_srf + = srf_b ;
result = ( storm_rational_function_ptr ) result_srf ;
}
LOG_O ( " plus " )
return result ;
}
storm_rational_function_ptr storm_rational_function_minus ( storm_rational_function_ptr a , storm_rational_function_ptr b ) {
LOG_I ( " minus " )
storm : : RationalFunction & srf_a = * ( storm : : RationalFunction * ) a ;
storm : : RationalFunction & srf_b = * ( storm : : RationalFunction * ) b ;
std : : lock_guard < std : : mutex > lock ( carlMutex ) ;
storm_rational_function_ptr result = ( storm_rational_function_ptr ) nullptr ;
storm : : RationalFunction * result_srf = new storm : : RationalFunction ( srf_a ) ;
if ( result_srf = = nullptr ) {
std : : cerr < < " Could not allocate memory in storm_rational_function_minus()! " < < std : : endl ;
return ( storm_rational_function_ptr ) nullptr ;
}
{
storm : : RationalFunction & srf_a = * ( storm : : RationalFunction * ) a ;
storm : : RationalFunction & srf_b = * ( storm : : RationalFunction * ) b ;
* result_srf - = srf_b ;
storm_rational_function_ptr result = ( storm_rational_function_ptr ) result_srf ;
LOG_O ( " minus " )
storm : : RationalFunction * result_srf = new storm : : RationalFunction ( srf_a ) ;
if ( result_srf = = nullptr ) {
std : : cerr < < " Could not allocate memory in storm_rational_function_minus()! " < < std : : endl ;
return result ;
}
* result_srf - = srf_b ;
result = ( storm_rational_function_ptr ) result_srf ;
}
return result ;
}
storm_rational_function_ptr storm_rational_function_times ( storm_rational_function_ptr a , storm_rational_function_ptr b ) {
LOG_I ( " times " )
storm : : RationalFunction & srf_a = * ( storm : : RationalFunction * ) a ;
storm : : RationalFunction & srf_b = * ( storm : : RationalFunction * ) b ;
std : : lock_guard < std : : mutex > lock ( carlMutex ) ;
storm_rational_function_ptr result = ( storm_rational_function_ptr ) nullptr ;
storm : : RationalFunction * result_srf = new storm : : RationalFunction ( srf_a ) ;
if ( result_srf = = nullptr ) {
std : : cerr < < " Could not allocate memory in storm_rational_function_times()! " < < std : : endl ;
return ( storm_rational_function_ptr ) nullptr ;
}
{
storm : : RationalFunction & srf_a = * ( storm : : RationalFunction * ) a ;
storm : : RationalFunction & srf_b = * ( storm : : RationalFunction * ) b ;
* result_srf * = srf_b ;
storm_rational_function_ptr result = ( storm_rational_function_ptr ) result_srf ;
LOG_O ( " times " )
storm : : RationalFunction * result_srf = new storm : : RationalFunction ( srf_a ) ;
if ( result_srf = = nullptr ) {
std : : cerr < < " Could not allocate memory in storm_rational_function_times()! " < < std : : endl ;
return result ;
}
* result_srf * = srf_b ;
result = ( storm_rational_function_ptr ) result_srf ;
}
return result ;
}
storm_rational_function_ptr storm_rational_function_divide ( storm_rational_function_ptr a , storm_rational_function_ptr b ) {
LOG_I ( " divide " )
storm : : RationalFunction & srf_a = * ( storm : : RationalFunction * ) a ;
storm : : RationalFunction & srf_b = * ( storm : : RationalFunction * ) b ;
std : : lock_guard < std : : mutex > lock ( carlMutex ) ;
storm_rational_function_ptr result = ( storm_rational_function_ptr ) nullptr ;
storm : : RationalFunction * result_srf = new storm : : RationalFunction ( srf_a ) ;
if ( result_srf = = nullptr ) {
std : : cerr < < " Could not allocate memory in storm_rational_function_divide()! " < < std : : endl ;
return ( storm_rational_function_ptr ) nullptr ;
}
{
storm : : RationalFunction & srf_a = * ( storm : : RationalFunction * ) a ;
storm : : RationalFunction & srf_b = * ( storm : : RationalFunction * ) b ;
* result_srf / = srf_b ;
storm_rational_function_ptr result = ( storm_rational_function_ptr ) result_srf ;
LOG_O ( " divide " )
storm : : RationalFunction * result_srf = new storm : : RationalFunction ( srf_a ) ;
if ( result_srf = = nullptr ) {
std : : cerr < < " Could not allocate memory in storm_rational_function_divide()! " < < std : : endl ;
return result ;
}
* result_srf / = srf_b ;
result = ( storm_rational_function_ptr ) result_srf ;
}
return result ;
}
uint64_t storm_rational_function_hash ( storm_rational_function_ptr const a , uint64_t const seed ) {
LOG_I ( " hash " )
storm : : RationalFunction & srf_a = * ( storm : : RationalFunction * ) a ;
size_t hash = carl : : hash_value ( srf_a ) ;
# ifdef DEBUG_STORM_FUNCTION_WRAPPER
std : : cout < < " storm_rational_function_hash of value " < < srf_a < < " is " < < hash < < std : : endl ;
# endif
uint64_t result = hash ^ seed ;
LOG_O ( " hash " )
std : : lock_guard < std : : mutex > lock ( carlMutex ) ;
uint64_t result = 0 ;
{
storm : : RationalFunction & srf_a = * ( storm : : RationalFunction * ) a ;
size_t hash = carl : : hash_value ( srf_a ) ;
result = hash ^ seed ;
}
return result ;
}
storm_rational_function_ptr storm_rational_function_negate ( storm_rational_function_ptr a ) {
LOG_I ( " negate " )
storm : : RationalFunction & srf_a = * ( storm : : RationalFunction * ) a ;
std : : lock_guard < std : : mutex > lock ( carlMutex ) ;
storm_rational_function_ptr result = ( storm_rational_function_ptr ) nullptr ;
storm : : RationalFunction * result_srf = new storm : : RationalFunction ( srf_a ) ;
if ( result_srf = = nullptr ) {
std : : cerr < < " Could not allocate memory in storm_rational_function_negate()! " < < std : : endl ;
return ( storm_rational_function_ptr ) nullptr ;
}
{
storm : : RationalFunction & srf_a = * ( storm : : RationalFunction * ) a ;
* result_srf = - srf_a ;
storm_rational_function_ptr result = ( storm_rational_function_ptr ) result_srf ;
LOG_O ( " negate " )
storm : : RationalFunction * result_srf = new storm : : RationalFunction ( srf_a ) ;
if ( result_srf = = nullptr ) {
std : : cerr < < " Could not allocate memory in storm_rational_function_negate()! " < < std : : endl ;
return result ;
}
* result_srf = - srf_a ;
result = ( storm_rational_function_ptr ) result_srf ;
}
return result ;
}
int storm_rational_function_is_zero ( storm_rational_function_ptr a ) {
LOG_I ( " isZero " )
storm : : RationalFunction & srf_a = * ( storm : : RationalFunction * ) a ;
std : : lock_guard < std : : mutex > lock ( carlMutex ) ;
if ( srf_a . isZero ( ) ) {
bool resultIsZero = false ;
{
storm : : RationalFunction & srf_a = * ( storm : : RationalFunction * ) a ;
resultIsZero = srf_a . isZero ( ) ;
}
if ( resultIsZero ) {
return 1 ;
} else {
return 0 ;
}
}
double storm_rational_function_get_constant ( storm_rational_function_ptr a ) {
std : : lock_guard < std : : mutex > lock ( carlMutex ) ;
double result = - 1.0 ;
{
storm : : RationalFunction & srf_a = * ( storm : : RationalFunction * ) a ;
if ( srf_a . isConstant ( ) ) {
result = carl : : toDouble ( storm : : RationalNumber ( srf_a . nominatorAsNumber ( ) / srf_a . denominatorAsNumber ( ) ) ) ;
} else {
std : : cout < < " Defaulting to -1.0 since this is not a constant: " < < srf_a < < std : : endl ;
}
}
return result ;
}
storm_rational_function_ptr storm_rational_function_get_zero ( ) {
static storm : : RationalFunction zeroFunction ( 0 ) ;
LOG_I ( " getZero " )
return ( storm_rational_function_ptr ) ( & zeroFunction ) ;
std : : lock_guard < std : : mutex > lock ( carlMutex ) ;
storm_rational_function_ptr result = ( storm_rational_function_ptr ) nullptr ;
{
storm : : RationalFunction * result_srf = new storm : : RationalFunction ( 0 ) ;
if ( result_srf = = nullptr ) {
std : : cerr < < " Could not allocate memory in storm_rational_function_get_zero()! " < < std : : endl ;
return result ;
}
result = ( storm_rational_function_ptr ) result_srf ;
}
return result ;
}
storm_rational_function_ptr storm_rational_function_get_one ( ) {
static storm : : RationalFunction oneFunction ( 1 ) ;
LOG_I ( " getOne " )
return ( storm_rational_function_ptr ) ( & oneFunction ) ;
std : : lock_guard < std : : mutex > lock ( carlMutex ) ;
storm_rational_function_ptr result = ( storm_rational_function_ptr ) nullptr ;
{
storm : : RationalFunction * result_srf = new storm : : RationalFunction ( 1 ) ;
if ( result_srf = = nullptr ) {
std : : cerr < < " Could not allocate memory in storm_rational_function_get_one()! " < < std : : endl ;
return result ;
}
result = ( storm_rational_function_ptr ) result_srf ;
}
return result ;
}
void print_storm_rational_function ( storm_rational_function_ptr a ) {
storm : : RationalFunction & srf_a = * ( storm : : RationalFunction * ) a ;
std : : cout < < srf_a < < std : : flush ;
std : : lock_guard < std : : mutex > lock ( carlMutex ) ;
{
storm : : RationalFunction & srf_a = * ( storm : : RationalFunction * ) a ;
std : : cout < < srf_a < < std : : flush ;
}
}
void print_storm_rational_function_to_file ( storm_rational_function_ptr a , FILE * out ) {
std : : stringstream ss ;
storm : : RationalFunction & srf_a = * ( storm : : RationalFunction * ) a ;
ss < < srf_a ;
std : : string s = ss . str ( ) ;
fprintf ( out , " %s " , s . c_str ( ) ) ;
std : : lock_guard < std : : mutex > lock ( carlMutex ) ;
{
std : : stringstream ss ;
storm : : RationalFunction & srf_a = * ( storm : : RationalFunction * ) a ;
ss < < srf_a ;
std : : string s = ss . str ( ) ;
fprintf ( out , " %s " , s . c_str ( ) ) ;
}
}
MTBDD testiTest ( storm : : RationalFunction const & currentFunction , std : : map < storm : : RationalFunctionVariable , std : : pair < uint32_t , std : : pair < storm : : RationalNumber , storm : : RationalNumber > > > const & replacements ) {
MTBDD testiTest ( storm : : RationalFunction const & currentFunction , std : : map < uint32_t , std : : pair < storm : : RationalFunctionVariable , std : : pair < storm : : RationalNumber , storm : : RationalNumber > > > const & replacements ) {
if ( currentFunction . isConstant ( ) ) {
return mtbdd_storm_rational_function ( ( storm_rational_function_ptr ) & currentFunction ) ;
}
std : : set < storm : : RationalFunctionVariable > variablesInFunction = currentFunction . gatherVariables ( ) ;
std : : map < storm : : RationalFunctionVariable , std : : pair < uint32_t , std : : pair < storm : : RationalNumber , storm : : RationalNumber > > > : : const_iterator it = replacements . cbegin ( ) ;
std : : map < storm : : RationalFunctionVariable , std : : pair < uint32_t , std : : pair < storm : : RationalNumber , storm : : RationalNumber > > > : : const_iterator end = replacements . cend ( ) ;
std : : map < uint32_t , std : : pair < storm : : RationalFunctionVariable , std : : pair < storm : : RationalNumber , storm : : RationalNumber > > > : : const_iterator it = replacements . cbegin ( ) ;
std : : map < uint32_t , std : : pair < storm : : RationalFunctionVariable , std : : pair < storm : : RationalNumber , storm : : RationalNumber > > > : : const_iterator end = replacements . cend ( ) ;
// Walking the (ordered) map enforces an ordering on the MTBDD
for ( ; it ! = end ; + + it ) {
if ( variablesInFunction . find ( it - > first ) ! = variablesInFunction . cend ( ) ) {
std : : map < storm : : RationalFunctionVariable , storm : : RationalNumber > highReplacement = { { it - > first , it - > second . second . first } } ;
std : : map < storm : : RationalFunctionVariable , storm : : RationalNumber > lowReplacement = { { it - > first , it - > second . second . second } } ;
MTBDD high = testiTest ( currentFunction . substitute ( highReplacement ) , replacements ) ;
MTBDD low = testiTest ( currentFunction . substitute ( lowReplacement ) , replacements ) ;
if ( variablesInFunction . find ( it - > second . first ) ! = variablesInFunction . cend ( ) ) {
std : : map < storm : : RationalFunctionVariable , storm : : RationalNumber > highReplacement = { { it - > second . first , it - > second . second . first } } ;
std : : map < storm : : RationalFunctionVariable , storm : : RationalNumber > lowReplacement = { { it - > second . first , it - > second . second . second } } ;
std : : lock_guard < std : : mutex > * lock = new std : : lock_guard < std : : mutex > ( carlMutex ) ;
storm : : RationalFunction const highSrf = currentFunction . substitute ( highReplacement ) ;
storm : : RationalFunction const lowSrf = currentFunction . substitute ( lowReplacement ) ;
delete lock ;
MTBDD high = testiTest ( highSrf , replacements ) ;
MTBDD low = testiTest ( lowSrf , replacements ) ;
LACE_ME
return mtbdd_ite ( mtbdd_ithvar ( it - > second . first ) , high , low ) ;
return mtbdd_ite ( mtbdd_ithvar ( it - > first ) , high , low ) ;
} else {
//std::cout << "No match for variable " << it->second.first << std::endl;
}
}
@ -238,12 +283,16 @@ MTBDD testiTest(storm::RationalFunction const& currentFunction, std::map<storm::
}
MTBDD storm_rational_function_leaf_parameter_replacement ( MTBDD dd , storm_rational_function_ptr a , void * context ) {
MTBDD storm_rational_function_leaf_parameter_replacement ( MTBDD dd , storm_rational_function_ptr a , void * context ) {
storm : : RationalFunction & srf_a = * ( storm : : RationalFunction * ) a ;
if ( srf_a . isConstant ( ) ) {
return dd ;
{
// Scope the lock.
std : : lock_guard < std : : mutex > lock ( carlMutex ) ;
if ( srf_a . isConstant ( ) ) {
return dd ;
}
}
std : : map < storm : : RationalFunctionVariable , std : : pair < uint32_t , std : : pair < storm : : RationalNumber , storm : : RationalNumber > > > * replacements = ( std : : map < storm : : RationalFunctionVariable , std : : pair < uint32_t , std : : pair < storm : : RationalNumber , storm : : RationalNumber > > > * ) context ;
std : : map < uint32_t , std : : pair < storm : : RationalFunctionVariable , std : : pair < storm : : RationalNumber , storm : : RationalNumber > > > * replacements = ( std : : map < uint32_t , std : : pair < storm : : RationalFunctionVariable , std : : pair < storm : : RationalNumber , storm : : RationalNumber > > > * ) context ;
return testiTest ( srf_a , * replacements ) ;
}