From 0ff58b8fbf0160fb411f16e8445f7e23ac2476cf Mon Sep 17 00:00:00 2001 From: steveyuwono Date: Thu, 7 Dec 2023 15:50:31 +0100 Subject: [PATCH] update GTFunction #758 --- src/mlpro/gt/native/basics.py | 7 ++- .../pool/native/games/prisonersdilemma_2p.py | 3 +- .../pool/native/games/prisonersdilemma_3p.py | 7 ++- ...wto_gt_native_002_prisonners_dilemma_3p.py | 56 +++++++++++++++++++ 4 files changed, 67 insertions(+), 6 deletions(-) create mode 100644 test/howtos/gt/howto_gt_native_002_prisonners_dilemma_3p.py diff --git a/src/mlpro/gt/native/basics.py b/src/mlpro/gt/native/basics.py index 4b1ef0e59..81dfba4d3 100644 --- a/src/mlpro/gt/native/basics.py +++ b/src/mlpro/gt/native/basics.py @@ -68,7 +68,7 @@ class GTFunction (Persistent): ## ------------------------------------------------------------------------------------------------- - def __init__(self, p_func_type:int, p_dim_elems:list=None, p_logging=Log.C_LOG_ALL): + def __init__(self, p_func_type:int, p_dim_elems:list=None, p_num_coalisions:int=None, p_logging=Log.C_LOG_ALL): super().__init__(p_id=None, p_logging=p_logging) @@ -79,8 +79,11 @@ def __init__(self, p_func_type:int, p_dim_elems:list=None, p_logging=Log.C_LOG_A if p_dim_elems is None: raise ParamError("p_dim_elems is not defined!") + + if p_num_coalisions is None: + raise ParamError("p_num_coalisions is not defined!") - self._num_coals = len(p_dim_elems) + self._num_coals = p_num_coalisions dim_elems = [self._num_coals] dim_elems.extend(p_dim_elems) diff --git a/src/mlpro/gt/pool/native/games/prisonersdilemma_2p.py b/src/mlpro/gt/pool/native/games/prisonersdilemma_2p.py index 80db53672..b5bfc0282 100644 --- a/src/mlpro/gt/pool/native/games/prisonersdilemma_2p.py +++ b/src/mlpro/gt/pool/native/games/prisonersdilemma_2p.py @@ -132,7 +132,8 @@ def _setup(self, p_mode, p_ada:bool, p_visualize:bool, p_logging) -> Model: self._payoff = GTPayoffMatrix( p_function=PayoffFunction_PD2P( p_func_type=GTFunction.C_FUNC_PAYOFF_MATRIX, - p_dim_elems=[2,2] + p_dim_elems=[2,2], + p_num_coalisions=2 ), p_player_ids=coal_ids ) diff --git a/src/mlpro/gt/pool/native/games/prisonersdilemma_3p.py b/src/mlpro/gt/pool/native/games/prisonersdilemma_3p.py index cfd40d6d6..575e483da 100644 --- a/src/mlpro/gt/pool/native/games/prisonersdilemma_3p.py +++ b/src/mlpro/gt/pool/native/games/prisonersdilemma_3p.py @@ -161,7 +161,7 @@ def _setup(self, p_mode, p_ada:bool, p_visualize:bool, p_logging) -> Model: p_logging=p_logging ) - p2 = GTPlayer( + p3 = GTPlayer( p_solver=[solver3a,solver3b], p_name="Player of Prisoner 3", p_visualize=p_visualize, @@ -173,7 +173,7 @@ def _setup(self, p_mode, p_ada:bool, p_visualize:bool, p_logging) -> Model: p_name="Coalition of Prisoner 3", p_coalition_type=GTCoalition.C_COALITION_SUM ) - coal2.add_player(p3) + coal3.add_player(p3) competition = GTCompetition( @@ -189,7 +189,8 @@ def _setup(self, p_mode, p_ada:bool, p_visualize:bool, p_logging) -> Model: self._payoff = GTPayoffMatrix( p_function=PayoffFunction_PD3P( p_func_type=GTFunction.C_FUNC_PAYOFF_MATRIX, - p_dim_elems=[2,2,2] + p_dim_elems=[2,4], + p_num_coalisions=3 ), p_player_ids=coal_ids ) diff --git a/test/howtos/gt/howto_gt_native_002_prisonners_dilemma_3p.py b/test/howtos/gt/howto_gt_native_002_prisonners_dilemma_3p.py new file mode 100644 index 000000000..23203664e --- /dev/null +++ b/test/howtos/gt/howto_gt_native_002_prisonners_dilemma_3p.py @@ -0,0 +1,56 @@ +## ------------------------------------------------------------------------------------------------- +## -- Project : MLPro - A Synoptic Framework for Standardized Machine Learning Tasks +## -- Package : mlpro.gt.examples +## -- Module : howto_gt_native_002_prisonners_dilemma_3p.py +## ------------------------------------------------------------------------------------------------- +## -- History : +## -- yyyy-mm-dd Ver. Auth. Description +## -- 2023-12-07 0.0.0 SY Creation +## -- 2023-12-07 1.0.0 SY Release of first version +## ------------------------------------------------------------------------------------------------- + +""" +Ver. 1.0.0 (2023-12-07) + +This module shows how to run a game, namely 3P Prisoners' Dilemma with two solvers, such as random +solver and min greedy policy. + +You will learn: + +1) How to set up a game, including solver, competition, coalition, payoff, and more + +2) How to run the game + +3) How to analyse the game + +""" + +from mlpro.gt.native.basics import * +from mlpro.gt.pool.native.games.prisonersdilemma_3p import * +from pathlib import Path + + + +if __name__ == "__main__": + cycle_limit = 10 + logging = Log.C_LOG_ALL + visualize = False + path = str(Path.home()) + +else: + cycle_limit = 1 + logging = Log.C_LOG_NOTHING + visualize = False + path = None + +PD2P_Game = PrisonersDilemma3PGame() + +training = GTTraining( + p_game_cls=PrisonersDilemma3PGame, + p_cycle_limit=cycle_limit, + p_path=path, + p_visualize=visualize, + p_logging=logging + ) + +training.run() \ No newline at end of file