|
1 | 1 | import uuid |
| 2 | +import math |
2 | 3 | from fastapi import APIRouter, Depends |
3 | 4 | from src.project_lock_manager import ProjectQueueManager |
4 | 5 | from src.services.solver_service import SolverService |
@@ -29,14 +30,26 @@ async def get_optimal_decisions_for_project_with_evidence( |
29 | 30 | evidence_state_ids = [e.state_ids for e in evidence] |
30 | 31 | results: list[SolutionDto] = await solver_service.find_optimal_decision_pyagrum_from_with_evidence(issues, edges, evidence_state_ids) |
31 | 32 | # 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 = [ |
33 | 34 | EvidenceOutgoingDto( |
34 | 35 | evidence_id=evi.evidence_id, |
35 | 36 | 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, |
37 | 41 | ) |
38 | 42 | for n, evi in enumerate(evidence) |
39 | 43 | ] |
| 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 |
40 | 53 |
|
41 | 54 |
|
42 | 55 | @router.get("/solvers/project/{project_id}/decision_tree/v2") |
|
0 commit comments