Browse Source

Minor improvements in DRN parser

tempestpy_adaptions
Matthias Volk 7 years ago
parent
commit
76d5ddad30
  1. 35
      src/storm/parser/DirectEncodingParser.cpp
  2. 31
      src/storm/utility/DirectEncodingExporter.cpp
  3. 4
      src/storm/utility/DirectEncodingExporter.h

35
src/storm/parser/DirectEncodingParser.cpp

@ -40,26 +40,27 @@ namespace storm {
bool sawParameters = false; bool sawParameters = false;
size_t nrStates = 0; size_t nrStates = 0;
storm::models::ModelType type; storm::models::ModelType type;
std::vector<std::string> rewardModelNames; std::vector<std::string> rewardModelNames;
std::shared_ptr<storm::storage::sparse::ModelComponents<ValueType, RewardModelType>> modelComponents;
std::shared_ptr<storm::storage::sparse::ModelComponents<ValueType, RewardModelType>> modelComponents;
// Parse header // Parse header
while(std::getline(file, line)) {
if(line.empty() || boost::starts_with(line, "//")) {
while (std::getline(file, line)) {
if (line.empty() || boost::starts_with(line, "//")) {
continue; continue;
} }
if (boost::starts_with(line, "@type: ")) { if (boost::starts_with(line, "@type: ")) {
// Parse type
STORM_LOG_THROW(!sawType, storm::exceptions::WrongFormatException, "Type declared twice"); STORM_LOG_THROW(!sawType, storm::exceptions::WrongFormatException, "Type declared twice");
type = storm::models::getModelType(line.substr(7)); type = storm::models::getModelType(line.substr(7));
STORM_LOG_TRACE("Model type: " << type); STORM_LOG_TRACE("Model type: " << type);
STORM_LOG_THROW(type != storm::models::ModelType::MarkovAutomaton, storm::exceptions::NotSupportedException, "Markov Automata in DRN format are not supported (unclear indication of Markovian Choices in DRN format)"); STORM_LOG_THROW(type != storm::models::ModelType::MarkovAutomaton, storm::exceptions::NotSupportedException, "Markov Automata in DRN format are not supported (unclear indication of Markovian Choices in DRN format)");
STORM_LOG_THROW(type != storm::models::ModelType::S2pg, storm::exceptions::NotSupportedException, "Stochastic Two Player Games in DRN format are not supported."); STORM_LOG_THROW(type != storm::models::ModelType::S2pg, storm::exceptions::NotSupportedException, "Stochastic Two Player Games in DRN format are not supported.");
sawType = true; sawType = true;
}
if(line == "@parameters") {
} else if (line == "@parameters") {
// Parse parameters
STORM_LOG_THROW(!sawParameters, storm::exceptions::WrongFormatException, "Parameters declared twice");
std::getline(file, line); std::getline(file, line);
if (line != "") { if (line != "") {
std::vector<std::string> parameters; std::vector<std::string> parameters;
@ -70,26 +71,28 @@ namespace storm {
} }
} }
sawParameters = true; sawParameters = true;
}
if(line == "@reward_models") {
} else if (line == "@reward_models") {
// Parse reward models
STORM_LOG_THROW(rewardModelNames.size() == 0, storm::exceptions::WrongFormatException, "Reward model names declared twice"); STORM_LOG_THROW(rewardModelNames.size() == 0, storm::exceptions::WrongFormatException, "Reward model names declared twice");
std::getline(file, line); std::getline(file, line);
boost::split(rewardModelNames, line, boost::is_any_of("\t ")); boost::split(rewardModelNames, line, boost::is_any_of("\t "));
}
if(line == "@nr_states") {
} else if (line == "@nr_states") {
// Parse no. of states
STORM_LOG_THROW(nrStates == 0, storm::exceptions::WrongFormatException, "Number states declared twice"); STORM_LOG_THROW(nrStates == 0, storm::exceptions::WrongFormatException, "Number states declared twice");
std::getline(file, line); std::getline(file, line);
nrStates = NumberParser<size_t>::parse(line); nrStates = NumberParser<size_t>::parse(line);
}
if(line == "@model") {
} else if (line == "@model") {
// Parse rest of the model
STORM_LOG_THROW(sawType, storm::exceptions::WrongFormatException, "Type has to be declared before model."); STORM_LOG_THROW(sawType, storm::exceptions::WrongFormatException, "Type has to be declared before model.");
STORM_LOG_THROW(sawParameters, storm::exceptions::WrongFormatException, "Parameters have to be declared before model."); STORM_LOG_THROW(sawParameters, storm::exceptions::WrongFormatException, "Parameters have to be declared before model.");
STORM_LOG_THROW(nrStates != 0, storm::exceptions::WrongFormatException, "Nr States has to be declared before model.");
STORM_LOG_THROW(nrStates != 0, storm::exceptions::WrongFormatException, "No. of states has to be declared before model.");
// Construct model components // Construct model components
modelComponents = parseStates(file, type, nrStates, valueParser, rewardModelNames); modelComponents = parseStates(file, type, nrStates, valueParser, rewardModelNames);
break; break;
} else {
STORM_LOG_THROW(false, storm::exceptions::WrongFormatException, "Could not parse line '" << line << "'.");
} }
} }
// Done parsing // Done parsing
@ -182,8 +185,8 @@ namespace storm {
} else { } else {
++row; ++row;
} }
line = line.substr(8);
STORM_LOG_TRACE("New action: " << row); STORM_LOG_TRACE("New action: " << row);
line = line.substr(8); //Remove "\taction "
// Check for rewards // Check for rewards
if (boost::starts_with(line, "[")) { if (boost::starts_with(line, "[")) {
// Rewards found // Rewards found

31
src/storm/utility/DirectEncodingExporter.cpp

@ -55,7 +55,8 @@ namespace storm {
os << "@model" << std::endl; os << "@model" << std::endl;
storm::storage::SparseMatrix<ValueType> const& matrix = sparseModel->getTransitionMatrix(); storm::storage::SparseMatrix<ValueType> const& matrix = sparseModel->getTransitionMatrix();
// Iterate over states and export state information and outgoing transitions
for (typename storm::storage::SparseMatrix<ValueType>::index_type group = 0; group < matrix.getRowGroupCount(); ++group) { for (typename storm::storage::SparseMatrix<ValueType>::index_type group = 0; group < matrix.getRowGroupCount(); ++group) {
os << "state " << group; os << "state " << group;
@ -92,22 +93,23 @@ namespace storm {
// Iterate over all actions // Iterate over all actions
for (typename storm::storage::SparseMatrix<ValueType>::index_type row = start; row < end; ++row) { for (typename storm::storage::SparseMatrix<ValueType>::index_type row = start; row < end; ++row) {
// Print the actual row.
// Write choice
if (sparseModel->hasChoiceLabeling()) { if (sparseModel->hasChoiceLabeling()) {
os << "\taction "; os << "\taction ";
bool lfirst = true; bool lfirst = true;
for (auto const& label : sparseModel->getChoiceLabeling().getLabelsOfChoice(row)) { for (auto const& label : sparseModel->getChoiceLabeling().getLabelsOfChoice(row)) {
if (!lfirst) { if (!lfirst) {
os << "_"; os << "_";
lfirst = false;
} }
os << label; os << label;
lfirst = false;
} }
} else { } else {
os << "\taction " << row - start; os << "\taction " << row - start;
} }
// Write action rewards
bool first = true; bool first = true;
// Write transition rewards
for (auto const& rewardModelEntry : sparseModel->getRewardModels()) { for (auto const& rewardModelEntry : sparseModel->getRewardModels()) {
if (first) { if (first) {
os << " ["; os << " [";
@ -116,7 +118,7 @@ namespace storm {
os << ", "; os << ", ";
} }
if(rewardModelEntry.second.hasStateActionRewards()) {
if (rewardModelEntry.second.hasStateActionRewards()) {
os << storm::utility::to_string(rewardModelEntry.second.getStateActionRewardVector().at(row)); os << storm::utility::to_string(rewardModelEntry.second.getStateActionRewardVector().at(row));
} else { } else {
os << "0"; os << "0";
@ -126,19 +128,18 @@ namespace storm {
if (!first) { if (!first) {
os << "]"; os << "]";
} }
os << std::endl; os << std::endl;
// Write probabilities
for(auto it = matrix.begin(row); it != matrix.end(row); ++it) {
// Write transitions
for (auto it = matrix.begin(row); it != matrix.end(row); ++it) {
ValueType prob = it->getValue(); ValueType prob = it->getValue();
os << "\t\t" << it->getColumn() << " : "; os << "\t\t" << it->getColumn() << " : ";
os << storm::utility::to_string(prob) << std::endl; os << storm::utility::to_string(prob) << std::endl;
} }
} }
} // end matrix iteration
} // end state iteration
} }
template<typename ValueType> template<typename ValueType>
@ -146,9 +147,6 @@ namespace storm {
return {}; return {};
} }
template void explicitExportSparseModel<double>(std::ostream& os, std::shared_ptr<storm::models::sparse::Model<double>> sparseModel, std::vector<std::string> const& parameters);
#ifdef STORM_HAVE_CARL
template<> template<>
std::vector<std::string> getParameters(std::shared_ptr<storm::models::sparse::Model<storm::RationalFunction>> sparseModel) { std::vector<std::string> getParameters(std::shared_ptr<storm::models::sparse::Model<storm::RationalFunction>> sparseModel) {
std::vector<std::string> parameters; std::vector<std::string> parameters;
@ -167,8 +165,9 @@ namespace storm {
return parameters; return parameters;
} }
// Template instantiations
template void explicitExportSparseModel<double>(std::ostream& os, std::shared_ptr<storm::models::sparse::Model<double>> sparseModel, std::vector<std::string> const& parameters);
template void explicitExportSparseModel<storm::RationalNumber>(std::ostream& os, std::shared_ptr<storm::models::sparse::Model<storm::RationalNumber>> sparseModel, std::vector<std::string> const& parameters); template void explicitExportSparseModel<storm::RationalNumber>(std::ostream& os, std::shared_ptr<storm::models::sparse::Model<storm::RationalNumber>> sparseModel, std::vector<std::string> const& parameters);
template void explicitExportSparseModel<storm::RationalFunction>(std::ostream& os, std::shared_ptr<storm::models::sparse::Model<storm::RationalFunction>> sparseModel, std::vector<std::string> const& parameters); template void explicitExportSparseModel<storm::RationalFunction>(std::ostream& os, std::shared_ptr<storm::models::sparse::Model<storm::RationalFunction>> sparseModel, std::vector<std::string> const& parameters);
#endif
} }
} }

4
src/storm/utility/DirectEncodingExporter.h

@ -18,13 +18,13 @@ namespace storm {
void explicitExportSparseModel(std::ostream& os, std::shared_ptr<storm::models::sparse::Model<ValueType>> sparseModel, std::vector<std::string> const& parameters); void explicitExportSparseModel(std::ostream& os, std::shared_ptr<storm::models::sparse::Model<ValueType>> sparseModel, std::vector<std::string> const& parameters);
/*! /*!
* Accumalate parameters in the model.
* Accumulate parameters in the model.
* *
* @param sparseModel Model. * @param sparseModel Model.
* @return List of parameters in the model. * @return List of parameters in the model.
*/ */
template<typename ValueType> template<typename ValueType>
std::vector<std::string> getParameters(std::shared_ptr<storm::models::sparse::Model<ValueType>> sparseModel); std::vector<std::string> getParameters(std::shared_ptr<storm::models::sparse::Model<ValueType>> sparseModel);
} }
} }
Loading…
Cancel
Save