Skip to content

Commit ab1c7aa

Browse files
Fix/raise exception on impossible state (#370)
* fix: raise exception for impossible evidence states in decision-making * fix: handle NaN values in expected utility calculation for evidence
1 parent f660b1f commit ab1c7aa

1 file changed

Lines changed: 15 additions & 2 deletions

File tree

PrismaFastApi/src/routes/solver_routes.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import uuid
2+
import math
23
from fastapi import APIRouter, Depends
34
from src.project_lock_manager import ProjectQueueManager
45
from src.services.solver_service import SolverService
@@ -29,14 +30,26 @@ async def get_optimal_decisions_for_project_with_evidence(
2930
evidence_state_ids = [e.state_ids for e in evidence]
3031
results: list[SolutionDto] = await solver_service.find_optimal_decision_pyagrum_from_with_evidence(issues, edges, evidence_state_ids)
3132
# decision_solutions[0].mean is the expected utility for the first optimal decision, i.e. the root node which represents the expected utility for the model
32-
return [
33+
populated_evidence = [
3334
EvidenceOutgoingDto(
3435
evidence_id=evi.evidence_id,
3536
state_ids=evi.state_ids,
36-
expected_utility=results[n].decision_solutions[0].mean if results[n].decision_solutions else None,
37+
expected_utility=results[n].decision_solutions[0].mean
38+
if results[n].decision_solutions
39+
and not math.isnan(results[n].decision_solutions[0].mean)
40+
else None,
3741
)
3842
for n, evi in enumerate(evidence)
3943
]
44+
exception_message = ""
45+
for populated in populated_evidence:
46+
if populated.expected_utility is None:
47+
exception_message += f"Impossible state reached for evidence {populated.evidence_id} with state_ids {populated.state_ids}\n"
48+
# If any of the evidence leads to an impossible state, we raise an exception with the details of which evidence caused the issue.
49+
if exception_message:
50+
raise ValueError(f"One or more evidence states lead to an impossible state:\n{exception_message}")
51+
52+
return populated_evidence
4053

4154

4255
@router.get("/solvers/project/{project_id}/decision_tree/v2")

0 commit comments

Comments
 (0)