用 Python 模擬囚徒困境(上):從海洋生態合作到遊戲理論哲學
前言:當演算法遇見海洋智慧
在臺灣周圍的海域中,珊瑚與藻類進行著一場古老的合作遊戲。牠們面臨著一個經典的選擇:是要無私地分享資源,還是自私地獨占養分?這個看似簡單的生態互動,實際上蘊含著深刻的遊戲理論智慧——囚徒困境(Prisoner’s Dilemma)。
今天,我們將用 Python 來模擬這個經典的哲學問題,從海洋生態的角度重新審視合作與競爭的本質,並探討其背後的哲學意涵。
第1章:囚徒困境的核心邏輯
經典囚徒困境
囚徒困境是博弈論中的經典案例。兩名囚犯被分別審訊,他們可以選擇「合作」(保持沉默)或「背叛」(指證對方)。每個人的最佳策略取決於對方的選擇,但理性的個體決策往往導致雙輸的結果。
讓我們用 Python 來建立基本的囚徒困境模型:
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from collections import defaultdict
import random
class PrisonersDilemma:
"""囚徒困境遊戲類別"""
def __init__(self):
# 收益矩陣:(玩家1收益, 玩家2收益)
# 策略:0=合作(C), 1=背叛(D)
self.payoff_matrix = {
(0, 0): (3, 3), # 雙方合作:中等收益
(0, 1): (0, 5), # 自己合作,對方背叛:自己受損
(1, 0): (5, 0), # 自己背叛,對方合作:自己得利
(1, 1): (1, 1) # 雙方背叛:雙輸
}
def play_round(self, strategy1, strategy2):
"""進行一輪遊戲"""
return self.payoff_matrix[(strategy1, strategy2)]
def get_payoff_description(self, s1, s2):
"""取得策略組合的描述"""
strategies = {0: "合作", 1: "背叛"}
return f"玩家1: {strategies[s1]}, 玩家2: {strategies[s2]}"
# 示例:基本遊戲
game = PrisonersDilemma()
print("囚徒困境收益矩陣:")
print("=" * 40)
for (s1, s2), (p1, p2) in game.payoff_matrix.items():
desc = game.get_payoff_description(s1, s2)
print(f"{desc} -> 收益: ({p1}, {p2})")
海洋生態中的囚徒困境
在海洋生態系統中,我們可以觀察到許多類似囚徒困境的現象:
清潔魚與宿主魚的互動:
- 合作:清潔魚專心清除寄生蟲,宿主魚保持靜止
- 背叛:清潔魚咬食宿主的健康組織,或宿主魚突然游走
珊瑚與共生藻的關係:
- 合作:珊瑚提供庇護,共生藻進行光合作用分享養分
- 背叛:任一方過度消耗資源而不給予回報
第2章:Python 實作海洋生態囚徒困境
海洋生物策略模擬
讓我們創建一個模擬海洋生物互動的進階模型:
class MarineCreature:
"""海洋生物基類"""
def __init__(self, name, strategy_type="random"):
self.name = name
self.strategy_type = strategy_type
self.total_payoff = 0
self.interaction_history = []
self.reputation = 0.5 # 信譽值:0-1之間
def choose_strategy(self, opponent_history, current_round):
"""根據策略類型選擇行動"""
if self.strategy_type == "always_cooperate":
return 0 # 總是合作
elif self.strategy_type == "always_defect":
return 1 # 總是背叛
elif self.strategy_type == "tit_for_tat":
# 以牙還牙:第一輪合作,之後模仿對手上一輪行為
if current_round == 0 or len(opponent_history) == 0:
return 0
return opponent_history[-1]
elif self.strategy_type == "tit_for_two_tats":
# 雙重以牙還牙:對手連續背叛兩次才報復
if len(opponent_history) < 2:
return 0
if opponent_history[-1] == 1 and opponent_history[-2] == 1:
return 1
return 0
elif self.strategy_type == "generous_tit_for_tat":
# 寬容的以牙還牙:有機率原諒對手的背叛
if current_round == 0 or len(opponent_history) == 0:
return 0
if opponent_history[-1] == 1:
return 1 if random.random() > 0.1 else 0 # 90%機率報復
return 0
else: # random
return random.choice([0, 1])
def update_reputation(self, own_action, opponent_action):
"""更新信譽值"""
if own_action == 0: # 合作
self.reputation = min(1.0, self.reputation + 0.01)
else: # 背叛
self.reputation = max(0.0, self.reputation - 0.02)
class MarineEcosystemSimulation:
"""海洋生態系統模擬"""
def __init__(self):
self.game = PrisonersDilemma()
self.creatures = []
self.simulation_history = []
def add_creature(self, creature):
"""添加海洋生物"""
self.creatures.append(creature)
def simulate_interaction(self, creature1, creature2, rounds=100):
"""模擬兩個生物之間的互動"""
history1, history2 = [], []
payoffs1, payoffs2 = [], []
for round_num in range(rounds):
# 選擇策略
action1 = creature1.choose_strategy(history2, round_num)
action2 = creature2.choose_strategy(history1, round_num)
# 計算收益
payoff1, payoff2 = self.game.play_round(action1, action2)
# 更新記錄
history1.append(action1)
history2.append(action2)
payoffs1.append(payoff1)
payoffs2.append(payoff2)
# 更新總收益和信譽
creature1.total_payoff += payoff1
creature2.total_payoff += payoff2
creature1.update_reputation(action1, action2)
creature2.update_reputation(action2, action1)
# 儲存互動歷史
interaction_record = {
'creature1': creature1.name,
'creature2': creature2.name,
'strategy1': creature1.strategy_type,
'strategy2': creature2.strategy_type,
'history1': history1,
'history2': history2,
'payoffs1': payoffs1,
'payoffs2': payoffs2,
'cooperation_rate1': history1.count(0) / len(history1),
'cooperation_rate2': history2.count(0) / len(history2),
'total_payoff1': sum(payoffs1),
'total_payoff2': sum(payoffs2),
'final_reputation1': creature1.reputation,
'final_reputation2': creature2.reputation
}
self.simulation_history.append(interaction_record)
return interaction_record
# 創建海洋生態系統模擬
ecosystem = MarineEcosystemSimulation()
# 創建不同策略的海洋生物
creatures = [
MarineCreature("善良珊瑚", "always_cooperate"),
MarineCreature("自私海葵", "always_defect"),
MarineCreature("聰明清潔魚", "tit_for_tat"),
MarineCreature("寬容海龜", "generous_tit_for_tat"),
MarineCreature("謹慎小丑魚", "tit_for_two_tats"),
MarineCreature("隨機水母", "random")
]
for creature in creatures:
ecosystem.add_creature(creature)
print("海洋生態系統中的生物:")
for creature in creatures:
print(f"- {creature.name} ({creature.strategy_type})")
大規模生態競賽模擬
接著再針對我們上面創建出的生物系統環境的模擬中,進行一場場巡迴的比試
def run_ecosystem_tournament(ecosystem, rounds_per_match=100):
"""執行生態系統錦標賽"""
print(f"\n開始海洋生態錦標賽(每場對戰 {rounds_per_match} 輪)...")
print("=" * 60)
# 重置所有生物的總收益
for creature in ecosystem.creatures:
creature.total_payoff = 0
creature.reputation = 0.5
# 每對生物都進行對戰
match_count = 0
for i in range(len(ecosystem.creatures)):
for j in range(i + 1, len(ecosystem.creatures)):
creature1 = ecosystem.creatures[i]
creature2 = ecosystem.creatures[j]
print(f"對戰 {match_count + 1}: {creature1.name} vs {creature2.name}")
interaction = ecosystem.simulate_interaction(
creature1, creature2, rounds_per_match
)
print(f" 合作率: {creature1.name} {interaction['cooperation_rate1']:.2%}, "
f"{creature2.name} {interaction['cooperation_rate2']:.2%}")
print(f" 總收益: {creature1.name} {interaction['total_payoff1']}, "
f"{creature2.name} {interaction['total_payoff2']}")
print(f" 最終信譽: {creature1.name} {interaction['final_reputation1']:.3f}, "
f"{creature2.name} {interaction['final_reputation2']:.3f}")
print()
match_count += 1
return ecosystem.simulation_history
# 執行錦標賽
tournament_results = run_ecosystem_tournament(ecosystem, 200)
# 分析結果
print("錦標賽最終排名:")
print("=" * 40)
sorted_creatures = sorted(ecosystem.creatures,
key=lambda x: x.total_payoff, reverse=True)
for rank, creature in enumerate(sorted_creatures, 1):
avg_payoff = creature.total_payoff / (len(ecosystem.creatures) - 1)
print(f"{rank}. {creature.name:12} | "
f"總收益: {creature.total_payoff:4d} | "
f"平均: {avg_payoff:6.1f} | "
f"信譽: {creature.reputation:.3f} | "
f"策略: {creature.strategy_type}")
本文上篇主要描述核心的邏輯架構,以及建立相關的Python模型,下篇將繼續講述如何進行資料的視覺化與分析、以及我們從中得到的哲學反思。