java谢林(schelling)隔离模型模拟

java谢林(schelling)隔离模型模拟

通过java的图形化界面对谢林隔离模型进行模拟
社会空间中存在的两个不同类型的人,接下来定义一个门槛值t,这个门槛值表示如果某个代理当前同类型邻居的数量小于这个值时,就会发生移动,直到他找到合适位置可以使他的同类型邻居数量大于等于门槛值为止。
通过两个不同颜色的Button来表示不同类的人,白色表示空缺
简易效果如下
java谢林(schelling)隔离模型模拟_第1张图片
核心方法:

						int n = Integer.parseInt(tf1.getText().trim());
						int gate = Integer.parseInt(tf2.getText().trim());//获取输入框数据
						westPanel.setLayout(new GridLayout(n, n));
						frame.add(westPanel, BorderLayout.CENTER);
						lightBtns = new LightBtn[n * n];//生成按钮组
						int a[][] = new int[n][n];
						for (int i = 0; i < n; i++) {
     //随机生成A种人:B种人:空白=5:5:1
							for (int j = 0; j < n; j++) {
     
								if ((int) (Math.random() * (11)) == 0) {
     
									a[i][j] = 0;
								} else if ((int) (Math.random() * (11)) <= 5) {
     
									a[i][j] = 1;
								} else {
     
									a[i][j] = 2;
								}
							}
						}
						for (int i = 0; i < n; i++) {
     //给按钮上色
							for (int j = 0; j < n; j++) {
     
								LightBtn button = new LightBtn();
								if (a[i][j] == 0) {
     
									button.setBackground(Color.WHITE);
								} else if (a[i][j] == 1) {
     
									button.setBackground(Color.BLUE);
								} else if (a[i][j] == 2) {
     
									button.setBackground(Color.RED);
								}
								lightBtns[i * n + j] = button;
								westPanel.add(button);
							}
							westPanel.updateUI();
						}
						westPanel.updateUI();
						new Thread() {
     
							public void run() {
     
								int h = 1;
								while (h != 0) {
     如果都不搬家了就停下
									h = 0;
									for (int i = 0; i < n; i++) {
     //遍历每一个按钮
										for (int j = 0; j < n; j++) {
     //遍历每一个按钮
											if (a[i][j] != 0) {
     //如果不是空白就看他需不需要搬家
												int p = a[i][j];
												int count = -1;//计算周围邻居是否达到闸值
												for (int k = i - 1; k <= i + 1; k++) {
     
													for (int l = j - 1; l <= j + 1; l++) {
     
														if (k >= 0 && k < n && l >= 0 && l < n) {
     
															if (a[k][l] == p) {
     
																count++;
															}
														}
													}
												}
												if (count <= gate) {
     小于闸值就搬家到随机空白处
													h = 1;
													int x = i, y = j;
													while (a[x][y] != 0) {
     
														y = (int) (Math.random() * (n));
														x = (int) (Math.random() * (n));
													}
													a[i][j] = 0;
													a[x][y] = p;
												}
											}
										}
									}
									for (int i = 0; i < n; i++) {
     
										for (int j = 0; j < n; j++) {
     变换按钮颜色呈现效果
											lightBtns[i * n + j].switchBackgroundColor(a[i][j]);
										}
									}

								}
							}
						}

当闸值大于4时很难产生结果
按钮类

	class LightBtn extends JButton {
     
		int a;

		public void turnOff() {
     
			a = 0;
			this.setBackground(Color.WHITE);
		}

		public void switchBackgroundColor(int a) {
     

			if (a == 0) {
     
				this.setBackground(Color.WHITE);
			} else if (a == 1) {
     
				this.setBackground(Color.BLUE);
			} else if (a == 2) {
     
				this.setBackground(Color.RED);
			}
		}
	}

其实这个用python写更佳,但是我python学的还不好只能用Java了

你可能感兴趣的:(java,java)