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&, std::pair<std::vector<OffT1>
const*, OffT2>
const& offsets,
240 uint64_t offsetIndex)
const {
241 return {(*offsets.first)[offsetIndex], offsets.second};
244 template<OptimizationDirection RobustDirection,
typename OpT,
typename OffT>
245 OpT robustInitializeRowRes(std::vector<OpT>
const&, OffT
const& offsets, uint64_t offsetIndex)
const {
246 if constexpr (RobustDirection == OptimizationDirection::Maximize) {
247 return offsets.upper();
249 return offsets.lower();
253 template<OptimizationDirection RobustDirection,
typename OpT,
typename OffT>
254 OpT robustInitializeRowRes(std::vector<OpT>
const&, std::vector<OffT>
const& offsets, uint64_t offsetIndex)
const {
255 if constexpr (RobustDirection == OptimizationDirection::Maximize) {
256 return offsets[offsetIndex].upper();
258 return offsets[offsetIndex].lower();
262 template<OptimizationDirection RobustDirection,
typename OpT1,
typename OpT2,
typename OffT>
263 std::pair<OpT1, OpT2> robustInitializeRowRes(std::pair<std::vector<OpT1>, std::vector<OpT2>>
const&, std::vector<OffT>
const& offsets,
264 uint64_t offsetIndex)
const {
265 STORM_LOG_THROW(
false, storm::exceptions::NotImplementedException,
"Value Iteration is not implemented with pairs and interval-models.");
267 return {offsets[offsetIndex].upper(), offsets[offsetIndex].upper()};
270 template<OptimizationDirection RobustDirection,
typename OpT1,
typename OpT2,
typename OffT1,
typename OffT2>
271 std::pair<OpT1, OpT2> robustInitializeRowRes(std::pair<std::vector<OpT1>, std::vector<OpT2>>
const&,
272 std::pair<std::vector<OffT1>
const*, OffT2>
const& offsets, uint64_t offsetIndex)
const {
273 STORM_LOG_THROW(
false, storm::exceptions::NotImplementedException,
"Value Iteration is not implemented with pairs and interval-models.");
275 return {(*offsets.first)[offsetIndex], offsets.second};
281 template<OptimizationDirection RobustDirection,
typename OperandType,
typename OffsetType>
282 auto applyRow(std::vector<IndexType>::const_iterator& matrixColumnIt,
typename std::vector<ValueType>::const_iterator& matrixValueIt,
283 OperandType
const& operand, OffsetType
const& offsets, uint64_t offsetIndex)
const {
285 return applyRowRobust<RobustDirection>(matrixColumnIt, matrixValueIt, operand, offsets, offsetIndex);
287 return applyRowStandard(matrixColumnIt, matrixValueIt, operand, offsets, offsetIndex);
291 template<
typename OperandType,
typename OffsetType>
292 auto applyRowStandard(std::vector<IndexType>::const_iterator& matrixColumnIt,
typename std::vector<ValueType>::const_iterator& matrixValueIt,
293 OperandType
const& operand, OffsetType
const& offsets, uint64_t offsetIndex)
const {
294 STORM_LOG_ASSERT(*matrixColumnIt >= StartOfRowIndicator,
"VI Operator in invalid state.");
295 auto result{initializeRowRes(operand, offsets, offsetIndex)};
296 for (++matrixColumnIt; *matrixColumnIt < StartOfRowIndicator; ++matrixColumnIt, ++matrixValueIt) {
297 if constexpr (isPair<OperandType>::value) {
298 result.first += operand.first[*matrixColumnIt] * (*matrixValueIt);
299 result.second += operand.second[*matrixColumnIt] * (*matrixValueIt);
301 result += operand[*matrixColumnIt] * (*matrixValueIt);
308 template<OptimizationDirection RobustDirection>
310 bool operator()(
const std::pair<SolutionType, std::pair<SolutionType, uint64_t>>& a,
311 const std::pair<SolutionType, std::pair<SolutionType, uint64_t>>& b)
const {
312 if constexpr (RobustDirection == OptimizationDirection::Maximize) {
313 return a.first > b.first;
315 return a.first < b.first;
320 template<OptimizationDirection RobustDirection,
typename OperandType,
typename OffsetType>
321 auto applyRowRobust(std::vector<IndexType>::const_iterator& matrixColumnIt,
typename std::vector<ValueType>::const_iterator& matrixValueIt,
322 OperandType
const& operand, OffsetType
const& offsets, uint64_t offsetIndex)
const {
323 STORM_LOG_ASSERT(*matrixColumnIt >= StartOfRowIndicator,
"VI Operator in invalid state.");
324 SolutionType result{robustInitializeRowRes<RobustDirection>(operand, offsets, offsetIndex)};
326 applyCache.robustOrder.clear();
328 if (applyCache.hasOnlyConstants.size() > 0 && applyCache.hasOnlyConstants.get(offsetIndex)) {
329 for (++matrixColumnIt; *matrixColumnIt < StartOfRowIndicator; ++matrixColumnIt, ++matrixValueIt) {
330 SolutionType
const lower = matrixValueIt->lower();
331 if constexpr (isPair<OperandType>::value) {
332 STORM_LOG_THROW(
false, storm::exceptions::NotImplementedException,
"Value Iteration is not implemented with pairs and interval-models.");
335 result += operand[*matrixColumnIt] * lower;
342 uint64_t orderCounter = 0;
343 for (++matrixColumnIt; *matrixColumnIt < StartOfRowIndicator; ++matrixColumnIt, ++matrixValueIt, ++orderCounter) {
344 SolutionType
const lower = matrixValueIt->lower();
345 if constexpr (isPair<OperandType>::value) {
346 STORM_LOG_THROW(
false, storm::exceptions::NotImplementedException,
"Value Iteration is not implemented with pairs and interval-models.");
349 result += operand[*matrixColumnIt] * lower;
351 remainingValue -= lower;
352 SolutionType
const diameter = matrixValueIt->upper() - lower;
354 applyCache.robustOrder.emplace_back(operand[*matrixColumnIt], std::make_pair(diameter, orderCounter));
361 static AuxCompare<RobustDirection> cmp;
362 std::sort(applyCache.robustOrder.begin(), applyCache.robustOrder.end(), cmp);
364 for (
auto const& pair : applyCache.robustOrder) {
365 SolutionType availableMass = std::min(pair.second.first, remainingValue);
366 result += availableMass * pair.first;
367 remainingValue -= availableMass;
375 "Remaining value should be zero (all prob mass taken) or it should be a sad state, but is " << remainingValue);
380 template<
bool Backward>
382 if constexpr (Backward) {
383 return boost::adaptors::reverse(boost::irange(start, end));
385 return boost::irange(start, end);
390 uint64_t getSize(std::vector<T>
const& vec)
const {
394 template<
typename T1,
typename T2>
395 uint64_t getSize(std::pair<T1, T2>
const& pairOfVec)
const {
396 return pairOfVec.first.size();
400 struct isPair : std::false_type {};
402 template<
typename T1,
typename T2>
403 struct isPair<std::pair<T1, T2>> : std::true_type {};
408 template<
bool Backward = true>
414 void moveToEndOfRow(std::vector<IndexType>::iterator& matrixColumnIt)
const;
419 bool skipIgnoredRow(std::vector<IndexType>::const_iterator& matrixColumnIt,
typename std::vector<ValueType>::const_iterator& matrixValueIt)
const;
424 uint64_t skipMultipleIgnoredRows(std::vector<IndexType>::const_iterator& matrixColumnIt,
425 typename std::vector<ValueType>::const_iterator& matrixValueIt)
const;
430 std::vector<ValueType> matrixValues;
436 std::vector<IndexType> matrixColumns;
441 std::vector<IndexType>
const* rowGroupIndices;
446 bool backwards{
true};
451 bool hasSkippedRows{
false};
456 std::vector<SolutionType> auxiliaryVector;
461 bool auxiliaryVectorUsedExternally{
false};
465 template<
typename ApplyValueType,
typename Dummy>
466 struct ApplyCache {};
468 template<
typename Dummy>
469 struct ApplyCache<storm::
Interval, Dummy> {
470 mutable std::vector<std::pair<SolutionType, std::pair<SolutionType, uint64_t>>> robustOrder;
471 storage::BitVector hasOnlyConstants;
474 template<
typename Dummy>
476 mutable std::vector<std::pair<SolutionType, std::pair<SolutionType, uint64_t>>> robustOrder;
477 storage::BitVector hasOnlyConstants;
483 ApplyCache<ValueType, int> applyCache;
488 static constexpr IndexType StartOfRowIndicator = 1ull << 63;
493 static constexpr IndexType StartOfRowGroupIndicator = StartOfRowIndicator + (1ull << 62);
498 static constexpr IndexType SkipNumEntriesMask = ~StartOfRowGroupIndicator;