实现一个简单的谢林模型(Schelling Model)

编程入门 行业动态 更新时间:2024-10-28 12:20:28

实现一个简单的谢林<a href=https://www.elefans.com/category/jswz/34/1771358.html style=模型(Schelling Model)"/>

实现一个简单的谢林模型(Schelling Model)

by tjuarch

1 简介

谢林模型,也叫谢林隔离模型,是由美国经济学家托马斯·谢林于1971年提出,描述同质性在空间隔离上的影响和作用。
它是基于智能体的模型,包含有三个元素:
1、会产生行为的智能体
2、智能体行为遵循一定的规则
3、智能体产生的行为会导致宏观上的结果

2 python实现

模型揭示的一些事实在实际中得到了验证,人们对于身边各种不同阶层邻居的存在,但是最终经过有限次的迁徙后,却形成了隔离。
采用python语言,在matplotlib中进行画图,用numpy计算矩阵。
主要流程包括几个步骤:
1、初始化网格,50x50的点阵,用三种颜色表示,包括两个分类和一个空白格子。设定满意度阈值、画布大小以及三类格子的生成比例(40%,40%,20%)。

import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline# np.random.seed(198108)N = 50
threshold = 0.7
figsize=(12,12)x = np.arange(N)
y = np.arange(N)
X, Y = np.meshgrid(x, y)
# Z = np.random.rand(N, N)
status = ["C1", "C4", 'white']
prob = [0.4, 0.4, 0.2]def init_z():Z = np.random.choice(a=status, size=(N**2), p=prob)Z.shape = (N, N)return ZZ = init_z()

2、计算每个点的满意程度get_cell_happiness(),也即周边同种类格子的比例,得到一个满意度矩阵get_all_happiness()。
3、计算所有格子的平均满意度(hap_mean)、不满意格子的比例(unhap_ratio),计算阈值下不满意格子的位置get_unhap_cells()。
4、随机选择一个不满意的格子搬家。但是要注意搬走之后形成新的空位以及及时刷新空格的位置列表。
5、重新计算满意程度,进行新一轮的搬家行动,最终计算经过多少步能达到目的,及不满意格子的比例达到一个比较低的水平。

def get_null_cells(Z):"""获取空白格子的位置Z:np.array, N*Nreturn:list of cells position"""if not Z.shape == (N, N):Z.shape = (N, N)cells = np.where(Z == "white")return cellsdef get_cell_happiness(Z, row, col):"""获取每个单元格的满意程度阈值Z: N*N np.arrayrow: int, col:intreturn: happiness:int"""if not Z.shape == (N, N):Z.shape = (N, N)if Z[row, col] == "white":return np.NaNsame, count = 0, 0left = 0 if col==0 else col-1right = Z.shape[1] if col==Z.shape[1]-1 else col+2top = 0 if row==0 else row-1bottom = Z.shape[0] if row==Z.shape[0]-1 else row+2# print(top, bottom, left, right)for i in range(top, bottom):for j in range(left, right):# print(list(range(left, right)))if (i, j) == (row, col) or Z[i,j] == "white":continue# print(Z[i,j], i, j)elif Z[i, j] == Z[row, col]:same += 1count += 1else:count += 1# print('in',same,count)if not count == 0:happiness = same / countelse:happiness = 0return happinessdef get_all_happiness(Z):"""得到所有格子的满意度return: np.array N*N"""hap_scores = []for row in range(Z.shape[0]):for col in range(Z.shape[1]):# print(row, col)hap_scores.append(get_cell_happiness(Z, row, col))hap_scores = np.array(hap_scores)hap_scores.shape = Z.shapereturn hap_scoresdef hap_mean(Z):"""所有格子的平均满意度return: res -> int"""hap_scores = get_all_happiness(Z)res = hap_scores[np.where(hap_scores>=0)].mean()return resdef get_unhap_cells(Z=Z, threshold=threshold):"""得到不满意的格子return: tuple 2 items"""hap_scores = get_all_happiness(Z)res = np.where(hap_scores < threshold)return resdef unhap_ratio(Z):hap_scores = get_all_happiness(Z)res = np.sum(hap_scores<threshold) / np.sum(hap_scores>=0)# unhap_count = len(get_unhap_cells()[0])# print(unhap_count)# res = unhap_count / len(Z[np.where(Z!="white")])return resdef move(Z):unhap_cells = get_unhap_cells()for i in range(len(unhap_cells[0])):blank_cells = get_null_cells(Z)unhap_row = unhap_cells[0][i]unhap_col = unhap_cells[1][i]j = np.random.choice(range(len(blank_cells[0])))blank_row = blank_cells[0][j]blank_col = blank_cells[1][j]Z[unhap_row, unhap_col], Z[blank_row, blank_row] = Z[blank_row, blank_row], Z[unhap_row, unhap_col]

6、用matplotlib画图,定义了几个画图函数:
draw_raw():画出现有的点矩阵情况
draw_nullcells():画出空格,并随机选一个空格
draw_happiness():全局的满意度得分
draw_unhapcells():全局的不满意格子分布
find_times_equal():计算达到均衡需要的搬家次数并绘图

3 简单分析

1、生成50 * 50 点阵,参数如图

这是一个平均满意度50.42%, 不满意率40.98%的模拟2500人的矩阵。
2、各种参数
空白格子以及随机的一个空格

全局满意度
不满意格子的分布
迁移5次以后的分布情况
最终经过21轮迁移,不满意度降低到1%

整体满意得分88.25

4源码

源码在此,欢迎下载,祝大家玩得愉快!
.ipynb

更多推荐

实现一个简单的谢林模型(Schelling Model)

本文发布于:2024-03-07 16:50:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1718368.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模型   简单   Model   谢林   Schelling

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!