Storm 2.0.0.3
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
ScpLpEncoderTest.cpp
Go to the documentation of this file.
1#include "storm-config.h"
3#include "test/storm_gtest.h"
4
5#include <algorithm>
6
15
16using namespace storm::pars::scp;
17
18namespace {
19
20storm::RationalFunction makeVarFunction(carl::Variable const& var, std::shared_ptr<storm::RawPolynomialCache> const& cache) {
22}
23
24storm::models::sparse::StateLabeling trivialLabeling(uint64_t numStates) {
25 storm::models::sparse::StateLabeling labeling(numStates);
26 labeling.addLabel("init");
27 labeling.addLabelToState("init", 0);
28 return labeling;
29}
30
31std::vector<uint64_t> toU64(std::vector<uint_fast64_t> const& v) {
32 return std::vector<uint64_t>(v.begin(), v.end());
33}
34
35} // namespace
36
38
39// 3-state DTMC: 0 -[p]-> 1 (Prob1), 0 -[1-p]-> 2 (Prob0). Exercises a real LP optimization (not a
40// degenerate single-point trust region) together with Prob0/Prob1 handling, the threshold
41// constraint, and the max-violation objective mode.
42//
43// Hand-derived optimum: AtLeast maximizes p_0 subject to "p_0 <= v_p + tau_0" (the Pmin-pairing;
44// see ScpLpEncoder's class-level doc comment). To let p_0 grow without paying violation, v_p must
45// grow to support it, so both are pushed to their trust-region *upper* bound (1.0), where
46// tau_0 = 0 is achievable (0.6 - 1.0 < 0) and p_0's bias term is maximal. Both are unique optima,
47// not just feasible points: any smaller v_p would cap p_0 below 1.0, which the bias strictly
48// disprefers.
49TEST_P(ScpLpEncoderTest, RealOptimization_AtLeast_MaxViolationObjective) {
50 carl::VariablePool::getInstance().clear();
51 auto cache = std::make_shared<storm::RawPolynomialCache>();
52 carl::Variable varP = storm::createRFVariable("p");
53 auto p = makeVarFunction(varP, cache);
54 auto one = storm::RationalFunction(1);
55
57 builder.addNextValue(0, 1, p);
58 builder.addNextValue(0, 2, one - p);
59 builder.addNextValue(1, 1, one);
60 builder.addNextValue(2, 2, one);
61 auto matrix = builder.build();
62 storm::models::sparse::Dtmc<storm::RationalFunction> dtmc(matrix, trivialLabeling(3));
63 AffinePMdpDecomposition decomposition(dtmc);
64
65 storm::storage::BitVector zeroStates(3), oneStates(3), infiniteStates(3);
66 zeroStates.set(2);
67 oneStates.set(1);
68 storm::storage::BitVector excludedRows(matrix.getRowCount(), false);
69
70 std::map<carl::Variable, std::pair<double, double>> bounds{{varP, {0.0, 1.0}}};
71 ScpOptions options;
72 options.useMaxViolationObjective = true;
73
74 ScpLpEncoder encoder(decomposition, toU64(matrix.getRowGroupIndices()), zeroStates, oneStates, infiniteStates, excludedRows, /*initialState=*/0, bounds,
75 /*threshold=*/0.6, ThresholdDirection::AtLeast, PropertyKind::Probability, options);
76
77 std::map<carl::Variable, double> valuation{{varP, 0.5}};
78 std::vector<double> stateValues{0.5, 0.0, 0.0}; // only index 0 (a free state) is ever read.
79
80 auto lp = encoder.encode(env(), valuation, stateValues, /*trustRegionRadius=*/2.0);
81 lp.solver->optimize();
82 ASSERT_TRUE(lp.solver->isOptimal());
83
84 EXPECT_NEAR(1.0, lp.solver->getContinuousValue(lp.stateVariables[0]), 1e-6);
85 EXPECT_NEAR(1.0, lp.solver->getContinuousValue(lp.parameterVariables.at(varP)), 1e-6);
86 EXPECT_NEAR(-1e-4, lp.solver->getObjectiveValue(), 1e-8);
87}
88
89// Same model as above, but AtMost direction and the sum-of-violations objective mode. Hand
90// derivation gives a unique optimum at v_p = p_0 = 0.25 (both trust-region lower bounds), zero
91// violation, objective = 1e-4 * 0.25.
92TEST_P(ScpLpEncoderTest, RealOptimization_AtMost_SumViolationObjective) {
93 carl::VariablePool::getInstance().clear();
94 auto cache = std::make_shared<storm::RawPolynomialCache>();
95 carl::Variable varP = storm::createRFVariable("p");
96 auto p = makeVarFunction(varP, cache);
97 auto one = storm::RationalFunction(1);
98
100 builder.addNextValue(0, 1, p);
101 builder.addNextValue(0, 2, one - p);
102 builder.addNextValue(1, 1, one);
103 builder.addNextValue(2, 2, one);
104 auto matrix = builder.build();
105 storm::models::sparse::Dtmc<storm::RationalFunction> dtmc(matrix, trivialLabeling(3));
106 AffinePMdpDecomposition decomposition(dtmc);
107
108 storm::storage::BitVector zeroStates(3), oneStates(3), infiniteStates(3);
109 zeroStates.set(2);
110 oneStates.set(1);
111 storm::storage::BitVector excludedRows(matrix.getRowCount(), false);
112
113 std::map<carl::Variable, std::pair<double, double>> bounds{{varP, {0.0, 1.0}}};
114 ScpOptions options;
115 options.useMaxViolationObjective = false;
116
117 ScpLpEncoder encoder(decomposition, toU64(matrix.getRowGroupIndices()), zeroStates, oneStates, infiniteStates, excludedRows, /*initialState=*/0, bounds,
118 /*threshold=*/0.3, ThresholdDirection::AtMost, PropertyKind::Probability, options);
119
120 std::map<carl::Variable, double> valuation{{varP, 0.5}};
121 std::vector<double> stateValues{0.5, 0.0, 0.0};
122
123 auto lp = encoder.encode(env(), valuation, stateValues, /*trustRegionRadius=*/2.0);
124 lp.solver->optimize();
125 ASSERT_TRUE(lp.solver->isOptimal());
126
127 EXPECT_NEAR(0.25, lp.solver->getContinuousValue(lp.stateVariables[0]), 1e-6);
128 EXPECT_NEAR(0.25, lp.solver->getContinuousValue(lp.parameterVariables.at(varP)), 1e-6);
129 EXPECT_NEAR(2.5e-5, lp.solver->getObjectiveValue(), 1e-8);
130}
131
132// 4-state DTMC: 0 -[p]-> 1 (free), 0 -[q]-> 3 (Prob1), 0 -[1-p-q]-> 2 (Prob0); 1 -[1]-> 1.
133// Uses a trust region radius a hair above 1.0 (exactly 1.0 collapses variable bounds to lb==ub,
134// which some LP backends -- e.g. Glpk's simplex -- reject as an invalid double-bounded variable
135// rather than treating as fixed), which pins every variable to within ~1e-6 of its linearization
136// value -- turning the LP into essentially "evaluate the linearized constraints at their own
137// center", with no meaningful optimization freedom. This directly checks the bilinear
138// Taylor-expansion formula (for the p*state1 term) and the exact/no-approximation handling of the
139// Prob1 successor (the q term) against arithmetic computed independently in the test, without
140// needing to solve a nontrivial LP by hand.
141TEST_P(ScpLpEncoderTest, DegenerateTrustRegion_ChecksLinearizationArithmetic) {
142 carl::VariablePool::getInstance().clear();
143 auto cache = std::make_shared<storm::RawPolynomialCache>();
144 carl::Variable varP = storm::createRFVariable("p");
145 carl::Variable varQ = storm::createRFVariable("q");
146 auto p = makeVarFunction(varP, cache);
147 auto q = makeVarFunction(varQ, cache);
148 auto one = storm::RationalFunction(1);
149
151 builder.addNextValue(0, 1, p);
152 builder.addNextValue(0, 3, q);
153 builder.addNextValue(0, 2, one - p - q);
154 builder.addNextValue(1, 1, one);
155 builder.addNextValue(2, 2, one);
156 builder.addNextValue(3, 3, one);
157 auto matrix = builder.build();
158 storm::models::sparse::Dtmc<storm::RationalFunction> dtmc(matrix, trivialLabeling(4));
159 AffinePMdpDecomposition decomposition(dtmc);
160
161 storm::storage::BitVector zeroStates(4), oneStates(4), infiniteStates(4);
162 zeroStates.set(2);
163 oneStates.set(3);
164 storm::storage::BitVector excludedRows(matrix.getRowCount(), false);
165
166 std::map<carl::Variable, std::pair<double, double>> bounds{{varP, {0.0, 1.0}}, {varQ, {0.0, 1.0}}};
167 ScpOptions options;
168 options.useMaxViolationObjective = true;
169
170 // threshold chosen low enough that it never binds tighter than the Bellman-derived slack.
171 ScpLpEncoder encoder(decomposition, toU64(matrix.getRowGroupIndices()), zeroStates, oneStates, infiniteStates, excludedRows, /*initialState=*/0, bounds,
172 /*threshold=*/0.05, ThresholdDirection::AtLeast, PropertyKind::Probability, options);
173
174 double const pHat = 0.3, qHat = 0.2, p0Hat = 0.1, p1Hat = 0.7;
175 std::map<carl::Variable, double> valuation{{varP, pHat}, {varQ, qHat}};
176 std::vector<double> stateValues{p0Hat, p1Hat, 0.0, 0.0};
177
178 auto lp = encoder.encode(env(), valuation, stateValues, /*trustRegionRadius=*/1.0 + 1e-6);
179 lp.solver->optimize();
180 ASSERT_TRUE(lp.solver->isOptimal());
181
182 // Every free/parameter variable is pinned to within ~1e-6 (relative) of its hat value by the
183 // near-degenerate trust region.
184 EXPECT_NEAR(pHat, lp.solver->getContinuousValue(lp.parameterVariables.at(varP)), 1e-5);
185 EXPECT_NEAR(qHat, lp.solver->getContinuousValue(lp.parameterVariables.at(varQ)), 1e-5);
186 EXPECT_NEAR(p0Hat, lp.solver->getContinuousValue(lp.stateVariables[0]), 1e-5);
187 EXPECT_NEAR(p1Hat, lp.solver->getContinuousValue(lp.stateVariables[1]), 1e-5);
188
189 // RHS = pHat*p1Hat (linearized bilinear term, exact at its own center) + qHat (exact Prob1 term).
190 // AtLeast constrains p_0 <= RHS + tau_0 (the Pmin-pairing), so tau_0 >= p_0 - RHS.
191 double expectedRhs = pHat * p1Hat + qHat;
192 double expectedTau0 = std::max(0.0, p0Hat - expectedRhs);
193 double expectedObjective = expectedTau0 - 1e-4 * p0Hat;
194
195 EXPECT_NEAR(expectedObjective, lp.solver->getObjectiveValue(), 1e-5);
196}
197
198// 3-state DTMC: 0 -[p]-> 1 (free), 0 -[1-p]-> 2 (zeroStates/target). Row 0's reward is "2*p + 3"
199// (affine, parametric -- contributes to the RHS directly, with no linearization needed since it
200// never multiplies a p_succ variable). Reward-mode state values are on a reward scale (state0's
201// hat value is 5.0, well above 1 -- probability mode would clamp its LP variable's upper bound to
202// 1.0, which is *below* its near-degenerate lower bound of ~5.0, making the LP infeasible/invalid;
203// this only works at all if the unbounded-above domain for reward-mode state variables is wired up
204// correctly). Same near-degenerate trust region trick as the probability test above, to make the
205// expected LP values checkable by direct arithmetic instead of by solving a nontrivial LP by hand.
206TEST_P(ScpLpEncoderTest, Reward_ChecksRewardTermAndUnboundedDomain) {
207 carl::VariablePool::getInstance().clear();
208 auto cache = std::make_shared<storm::RawPolynomialCache>();
209 carl::Variable varP = storm::createRFVariable("p");
210 auto p = makeVarFunction(varP, cache);
211 auto one = storm::RationalFunction(1);
212 auto three = storm::RationalFunction(3);
213
215 builder.addNextValue(0, 1, p);
216 builder.addNextValue(0, 2, one - p);
217 builder.addNextValue(1, 1, one);
218 builder.addNextValue(2, 2, one);
219 auto matrix = builder.build();
220 storm::models::sparse::Dtmc<storm::RationalFunction> dtmc(matrix, trivialLabeling(3));
221
222 std::vector<storm::RationalFunction> stateActionRewards{p + p + three, storm::utility::zero<storm::RationalFunction>(),
224 storm::models::sparse::StandardRewardModel<storm::RationalFunction> rewardModel(std::nullopt, stateActionRewards);
225 AffinePMdpDecomposition decomposition(dtmc, &rewardModel);
226
227 storm::storage::BitVector zeroStates(3), oneStates(3), infiniteStates(3);
228 zeroStates.set(2);
229 storm::storage::BitVector excludedRows(matrix.getRowCount(), false);
230
231 std::map<carl::Variable, std::pair<double, double>> bounds{{varP, {0.0, 1.0}}};
232 ScpOptions options;
233 options.useMaxViolationObjective = true;
234
235 // threshold chosen low enough that it never binds tighter than the Bellman-derived slack.
236 ScpLpEncoder encoder(decomposition, toU64(matrix.getRowGroupIndices()), zeroStates, oneStates, infiniteStates, excludedRows, /*initialState=*/0, bounds,
237 /*threshold=*/0.5, ThresholdDirection::AtLeast, PropertyKind::Reward, options);
238
239 double const pHat = 0.4, p0Hat = 5.0, p1Hat = 2.0;
240 std::map<carl::Variable, double> valuation{{varP, pHat}};
241 std::vector<double> stateValues{p0Hat, p1Hat, 0.0};
242
243 auto lp = encoder.encode(env(), valuation, stateValues, /*trustRegionRadius=*/1.0 + 1e-6);
244 lp.solver->optimize();
245 ASSERT_TRUE(lp.solver->isOptimal());
246
247 EXPECT_NEAR(pHat, lp.solver->getContinuousValue(lp.parameterVariables.at(varP)), 1e-5);
248 EXPECT_NEAR(p0Hat, lp.solver->getContinuousValue(lp.stateVariables[0]), 1e-4);
249 EXPECT_NEAR(p1Hat, lp.solver->getContinuousValue(lp.stateVariables[1]), 1e-4);
250
251 // RHS = reward(row0) [affine in v_p directly, no linearization] + bilinear(p, state1), each
252 // evaluated at its own linearization center since the near-degenerate trust region pins every
253 // variable there.
254 double expectedReward = 2 * pHat + 3;
255 double expectedBilinear = pHat * p1Hat;
256 double expectedRhs = expectedReward + expectedBilinear;
257 double expectedTau0 = std::max(0.0, p0Hat - expectedRhs);
258 ASSERT_GT(expectedTau0, 0.0); // a nontrivial (non-zero) violation, not a degenerate all-zero check.
259 double expectedObjective = expectedTau0 - 1e-4 * p0Hat;
260
261 EXPECT_NEAR(expectedObjective, lp.solver->getObjectiveValue(), 1e-4);
262}
263
266// The suite is empty if no LP solver was compiled in.
INSTANTIATE_TEST_SUITE_P(LpSolvers, ScpLpEncoderTest, testing::ValuesIn(storm::pars::scp::testutils::lpSolversUnderTest()), storm::pars::scp::testutils::LpSolverTest::solverName)
TEST_P(ScpLpEncoderTest, RealOptimization_AtLeast_MaxViolationObjective)
GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(ScpLpEncoderTest)
This class represents a discrete-time Markov chain.
Definition Dtmc.h:16
This class manages the labeling of the state space with a number of (atomic) labels.
Decomposes the transition matrix of an affine pMDP (or pDTMC), and optionally one reward model's stat...
Builds the linearized-Bellman LP of one SCP iteration (Cubuktepe et al., "Convex Optimization forPara...
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 ...
static std::string solverName(testing::TestParamInfo< storm::solver::LpSolverType > const &info)
A bit vector that is internally represented as a vector of 64-bit values.
Definition BitVector.h:16
A class that can be used to build a sparse matrix by adding value by value.
std::vector< storm::solver::LpSolverType > lpSolversUnderTest()
ValueType zero()
Definition constants.cpp:26
carl::FactorizedPolynomial< RawPolynomial > Polynomial
RationalFunctionVariable createRFVariable(std::string const &name)
carl::RationalFunction< Polynomial, true > RationalFunction
carl::MultivariatePolynomial< RationalFunctionCoefficient > RawPolynomial
std::unique_ptr< storm::solver::LpSolver< double > > solver
Configuration for the SCP feasibility search (SparseScpInstantiationSearcher).
Definition ScpOptions.h:16
bool useMaxViolationObjective
If true, the LP objective minimizes the maximum per-state constraint violation; if false,...
Definition ScpOptions.h:36