Skip to content

Commit

Permalink
GT-Native: draft howto 2p Prisoners' Dilemma #758
Browse files Browse the repository at this point in the history
  • Loading branch information
steveyuwono committed Sep 22, 2023
1 parent b95383d commit 21984aa
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<mxfile host="Electron" modified="2023-09-22T09:25:41.584Z" agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) draw.io/21.7.5 Chrome/114.0.5735.289 Electron/25.8.1 Safari/537.36" etag="0mJ68IiRPQVwOo6eAay1" version="21.7.5" type="device">
<mxfile host="Electron" modified="2023-09-22T11:20:19.360Z" agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) draw.io/21.7.5 Chrome/114.0.5735.289 Electron/25.8.1 Safari/537.36" etag="M8x-vR0TarWEC07UTxIC" version="21.7.5" type="device">
<diagram id="C5RBs43oDa-KdzZeNtuy" name="Page-1">
<mxGraphModel dx="3752" dy="2592" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="2336" pageHeight="1654" math="0" shadow="0">
<root>
Expand Down Expand Up @@ -73,7 +73,7 @@
<mxGeometry relative="1" as="geometry" />
</mxCell>
<mxCell id="B3U6xe70-WNpAOuPaKyA-3" value="GTGame (bf.ml.Scenario)" style="swimlane;fontStyle=3;align=center;verticalAlign=top;childLayout=stackLayout;horizontal=1;startSize=26;horizontalStack=0;resizeParent=1;resizeLast=0;collapsible=1;marginBottom=0;rounded=0;shadow=1;strokeWidth=1;fillColor=#dae8fc;strokeColor=#6c8ebf;swimlaneFillColor=#ffffff;gradientColor=#FFFFFF;" parent="WIyWlLk6GJQsqaUBKTNV-1" vertex="1">
<mxGeometry x="-870" y="-30" width="260" height="440" as="geometry">
<mxGeometry x="-870" y="-48" width="260" height="480" as="geometry">
<mxRectangle x="230" y="140" width="160" height="26" as="alternateBounds" />
</mxGeometry>
</mxCell>
Expand All @@ -86,26 +86,26 @@
<mxCell id="B3U6xe70-WNpAOuPaKyA-5" value="" style="line;html=1;strokeWidth=1;align=left;verticalAlign=middle;spacingTop=-1;spacingLeft=3;spacingRight=3;rotatable=0;labelPosition=right;points=[];portConstraint=eastwest;" parent="B3U6xe70-WNpAOuPaKyA-3" vertex="1">
<mxGeometry y="78" width="260" height="8" as="geometry" />
</mxCell>
<mxCell id="B3U6xe70-WNpAOuPaKyA-6" value="__init__( p_payoff_matrix:GTPayoffMatrix,&#xa;               p_visualize:bool = False,&#xa;               p_logging = Log.C_LOG_ALL )" style="text;align=left;verticalAlign=top;spacingLeft=4;spacingRight=4;overflow=hidden;rotatable=0;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;fontColor=#000000;strokeColor=none;dashed=1;" parent="B3U6xe70-WNpAOuPaKyA-3" vertex="1">
<mxGeometry y="86" width="260" height="44" as="geometry" />
<mxCell id="B3U6xe70-WNpAOuPaKyA-6" value="__init__( p_mode=Mode.C_MODE_SIM,&#xa;               p_ada=False,&#xa;               p_cycle_limit=1,&#xa;               p_visualize:bool=False,&#xa;               p_logging=Log.C_LOG_ALL )" style="text;align=left;verticalAlign=top;spacingLeft=4;spacingRight=4;overflow=hidden;rotatable=0;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;fontColor=#000000;strokeColor=none;dashed=1;" parent="B3U6xe70-WNpAOuPaKyA-3" vertex="1">
<mxGeometry y="86" width="260" height="84" as="geometry" />
</mxCell>
<mxCell id="B3U6xe70-WNpAOuPaKyA-7" value="_setup( p_mode,&#xa;             p_ada:bool,&#xa;             p_visualize:bool,&#xa;             p_logging ): Model" style="text;align=left;verticalAlign=top;spacingLeft=4;spacingRight=4;overflow=hidden;rotatable=0;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;fontColor=#0000FF;" parent="B3U6xe70-WNpAOuPaKyA-3" vertex="1">
<mxGeometry y="130" width="260" height="70" as="geometry" />
<mxGeometry y="170" width="260" height="70" as="geometry" />
</mxCell>
<mxCell id="B3U6xe70-WNpAOuPaKyA-8" value="_run_cycle()" style="text;align=left;verticalAlign=top;spacingLeft=4;spacingRight=4;overflow=hidden;rotatable=0;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;" parent="B3U6xe70-WNpAOuPaKyA-3" vertex="1">
<mxGeometry y="200" width="260" height="26" as="geometry" />
<mxGeometry y="240" width="260" height="26" as="geometry" />
</mxCell>
<mxCell id="B3U6xe70-WNpAOuPaKyA-9" value="_get_evaluation(&#xa;    p_player_ids:Union[str, list] = None,&#xa;    p_coalition_id = None&#xa;    ): Union[float, list]" style="text;align=left;verticalAlign=top;spacingLeft=4;spacingRight=4;overflow=hidden;rotatable=0;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;" parent="B3U6xe70-WNpAOuPaKyA-3" vertex="1">
<mxGeometry y="226" width="260" height="74" as="geometry" />
<mxGeometry y="266" width="260" height="74" as="geometry" />
</mxCell>
<mxCell id="B3U6xe70-WNpAOuPaKyA-129" value="connect_data_logger(&#xa;    p_ds_strategies:GTDataStoring = None,&#xa;    p_ds_payoffs:GTDataStoring = None )" style="text;align=left;verticalAlign=top;spacingLeft=4;spacingRight=4;overflow=hidden;rotatable=0;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;" parent="B3U6xe70-WNpAOuPaKyA-3" vertex="1">
<mxGeometry y="300" width="260" height="60" as="geometry" />
<mxGeometry y="340" width="260" height="60" as="geometry" />
</mxCell>
<mxCell id="r4KT35VXYHTNUlkVge_L-0" value="is_zerosum(): bool" style="text;align=left;verticalAlign=top;spacingLeft=4;spacingRight=4;overflow=hidden;rotatable=0;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;fontColor=#000000;" parent="B3U6xe70-WNpAOuPaKyA-3" vertex="1">
<mxGeometry y="360" width="260" height="26" as="geometry" />
<mxGeometry y="400" width="260" height="26" as="geometry" />
</mxCell>
<mxCell id="r4KT35VXYHTNUlkVge_L-1" value="_is_bestresponse( p_coalition_id:str,&#xa;                               p_coalition:GTCoalition&#xa;                              ): Union[list,float]&#xa;" style="text;align=left;verticalAlign=top;spacingLeft=4;spacingRight=4;overflow=hidden;rotatable=0;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;fontColor=#000000;" parent="B3U6xe70-WNpAOuPaKyA-3" vertex="1">
<mxGeometry y="386" width="260" height="54" as="geometry" />
<mxGeometry y="426" width="260" height="54" as="geometry" />
</mxCell>
<mxCell id="B3U6xe70-WNpAOuPaKyA-10" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=0.5;exitY=1;exitDx=0;exitDy=0;strokeColor=#000000;fontSize=12;fontColor=#FF0000;endArrow=block;endFill=0;" parent="WIyWlLk6GJQsqaUBKTNV-1" source="B3U6xe70-WNpAOuPaKyA-11" target="B3U6xe70-WNpAOuPaKyA-34" edge="1">
<mxGeometry relative="1" as="geometry" />
Expand All @@ -121,7 +121,7 @@
<mxCell id="B3U6xe70-WNpAOuPaKyA-13" value="" style="line;html=1;strokeWidth=1;align=left;verticalAlign=middle;spacingTop=-1;spacingLeft=3;spacingRight=3;rotatable=0;labelPosition=right;points=[];portConstraint=eastwest;" parent="B3U6xe70-WNpAOuPaKyA-11" vertex="1">
<mxGeometry y="52" width="340" height="8" as="geometry" />
</mxCell>
<mxCell id="B3U6xe70-WNpAOuPaKyA-14" value="__init__( p_solver:Union[list,GTSolver],&#xa;               p_name=&#39;&#39; &quot;,&#xa;               p_visualize:bool=False,&#xa;               p_logging=Log.C_LOG_ALL,&#xa;              p_random_solver:bool=False,&#xa;               **p_param)" style="text;align=left;verticalAlign=top;spacingLeft=4;spacingRight=4;overflow=hidden;rotatable=0;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;fontColor=#000000;strokeColor=none;dashed=1;" parent="B3U6xe70-WNpAOuPaKyA-11" vertex="1">
<mxCell id="B3U6xe70-WNpAOuPaKyA-14" value="__init__( p_solver:Union[list,GTSolver],&#xa;               p_name=&#39;&#39; &quot;,&#xa;               p_visualize:bool=False,&#xa;               p_logging=Log.C_LOG_ALL,&#xa;               p_random_solver:bool=False,&#xa;               **p_param)" style="text;align=left;verticalAlign=top;spacingLeft=4;spacingRight=4;overflow=hidden;rotatable=0;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;fontColor=#000000;strokeColor=none;dashed=1;" parent="B3U6xe70-WNpAOuPaKyA-11" vertex="1">
<mxGeometry y="60" width="340" height="90" as="geometry" />
</mxCell>
<mxCell id="B3U6xe70-WNpAOuPaKyA-15" value="_init_hyperparam( **p_param )" style="text;align=left;verticalAlign=top;spacingLeft=4;spacingRight=4;overflow=hidden;rotatable=0;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;" parent="B3U6xe70-WNpAOuPaKyA-11" vertex="1">
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
## -------------------------------------------------------------------------------------------------
## -- Project : MLPro - A Synoptic Framework for Standardized Machine Learning Tasks
## -- Package : mlpro.gt.examples
## -- Module : howto_gt_native_001_run_prisonners_dilemma_2p.py
## -- Module : howto_gt_native_001_prisonners_dilemma_2p.py
## -------------------------------------------------------------------------------------------------
## -- History :
## -- yyyy-mm-dd Ver. Auth. Description
Expand All @@ -22,4 +22,34 @@
3) How to analyse the game
"""
"""

from mlpro.gt.native.basics import *
from mlpro.gt.pool.native.games.prisonersdilemma_2p import *
from pathlib import Path




if __name__ == "__main__":
cycle_limit = 1
logging = Log.C_LOG_WE
visualize = False
path = str(Path.home())

else:
cycle_limit = 1
logging = Log.C_LOG_NOTHING
visualize = False
path = None

PD2P_Game = PrisonersDilemma2PGame()

training = GTTraining(
p_game_cls=PrisonersDilemma2PGame,
p_cycle_limit=cycle_limit,
p_path=path,
p_visualize=visualize,
p_logging=logging )

training.run()
34 changes: 22 additions & 12 deletions src/mlpro/gt/native/basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
## -- - Enhancement of GTStrategy
## -- - Add TransferFunction as another option on GTFunction
## -- 2023-09-21 1.0.3 SY Refactoring
## -- 2023-09-22 1.0.4 SY Update __init__ and _setup of GTGame
## -------------------------------------------------------------------------------------------------

"""
Ver. 1.0.3 (2023-09-21)
Ver. 1.0.4 (2023-09-22)
This module provides model classes for tasks related to a Native Game Theory.
"""
Expand Down Expand Up @@ -414,11 +415,19 @@ def __init__(self,
self._random_solver = False
self._list_solvers = [p_solver]
self._num_solvers = len(self._list_solvers)
self.switch_solver()

self._visualize = p_visualize
self._logging = p_logging
self._param = p_param

GTSolver.__init__(self,
p_strategy_space = self._list_solvers[0].get_strategy_space(),
p_id = self._list_solvers[0].get_id(),
p_visualize = self._visualize,
p_logging = self._logging,
**self._param)

self.switch_solver()


## -------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -476,7 +485,8 @@ def get_solver(self) -> GTSolver:
def switch_solver(self):

if len(self._list_solvers) == 1:
self.log(self.C_LOG_TYPE_I, 'Player %s is keeping the same solver %s'%(self._id, self._solver.get_id()))
self._solver = self._list_solvers[0]
self.log(self.C_LOG_TYPE_I, '%s is keeping the same solver %s'%(self.get_name(), self._solver.get_id()))
else:
if self._random_solver:
rnd = random.randint(0, self._num_solvers-1)
Expand Down Expand Up @@ -861,17 +871,17 @@ class GTGame (Scenario):

## -------------------------------------------------------------------------------------------------
def __init__(self,
p_payoff_matrix:GTPayoffMatrix,
p_visualize:bool = False,
p_logging = Log.C_LOG_ALL):
p_mode=Mode.C_MODE_SIM,
p_ada=False,
p_cycle_limit=1,
p_visualize:bool=False,
p_logging=Log.C_LOG_ALL):

super().__init__(p_mode=Mode.C_MODE_SIM,
p_ada=False,
p_cycle_limit=1,
super().__init__(p_mode=p_mode,
p_ada=p_ada,
p_cycle_limit=p_cycle_limit,
p_visualize=p_visualize,
p_logging=p_logging)

self._payoff = p_payoff_matrix
self._strategies = None

self.connect_data_logger()
Expand All @@ -880,7 +890,7 @@ def __init__(self,
## -------------------------------------------------------------------------------------------------
def _setup(self, p_mode, p_ada:bool, p_visualize:bool, p_logging) -> Model:
"""
Custom setup of GT Game.
Custom setup of GT Game. Payoff matrix has to be defined here as self._payoff.
Parameters
----------
Expand Down
7 changes: 6 additions & 1 deletion src/mlpro/gt/pool/native/games/prisonersdilemma_2p.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class PrisonersDilemma2PGame (GTGame):
def _setup(self, p_mode, p_ada:bool, p_visualize:bool, p_logging) -> Model:

solver1 = RandomSolver(
p_strategy_space=MSpace().add_dim('RStr','Z','Random Strategy','','','',[0,1]),
p_strategy_space=MSpace().add_dim(Dimension('RStr','Z','Random Strategy','','','',[0,1])),
p_id=1,
p_visualize=p_visualize,
p_logging=p_logging
Expand Down Expand Up @@ -117,6 +117,11 @@ def _setup(self, p_mode, p_ada:bool, p_visualize:bool, p_logging) -> Model:
)
competition.add_coalition(coal1)
competition.add_coalition(coal2)

self._payoff = GTPayoffMatrix(
p_function=PayoffFunction_PD2P,
p_player_ids=[1,2]
)

return competition

Expand Down

0 comments on commit 21984aa

Please sign in to comment.