import panel as pn
import plotly.graph_objects as go
pn.extension('plotly')
def create_sankey(player='Human', initial_decision='Cooperate'):
source = [0, 0, 1, 1]
target = [2, 3, 2, 3]
# based on the stats above and the current type of player, we get the following value arrays.
if player == 'Human':
if initial_decision == 'Cooperate':
value = [12035, 3173, 4963, 8440]
else:
value = [3821, 7686, 3635, 10584]
elif player == 'ChatGPT-4':
if initial_decision == 'Cooperate':
value = [29, 0, 0, 1]
else:
value = [0, 26, 1, 3]
else: # ChatGPT-3
if initial_decision == 'Cooperate':
value = [21, 3, 3, 3]
else:
value = [7, 15, 4, 4]
link_percentages = [value[0]/(value[0]+value[1])*100, value[1]/(value[0]+value[1])*100, value[2]/(value[2]+value[3])*100, value[3]/(value[2]+value[3])*100]
node_percentages = [(value[0]+value[1])/sum(value)*100, (value[2]+value[3])/sum(value)*100, (value[0]+value[2])/sum(value)*100, (value[1]+value[3])/sum(value)*100]
node_customdata = [("Cooperate in Round 1", node_percentages[0]), ("Defect in Round 1", node_percentages[1]),
("Cooperate in Round 2", node_percentages[2]), ("Defect in Round 2", node_percentages[3])]
# Create sankey diagram
fig = go.Figure(data=[go.Sankey(
node = dict(
pad = 15,
thickness = 20,
label = ["Cooperate", "Defect", "Cooperate", "Defect"],
color = ["#B0C5A4", "#D37676", "#B0C5A4", "#D37676"],
# customdata = ["Cooperate in Round 1", "Defect in Round 1", "Cooperate in Round 2", "Defect in Round 2"],
customdata = node_customdata,
hovertemplate='%{value:.0f} players (%{customdata[1]:.02f}%) chose to %{customdata[0]}',
),
link = dict(
source = source,
target = target,
value = value,
customdata = link_percentages,
hovertemplate='Of the %{source.value} players who chose to
'+
'%{source.customdata[0]}, %{value:.0f} (%{customdata:.02f}%)
'+
'chose to %{target.customdata[0]}',
color = ["#B0C5A4", "#B0C5A4", "#D37676", "#D37676"],
),
)])
annotations=[
dict(x=0, y=-0.2, xref='paper', yref='paper', showarrow=False,
text="Round 1"),
dict(x=0.5, y=-0.2, xref='paper', yref='paper', showarrow=False,
text="Other Player Defects in Round 1" if initial_decision == "Defect" else "Other Player Cooperates in Round 1", font=dict(size=14, color='grey')),
dict(x=1, y=-0.2, xref='paper', yref='paper', showarrow=False,
text="2")
]
fig.update_layout(hovermode='x', title="Prisoner's Dilemma Decision Flows", annotations=annotations, font_size=14, title_x=0.5)
return fig
# Interactive widgets for filtering
select_player = pn.widgets.Select(name='Type of Player', options=['Human', 'ChatGPT-4', 'ChatGPT-3'])
select_decision = pn.widgets.Select(name='Round 1 Decision by Other Player', options=['Cooperate', 'Defect'])
# Textual explanation of each chart based on type of player and round 1 decision (should have 6 scenarios!)
# https://panel.holoviz.org/reference/widgets/StaticText.html
# Dynamic binding of the sankey diagram function to the widgets' values
@pn.depends(select_player.param.value, select_decision.param.value)
def interactive_sankey(chatgpt_version, initial_decision):
return create_sankey(chatgpt_version, initial_decision)
@pn.depends(select_player.param.value, select_decision.param.value)
def setup_description(chatgpt_version, initial_decision):
if chatgpt_version == 'Human':
if initial_decision == 'Cooperate':
scenario_desc="Here's what happens in reality (for us humans) when the other player cooperates in Round 1! We can see that about 50% of the people start on either side of the coin (cooperate or defect) and then the numbers lean more heavily (around 60%) onto the cooperate side by Round 2. This shows that humans would more naturally lean towards cooperation if the other player does the same. Check out what happens when the other player defects in Round 1 by selecting the Defect option above!"
else:
scenario_desc="Here's what happens for humans when the other player defects in Round 1! Just like in the first scenario (other player cooperates) we see that about 50% of the people start on either side of the coin (cooperate or defect). This time, however, the numbers lean MUCH more heavily (around 70%) onto the defect side by Round 2 (especially coming from those who initially cooperated). This shows that humans would more naturally lean towards defecting (moreso than cooperation) if the other player does the same. Now check out how AI would make this decision by selecting either ChatGPT-3 or ChatGPT-4 in the options above!"
elif chatgpt_version == 'ChatGPT-4':
if initial_decision == 'Cooperate':
scenario_desc="Here's a peek into ChatGPT-4's world when faced with a cooperating partner in Round 1! ChatGPT-4 shows a strong tendency towards reciprocating cooperation, with ALL of the players who decided to cooperate in the first place (95%) choosing to cooperate again. It appears to demonstrate a high capacity for trust and cooperative behavior, mirroring an ideal strategic participant who values mutual benefit over individual gain. Curious about how ChatGPT-4 reacts to a defect? Switch to the Defect option above to explore further!"
else:
scenario_desc="Exploring ChatGPT-4's strategy when the other player decides to defect in Round 1 unveils an interesting approach! Just like humans, when ChatGPT-4 is 'betrayed' in a sense, it goes HEAVILY into defecting (with a total table turning 96% defecting in the second round). It's an intriguing stance that differs only in terms of extremeties compared to human reaction patterns. Now, check how ChatGPT-3 handles this by selecting it in the options above!"
else:
if initial_decision == 'Cooperate':
scenario_desc="Diving into ChatGPT-3's choices when the game starts with cooperation, it shows something in between both humans and ChatGPT-4; the exact same amount of responses (80%) end up in cooperation despite there having been some players changing to defecting and some initial defectors changing to cooperation. It is fascinating to see how this earlier version of ChatGPT balances trust and strategy. Want to see what happens when the start is less friendly? Choose the Defect option to discover."
else:
scenario_desc="When the initial move is to defect, ChatGPT-3 displays a mixed strategy (again, in between humans and ChatGPT-4). Despite starting with more on the cooperation side (~70%), they end up with the majority of players defecting by the second round (63% - which is much closer to the humans which were at 70%). This variance from ChatGPT-4's strategy offers a glimpse into the evolution and differences in approach between the two AI versions. Toggle to ChatGPT-4 or Human to compare both of these to what you found out about ChatGPT-3!"
static_text = pn.widgets.StaticText(name='Description', value=f"{scenario_desc}")
static_text.value = f"{scenario_desc}"
return static_text
# Layout
layout = pn.Column(pn.Row(select_player, select_decision), interactive_sankey, setup_description)
layout.servable()