這篇文章給大家介紹基于 Agent的Python是怎么實現(xiàn)隔離仿真,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
我們一直強調(diào)成都網(wǎng)站制作、網(wǎng)站設(shè)計對于企業(yè)的重要性,如果您也覺得重要,那么就需要我們慎重對待,選擇一個安全靠譜的網(wǎng)站建設(shè)公司,企業(yè)網(wǎng)站我們建議是要么不做,要么就做好,讓網(wǎng)站能真正成為企業(yè)發(fā)展過程中的有力推手。專業(yè)網(wǎng)站制作公司不一定是大公司,創(chuàng)新互聯(lián)公司作為專業(yè)的網(wǎng)絡(luò)公司選擇我們就是放心。
我會向你介紹用基于 Agent 的模型理解復(fù)雜現(xiàn)象的威力。為此,我們會用到一些 Python,社會學(xué)的案例分析和 Schelling 模型。
如果你觀察多民族(multi-ethnic)混居城市的種族(racial)分布,你會對不可思議的種族隔離感到驚訝。舉個例子,下面是美國人口普查局(US Census)用種族和顏色對應(yīng)標(biāo)記的紐約市地圖。種族隔離清晰可見。
許多人會從這個現(xiàn)象中認(rèn)定人是偏隘的(intolerant),不愿與其它種族比鄰而居。然而進一步看,會發(fā)現(xiàn)細(xì)微的差別。2005 年的諾貝爾經(jīng)濟學(xué)獎得主托馬斯·謝林(Thomas Schelling)在上世紀(jì)七十年代,就對這方面非常感興趣,并建立了一個基于 Agent 的模型——“Schelling 隔離模型”的來解釋這種現(xiàn)象。借助這個極其簡單的模型,Schelling 會告訴我們,宏觀所見并非微觀所為(What’s going down)。
我們會借助 Schelling 模型模擬一些仿真來深刻理解隔離現(xiàn)象。
基于 Agent 的模型需要三個參數(shù):1)Agents,2)行為(規(guī)則)和 3)總體層面(aggregate level)的評估。在 Schelling 模型中,Agents 是市民,行為則是基于相似比(similarity ratio )的搬遷,而總體評估評估就是相似比。
假設(shè)城市有 n 個種族。我們用唯一的顏色來標(biāo)識他們,并用網(wǎng)格來代表城市,每個單元格則是一間房。要么是空房子,要么有居民,且數(shù)量為 1。如果房子是空的,我們用白色標(biāo)識。如果有居民,我們用此人的種群顏色來標(biāo)識。我們把每個人周邊房屋(上下左右、左上右上、左下右下)定義為鄰居。
Schelling 的目的是想測試當(dāng)居民更傾向于選擇同種族的鄰居(甚至多種族)時會如果發(fā)生什么。如果同種族鄰居的比例上升到確定閾值(稱之為相似性閾值(Similarity Threshold)),那么我們認(rèn)為這個人已經(jīng)滿意(satisfied)了。如果還沒有,就是不滿意。
Schelling 的仿真如下。首先我們將人隨機分配到城里并留空一些房子。對于每個居民,我們都會檢查他(她)是否滿意。如果滿意,我們什么也不做。但如果不滿意,我們把他分配到空房子。仿真經(jīng)過幾次迭代后,我們會觀察最終的種族分布。
早在上世紀(jì) 70 年代,Schelling 就用鉛筆和硬幣在紙上完成了他的仿真。我們現(xiàn)在則用 Python 來完成相同的仿真。
為了模擬仿真,我們首先導(dǎo)入一些必要的庫。除了 Matplotlib 以外,其它庫都是 Python 默認(rèn)安裝的。
Python
import matplotlib.pyplot as plt import itertools import random import copy
|
接下來,定義名為 Schelling 的類,涉及到 6 個參數(shù):城市的寬和高,空房子的比例,相似性閾值,迭代數(shù)和種族數(shù)。我們在這個類中定義了 4 個方法:populate,is_unsatisfied,update,move_to_empty, 還有 plot)。
Python
class Schelling: def __init__(self, width, height, empty_ratio, similarity_threshold, n_iterations, races = 2): self.width = width self.height = height self.races = races self.empty_ratio = empty_ratio self.similarity_threshold = similarity_threshold self.n_iterations = n_iterations self.empty_houses = [] self.agents = {}
def populate(self): ....
def is_unsatisfied(self, x, y): ....
def update(self): ....
def move_to_empty(self, x, y): ....
def plot(self): ....
|
poplate 方法被用在仿真的開頭,這個方法將居民隨機分配在網(wǎng)格上。
Python
def populate(self): self.all_houses = list(itertools.product(range(self.width),range(self.height))) random.shuffle(self.all_houses)
self.n_empty = int( self.empty_ratio * len(self.all_houses) ) self.empty_houses = self.all_houses[:self.n_empty]
self.remaining_houses = self.all_houses[self.n_empty:] houses_by_race = [self.remaining_houses[i::self.races] for i in range(self.races)] for i in range(self.races): # 為每個種族創(chuàng)建 agent self.agents = dict( self.agents.items() + dict(zip(houses_by_race[i], [i+1]*len(houses_by_race[i]))).items()
|
is_unsatisfied 方法把房屋的 (x, y) 坐標(biāo)作為傳入?yún)?shù),查看同種群鄰居的比例,如果比理想閾值(happiness threshold)高則返回 True,否則返回 False。
Python
def is_unsatisfied(self, x, y):
race = self.agents[(x,y)] count_similar = 0 count_different = 0
if x > 0 and y > 0 and (x-1, y-1) not in self.empty_houses: if self.agents[(x-1, y-1)] == race: count_similar += 1 else: count_different += 1 if y > 0 and (x,y-1) not in self.empty_houses: if self.agents[(x,y-1)] == race: count_similar += 1 else: count_different += 1 if x < (self.width-1) and y > 0 and (x+1,y-1) not in self.empty_houses: if self.agents[(x+1,y-1)] == race: count_similar += 1 else: count_different += 1 if x > 0 and (x-1,y) not in self.empty_houses: if self.agents[(x-1,y)] == race: count_similar += 1 else: count_different += 1 if x < (self.width-1) and (x+1,y) not in self.empty_houses: if self.agents[(x+1,y)] == race: count_similar += 1 else: count_different += 1 if x > 0 and y < (self.height-1) and (x-1,y+1) not in self.empty_houses: if self.agents[(x-1,y+1)] == race: count_similar += 1 else: count_different += 1 if x > 0 and y < (self.height-1) and (x,y+1) not in self.empty_houses: if self.agents[(x,y+1)] == race: count_similar += 1 else: count_different += 1 if x < (self.width-1) and y < (self.height-1) and (x+1,y+1) not in self.empty_houses: if self.agents[(x+1,y+1)] == race: count_similar += 1 else: count_different += 1
if (count_similar+count_different) == 0: return False else: return float(count_similar)/(count_similar+count_different) < self.happy_threshold
|
update 方法將查看網(wǎng)格上的居民是否尚未滿意,如果尚未滿意,將隨機把此人分配到空房子中。并模擬 n_iterations 次。
Python
def update(self): for i in range(self.n_iterations): self.old_agents = copy.deepcopy(self.agents) n_changes = 0 for agent in self.old_agents: if self.is_unhappy(agent[0], agent[1]): agent_race = self.agents[agent] empty_house = random.choice(self.empty_houses) self.agents[empty_house] = agent_race del self.agents[agent] self.empty_houses.remove(empty_house) self.empty_houses.append(agent) n_changes += 1 print n_changes if n_changes == 0: break
|
move_to_empty 方法把房子坐標(biāo)(x, y)作為傳入?yún)?shù),并將 (x, y) 房間內(nèi)的居民遷入空房子。這個方法被 update 方法調(diào)用,會把尚不滿意的人遷入空房子。
Python
def move_to_empty(self, x, y): race = self.agents[(x,y)] empty_house = random.choice(self.empty_houses) self.updated_agents[empty_house] = race del self.updated_agents[(x, y)] self.empty_houses.remove(empty_house) self.empty_houses.append((x, y))
|
plot 方法用來繪制整個城市和居民。我們隨時可以調(diào)用這個方法來了解城市的居民分布。這個方法有兩個傳入?yún)?shù):title 和 file_name。
Python
def plot(self, title, file_name): fig, ax = plt.subplots() # 如果要進行超過 7 種顏色的仿真,你應(yīng)該相應(yīng)地進行設(shè)置 agent_colors = {1:'b', 2:'r', 3:'g', 4:'c', 5:'m', 6:'y', 7:'k'} for agent in self.agents: ax.scatter(agent[0]+0.5, agent[1]+0.5, color=agent_colors[self.agents[agent]])
ax.set_title(title, fontsize=10, fontweight='bold') ax.set_xlim([0, self.width]) ax.set_ylim([0, self.height]) ax.set_xticks([]) ax.set_yticks([]) plt.savefig(file_name)
|
現(xiàn)在我們實現(xiàn)了 Schelling 類,可以模擬仿真并繪制結(jié)果。我們會按照下面的需求(characteristics)進行三次仿真:
寬 = 50,而高 = 50(包含 2500 間房子)
30% 的空房子
相似性閾值 = 30%(針對仿真 1),相似性閾值 = 50%(針對仿真 2),相似性閾值 = 80%(針對仿真 3)
最大迭代數(shù) = 500
種族數(shù)量 = 2
創(chuàng)建并“填充”城市。
Python
schelling_1 = Schelling(50, 50, 0.3, 0.3, 500, 2) schelling_1.populate()
schelling_2 = Schelling(50, 50, 0.3, 0.5, 500, 2) schelling_2.populate()
schelling_3 = Schelling(50, 50, 0.3, 0.8, 500, 2) schelling_3.populate()
|
接下來,我們繪制初始階段的城市。注意,相似性閾值在城市的初始狀態(tài)不起作用。
Python
schelling_1_1.plot('Schelling Model with 2 colors: Initial State', 'schelling_2_initial.png')
|
下面我們運行 update 方法,繪制每個相似性閾值的最終分布。
Python
schelling_1.update() schelling_2.update() schelling_3.update()
schelling_1.plot('Schelling Model with 2 colors: Final State with Similarity Threshold 30%', 'schelling_2_30_final.png') schelling_2.plot('Schelling Model with 2 colors: Final State with Similarity Threshold 50%', 'schelling_2_50_final.png') schelling_3.plot('Schelling Model with 2 colors: Final State with Similarity Threshold 80%', 'schelling_2_80_final.png')
|
我們發(fā)現(xiàn)相似性閾值越高,城市的隔離度就越高。此外,我們還會發(fā)現(xiàn)即便相似性閾值很小,城市依舊會產(chǎn)生隔離。換言之,即使居民非常包容(tolerant)(相當(dāng)于相似性閾值很小),還是會以隔離告終。我們可以總結(jié)出:宏觀所見并非微觀所為。
以上仿真,我們只通過可視化來確認(rèn)隔離發(fā)生。然而,我們卻沒有對隔離的計算進行定量評估。本節(jié)我們會定義這個評估標(biāo)準(zhǔn),我們也會模擬一些仿真來確定理想閾值和隔離程度的關(guān)系。
首先在 Schelling 類中添加 calculate_similarity 方法。這個方法會計算每個 Agent 的相似性并得出均值。我們會用平均相似比評估隔離程度。
Python
def calculate_similarity(self): similarity = [] for agent in self.agents: count_similar = 0 count_different = 0 x = agent[0] y = agent[1] race = self.agents[(x,y)] if x > 0 and y > 0 and (x-1, y-1) not in self.empty_houses: if self.agents[(x-1, y-1)] == race: count_similar += 1 else: count_different += 1 if y > 0 and (x,y-1) not in self.empty_houses: if self.agents[(x,y-1)] == race: count_similar += 1 else: count_different += 1 if x < (self.width-1) and y > 0 and (x+1,y-1) not in self.empty_houses: if self.agents[(x+1,y-1)] == race: count_similar += 1 else: count_different += 1 if x > 0 and (x-1,y) not in self.empty_houses: if self.agents[(x-1,y)] == race: count_similar += 1 else: count_different += 1 if x < (self.width-1) and (x+1,y) not in self.empty_houses: if self.agents[(x+1,y)] == race: count_similar += 1 else: count_different += 1 if x > 0 and y < (self.height-1) and (x-1,y+1) not in self.empty_houses: if self.agents[(x-1,y+1)] == race: count_similar += 1 else: count_different += 1 if x > 0 and y < (self.height-1) and (x,y+1) not in self.empty_houses: if self.agents[(x,y+1)] == race: count_similar += 1 else: count_different += 1 if x < (self.width-1) and y < (self.height-1) and (x+1,y+1) not in self.empty_houses: if self.agents[(x+1,y+1)] == race: count_similar += 1 else: count_different += 1 try: similarity.append(float(count_similar)/(count_similar+count_different)) except: similarity.append(1) return sum(similarity)/len(similarity)
|
接下去,我們算出每個相似性閾值的平均相似比,并繪制出相似性閾值和相似比之間的關(guān)系。
Python
similarity_threshold_ratio = {} for i in [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7]: schelling = Schelling(50, 50, 0.3, i, 500, 2) schelling.populate() schelling.update() similarity_threshold_ratio[i] = schelling.calculate_similarity()
fig, ax = plt.subplots() plt.plot(similarity_threshold_ratio.keys(), similarity_threshold_ratio.values(), 'ro') ax.set_title('Similarity Threshold vs. Mean Similarity Ratio', fontsize=15, fontweight='bold') ax.set_xlim([0, 1]) ax.set_ylim([0, 1.1]) ax.set_xlabel("Similarity Threshold") ax.set_ylabel("Mean Similarity Ratio") plt.savefig('schelling_segregation_measure.png')
|
關(guān)于基于 Agent的Python是怎么實現(xiàn)隔離仿真就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
網(wǎng)頁名稱:基于Agent的Python是怎么實現(xiàn)隔離仿真
標(biāo)題網(wǎng)址:http://www.2m8n56k.cn/article6/iesoog.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供電子商務(wù)、網(wǎng)站營銷、服務(wù)器托管、App開發(fā)、域名注冊、品牌網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:[email protected]。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)