Esempio n. 1
0
    def claim_new_active_policy_for_player(
        self, player
    ) -> Union[Tuple[Dict[int, StrategySpec], Dict[int, List[StrategySpec]],
                     int], Tuple[None, None, None]]:
        request = NXDOPlayer(player=player)
        response: NXDONewBestResponseParams = self._stub.ClaimNewActivePolicyForPlayer(
            request)

        if response.policy_num == -1:
            return None, None, None

        assert len(response.metanash_specs_for_players.policy_spec_list) in [
            self.n_players(), 0
        ]
        assert len(
            response.delegate_specs_for_players) in [self.n_players(), 0]

        metanash_json_specs_for_other_players = [
            elem.policy_spec_json
            for elem in response.metanash_specs_for_players.policy_spec_list
        ]

        metanash_specs_for_players = {
            player: StrategySpec.from_json(json_spec)
            for player, json_spec in enumerate(
                metanash_json_specs_for_other_players)
        }

        delegate_json_spec_lists_for_other_players = [[
            elem.policy_spec_json
            for elem in player_delegate_list.policy_spec_list
        ] for player_delegate_list in response.delegate_specs_for_players]
        delegate_specs_for_players = {
            player: [
                StrategySpec.from_json(json_spec)
                for json_spec in player_delegate_json_list
            ]
            for player, player_delegate_json_list in enumerate(
                delegate_json_spec_lists_for_other_players)
        }

        if len(metanash_specs_for_players) == 0:
            metanash_specs_for_players = None

        if len(delegate_specs_for_players) == 0:
            delegate_specs_for_players = None

        return (metanash_specs_for_players, delegate_specs_for_players,
                response.policy_num)
Esempio n. 2
0
 def RequestExternalEval(self, request: EvalRequest, context):
     policy_specs_for_each_player = tuple(
         StrategySpec.from_json(json_string=json_string)
         for json_string in request.json_policy_specs_for_each_player)
     self._manager.request_external_eval(
         policy_specs_for_each_player=policy_specs_for_each_player)
     return Confirmation(result=True)
Esempio n. 3
0
 def take_eval_job(self) -> (Union[None, Tuple[StrategySpec]], int):
     response: EvalJob = self._stub.TakeEvalJob(Empty())
     policy_specs_for_each_player = tuple(
         StrategySpec.from_json(json_string=json_string)
         for json_string in response.json_policy_specs_for_each_player)
     if len(policy_specs_for_each_player) == 0:
         return None, response.required_games_to_play
     return policy_specs_for_each_player, response.required_games_to_play
Esempio n. 4
0
 def SubmitEvalJobResult(self, request: EvalJobResult, context):
     policy_specs_for_each_player = tuple(
         StrategySpec.from_json(json_string=json_string)
         for json_string in request.json_policy_specs_for_each_player)
     self._eval_dispatcher.submit_eval_job_result(
         policy_specs_for_each_player_tuple=policy_specs_for_each_player,
         payoffs_for_each_player=request.payoffs_for_each_player,
         games_played=request.games_played)
     return EvalConfirmation(result=True)
Esempio n. 5
0
 def SubmitEmpiricalPayoffResult(self, request: PayoffResult, context):
     policy_specs_for_each_player = tuple(
         StrategySpec.from_json(json_string=json_string)
         for json_string in request.json_policy_specs_for_each_player)
     # noinspection PyTypeChecker
     self._manager.submit_empirical_payoff_result(
         policy_specs_for_each_player=policy_specs_for_each_player,
         payoffs_for_each_player=request.payoffs_for_each_player,
         games_played=request.games_played,
         override_all_previous_results=request.override_all_previous_results
     )
     return Confirmation(result=True)
Esempio n. 6
0
 def claim_new_active_policy_for_player(
         self, player, new_policy_metadata_dict) -> StrategySpec:
     try:
         metadata_json = json.dumps(obj=new_policy_metadata_dict)
     except (TypeError, OverflowError) as json_err:
         raise ValueError(
             f"new_policy_metadata_dict must be JSON serializable."
             f"When attempting to serialize, got this error:\n{json_err}")
     request = NewActivePolicyRequest(player=player,
                                      metadata_json=metadata_json)
     response: PolicySpecJson = self._stub.ClaimNewActivePolicyForPlayer(
         request)
     return StrategySpec.from_json(response.policy_spec_json)
Esempio n. 7
0
 def set_active_policy_as_fixed(self, player, policy_num,
                                final_metadata_dict) -> StrategySpec:
     try:
         metadata_json = json.dumps(obj=final_metadata_dict,
                                    cls=SafeFallbackJSONEncoder)
     except (TypeError, OverflowError) as json_err:
         raise ValueError(
             f"final_metadata_dict must be JSON serializable."
             f"When attempting to serialize, got this error:\n{json_err}")
     request = PolicyMetadataRequest(player=player,
                                     policy_num=policy_num,
                                     metadata_json=metadata_json)
     response: PolicySpecJson = self._stub.SetActivePolicyAsFixed(request)
     return StrategySpec.from_json(response.policy_spec_json)
Esempio n. 8
0
 def submit_new_active_policy_metadata(self, player, policy_num,
                                       metadata_dict) -> StrategySpec:
     try:
         metadata_json = json.dumps(obj=metadata_dict)
     except (TypeError, OverflowError) as json_err:
         raise ValueError(
             f"metadata_dict must be JSON serializable."
             f"When attempting to serialize, got this error:\n{json_err}")
     request = PolicyMetadataRequest(player=player,
                                     policy_num=policy_num,
                                     metadata_json=metadata_json)
     response: PolicySpecJson = self._stub.SubmitNewActivePolicyMetadata(
         request)
     return StrategySpec.from_json(response.policy_spec_json)