Storm 2.0.0.3
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
ScpLpEncoder.cpp
Go to the documentation of this file.
2
3#include <algorithm>
4#include <optional>
5
11
12namespace storm::pars::scp {
13
14ScpLpEncoder::ScpLpEncoder(AffinePMdpDecomposition const& decomposition, std::vector<uint64_t> rowGroupIndices, storm::storage::BitVector zeroStates,
16 uint64_t initialState, std::map<storm::RationalFunctionVariable, std::pair<double, double>> parameterBounds, double threshold,
17 ThresholdDirection direction, PropertyKind propertyKind, ScpOptions options)
18 : decomposition(decomposition),
19 rowGroupIndices(std::move(rowGroupIndices)),
20 zeroStates(std::move(zeroStates)),
21 oneStates(std::move(oneStates)),
22 infiniteStates(std::move(infiniteStates)),
23 excludedRows(std::move(excludedRows)),
24 initialState(initialState),
25 parameterBounds(std::move(parameterBounds)),
26 threshold(threshold),
27 direction(direction),
28 propertyKind(propertyKind),
29 options(options) {
30 STORM_LOG_ASSERT(!this->zeroStates.get(initialState) && !this->oneStates.get(initialState) && !this->infiniteStates.get(initialState),
31 "The initial state's value must not be a priori known; the caller should short-circuit before building an LP in that case.");
32 STORM_LOG_ASSERT(propertyKind != PropertyKind::Reward || this->oneStates.getNumberOfSetBits() == 0,
33 "A reward property has no 'known value 1' states; oneStates must be empty.");
34}
35
36EncodedLp ScpLpEncoder::encode(storm::Environment const& env, std::map<storm::RationalFunctionVariable, double> const& valuation,
37 std::vector<double> const& stateValues, double trustRegionRadius) const {
38 STORM_LOG_ASSERT(trustRegionRadius >= 1.0, "Trust region radius must be >= 1.");
39 uint64_t const numberOfStates = rowGroupIndices.size() - 1;
40
41 std::unique_ptr<storm::solver::LpSolver<double>> solver = storm::utility::solver::getLpSolver<double>(env, "scp");
42
43 EncodedLp result;
44 result.stateVariables.resize(numberOfStates);
45
46 // Parameter variables, trust-region-bounded within the outer search region.
47 for (auto const& [var, bounds] : parameterBounds) {
48 double vHat = valuation.at(var);
49 double lb = std::max(bounds.first, vHat / trustRegionRadius);
50 double ub = std::min(bounds.second, vHat * trustRegionRadius);
51 result.parameterVariables.emplace(var, solver->addBoundedContinuousVariable("v_" + var.name(), lb, ub));
52 }
53
54 // State variables exist only for states without a known value. Probability variables are capped
55 // by 1; the trust region bounds both kinds.
56 for (uint64_t s = 0; s < numberOfStates; ++s) {
57 if (zeroStates.get(s) || oneStates.get(s) || infiniteStates.get(s)) {
58 continue;
59 }
60 double pHat = stateValues.at(s);
61 double lb = std::max(0.0, pHat / trustRegionRadius);
62 double ub = pHat * trustRegionRadius;
63 if (propertyKind == PropertyKind::Probability) {
64 ub = std::min(1.0, ub);
65 }
66 double objectiveCoefficient = 0.0;
67 if (s == initialState) {
68 objectiveCoefficient = (direction == ThresholdDirection::AtLeast) ? -options.initialStateBias : options.initialStateBias;
69 }
70 result.stateVariables[s] = solver->addBoundedContinuousVariable("p_" + std::to_string(s), lb, ub, objectiveCoefficient);
71 }
72
73 // Per-state slack (violation) variables, and (if configured) the shared max-violation variable.
74 std::vector<storm::expressions::Variable> tau(numberOfStates);
75 for (uint64_t s = 0; s < numberOfStates; ++s) {
76 if (zeroStates.get(s) || oneStates.get(s) || infiniteStates.get(s)) {
77 continue;
78 }
79 double objectiveCoefficient = options.useMaxViolationObjective ? 0.0 : 1.0;
80 tau[s] = solver->addLowerBoundedContinuousVariable("tau_" + std::to_string(s), 0.0, objectiveCoefficient);
81 }
83 if (options.useMaxViolationObjective) {
84 maxViolation = solver->addLowerBoundedContinuousVariable("max_violation", 0.0, 1.0);
85 }
86
87 solver->update();
88
89 // The linearized contribution of one transition to a Bellman right-hand side: zero successors
90 // contribute nothing, one successors their exact affine value, and other successors the
91 // first-order expansion of P(v) * p_succ around (valuation, stateValues). Rows with infinite
92 // successors are excluded before this is called.
93 auto successorContribution = [&](UnpackedTransition const& ut) -> std::optional<storm::expressions::Expression> {
94 if (zeroStates.get(ut.successor)) {
95 return std::nullopt;
96 }
97 if (oneStates.get(ut.successor)) {
98 storm::expressions::Expression expr = solver->getConstant(ut.constant);
99 for (auto const& [var, coeff] : ut.parameterCoefficients) {
100 expr = expr + result.parameterVariables.at(var).getExpression() * solver->getConstant(coeff);
101 }
102 return expr;
103 }
104 STORM_LOG_ASSERT(!infiniteStates.get(ut.successor), "A row with an infinite-value successor was not excluded before building its Bellman constraint.");
105 double pHatSucc = stateValues.at(ut.successor);
106 storm::expressions::Expression pSucc = result.stateVariables.at(ut.successor).getExpression();
107 storm::expressions::Expression expr = pSucc * solver->getConstant(ut.constant);
108 for (auto const& [var, coeff] : ut.parameterCoefficients) {
109 double vHat = valuation.at(var);
110 storm::expressions::Expression v = result.parameterVariables.at(var).getExpression();
111 // First-order Taylor expansion of v * p_succ around (vHat, pHatSucc):
112 // vHat*pHatSucc + pHatSucc*(v - vHat) + vHat*(p_succ - pHatSucc) = pHatSucc*v + vHat*p_succ - vHat*pHatSucc
114 v * solver->getConstant(pHatSucc) + pSucc * solver->getConstant(vHat) - solver->getConstant(vHat * pHatSucc);
115 expr = expr + bilinear * solver->getConstant(coeff);
116 }
117 return expr;
118 };
119
120 for (uint64_t s = 0; s < numberOfStates; ++s) {
121 if (zeroStates.get(s) || oneStates.get(s) || infiniteStates.get(s)) {
122 continue;
123 }
124 for (uint64_t row = rowGroupIndices[s]; row < rowGroupIndices[s + 1]; ++row) {
125 if (excludedRows.get(row)) {
126 continue;
127 }
128 std::vector<storm::expressions::Expression> summands;
129 for (auto const& transition : decomposition.getRow(row)) {
130 if (auto contribution = successorContribution(transition)) {
131 summands.push_back(*contribution);
132 }
133 }
134 if (propertyKind == PropertyKind::Reward) {
135 UnpackedValue const& reward = decomposition.getReward(row);
136 storm::expressions::Expression rewardExpr = solver->getConstant(reward.constant);
137 for (auto const& [var, coeff] : reward.parameterCoefficients) {
138 rewardExpr = rewardExpr + result.parameterVariables.at(var).getExpression() * solver->getConstant(coeff);
139 }
140 summands.push_back(rewardExpr);
141 }
142 storm::expressions::Expression rhs = summands.empty() ? solver->getConstant(0.0) : storm::expressions::sum(summands);
143 // AtMost: "p_s >= rhs - tau_s" for every action, minimized -> the least supersolution
144 // (Pmax; the max scheduler's best action survives the minimization).
145 // AtLeast: "p_s <= rhs + tau_s" for every action, maximized -> the greatest subsolution
146 // (Pmin; the min scheduler's worst action survives the maximization).
147 if (direction == ThresholdDirection::AtLeast) {
148 solver->addConstraint("", result.stateVariables[s].getExpression() <= rhs + tau[s].getExpression());
149 } else {
150 solver->addConstraint("", result.stateVariables[s].getExpression() >= rhs - tau[s].getExpression());
151 }
152 if (options.useMaxViolationObjective) {
153 solver->addConstraint("", tau[s].getExpression() <= maxViolation.getExpression());
154 }
155 }
156 }
157
158 // Threshold constraint on the initial state, sharing its Bellman constraint's slack variable.
159 storm::expressions::Expression initialStateValue = result.stateVariables[initialState].getExpression();
160 if (direction == ThresholdDirection::AtLeast) {
161 solver->addConstraint("", initialStateValue + tau[initialState].getExpression() >= solver->getConstant(threshold));
162 } else {
163 solver->addConstraint("", initialStateValue <= solver->getConstant(threshold) + tau[initialState].getExpression());
164 }
165
166 result.solver = std::move(solver);
167 return result;
168}
169
170} // namespace storm::pars::scp
storm::expressions::Expression getExpression() const
Retrieves an expression that represents the variable.
Definition Variable.cpp:34
Decomposes the transition matrix of an affine pMDP (or pDTMC), and optionally one reward model's stat...
EncodedLp encode(storm::Environment const &env, std::map< storm::RationalFunctionVariable, double > const &valuation, std::vector< double > const &stateValues, double trustRegionRadius) const
Builds the LP for one SCP iteration, linearized around (valuation, stateValues) with the given trust ...
ScpLpEncoder(AffinePMdpDecomposition const &decomposition, std::vector< uint64_t > rowGroupIndices, storm::storage::BitVector zeroStates, storm::storage::BitVector oneStates, storm::storage::BitVector infiniteStates, storm::storage::BitVector excludedRows, uint64_t initialState, std::map< storm::RationalFunctionVariable, std::pair< double, double > > parameterBounds, double threshold, ThresholdDirection direction, PropertyKind propertyKind, ScpOptions options)
A bit vector that is internally represented as a vector of 64-bit values.
Definition BitVector.h:16
uint64_t getNumberOfSetBits() const
Returns the number of bits that are set to true in this bit vector.
bool get(uint64_t index) const
Retrieves the truth value of the bit at the given index and performs a bound check.
#define STORM_LOG_ASSERT(cond, message)
Definition macros.h:9
Expression sum(std::vector< storm::expressions::Expression > const &expressions)
PropertyKind
The kind of property the LP is built for.
ThresholdDirection
Which side of the threshold a witness is searched for, read robustly over schedulers:
std::unique_ptr< storm::solver::LpSolver< ValueType > > getLpSolver(storm::Environment const &env, std::string const &name, storm::solver::LpSolverTypeSelection solvType)
Definition solver.cpp:146
carl::Variable RationalFunctionVariable
The LP built for one SCP iteration, with the handles needed to read a solution back.
std::unique_ptr< storm::solver::LpSolver< double > > solver
std::vector< storm::expressions::Variable > stateVariables
std::map< storm::RationalFunctionVariable, storm::expressions::Variable > parameterVariables
Configuration for the SCP feasibility search (SparseScpInstantiationSearcher).
Definition ScpOptions.h:16
The affine part of a single transition (row, successor): UnpackedValue plus which successor state it ...
The affine decomposition of a single scalar quantity, so that everything downstream only ever touches...
std::vector< std::pair< storm::RationalFunctionVariable, double > > parameterCoefficients