Storm
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
ReduceVertexCloud.cpp
Go to the documentation of this file.
4#undef _DEBUG_REDUCE_VERTEX_CLOUD
5
6namespace storm {
7namespace storage {
8namespace geometry {
9
10template<typename ValueType>
11std::string toString(std::map<uint64_t, ValueType> const& point) {
12 std::stringstream sstr;
13 bool first = true;
14 for (auto const& entry : point) {
15 if (first) {
16 first = false;
17 } else {
18 sstr << ", ";
19 }
20 sstr << entry.first << " : " << entry.second;
21 }
22 return sstr.str();
23}
24
25template<typename ValueType>
26std::pair<storm::storage::BitVector, bool> ReduceVertexCloud<ValueType>::eliminate(std::vector<std::map<uint64_t, ValueType>> const& input,
27 uint64_t maxdimension) {
28 std::shared_ptr<storm::expressions::ExpressionManager> expressionManager = std::make_shared<storm::expressions::ExpressionManager>();
29 std::vector<storm::storage::BitVector> supports;
30 std::vector<storm::expressions::Variable> weightVariables;
31 std::vector<storm::expressions::Expression> weightVariableExpressions;
32
33 for (uint64_t pointIndex = 0; pointIndex < input.size(); ++pointIndex) {
34 // Compute the support vectors to quickly determine which input points could be relevant.
35 supports.push_back(storm::storage::BitVector(maxdimension));
36 for (auto const& entry : input[pointIndex]) {
37 supports.back().set(entry.first, true);
38 }
39 // Add a weight variable for each input point
40 weightVariables.push_back(expressionManager->declareRationalVariable("w_" + std::to_string(pointIndex)));
41 // For convenience and performance, obtain the expression.
42 weightVariableExpressions.push_back(weightVariables.back().getExpression());
43 }
44
45 std::unique_ptr<storm::solver::SmtSolver> smtSolver = smtSolverFactory->create(*expressionManager);
46 for (auto const& weightVariableExpr : weightVariableExpressions) {
47 // smtSolver->add((weightVariableExpr == expressionManager->rational(0.0)) || (weightVariableExpr > expressionManager->rational(0.00001)));
48 smtSolver->add((weightVariableExpr >= expressionManager->rational(0.0)));
49 smtSolver->add(weightVariableExpr < expressionManager->rational(1.0));
50 }
51 if (storm::utility::isZero(wiggle)) {
52 smtSolver->add(storm::expressions::sum(weightVariableExpressions) <= expressionManager->rational(1));
53 } else {
54 smtSolver->add(storm::expressions::sum(weightVariableExpressions) <= expressionManager->rational(1 + wiggle));
55 smtSolver->add(storm::expressions::sum(weightVariableExpressions) >= expressionManager->rational(1 - wiggle));
56 }
57
59 storm::utility::Stopwatch totalTime(true);
60 storm::storage::BitVector vertices(input.size());
61 for (uint64_t pointIndex = 0; pointIndex < input.size(); ++pointIndex) {
62#ifdef _DEBUG_REUCE_VERTEX_CLOUD
63 std::cout << pointIndex << " out of " << input.size() << '\n';
64#endif
65 smtSolver->push();
66 std::map<uint64_t, std::vector<storm::expressions::Expression>> dimensionTerms;
67 for (auto const& entry : input[pointIndex]) {
68 dimensionTerms[entry.first] = {expressionManager->rational(-entry.second)};
69 }
70 for (uint64_t potentialSupport = 0; potentialSupport < input.size(); ++potentialSupport) {
71 if (pointIndex == potentialSupport) {
72 smtSolver->add(weightVariableExpressions[potentialSupport] == expressionManager->rational(0.0));
73 } else if (potentialSupport < pointIndex && !vertices.get(potentialSupport)) {
74 smtSolver->add(weightVariableExpressions[potentialSupport] == expressionManager->rational(0.0));
75 } else if (supports[potentialSupport].isSubsetOf(supports[pointIndex])) {
76 for (auto const& entry : input[potentialSupport]) {
77 dimensionTerms[entry.first].push_back(weightVariableExpressions[potentialSupport] * expressionManager->rational(entry.second));
78 }
79 } else {
80 smtSolver->add(weightVariableExpressions[potentialSupport] == expressionManager->rational(0.0));
81 }
82 }
83 for (auto const& entry : dimensionTerms) {
84 smtSolver->add(storm::expressions::sum(entry.second) == expressionManager->rational(0.0));
85 }
86
87 solverTime.start();
88 auto result = smtSolver->check();
89 solverTime.stop();
91#ifdef _DEBUG_REDUCE_VERTEX_CLOUD
92 if (input[pointIndex].size() == 2) {
93 std::cout << "point " << toString(input[pointIndex]) << " is a vertex:";
94 std::cout << smtSolver->getSmtLibString() << '\n';
95 }
96#endif
97 vertices.set(pointIndex, true);
98 }
99#ifdef _DEBUG_REDUCE_VERTEX_CLOUD
100 else {
101 std::cout << "point " << toString(input[pointIndex]) << " is a convex combination of ";
102 auto val = smtSolver->getModelAsValuation();
103 uint64_t varIndex = 0;
104 for (auto const& wvar : weightVariables) {
105 if (!storm::utility::isZero(val.getRationalValue(wvar))) {
106 std::cout << toString(input[varIndex]) << " (weight: " << val.getRationalValue(wvar) << ")";
107 }
108 varIndex++;
109 }
110 std::cout << '\n';
111 }
112 if (timeOut >)
113#endif
114 if (timeOut > 0 && static_cast<uint64_t>(totalTime.getTimeInMilliseconds()) > timeOut) {
115 for (uint64_t remainingPoint = pointIndex + 1; remainingPoint < input.size(); ++remainingPoint) {
116 vertices.set(remainingPoint);
117 }
118 return {vertices, true};
119 }
120 smtSolver->pop();
121#ifdef _DEBUG_REDUCE_VERTEX_CLOUD
122 std::cout << "Solver time " << solverTime.getTimeInMilliseconds() << '\n';
123 std::cout << "Total time " << totalTime.getTimeInMilliseconds() << '\n';
124#endif
125 }
126 return {vertices, false};
127}
128
129template class ReduceVertexCloud<double>;
131} // namespace geometry
132} // namespace storage
133} // namespace storm
A bit vector that is internally represented as a vector of 64-bit values.
Definition BitVector.h:18
void set(uint_fast64_t index, bool value=true)
Sets the given truth value at the given index.
bool get(uint_fast64_t index) const
Retrieves the truth value of the bit at the given index and performs a bound check.
std::pair< storm::storage::BitVector, bool > eliminate(std::vector< std::map< uint64_t, ValueType > > const &input, uint64_t maxdimension)
A class that provides convenience operations to display run times.
Definition Stopwatch.h:14
MilisecondType getTimeInMilliseconds() const
Gets the measured time in milliseconds.
Definition Stopwatch.cpp:21
void start()
Start stopwatch (again) and start measuring time.
Definition Stopwatch.cpp:48
void stop()
Stop stopwatch and add measured time to total time.
Definition Stopwatch.cpp:42
Expression sum(std::vector< storm::expressions::Expression > const &expressions)
std::string toString(std::map< uint64_t, ValueType > const &point)
bool isZero(ValueType const &a)
Definition constants.cpp:41
LabParser.cpp.
Definition cli.cpp:18