Sebastian Junges
4 years ago
5 changed files with 185 additions and 42 deletions
-
63src/storm-pomdp/generator/NondeterministicBeliefTracker.cpp
-
3src/storm-pomdp/generator/NondeterministicBeliefTracker.h
-
13src/storm-pomdp/transformer/ObservationTraceUnfolder.cpp
-
119src/storm/storage/geometry/ReduceVertexCloud.cpp
-
29src/storm/storage/geometry/ReduceVertexCloud.h
@ -0,0 +1,119 @@ |
|||||
|
#include "storm/storage/geometry/ReduceVertexCloud.h"
|
||||
|
#include "storm/utility/Stopwatch.h"
|
||||
|
#undef _DEBUG_REDUCE_VERTEX_CLOUD
|
||||
|
|
||||
|
|
||||
|
namespace storm { |
||||
|
namespace storage { |
||||
|
namespace geometry { |
||||
|
|
||||
|
template<typename ValueType> |
||||
|
std::string toString(std::map<uint64_t, ValueType> const& point) { |
||||
|
std::stringstream sstr; |
||||
|
bool first = true; |
||||
|
for (auto const& entry : point) { |
||||
|
if (first) { |
||||
|
first = false; |
||||
|
} else { |
||||
|
sstr << ", "; |
||||
|
} |
||||
|
sstr << entry.first << " : " << entry.second; |
||||
|
} |
||||
|
return sstr.str(); |
||||
|
} |
||||
|
|
||||
|
template<typename ValueType> |
||||
|
storm::storage::BitVector ReduceVertexCloud<ValueType>::eliminate(std::vector<std::map<uint64_t, ValueType>> const& input, uint64_t maxdimension) { |
||||
|
std::shared_ptr<storm::expressions::ExpressionManager> expressionManager = std::make_shared<storm::expressions::ExpressionManager>(); |
||||
|
std::vector<storm::storage::BitVector> supports; |
||||
|
std::vector<storm::expressions::Variable> weightVariables; |
||||
|
std::vector<storm::expressions::Expression> weightVariableExpressions; |
||||
|
|
||||
|
for (uint64_t pointIndex = 0; pointIndex < input.size(); ++pointIndex) { |
||||
|
|
||||
|
// Compute the support vectors to quickly determine which input points could be relevant.
|
||||
|
supports.push_back(storm::storage::BitVector(maxdimension)); |
||||
|
for (auto const& entry : input[pointIndex]) { |
||||
|
supports.back().set(entry.first, true); |
||||
|
} |
||||
|
// Add a weight variable for each input point
|
||||
|
weightVariables.push_back(expressionManager->declareRationalVariable("w_"+ std::to_string(pointIndex))); |
||||
|
// For convenience and performance, obtain the expression.
|
||||
|
weightVariableExpressions.push_back(weightVariables.back().getExpression()); |
||||
|
} |
||||
|
|
||||
|
std::unique_ptr<storm::solver::SmtSolver> smtSolver = smtSolverFactory->create(*expressionManager); |
||||
|
for (auto const& weightVariableExpr : weightVariableExpressions) { |
||||
|
//smtSolver->add((weightVariableExpr == expressionManager->rational(0.0)) || (weightVariableExpr > expressionManager->rational(0.00001)));
|
||||
|
smtSolver->add((weightVariableExpr >= expressionManager->rational(0.0))); |
||||
|
smtSolver->add(weightVariableExpr < expressionManager->rational(1.0)); |
||||
|
} |
||||
|
smtSolver->add(storm::expressions::sum(weightVariableExpressions) <= expressionManager->rational(1.0 + wiggle)); |
||||
|
smtSolver->add(storm::expressions::sum(weightVariableExpressions) >= expressionManager->rational(1 - wiggle)); |
||||
|
|
||||
|
storm::utility::Stopwatch solverTime; |
||||
|
storm::utility::Stopwatch totalTime(true); |
||||
|
storm::storage::BitVector vertices(input.size()); |
||||
|
for (uint64_t pointIndex = 0; pointIndex < input.size(); ++pointIndex) { |
||||
|
std::cout << pointIndex << " out of " << input.size() << std::endl; |
||||
|
smtSolver->push(); |
||||
|
std::map<uint64_t, std::vector<storm::expressions::Expression>> dimensionTerms; |
||||
|
for (auto const& entry : input[pointIndex]) { |
||||
|
dimensionTerms[entry.first] = {expressionManager->rational(-entry.second)}; |
||||
|
} |
||||
|
for (uint64_t potentialSupport = 0; potentialSupport < input.size(); ++potentialSupport) { |
||||
|
if (pointIndex == potentialSupport) { |
||||
|
smtSolver->add(weightVariableExpressions[potentialSupport] == expressionManager->rational(0.0)); |
||||
|
} else if (potentialSupport < pointIndex && !vertices.get(potentialSupport)) { |
||||
|
smtSolver->add(weightVariableExpressions[potentialSupport] == expressionManager->rational(0.0)); |
||||
|
} else if (supports[potentialSupport].isSubsetOf(supports[pointIndex])) { |
||||
|
for (auto const& entry : input[potentialSupport]) { |
||||
|
dimensionTerms[entry.first].push_back(weightVariableExpressions[potentialSupport] * expressionManager->rational(entry.second)); |
||||
|
} |
||||
|
} else { |
||||
|
smtSolver->add(weightVariableExpressions[potentialSupport] == expressionManager->rational(0.0)); |
||||
|
} |
||||
|
} |
||||
|
for (auto const& entry : dimensionTerms) { |
||||
|
smtSolver->add(storm::expressions::sum(entry.second) == expressionManager->rational(0.0)); |
||||
|
} |
||||
|
|
||||
|
solverTime.start(); |
||||
|
auto result = smtSolver->check(); |
||||
|
solverTime.stop(); |
||||
|
if (result == storm::solver::SmtSolver::CheckResult::Unsat) { |
||||
|
#ifdef _DEBUG_REDUCE_VERTEX_CLOUD
|
||||
|
if (input[pointIndex].size() == 2) { |
||||
|
std::cout << "point " << toString(input[pointIndex]) << " is a vertex:"; |
||||
|
std::cout << smtSolver->getSmtLibString() << std::endl; |
||||
|
} |
||||
|
#endif
|
||||
|
vertices.set(pointIndex, true); |
||||
|
} |
||||
|
#ifdef _DEBUG_REDUCE_VERTEX_CLOUD
|
||||
|
else |
||||
|
{ |
||||
|
std::cout << "point " << toString(input[pointIndex]) << " is a convex combination of "; |
||||
|
auto val = smtSolver->getModelAsValuation(); |
||||
|
uint64_t varIndex = 0; |
||||
|
for (auto const& wvar : weightVariables) { |
||||
|
if (!storm::utility::isZero(val.getRationalValue(wvar))) { |
||||
|
std::cout << toString(input[varIndex]) << " (weight: " << val.getRationalValue(wvar) << ")"; |
||||
|
} |
||||
|
varIndex++; |
||||
|
} |
||||
|
std::cout << std::endl; |
||||
|
} |
||||
|
#endif
|
||||
|
smtSolver->pop(); |
||||
|
std::cout << "Solver time " << solverTime.getTimeInMilliseconds() << std::endl; |
||||
|
std::cout << "Total time " << totalTime.getTimeInMilliseconds() << std::endl; |
||||
|
} |
||||
|
return vertices; |
||||
|
|
||||
|
} |
||||
|
|
||||
|
template class ReduceVertexCloud<double>; |
||||
|
} |
||||
|
} |
||||
|
} |
@ -0,0 +1,29 @@ |
|||||
|
#pragma once |
||||
|
|
||||
|
#include "storm/storage/BitVector.h" |
||||
|
#include "storm/solver/SmtSolver.h" |
||||
|
#include "storm/utility/solver.h" |
||||
|
|
||||
|
namespace storm { |
||||
|
namespace storage { |
||||
|
namespace geometry { |
||||
|
template<typename ValueType> |
||||
|
class ReduceVertexCloud { |
||||
|
public: |
||||
|
ReduceVertexCloud(std::shared_ptr<storm::utility::solver::SmtSolverFactory>& smtSolverFactory, ValueType wiggle = storm::utility::convertNumber<ValueType>(0.001)) |
||||
|
: smtSolverFactory(smtSolverFactory), wiggle(wiggle) |
||||
|
{ |
||||
|
|
||||
|
} |
||||
|
|
||||
|
storm::storage::BitVector eliminate(std::vector<std::map<uint64_t, ValueType>> const& input, uint64_t maxdimension); |
||||
|
|
||||
|
private: |
||||
|
std::shared_ptr<storm::utility::solver::SmtSolverFactory>& smtSolverFactory; |
||||
|
ValueType wiggle; |
||||
|
|
||||
|
}; |
||||
|
|
||||
|
} |
||||
|
} |
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue