Browse Source
Added some more methods to valuations. Changed visitor invocation slightly. Moves ExpressionReturnType in separate file. Finished linearity checking visitor. Started on visitor that extracts coefficients of linear expressions.
Added some more methods to valuations. Changed visitor invocation slightly. Moves ExpressionReturnType in separate file. Finished linearity checking visitor. Started on visitor that extracts coefficients of linear expressions.
Former-commit-id: 6e3d0ec910
tempestpy_adaptions
dehnert
11 years ago
20 changed files with 389 additions and 58 deletions
-
10src/storage/expressions/BaseExpression.cpp
-
10src/storage/expressions/BaseExpression.h
-
17src/storage/expressions/Expression.cpp
-
15src/storage/expressions/Expression.h
-
15src/storage/expressions/ExpressionReturnType.cpp
-
17src/storage/expressions/ExpressionReturnType.h
-
4src/storage/expressions/IdentifierSubstitutionVisitor.cpp
-
2src/storage/expressions/IdentifierSubstitutionVisitor.h
-
70src/storage/expressions/LinearCoefficientVisitor.cpp
-
46src/storage/expressions/LinearCoefficientVisitor.h
-
93src/storage/expressions/LinearityCheckVisitor.cpp
-
10src/storage/expressions/LinearityCheckVisitor.h
-
59src/storage/expressions/SimpleValuation.cpp
-
14src/storage/expressions/SimpleValuation.h
-
4src/storage/expressions/SubstitutionVisitor.cpp
-
2src/storage/expressions/SubstitutionVisitor.h
-
4src/storage/expressions/TypeCheckVisitor.cpp
-
2src/storage/expressions/TypeCheckVisitor.h
-
36src/storage/expressions/Valuation.h
-
17test/functional/storage/ExpressionTest.cpp
@ -0,0 +1,15 @@ |
|||
#include "src/storage/expressions/ExpressionReturnType.h"
|
|||
|
|||
namespace storm { |
|||
namespace expressions { |
|||
std::ostream& operator<<(std::ostream& stream, ExpressionReturnType const& enumValue) { |
|||
switch (enumValue) { |
|||
case ExpressionReturnType::Undefined: stream << "undefined"; break; |
|||
case ExpressionReturnType::Bool: stream << "bool"; break; |
|||
case ExpressionReturnType::Int: stream << "int"; break; |
|||
case ExpressionReturnType::Double: stream << "double"; break; |
|||
} |
|||
return stream; |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,17 @@ |
|||
#ifndef STORM_STORAGE_EXPRESSIONS_EXPRESSIONRETURNTYPE_H_ |
|||
#define STORM_STORAGE_EXPRESSIONS_EXPRESSIONRETURNTYPE_H_ |
|||
|
|||
#include <iostream> |
|||
|
|||
namespace storm { |
|||
namespace expressions { |
|||
/*! |
|||
* Each node in an expression tree has a uniquely defined type from this enum. |
|||
*/ |
|||
enum class ExpressionReturnType {Undefined, Bool, Int, Double}; |
|||
|
|||
std::ostream& operator<<(std::ostream& stream, ExpressionReturnType const& enumValue); |
|||
} |
|||
} |
|||
|
|||
#endif /* STORM_STORAGE_EXPRESSIONS_EXPRESSIONRETURNTYPE_H_ */ |
@ -0,0 +1,70 @@ |
|||
#include "src/storage/expressions/LinearCoefficientVisitor.h"
|
|||
|
|||
#include "src/storage/expressions/Expressions.h"
|
|||
#include "src/exceptions/ExceptionMacros.h"
|
|||
#include "src/exceptions/InvalidArgumentException.h"
|
|||
|
|||
namespace storm { |
|||
namespace expressions { |
|||
std::pair<SimpleValuation, double> LinearCoefficientVisitor::getLinearCoefficients(Expression const& expression) { |
|||
expression.getBaseExpression().accept(this); |
|||
return resultStack.top(); |
|||
} |
|||
|
|||
void LinearCoefficientVisitor::visit(IfThenElseExpression const* expression) { |
|||
LOG_THROW(false, storm::exceptions::InvalidArgumentException, "Expression is non-linear."); |
|||
} |
|||
|
|||
void LinearCoefficientVisitor::visit(BinaryBooleanFunctionExpression const* expression) { |
|||
|
|||
} |
|||
|
|||
void LinearCoefficientVisitor::visit(BinaryNumericalFunctionExpression const* expression) { |
|||
|
|||
} |
|||
|
|||
void LinearCoefficientVisitor::visit(BinaryRelationExpression const* expression) { |
|||
|
|||
} |
|||
|
|||
void LinearCoefficientVisitor::visit(VariableExpression const* expression) { |
|||
SimpleValuation valuation; |
|||
switch (expression->getReturnType()) { |
|||
case ExpressionReturnType::Bool: LOG_THROW(false, storm::exceptions::InvalidArgumentException, "Expression is non-linear."); break; |
|||
case ExpressionReturnType::Int: |
|||
case ExpressionReturnType::Double: valuation.addDoubleIdentifier(expression->getVariableName(), 1); break; |
|||
case ExpressionReturnType::Undefined: LOG_THROW(false, storm::exceptions::InvalidArgumentException, "Illegal expression return type."); break; |
|||
} |
|||
|
|||
resultStack.push(std::make_pair(valuation, 0)); |
|||
} |
|||
|
|||
void LinearCoefficientVisitor::visit(UnaryBooleanFunctionExpression const* expression) { |
|||
LOG_THROW(false, storm::exceptions::InvalidArgumentException, "Expression is non-linear."); |
|||
} |
|||
|
|||
void LinearCoefficientVisitor::visit(UnaryNumericalFunctionExpression const* expression) { |
|||
if (expression->getOperatorType() == UnaryNumericalFunctionExpression::OperatorType::Minus) { |
|||
// Here, we need to negate all double identifiers.
|
|||
std::pair<SimpleValuation, double>& valuationConstantPair = resultStack.top(); |
|||
for (auto const& identifier : valuationConstantPair.first.getDoubleIdentifiers()) { |
|||
valuationConstantPair.first.setDoubleValue(identifier, -valuationConstantPair.first.getDoubleValue(identifier)); |
|||
} |
|||
} else { |
|||
LOG_THROW(false, storm::exceptions::InvalidArgumentException, "Expression is non-linear."); |
|||
} |
|||
} |
|||
|
|||
void LinearCoefficientVisitor::visit(BooleanLiteralExpression const* expression) { |
|||
LOG_THROW(false, storm::exceptions::InvalidArgumentException, "Expression is non-linear."); |
|||
} |
|||
|
|||
void LinearCoefficientVisitor::visit(IntegerLiteralExpression const* expression) { |
|||
resultStack.push(std::make_pair(SimpleValuation(), static_cast<double>(expression->getValue()))); |
|||
} |
|||
|
|||
void LinearCoefficientVisitor::visit(DoubleLiteralExpression const* expression) { |
|||
resultStack.push(std::make_pair(SimpleValuation(), expression->getValue())); |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,46 @@ |
|||
#ifndef STORM_STORAGE_EXPRESSIONS_LINEARCOEFFICIENTVISITOR_H_ |
|||
#define STORM_STORAGE_EXPRESSIONS_LINEARCOEFFICIENTVISITOR_H_ |
|||
|
|||
#include <stack> |
|||
|
|||
#include "src/storage/expressions/Expression.h" |
|||
#include "src/storage/expressions/ExpressionVisitor.h" |
|||
#include "src/storage/expressions/SimpleValuation.h" |
|||
|
|||
namespace storm { |
|||
namespace expressions { |
|||
class LinearCoefficientVisitor : public ExpressionVisitor { |
|||
public: |
|||
/*! |
|||
* Creates a linear coefficient visitor. |
|||
*/ |
|||
LinearCoefficientVisitor() = default; |
|||
|
|||
/*! |
|||
* Computes the (double) coefficients of all identifiers appearing in the expression if the expression |
|||
* was rewritten as a sum of atoms.. If the expression is not linear, an exception is thrown. |
|||
* |
|||
* @param expression The expression for which to compute the coefficients. |
|||
* @return A pair consisting of a mapping from identifiers to their coefficients and the coefficient of |
|||
* the constant atom. |
|||
*/ |
|||
std::pair<SimpleValuation, double> getLinearCoefficients(Expression const& expression); |
|||
|
|||
virtual void visit(IfThenElseExpression const* expression) override; |
|||
virtual void visit(BinaryBooleanFunctionExpression const* expression) override; |
|||
virtual void visit(BinaryNumericalFunctionExpression const* expression) override; |
|||
virtual void visit(BinaryRelationExpression const* expression) override; |
|||
virtual void visit(VariableExpression const* expression) override; |
|||
virtual void visit(UnaryBooleanFunctionExpression const* expression) override; |
|||
virtual void visit(UnaryNumericalFunctionExpression const* expression) override; |
|||
virtual void visit(BooleanLiteralExpression const* expression) override; |
|||
virtual void visit(IntegerLiteralExpression const* expression) override; |
|||
virtual void visit(DoubleLiteralExpression const* expression) override; |
|||
|
|||
private: |
|||
std::stack<std::pair<SimpleValuation, double>> resultStack; |
|||
}; |
|||
} |
|||
} |
|||
|
|||
#endif /* STORM_STORAGE_EXPRESSIONS_LINEARCOEFFICIENTVISITOR_H_ */ |
Write
Preview
Loading…
Cancel
Save
Reference in new issue