@@ -23,7 +23,10 @@ async def get_optimal_decisions_for_project_from_dtos(
2323 discrete_utilities : list [DiscreteUtilityOutgoingDto ] = [],
2424 solver_service : SolverService = Depends (get_solver_service ),
2525) -> SolutionDto :
26- return await solver_service .find_optimal_decision_pyagrum_from_dtos (issues , edges , discrete_probabilities , discrete_utilities )
26+ return await solver_service .find_optimal_decision_pyagrum_from_dtos (
27+ issues , edges , discrete_probabilities , discrete_utilities
28+ )
29+
2730
2831@router .post ("/solvers/project/{project_id}/with_evidence" )
2932async def get_optimal_decisions_for_project_with_evidence (
@@ -35,29 +38,37 @@ async def get_optimal_decisions_for_project_with_evidence(
3538 solver_service : SolverService = Depends (get_solver_service ),
3639) -> list [EvidenceOutgoingDto ]:
3740 evidence_state_ids = [e .state_ids for e in evidence ]
38- results : list [Optional [float ]] = await solver_service .get_MEU_given_evidence (issues , edges , discrete_probabilities , discrete_utilities , evidence_state_ids )
41+ results : list [Optional [float ]] = await solver_service .get_MEU_given_evidence (
42+ issues , edges , discrete_probabilities , discrete_utilities , evidence_state_ids
43+ )
3944 # 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
4045 populated_evidence = [
4146 EvidenceOutgoingDto (
4247 evidence_id = evi .evidence_id ,
4348 state_ids = evi .state_ids ,
44- expected_utility = results [n ]
45- if len (results ) > n and not math .isnan (results [n ]) # type: ignore
46- else None ,
49+ expected_utility = (
50+ results [n ]
51+ if len (results ) > n and not math .isnan (results [n ]) # type: ignore
52+ else None
53+ ),
4754 )
4855 for n , evi in enumerate (evidence )
4956 ]
5057 exception_message = ""
5158 for n , populated in enumerate (populated_evidence ):
5259 if n == 0 and populated .expected_utility is not None and populated .expected_utility < - 1e10 :
53- exception_message += f"Impossible state reached due to all possible paths being restricted"
54-
60+ exception_message += (
61+ "Impossible state reached due to all possible paths being restricted"
62+ )
63+
5564 if populated .expected_utility is None :
5665 exception_message += f"Impossible state reached for evidence { populated .evidence_id } with state_ids { populated .state_ids } \n "
57- # If any of the evidence leads to an impossible state, we raise an exception with the details of which evidence caused the issue.
66+ # If any of the evidence leads to an impossible state, we raise an exception with the details of which evidence caused the issue.
5867 if exception_message :
59- raise ValueError (f"Restrictions/Evidence states lead to an impossible state:\n { exception_message } " )
60-
68+ raise ValueError (
69+ f"Restrictions/Evidence states lead to an impossible state:\n { exception_message } "
70+ )
71+
6172 return populated_evidence
6273
6374
@@ -91,7 +102,8 @@ async def get_optimal_decisions_for_project_as_tree_tmp_from_dtos(
91102 return await solver_service .get_decision_tree_for_optimal_decisions_from_dtos (
92103 project_id , issues , edges , discrete_probabilities , discrete_utilities
93104 )
94-
105+
106+
95107@router .post ("/solvers/project/{project_id}/partial_decision_tree/v3" )
96108async def get_optimal_decisions_for_project_as_tree_tmp_from_dtos_v3 (
97109 project_id : uuid .UUID ,
@@ -105,6 +117,32 @@ async def get_optimal_decisions_for_project_as_tree_tmp_from_dtos_v3(
105117):
106118 async with lock_manager .acquire_project_lock (project_id ):
107119 return await solver_service .get_decision_tree_for_optimal_decisions_from_dtos_by_constructing_paths (
108- project_id , issues , edges , discrete_probabilities , discrete_utilities , paths ,
120+ project_id ,
121+ issues ,
122+ edges ,
123+ discrete_probabilities ,
124+ discrete_utilities ,
125+ paths ,
109126 )
110127
128+
129+ @router .post ("/solvers/project/{project_id}/policy_table" )
130+ async def get_policy_table_for_project (
131+ project_id : uuid .UUID ,
132+ issues : list [IssueOutgoingDto ],
133+ edges : list [EdgeOutgoingDto ],
134+ discrete_probabilities : list [DiscreteProbabilityOutgoingDto ] = [],
135+ discrete_utilities : list [DiscreteUtilityOutgoingDto ] = [],
136+ evidence : Optional [EvidenceIncomingDto ] = None ,
137+ solver_service : SolverService = Depends (get_solver_service ),
138+ lock_manager : ProjectQueueManager = Depends (get_project_lock_manager ),
139+ ) -> dict [str , list [dict [str , str | float ]]]:
140+ async with lock_manager .acquire_project_lock (project_id ):
141+ evidence_state_ids = evidence .state_ids if evidence else None
142+ return await solver_service .get_policy_table (
143+ issues = issues ,
144+ edges = edges ,
145+ discrete_probabilities = discrete_probabilities ,
146+ discrete_utilities = discrete_utilities ,
147+ evidence = evidence_state_ids ,
148+ )
0 commit comments