-
Notifications
You must be signed in to change notification settings - Fork 80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for discounted properties in MDPs and DTMCs #621
Open
AlexBork
wants to merge
9
commits into
moves-rwth:master
Choose a base branch
from
AlexBork:discounting-merge
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
806bd5c
Add discounting for MDPs and DTMCs
AlexBork 3b6f523
Add test case for discounting
AlexBork 0dcc557
Merge branch 'master' into discounting-merge
AlexBork 5069177
Add handling for discounting in bisimulation
AlexBork 6abe306
Merge branch 'master' into discounting-merge
AlexBork aa8ef93
Refactoring to use undiscounted formulae as parent classes
AlexBork 8beb6c6
Formatting
AlexBork a88a27f
Fix bound and add documentation
AlexBork 7004eb5
Cleanup
AlexBork File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
mdp | ||
|
||
module discounting | ||
|
||
s : [0..3] init 0; | ||
[a] s=0 -> (s'=1); | ||
[b] s=0 -> (s'=2); | ||
[] s=1 -> (s'=3); | ||
[] s=2 -> 0.8 : (s'=2) + 0.2 : (s'=3); | ||
[] s=3 -> true; | ||
|
||
endmodule | ||
|
||
rewards | ||
[] s=1 : 4; | ||
[] s=2 : 1; | ||
endrewards |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
#include "storm/logic/DiscountedCumulativeRewardFormula.h" | ||
#include <boost/any.hpp> | ||
#include <ostream> | ||
#include <utility> | ||
|
||
#include "storm/adapters/RationalNumberAdapter.h" | ||
#include "storm/logic/FormulaVisitor.h" | ||
|
||
#include "storm/exceptions/InvalidOperationException.h" | ||
#include "storm/exceptions/InvalidPropertyException.h" | ||
#include "storm/utility/constants.h" | ||
#include "storm/utility/macros.h" | ||
|
||
namespace storm { | ||
namespace logic { | ||
DiscountedCumulativeRewardFormula::DiscountedCumulativeRewardFormula(storm::expressions::Expression const& discountFactor, TimeBound const& bound, | ||
TimeBoundReference const& timeBoundReference, | ||
boost::optional<RewardAccumulation> rewardAccumulation) | ||
: CumulativeRewardFormula(bound, timeBoundReference, std::move(rewardAccumulation)), discountFactor(discountFactor) { | ||
// Intentionally left empty. | ||
} | ||
|
||
DiscountedCumulativeRewardFormula::DiscountedCumulativeRewardFormula(storm::expressions::Expression const& discountFactor, std::vector<TimeBound> const& bounds, | ||
std::vector<TimeBoundReference> const& timeBoundReferences, | ||
boost::optional<RewardAccumulation> rewardAccumulation) | ||
: CumulativeRewardFormula(bounds, timeBoundReferences, std::move(rewardAccumulation)), discountFactor(discountFactor) { | ||
// Intentionally left empty. | ||
} | ||
|
||
bool DiscountedCumulativeRewardFormula::isDiscountedCumulativeRewardFormula() const { | ||
return true; | ||
} | ||
|
||
void DiscountedCumulativeRewardFormula::checkNoVariablesInDiscountFactor(storm::expressions::Expression const& factor) { | ||
STORM_LOG_THROW(!factor.containsVariables(), storm::exceptions::InvalidOperationException, | ||
"Cannot evaluate discount factor '" << factor << "' as it contains undefined constants."); | ||
} | ||
|
||
storm::expressions::Expression const& DiscountedCumulativeRewardFormula::getDiscountFactor() const { | ||
return discountFactor; | ||
} | ||
|
||
template<> | ||
double DiscountedCumulativeRewardFormula::getDiscountFactor() const { | ||
checkNoVariablesInDiscountFactor(discountFactor); | ||
double value = discountFactor.evaluateAsDouble(); | ||
return value; | ||
} | ||
|
||
template<> | ||
storm::RationalNumber DiscountedCumulativeRewardFormula::getDiscountFactor() const { | ||
checkNoVariablesInDiscountFactor(discountFactor); | ||
storm::RationalNumber value = discountFactor.evaluateAsRational(); | ||
return value; | ||
} | ||
|
||
std::ostream& DiscountedCumulativeRewardFormula::writeToStream(std::ostream& out, bool /*allowParentheses*/) const { | ||
// No parentheses necessary | ||
out << "Cdiscount="; | ||
out << discountFactor; | ||
if (hasRewardAccumulation()) { | ||
out << "[" << getRewardAccumulation() << "]"; | ||
} | ||
if (this->isMultiDimensional()) { | ||
out << "^{"; | ||
} | ||
for (unsigned i = 0; i < this->getDimension(); ++i) { | ||
if (i > 0) { | ||
out << ", "; | ||
} | ||
if (this->getTimeBoundReference(i).isRewardBound()) { | ||
out << "rew"; | ||
if (this->getTimeBoundReference(i).hasRewardAccumulation()) { | ||
out << "[" << this->getTimeBoundReference(i).getRewardAccumulation() << "]"; | ||
} | ||
out << "{\"" << this->getTimeBoundReference(i).getRewardName() << "\"}"; | ||
} else if (this->getTimeBoundReference(i).isStepBound()) { | ||
out << "steps"; | ||
//} else if (this->getTimeBoundReference(i).isStepBound()) | ||
// Note: the 'time' keyword is optional. | ||
// out << "time"; | ||
} | ||
if (this->isBoundStrict(i)) { | ||
out << "<"; | ||
} else { | ||
out << "<="; | ||
} | ||
out << this->getBound(i); | ||
} | ||
if (this->isMultiDimensional()) { | ||
out << "}"; | ||
} | ||
return out; | ||
} | ||
|
||
boost::any DiscountedCumulativeRewardFormula::accept(FormulaVisitor const& visitor, boost::any const& data) const { | ||
return visitor.visit(*this, data); | ||
} | ||
|
||
} // namespace logic | ||
} // namespace storm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#pragma once | ||
|
||
#include "storm/logic/CumulativeRewardFormula.h" | ||
|
||
#include "storm/logic/TimeBound.h" | ||
#include "storm/logic/TimeBoundType.h" | ||
|
||
namespace storm { | ||
namespace logic { | ||
class DiscountedCumulativeRewardFormula : public CumulativeRewardFormula { | ||
public: | ||
DiscountedCumulativeRewardFormula(storm::expressions::Expression const& discountFactor, TimeBound const& bound, | ||
TimeBoundReference const& timeBoundReference = TimeBoundReference(TimeBoundType::Time), | ||
boost::optional<RewardAccumulation> rewardAccumulation = boost::none); | ||
DiscountedCumulativeRewardFormula(storm::expressions::Expression const& discountFactor, std::vector<TimeBound> const& bounds, | ||
std::vector<TimeBoundReference> const& timeBoundReferences, | ||
boost::optional<RewardAccumulation> rewardAccumulation = boost::none); | ||
|
||
virtual ~DiscountedCumulativeRewardFormula() = default; | ||
|
||
bool isDiscountedCumulativeRewardFormula() const override; | ||
|
||
std::ostream& writeToStream(std::ostream& out, bool allowParentheses = false) const override; | ||
|
||
storm::expressions::Expression const& getDiscountFactor() const; | ||
|
||
template<typename ValueType> | ||
ValueType getDiscountFactor() const; | ||
|
||
virtual boost::any accept(FormulaVisitor const& visitor, boost::any const& data) const override; | ||
|
||
private: | ||
static void checkNoVariablesInDiscountFactor(storm::expressions::Expression const& factor); | ||
|
||
storm::expressions::Expression const discountFactor; | ||
}; | ||
} // namespace logic | ||
} // namespace storm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#include "storm/logic/DiscountedTotalRewardFormula.h" | ||
#include <boost/any.hpp> | ||
|
||
#include <ostream> | ||
|
||
#include "storm/adapters/RationalNumberAdapter.h" | ||
|
||
#include "storm/exceptions/InvalidOperationException.h" | ||
#include "storm/logic/FormulaVisitor.h" | ||
|
||
#include "storm/utility/macros.h" | ||
|
||
namespace storm { | ||
namespace logic { | ||
DiscountedTotalRewardFormula::DiscountedTotalRewardFormula(storm::expressions::Expression const discountFactor, | ||
boost::optional<RewardAccumulation> rewardAccumulation) | ||
: TotalRewardFormula(rewardAccumulation), discountFactor(discountFactor) { | ||
// Intentionally left empty. | ||
} | ||
|
||
bool DiscountedTotalRewardFormula::isDiscountedTotalRewardFormula() const { | ||
return true; | ||
} | ||
|
||
std::ostream& DiscountedTotalRewardFormula::writeToStream(std::ostream& out, bool /* allowParentheses */) const { | ||
// No parentheses necessary | ||
out << "Cdiscount="; | ||
out << discountFactor.toString(); | ||
if (hasRewardAccumulation()) { | ||
out << "[" << getRewardAccumulation() << "]"; | ||
} | ||
return out; | ||
} | ||
|
||
storm::expressions::Expression const& DiscountedTotalRewardFormula::getDiscountFactor() const { | ||
return discountFactor; | ||
} | ||
|
||
template<> | ||
double DiscountedTotalRewardFormula::getDiscountFactor() const { | ||
checkNoVariablesInDiscountFactor(discountFactor); | ||
double value = discountFactor.evaluateAsDouble(); | ||
return value; | ||
} | ||
|
||
template<> | ||
storm::RationalNumber DiscountedTotalRewardFormula::getDiscountFactor() const { | ||
checkNoVariablesInDiscountFactor(discountFactor); | ||
storm::RationalNumber value = discountFactor.evaluateAsRational(); | ||
return value; | ||
} | ||
|
||
void DiscountedTotalRewardFormula::checkNoVariablesInDiscountFactor(storm::expressions::Expression const& factor) { | ||
STORM_LOG_THROW(!factor.containsVariables(), storm::exceptions::InvalidOperationException, | ||
"Cannot evaluate discount factor '" << factor << "' as it contains undefined constants."); | ||
} | ||
|
||
boost::any DiscountedTotalRewardFormula::accept(FormulaVisitor const& visitor, boost::any const& data) const { | ||
return visitor.visit(*this, data); | ||
} | ||
|
||
} // namespace logic | ||
} // namespace storm |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This means that
.isCumulativeRewardFormula()
is also true forDiscountedCumulativeRewardFormula
s.Does this have any unintended effects? (e.g. places like multi-objective model checking, where the discounted formula would be treaded as if it were undiscounted)? Maybe it's safer to also override
.isCumulativeRewardFormula()
tofalse
here, so things will rather fail instead of silently dropping the discount factor.Also, this probably needs to override
gatherUsedVariables
to make sure that variables in the discount factor are catched.Same for TotalRewardFormulas