@@ -18,7 +18,8 @@ def explicit_umb_to_explicit_ats(umb: umbi.umb.ExplicitUmb) -> ExplicitAts:
1818 ats = ExplicitAts ()
1919
2020 ## index
21- # skip format_version, format_revision and file_data
21+ # strip format_version, format_revision and file_data
22+ # load umb.index.model_data
2223 md = umb .index .model_data
2324 if md is not None :
2425 ats .model_info = ModelInfo (
@@ -30,33 +31,51 @@ def explicit_umb_to_explicit_ats(umb: umbi.umb.ExplicitUmb) -> ExplicitAts:
3031 doi = md .doi ,
3132 url = md .url ,
3233 )
34+
3335 # load index.transition_system
3436 ts = umb .index .transition_system
3537 ats .time = TimeType (ts .time )
3638 ats .num_players = ts .num_players
37- ats .num_states = ts .num_states
3839 ats .num_initial_states = ts .num_initial_states
39- ats .num_choices = ts .num_choices
4040 ats .num_choice_actions = ts .num_choice_actions
41- ats .num_branches = ts .num_branches
4241 ats .num_branch_actions = ts .num_branch_actions
4342 ats .player_to_name = ts .player_to_name
4443
4544 ## values
4645 ats .state_is_initial = umb .state_is_initial
47- ats .state_to_choice = umb .state_to_choices
4846 ats .state_to_player = umb .state_to_player
4947
5048 ats .state_is_markovian = umb .state_is_markovian
5149 ats .state_to_exit_rate = umb .state_to_exit_rate
5250
53- ats .choice_to_branches = umb .choice_to_branches
54- ats .branch_to_target = umb .branch_to_target
55- ats .branch_to_probability = umb .branch_to_probability
51+ for state_index in range (ts .num_states ):
52+ state = ats .add_state ()
53+ assert state == state_index
54+
55+ state_to_choices = (
56+ umb .state_to_choices if umb .state_to_choices is not None else [s for s in range (ats .num_states + 1 )]
57+ )
58+ if ts .num_choices > 0 :
59+ for state in range (ats .num_states ):
60+ for choice_idx in range (state_to_choices [state ], state_to_choices [state + 1 ]):
61+ choice = ats .add_state_choice (state = state )
62+ if ts .num_branches > 0 :
63+ assert umb .choice_to_branches is not None , "num_branches > 0 but choice_to_branches is None"
64+ for branch_idx in range (umb .choice_to_branches [choice_idx ], umb .choice_to_branches [choice_idx + 1 ]):
65+ assert umb .branch_to_target is not None , "num_branches > 0 but branch_to_target is None"
66+ target = umb .branch_to_target [branch_idx ]
67+ prob = umb .branch_to_probability [branch_idx ] if umb .branch_to_probability is not None else None
68+ branch_action = (
69+ umb .branch_to_branch_action [branch_idx ] if umb .branch_to_branch_action is not None else None
70+ )
71+ choice .add_branch (target = target , prob = prob , action = branch_action )
72+ if ts .num_choice_actions > 0 :
73+ assert umb .choice_to_choice_action is not None , (
74+ "num_choice_actions > 0 but choice_to_choice_action is None"
75+ )
76+ choice .action = umb .choice_to_choice_action [choice_idx ]
5677
57- ats .choice_to_choice_action = umb .choice_to_choice_action
5878 ats .choice_action_to_name = umb .choice_action_to_string
59- ats .branch_to_branch_action = umb .branch_to_branch_action
6079 ats .branch_action_to_name = umb .branch_action_to_string
6180
6281 # load annotations
@@ -167,7 +186,6 @@ def explicit_ats_to_explicit_umb(ats: ExplicitAts) -> umbi.umb.ExplicitUmb:
167186 # warning: this does not copy the values, but directly uses the lists from ats.
168187 # this is fine as long as we don't modify them
169188 umb .state_is_initial = ats .state_is_initial
170- umb .state_to_choices = ats .state_to_choice
171189 umb .state_to_player = ats .state_to_player
172190
173191 umb .state_is_markovian = ats .state_is_markovian
@@ -178,19 +196,40 @@ def explicit_ats_to_explicit_umb(ats: ExplicitAts) -> umbi.umb.ExplicitUmb:
178196 umb .index .transition_system .exit_rate_type = SizedType .for_type (target_type )
179197 umb .state_to_exit_rate = vector # type: ignore
180198
181- umb .choice_to_branches = ats .choice_to_branches
182- umb .branch_to_target = ats .branch_to_target
183-
184- if ats .branch_to_probability is not None :
185- # promote
186- target_type , vector = umbi .datatypes .promote_scalars (ats .branch_to_probability )
187- assert isinstance (target_type , umbi .datatypes .NumericType ), "branch probabilities must be numeric"
188- umb .index .transition_system .branch_probability_type = SizedType .for_type (target_type )
189- umb .branch_to_probability = vector # type: ignore
199+ umb .state_to_choices = []
200+ num_choices = 0
201+ for state in range (ats .num_states ):
202+ umb .state_to_choices .append (num_choices )
203+ num_choices += ats .num_state_choices (state )
204+ umb .state_to_choices .append (num_choices )
205+
206+ if ats .num_choices > 0 :
207+ if ats .num_choice_actions > 0 :
208+ umb .choice_to_choice_action = [choice .action for choice in ats .choices ] # type: ignore
209+
210+ branches = []
211+ umb .choice_to_branches = []
212+ for choice in ats .choices :
213+ umb .choice_to_branches .append (len (branches ))
214+ branches .extend (choice .branches )
215+ umb .choice_to_branches .append (len (branches ))
216+ if len (branches ) > 0 :
217+ umb .branch_to_target = [branch .target for branch in branches ]
218+ if any (branch .prob is not None for branch in branches ):
219+ # has branch probabilities
220+ branch_to_probability = [branch .prob if branch .prob is not None else 1 for branch in branches ]
221+ # promote
222+ target_type , vector = umbi .datatypes .promote_scalars (branch_to_probability )
223+ # assert isinstance(target_type, umbi.datatypes.NumericType), "branch probabilities must be numeric"
224+ umb .index .transition_system .branch_probability_type = SizedType .for_type (target_type )
225+ umb .branch_to_probability = vector # type: ignore
226+ if ats .num_branch_actions > 0 :
227+ branch_actions = [branch .action for branch in branches ]
228+ if any (action is None for action in branch_actions ):
229+ raise ValueError ("if num_branch_actions > 0, all branches must have an action" )
230+ umb .branch_to_branch_action = branch_actions # type: ignore
190231
191- umb .choice_to_choice_action = ats .choice_to_choice_action
192232 umb .choice_action_to_string = ats .choice_action_to_name
193- umb .branch_to_branch_action = ats .branch_to_branch_action
194233 umb .branch_action_to_string = ats .branch_action_to_name
195234
196235 # add annotations
0 commit comments