36 std::optional<std::unordered_map<uint64_t, ObservationSignature>> optionalIdToObservationSignature = std::nullopt;
37 std::optional<std::unordered_map<uint64_t, std::unordered_map<uint64_t, std::string>>> optionalIdToActionNameMap = std::nullopt;
39 if (pomdp.hasObservationValuations() && pomdp.getObservationValuations().getNumberOfEntities() >= pomdp.getNrObservations()) {
40 auto const& obsValuations = pomdp.getObservationValuations();
41 std::unordered_map<uint64_t, ObservationSignature> idToObservationSignatureMap;
42 std::vector<ObservationSignature> usedObservationSignatures;
43 bool observationSignaturesAreUnique =
true;
44 for (uint64_t i = 0; i < pomdp.getNrObservations(); ++i) {
46 if (observationSignature.values.empty() || std::ranges::find(usedObservationSignatures, observationSignature) != usedObservationSignatures.end()) {
47 observationSignaturesAreUnique =
false;
50 usedObservationSignatures.push_back(observationSignature);
51 idToObservationSignatureMap[i] = std::move(observationSignature);
53 if (observationSignaturesAreUnique) {
54 optionalIdToObservationSignature = std::move(idToObservationSignatureMap);
56 }
else if (pomdp.hasObservationValuations()) {
57 STORM_LOG_WARN(
"Omitting observation signatures from the exported FSC because observation valuations cover only "
58 << pomdp.getObservationValuations().getNumberOfEntities() <<
" of " << pomdp.getNrObservations()
59 <<
" observations. Observation matching will use numeric IDs.");
61 if (pomdp.hasChoiceLabeling()) {
62 auto const& choiceLabeling = pomdp.getChoiceLabeling();
63 std::unordered_map<uint64_t, std::unordered_map<uint64_t, std::string>> idToActionNameMap;
64 auto const nrStates = pomdp.getNumberOfStates();
65 std::vector<uint64_t> representativeStates(pomdp.getNrObservations(), nrStates);
66 for (uint64_t state = 0; state < nrStates; ++state) {
67 auto& representativeState = representativeStates[pomdp.getObservation(state)];
68 if (representativeState == nrStates) {
69 representativeState = state;
72 for (uint64_t obs = 0; obs < pomdp.getNrObservations(); ++obs) {
73 auto const representativeState = representativeStates[obs];
74 if (representativeState == nrStates) {
77 auto const rowGroupIndex = pomdp.getTransitionMatrix().getRowGroupIndices()[representativeState];
78 auto const rowGroupSize = pomdp.getTransitionMatrix().getRowGroupSize(representativeState);
79 for (uint64_t localChoice = 0; localChoice < rowGroupSize; ++localChoice) {
80 auto const globalChoiceIndex = rowGroupIndex + localChoice;
81 STORM_LOG_ASSERT(choiceLabeling.getLabelsOfChoice(globalChoiceIndex).size() <= 1,
82 "Expected choice labeling to have at most one label per choice, but choice ID "
83 << globalChoiceIndex <<
" has " << choiceLabeling.getLabelsOfChoice(globalChoiceIndex).size() <<
" labels.");
84 if (choiceLabeling.getLabelsOfChoice(globalChoiceIndex).empty()) {
85 idToActionNameMap[obs][localChoice] =
"";
87 idToActionNameMap[obs][localChoice] = *choiceLabeling.getLabelsOfChoice(globalChoiceIndex).begin();
91 optionalIdToActionNameMap = idToActionNameMap;
95 std::unordered_map<uint64_t, uint64_t> beliefStateToFscNodeMap;
96 std::unordered_map<uint64_t, uint64_t> cutoffPolicyToFscNodeMap;
97 using ObservationToActionMap = std::unordered_map<uint64_t, storm::storage::Distribution<PolicyValueType, uint64_t>>;
98 std::unordered_map<uint64_t, ObservationToActionMap> cutoffPolicyToObservationMap;
99 std::queue<uint64_t> statesToProcess;
100 uint64_t nextId = 1ul;
101 auto const addCutoffTransition = [&](uint64_t originNode, uint64_t cutoffBeliefState) {
103 auto const localCutoffChoice = beliefMdpScheduler.getChoice(cutoffBeliefState).getDeterministicChoice();
104 auto const globalCutoffChoice = beliefMdp.getTransitionMatrix().getRowGroupIndices()[cutoffBeliefState] + localCutoffChoice;
105 auto const& cutoffChoiceLabels = beliefMdp.getChoiceLabeling().getLabelsOfChoice(globalCutoffChoice);
106 STORM_LOG_THROW(cutoffChoiceLabels.size() == 1, storm::exceptions::UnexpectedException,
107 "Expected the selected cutoff choice to have exactly one scheduler label.");
108 std::string
const& cutoffChoiceLabel = *cutoffChoiceLabels.begin();
109 constexpr std::string_view schedulerLabelPrefix =
"__sched_";
110 STORM_LOG_THROW(cutoffChoiceLabel.starts_with(schedulerLabelPrefix), storm::exceptions::UnexpectedException,
111 "Expected cutoff scheduler label, but found '" << cutoffChoiceLabel <<
"'.");
112 uint64_t chosenCutoffPolicy = 0;
113 auto const schedulerIndex = std::string_view(cutoffChoiceLabel).substr(schedulerLabelPrefix.size());
114 auto const [parseEnd, parseError] = std::from_chars(schedulerIndex.begin(), schedulerIndex.end(), chosenCutoffPolicy);
115 STORM_LOG_THROW(parseError == std::errc() && parseEnd == schedulerIndex.end(), storm::exceptions::UnexpectedException,
116 "Could not parse cutoff scheduler label '" << cutoffChoiceLabel <<
"'.");
117 STORM_LOG_THROW(pomdpApproximationSchedulers.has_value() && chosenCutoffPolicy < pomdpApproximationSchedulers->size(),
118 storm::exceptions::UnexpectedException,
119 "Cutoff scheduler label '" << cutoffChoiceLabel <<
"' does not refer to an available approximation scheduler.");
120 if (!cutoffPolicyToFscNodeMap.contains(chosenCutoffPolicy)) {
122 cutoffPolicyToFscNodeMap[chosenCutoffPolicy] = nextId;
123 cutoffPolicyToObservationMap.emplace(chosenCutoffPolicy,
124 pomdpSchedulerToObservationBasedMap(pomdpApproximationSchedulers->at(chosenCutoffPolicy), 0));
127 auto const& dist = cutoffPolicyToObservationMap.at(chosenCutoffPolicy).at(beliefStateToObservationMap.at(cutoffBeliefState));
128 if (dist.size() == 1) {
130 cutoffPolicyToFscNodeMap[chosenCutoffPolicy]);
133 cutoffPolicyToFscNodeMap[chosenCutoffPolicy]);
138 auto const initialBeliefState = beliefMdp.getInitialStates().getNextSetIndex(0);
139 if (beliefMdp.getStateLabeling().getStateHasLabel(
"truncated", initialBeliefState)) {
140 addCutoffTransition(0ul, initialBeliefState);
142 auto const initialChoice = beliefMdpScheduler.getChoice(initialBeliefState).getDeterministicChoice();
144 beliefStateToFscNodeMap[initialBeliefState] = nextId++;
145 statesToProcess.push(initialBeliefState);
148 while (!statesToProcess.empty()) {
149 uint64_t currentState = statesToProcess.front();
150 statesToProcess.pop();
151 auto const currentChoice = beliefMdpScheduler.getChoice(currentState).getDeterministicChoice();
152 for (
auto const& entry : beliefMdp.getTransitionMatrix().getRow(currentState, currentChoice)) {
153 auto successorBeliefStateId = entry.getColumn();
154 if (beliefMdp.getStateLabeling().getStateHasLabel(
"truncated", successorBeliefStateId)) {
155 addCutoffTransition(beliefStateToFscNodeMap.at(currentState), successorBeliefStateId);
157 if (!beliefStateToFscNodeMap.contains(successorBeliefStateId)) {
158 beliefStateToFscNodeMap[successorBeliefStateId] = nextId;
160 statesToProcess.push(successorBeliefStateId);
162 if (beliefStateToObservationMap.contains(successorBeliefStateId)) {
163 auto const successorChoice = beliefMdpScheduler.getChoice(successorBeliefStateId).getDeterministicChoice();
165 successorChoice, beliefStateToFscNodeMap[successorBeliefStateId]);
171 if (pomdpApproximationSchedulers.has_value()) {
172 for (uint64_t i = 0; i < pomdpApproximationSchedulers->size(); ++i) {
174 if (cutoffPolicyToFscNodeMap.contains(i)) {
175 for (
auto const& [observationId, actionDist] : cutoffPolicyToObservationMap.at(i)) {
176 if (actionDist.size() == 1) {
178 cutoffPolicyToFscNodeMap[i]);