51 template<
bool Backward = true>
99 template<
typename OperandType,
typename OffsetType,
typename BackendType>
100 bool apply(OperandType
const& operandIn, OperandType& operandOut, OffsetType
const& offsets, BackendType& backend)
const {
105 template<OptimizationDirection RobustDir,
typename OperandType,
typename OffsetType,
typename BackendType>
106 bool applyRobust(OperandType
const& operandIn, OperandType& operandOut, OffsetType
const& offsets, BackendType& backend)
const {
107 if (hasSkippedRows) {
125 template<
typename OperandType,
typename OffsetType,
typename BackendType>
126 bool applyInPlace(OperandType& operand, OffsetType
const& offsets, BackendType& backend)
const {
127 return apply(operand, operand, offsets, backend);
130 template<OptimizationDirection RobustDir,
typename OperandType,
typename OffsetType,
typename BackendType>
131 bool applyInPlaceRobust(OperandType& operand, OffsetType
const& offsets, BackendType& backend)
const {
160 std::vector<SolutionType>&
allocateAuxiliaryVector(uint64_t size, std::optional<SolutionType>
const& initialValue = {});
172 template<
typename OperandType,
typename OffsetType,
typename BackendType,
bool Backward,
bool SkipIgnoredRows, OptimizationDirection RobustDirection>
173 bool apply(OperandType& operandOut, OperandType
const& operandIn, OffsetType
const& offsets, BackendType& backend)
const {
174 auto const outSize = TrivialRowGrouping ? getSize(operandOut) : rowGroupIndices->size() - 1;
175 STORM_LOG_ASSERT(TrivialRowGrouping || getSize(operandOut) >= outSize,
"Dimension mismatch.");
176 backend.startNewIteration();
177 auto matrixValueIt = matrixValues.cbegin();
178 auto matrixColumnIt = matrixColumns.cbegin();
179 for (
auto groupIndex : indexRange<Backward>(0, outSize)) {
180 STORM_LOG_ASSERT(matrixColumnIt != matrixColumns.end(),
"VI Operator in invalid state.");
181 STORM_LOG_ASSERT(*matrixColumnIt >= StartOfRowIndicator,
"VI Operator in invalid state.");
182 if constexpr (TrivialRowGrouping) {
184 if constexpr (std::is_same<BackendType, RobustSchedulerTrackingBackend<double, RobustDirection, TrivialRowGrouping>>::value) {
186 backend.processRobustRow(applyRow<RobustDirection>(matrixColumnIt, matrixValueIt, operandIn, offsets, groupIndex), groupIndex,
187 applyCache.robustOrder);
190 backend.firstRow(applyRow<RobustDirection>(matrixColumnIt, matrixValueIt, operandIn, offsets, groupIndex), groupIndex, groupIndex);
193 IndexType rowIndex = (*rowGroupIndices)[groupIndex];
194 if constexpr (SkipIgnoredRows) {
195 rowIndex += skipMultipleIgnoredRows(matrixColumnIt, matrixValueIt);
197 backend.firstRow(applyRow<RobustDirection>(matrixColumnIt, matrixValueIt, operandIn, offsets, rowIndex), groupIndex, rowIndex);
198 while (*matrixColumnIt < StartOfRowGroupIndicator) {
200 if (!SkipIgnoredRows || !skipIgnoredRow(matrixColumnIt, matrixValueIt)) {
201 backend.nextRow(applyRow<RobustDirection>(matrixColumnIt, matrixValueIt, operandIn, offsets, rowIndex), groupIndex, rowIndex);
205 if constexpr (isPair<OperandType>::value) {
206 backend.applyUpdate(operandOut.first[groupIndex], operandOut.second[groupIndex], groupIndex);
208 backend.applyUpdate(operandOut[groupIndex], groupIndex);
210 if (backend.abort()) {
211 return backend.converged();
214 STORM_LOG_ASSERT(matrixColumnIt + 1 == matrixColumns.cend(),
"Unexpected position of matrix column iterator.");
215 STORM_LOG_ASSERT(matrixValueIt == matrixValues.cend(),
"Unexpected position of matrix column iterator.");
216 backend.endOfIteration();
217 return backend.converged();
222 template<
typename OpT>
223 OpT initializeRowRes(std::vector<OpT>
const&, OpT
const& offsets, uint64_t offsetIndex)
const {
227 template<
typename OpT,
typename OffT>
228 OpT initializeRowRes(std::vector<OpT>
const&, std::vector<OffT>
const& offsets, uint64_t offsetIndex)
const {
229 return offsets[offsetIndex];
232 template<
typename OpT1,
typename OpT2,
typename OffT>
233 std::pair<OpT1, OpT2> initializeRowRes(std::pair<std::vector<OpT1>, std::vector<OpT2>>
const&, std::vector<OffT>
const& offsets,
234 uint64_t offsetIndex)
const {
235 return {offsets[offsetIndex], offsets[offsetIndex]};
238 template<
typename OpT1,
typename OpT2,
typename OffT1,
typename OffT2>
239 std::pair<OpT1, OpT2> initializeRowRes(std::pair<std::vector<OpT1>, std::vector<OpT2>>
const&,
240 std::pair<std::vector<OffT1>, std::vector<OffT2>>
const& offsets, uint64_t offsetIndex)
const {
241 return {offsets.first[offsetIndex], offsets.second[offsetIndex]};
244 template<
typename OpT1,
typename OpT2,
typename OffT1,
typename OffT2>
245 std::pair<OpT1, OpT2> initializeRowRes(std::pair<std::vector<OpT1>, std::vector<OpT2>>
const&, std::pair<std::vector<OffT1>
const*, OffT2>
const& offsets,
246 uint64_t offsetIndex)
const {
247 return {(*offsets.first)[offsetIndex], offsets.second};
250 template<OptimizationDirection RobustDirection,
typename OpT,
typename OffT>
251 OpT robustInitializeRowRes(std::vector<OpT>
const&, OffT
const& offsets, uint64_t offsetIndex)
const {
252 if constexpr (RobustDirection == OptimizationDirection::Maximize) {
253 return offsets.upper();
255 return offsets.lower();
259 template<OptimizationDirection RobustDirection,
typename OpT,
typename OffT>
260 OpT robustInitializeRowRes(std::vector<OpT>
const&, std::vector<OffT>
const& offsets, uint64_t offsetIndex)
const {
261 if constexpr (RobustDirection == OptimizationDirection::Maximize) {
262 return offsets[offsetIndex].upper();
264 return offsets[offsetIndex].lower();
268 template<OptimizationDirection RobustDirection,
typename OpT1,
typename OpT2,
typename OffT>
269 std::pair<OpT1, OpT2> robustInitializeRowRes(std::pair<std::vector<OpT1>, std::vector<OpT2>>
const&, std::vector<OffT>
const& offsets,
270 uint64_t offsetIndex)
const {
271 STORM_LOG_THROW(
false, storm::exceptions::NotImplementedException,
"Value Iteration is not implemented with pairs and interval-models.");
273 return {offsets[offsetIndex].upper(), offsets[offsetIndex].upper()};
276 template<OptimizationDirection RobustDirection,
typename OpT1,
typename OpT2,
typename OffT1,
typename OffT2>
277 std::pair<OpT1, OpT2> robustInitializeRowRes(std::pair<std::vector<OpT1>, std::vector<OpT2>>
const&,
278 std::pair<std::vector<OffT1>
const*, OffT2>
const& offsets, uint64_t offsetIndex)
const {
279 STORM_LOG_THROW(
false, storm::exceptions::NotImplementedException,
"Value Iteration is not implemented with pairs and interval-models.");
281 return {(*offsets.first)[offsetIndex], offsets.second};
287 template<OptimizationDirection RobustDirection,
typename OperandType,
typename OffsetType>
288 auto applyRow(std::vector<IndexType>::const_iterator& matrixColumnIt,
typename std::vector<ValueType>::const_iterator& matrixValueIt,
289 OperandType
const& operand, OffsetType
const& offsets, uint64_t offsetIndex)
const {
291 return applyRowRobust<RobustDirection>(matrixColumnIt, matrixValueIt, operand, offsets, offsetIndex);
293 return applyRowStandard(matrixColumnIt, matrixValueIt, operand, offsets, offsetIndex);
297 template<
typename OperandType,
typename OffsetType>
298 auto applyRowStandard(std::vector<IndexType>::const_iterator& matrixColumnIt,
typename std::vector<ValueType>::const_iterator& matrixValueIt,
299 OperandType
const& operand, OffsetType
const& offsets, uint64_t offsetIndex)
const {
300 STORM_LOG_ASSERT(*matrixColumnIt >= StartOfRowIndicator,
"VI Operator in invalid state.");
301 auto result{initializeRowRes(operand, offsets, offsetIndex)};
302 for (++matrixColumnIt; *matrixColumnIt < StartOfRowIndicator; ++matrixColumnIt, ++matrixValueIt) {
303 if constexpr (isPair<OperandType>::value) {
304 result.first += operand.first[*matrixColumnIt] * (*matrixValueIt);
305 result.second += operand.second[*matrixColumnIt] * (*matrixValueIt);
307 result += operand[*matrixColumnIt] * (*matrixValueIt);
314 template<OptimizationDirection RobustDirection>
316 bool operator()(
const std::pair<SolutionType, std::pair<SolutionType, uint64_t>>& a,
317 const std::pair<SolutionType, std::pair<SolutionType, uint64_t>>& b)
const {
318 if constexpr (RobustDirection == OptimizationDirection::Maximize) {
319 return a.first > b.first;
321 return a.first < b.first;
326 template<OptimizationDirection RobustDirection,
typename OperandType,
typename OffsetType>
327 auto applyRowRobust(std::vector<IndexType>::const_iterator& matrixColumnIt,
typename std::vector<ValueType>::const_iterator& matrixValueIt,
328 OperandType
const& operand, OffsetType
const& offsets, uint64_t offsetIndex)
const {
329 STORM_LOG_ASSERT(*matrixColumnIt >= StartOfRowIndicator,
"VI Operator in invalid state.");
330 SolutionType result{robustInitializeRowRes<RobustDirection>(operand, offsets, offsetIndex)};
332 applyCache.robustOrder.clear();
334 if (applyCache.hasOnlyConstants.size() > 0 && applyCache.hasOnlyConstants.get(offsetIndex)) {
335 for (++matrixColumnIt; *matrixColumnIt < StartOfRowIndicator; ++matrixColumnIt, ++matrixValueIt) {
336 SolutionType
const lower = matrixValueIt->lower();
337 if constexpr (isPair<OperandType>::value) {
338 STORM_LOG_THROW(
false, storm::exceptions::NotImplementedException,
"Value Iteration is not implemented with pairs and interval-models.");
341 result += operand[*matrixColumnIt] * lower;
348 uint64_t orderCounter = 0;
349 for (++matrixColumnIt; *matrixColumnIt < StartOfRowIndicator; ++matrixColumnIt, ++matrixValueIt, ++orderCounter) {
350 SolutionType
const lower = matrixValueIt->lower();
351 if constexpr (isPair<OperandType>::value) {
352 STORM_LOG_THROW(
false, storm::exceptions::NotImplementedException,
"Value Iteration is not implemented with pairs and interval-models.");
355 result += operand[*matrixColumnIt] * lower;
357 remainingValue -= lower;
358 SolutionType
const diameter = matrixValueIt->upper() - lower;
360 applyCache.robustOrder.emplace_back(operand[*matrixColumnIt], std::make_pair(diameter, orderCounter));
367 static AuxCompare<RobustDirection> cmp;
368 std::sort(applyCache.robustOrder.begin(), applyCache.robustOrder.end(), cmp);
370 for (
auto const& pair : applyCache.robustOrder) {
371 SolutionType availableMass = std::min(pair.second.first, remainingValue);
372 result += availableMass * pair.first;
373 remainingValue -= availableMass;
381 "Remaining value should be zero (all prob mass taken) or it should be a sad state, but is " << remainingValue);
386 template<
bool Backward>
389 return boost::adaptors::reverse(boost::irange(start, end));
391 return boost::irange(start, end);
396 uint64_t getSize(std::vector<T>
const& vec)
const {
400 template<
typename T1,
typename T2>
401 uint64_t getSize(std::pair<T1, T2>
const& pairOfVec)
const {
402 return pairOfVec.first.size();
406 struct isPair : std::false_type {};
408 template<
typename T1,
typename T2>
409 struct isPair<std::pair<T1, T2>> : std::true_type {};
414 template<
bool Backward = true>
420 void moveToEndOfRow(std::vector<IndexType>::iterator& matrixColumnIt)
const;
425 bool skipIgnoredRow(std::vector<IndexType>::const_iterator& matrixColumnIt,
typename std::vector<ValueType>::const_iterator& matrixValueIt)
const;
430 uint64_t skipMultipleIgnoredRows(std::vector<IndexType>::const_iterator& matrixColumnIt,
431 typename std::vector<ValueType>::const_iterator& matrixValueIt)
const;
436 std::vector<ValueType> matrixValues;
442 std::vector<IndexType> matrixColumns;
447 std::vector<IndexType>
const* rowGroupIndices;
452 bool backwards{
true};
457 bool hasSkippedRows{
false};
462 std::vector<SolutionType> auxiliaryVector;
467 bool auxiliaryVectorUsedExternally{
false};
471 template<
typename ApplyValueType,
typename Dummy>
472 struct ApplyCache {};
474 template<
typename Dummy>
475 struct ApplyCache<storm::
Interval, Dummy> {
476 mutable std::vector<std::pair<SolutionType, std::pair<SolutionType, uint64_t>>> robustOrder;
477 storage::BitVector hasOnlyConstants;
480 template<
typename Dummy>
482 mutable std::vector<std::pair<SolutionType, std::pair<SolutionType, uint64_t>>> robustOrder;
483 storage::BitVector hasOnlyConstants;
489 ApplyCache<ValueType, int> applyCache;
494 static constexpr IndexType StartOfRowIndicator = 1ull << 63;
499 static constexpr IndexType StartOfRowGroupIndicator = StartOfRowIndicator + (1ull << 62);
504 static constexpr IndexType SkipNumEntriesMask = ~StartOfRowGroupIndicator;