diff --git a/src/settings/modules/GeneralSettings.cpp b/src/settings/modules/GeneralSettings.cpp index 893dd4717..3c422755f 100644 --- a/src/settings/modules/GeneralSettings.cpp +++ b/src/settings/modules/GeneralSettings.cpp @@ -23,6 +23,7 @@ namespace storm { const std::string GeneralSettings::precisionOptionName = "precision"; const std::string GeneralSettings::precisionOptionShortName = "eps"; const std::string GeneralSettings::exportDotOptionName = "exportdot"; + const std::string GeneralSettings::exportMatOptionName = "exportmat"; const std::string GeneralSettings::configOptionName = "config"; const std::string GeneralSettings::configOptionShortName = "c"; const std::string GeneralSettings::explicitOptionName = "explicit"; @@ -70,6 +71,8 @@ namespace storm { .addArgument(storm::settings::ArgumentBuilder::createDoubleArgument("value", "The precision to use.").setDefaultValueDouble(1e-06).addValidationFunctionDouble(storm::settings::ArgumentValidators::doubleRangeValidatorExcluding(0.0, 1.0)).build()).build()); this->addOption(storm::settings::OptionBuilder(moduleName, exportDotOptionName, "", "If given, the loaded model will be written to the specified file in the dot format.") .addArgument(storm::settings::ArgumentBuilder::createStringArgument("filename", "The name of the file to which the model is to be written.").build()).build()); + this->addOption(storm::settings::OptionBuilder(moduleName, exportMatOptionName, "", "If given, the loaded model will be written to the specified file in the mat format.") + .addArgument(storm::settings::ArgumentBuilder::createStringArgument("filename", "the name of the file to which the model is to be writen.").build()).build()); this->addOption(storm::settings::OptionBuilder(moduleName, configOptionName, false, "If given, this file will be read and parsed for additional configuration settings.").setShortName(configOptionShortName) .addArgument(storm::settings::ArgumentBuilder::createStringArgument("filename", "The name of the file from which to read the configuration.").addValidationFunctionString(storm::settings::ArgumentValidators::existingReadableFileValidator()).build()).build()); this->addOption(storm::settings::OptionBuilder(moduleName, explicitOptionName, false, "Parses the model given in an explicit (sparse) representation.").setShortName(explicitOptionShortName) diff --git a/src/settings/modules/GeneralSettings.h b/src/settings/modules/GeneralSettings.h index 1c7abdc98..3d36877fa 100644 --- a/src/settings/modules/GeneralSettings.h +++ b/src/settings/modules/GeneralSettings.h @@ -371,6 +371,7 @@ namespace storm { static const std::string precisionOptionName; static const std::string precisionOptionShortName; static const std::string exportDotOptionName; + static const std::string exportMatOptionName; static const std::string configOptionName; static const std::string configOptionShortName; static const std::string explicitOptionName; diff --git a/src/storage/SparseMatrix.cpp b/src/storage/SparseMatrix.cpp index 6850eee09..93010b53e 100644 --- a/src/storage/SparseMatrix.cpp +++ b/src/storage/SparseMatrix.cpp @@ -1180,6 +1180,31 @@ namespace storm { return out; } + template + void SparseMatrix::printAsMatlabMatrix(std::ostream& out) const { + // Iterate over all row groups. + for (typename SparseMatrix::index_type group = 0; group < this->getRowGroupCount(); ++group) { + assert(this->getRowGroupSize(group) == 1); + for (typename SparseMatrix::index_type i = this->getRowGroupIndices()[group]; i < this->getRowGroupIndices()[group + 1]; ++i) { + typename SparseMatrix::index_type nextIndex = this->rowIndications[i]; + + // Print the actual row. + out << i << "\t("; + typename SparseMatrix::index_type currentRealIndex = 0; + while (currentRealIndex < this->columnCount) { + if (nextIndex < this->rowIndications[i + 1] && currentRealIndex == this->columnsAndValues[nextIndex].getColumn()) { + out << this->columnsAndValues[nextIndex].getValue() << " "; + ++nextIndex; + } else { + out << "0 "; + } + ++currentRealIndex; + } + out << ";" << std::endl; + } + } + } + template std::size_t SparseMatrix::hash() const { std::size_t result = 0; diff --git a/src/storage/SparseMatrix.h b/src/storage/SparseMatrix.h index c31e0b344..7cc81fdb5 100644 --- a/src/storage/SparseMatrix.h +++ b/src/storage/SparseMatrix.h @@ -761,7 +761,15 @@ namespace storm { template friend std::ostream& operator<<(std::ostream& out, SparseMatrix const& matrix); - + + /*! + * Prints the matrix in a dense format, as also used by e.g. Matlab. + * Notice that the format does not support multiple rows in a rowgroup. + * + * @out The stream to output to. + */ + void printAsMatlabMatrix(std::ostream& out) const; + /*! * Returns the size of the matrix in memory measured in bytes. * diff --git a/src/utility/storm.h b/src/utility/storm.h index be76720fe..b53647522 100644 --- a/src/utility/storm.h +++ b/src/utility/storm.h @@ -315,7 +315,15 @@ namespace storm { } return result; } - + + template + void exportMatrixToFile(std::shared_ptr> model, std::string const& filepath) { + STORM_LOG_THROW(model->getType() != storm::models::ModelType::Ctmc, storm::exceptions::NotImplementedException, "This functionality is not yet implemented." ); + std::ofstream ofs; + ofs.open (filepath, std::ofstream::out); + model->getTransitionMatrix().printAsMatlabMatrix(ofs); + ofs.close(); + } }