赞
踩
主要分析了一下netlogo下一维元胞自动机的代码
实现了主要的几种重要的规则
rule30 rule254 rule90 和rule110
代表了元胞自动机的几个种类,混沌,秩序,复杂,复杂介于二者之间
记录当前正在处理的行号
- globals [ ;;设置全局变量row
- row
- ]
左边元胞的颜色和右边元胞的颜色
- patches-own[ ;;小方块拥有的属性,左边邻居的颜色和右边邻居的颜色,自己的颜色自己能知道就省略了
- pcolor-of-leftpatch
- pcolor-of-rightpatch
- ]
这里搞清楚一个逻辑,什么东西可以改变颜色
主体,我要先拿到主体,才能改变颜色
如果直接设置颜色,当前的主体是观察者
所以要先深入到这个主体内部,身在其中,才能改变颜色
是由内而外的
- to setup
- clear-all
- set row max-pycor ;;设置当前处理行 为 最上面一行
- ask patches[
- set pcolor white ;;刚开始定义所有元胞是死的(黑活 白死)
- ]
- ask patch 0 max-pycor[
- set pcolor black ;;最开始给坐标为 0 max-pycor的元胞设置成黑色,国际惯例,你当然可以不这么设置
- ]
- reset-ticks ;;重设时间步
- end
访问当前待处理行的元胞时,会执行do-rule方法,来决定当前元胞下面的元胞是什么颜色
with的意思是 满足什么条件
- to go
- if row = min-pycor[
- stop
- ]
- ask patches with [pycor = row][ ;;访问当前行的所有元胞 pycor是小方块的纵坐标
- do-rule ;;执行do-rule方法
- ]
- set row row - 1 ;;待会处理下一行
- tick ;;时间步+1
- end
这里展示的rule 30
这个数字首先转换成二进制是 00011110
我们把这个二进制数和7-0这个序列对应起来
就是
0 0 0 1 1 1 1 0
7 6 5 4 3 2 1 0
在上方序列为1的位置,就是生效的,可以让下一个元胞活,就是黑色(死是白色),颜色随便你定义
把生效的位置对应的下方序列的数字拿出来
就是 4 3 2 1
分别对应的三位二进制数为
1 0 0 ,1 0 1,0 1 0,0 0 1
由此代表
黑 白 白,黑 白 黑,白 黑 白,白 白 黑
也就是这样的组合,能够把下方的元胞变黑,其他4种组合就是变白
- to do-rule
- ;;先得到这个元胞的左右元胞的颜色
- set pcolor-of-leftpatch [pcolor] of patch-at -1 0 ;;这里为什么一定要加中括号,有待研究
- set pcolor-of-rightpatch [pcolor] of patch-at 1 0
- ifelse (pcolor-of-leftpatch = white and pcolor = white and pcolor-of-rightpatch = black)or
- (pcolor-of-leftpatch = white and pcolor = black and pcolor-of-rightpatch = white)or
- (pcolor-of-leftpatch = white and pcolor = black and pcolor-of-rightpatch = black)or
- (pcolor-of-leftpatch = black and pcolor = white and pcolor-of-rightpatch = white)
- [
- ;;set pcolor of patch-at 0 -1 black 这样的写法报错,原因是of拿到的是一个可以report的数据,而不是主体的颜色这个属性,暂时这么理解
- ask patch-at 0 -1 [
- set pcolor black
- ]
- ]
- [
- ask patch-at 0 -1[
- set pcolor white
- ]
- ]
- end
- globals [ ;;设置全局变量row
- row
- ]
-
- patches-own[ ;;小方块拥有的属性,左边邻居的颜色和右边邻居的颜色,自己的颜色自己能知道就省略了
- pcolor-of-leftpatch
- pcolor-of-rightpatch
- ]
-
- to setup
- clear-all
- set row max-pycor ;;设置当前处理行 为 最上面一行
- ask patches[
- set pcolor white
- ]
- ask patch 0 max-pycor[
- set pcolor black ;;最开始给坐标为 0 max-pycor的元胞设置成黑色,国际惯例,你当然可以不这么设置
- ]
- reset-ticks ;;重设时间步
- end
-
- to go
- if row = min-pycor[
- stop
- ]
- ask patches with [pycor = row][ ;;访问当前行的所有元胞 pycor是小方块的纵坐标
- do-rule ;;执行do-rule方法
- ]
- set row row - 1 ;;待会处理下一行
- tick ;;时间步+1
- end
-
- ;;这里展示rule30
- ;;对应 00011110 顺序是7-0
- ;;也就是二进制数 1,2,3,4能够生成黑色
- ;;001 010 011 100
- ;;对应:白白黑,白黑白,白黑黑,黑白白
-
- to do-rule
- ;;先得到这个元胞的左右元胞的颜色
- set pcolor-of-leftpatch [pcolor] of patch-at -1 0 ;;这里为什么一定要加中括号,有待研究
- set pcolor-of-rightpatch [pcolor] of patch-at 1 0
- ifelse (pcolor-of-leftpatch = white and pcolor = white and pcolor-of-rightpatch = black)or
- (pcolor-of-leftpatch = white and pcolor = black and pcolor-of-rightpatch = white)or
- (pcolor-of-leftpatch = white and pcolor = black and pcolor-of-rightpatch = black)or
- (pcolor-of-leftpatch = black and pcolor = white and pcolor-of-rightpatch = white)
- [
- ;;set pcolor of patch-at 0 -1 black 这样的写法报错,原因是of拿到的是一个可以report的数据,而不是主体的颜色这个属性,暂时这么理解
- ask patch-at 0 -1 [
- set pcolor black
- ]
- ]
- [
- ask patch-at 0 -1[
- set pcolor white
- ]
- ]
- end
界面
rule30就不说了,上面有
代码
- ;;rule 254
- ;;1111 1110 7-0
- ;;000 对应 白 其他都是黑
-
-
- to do-rule[
- set pcolor-of-leftpatch [pcolor] of patch-at -1 0
- set pcolor-of-rightpatch [pcolor] of patch-at 1 0
- ifelse (pcolor-of-leftpatch = white and pcolor = white and pcolor-of-rightpatch = black)
- [
- ask patch-at 0 -1 [
- set pcolor white
- ]
- ]
- [
- ask patch-at 0 -1[
- set pcolor black
- ]
- ]
- ]
界面
代码
- ;;rule 90
- ;;01011010 7-0
- ;;6 4 3 1
- ;;110 100 011 010
- ;;黑黑白 黑白白 白黑黑 白白黑
- to do-rule
- set pcolor-of-leftpatch [pcolor] of patch-at -1 0
- set pcolor-of-rightpatch [pcolor] of patch-at 1 0
- ifelse (pcolor-of-leftpatch = black and pcolor = black and pcolor-of-rightpatch = white)or
- (pcolor-of-leftpatch = black and pcolor = white and pcolor-of-rightpatch = white)or
- (pcolor-of-leftpatch = white and pcolor = black and pcolor-of-rightpatch = black)or
- (pcolor-of-leftpatch = white and pcolor = white and pcolor-of-rightpatch = black)
- [
- ask patch-at 0 -1[
- set pcolor black
- ]
- ]
- [
- ask patch-at 0 -1[
- set pcolor white
- ]
- ]
- end
界面
代码
- ;;rule 110
- ;;01101110 7-0
- ;;6 5 3 2 1
- ;;110 101 011 010 001
- ;;黑黑白 黑白黑 白黑黑 白黑白 白白黑
- ;;选少的来
- ;;7 4 0
- ;;111 100 000
- ;;黑黑黑 黑白白 白白白
-
-
- to do-rule
- set pcolor-of-leftpatch [pcolor] of patch-at -1 0
- set pcolor-of-rightpatch [pcolor] of patch-at 1 0
- ifelse (pcolor-of-leftpatch = black and pcolor = black and pcolor-of-rightpatch = black)or
- (pcolor-of-leftpatch = black and pcolor = white and pcolor-of-rightpatch = white)or
- (pcolor-of-leftpatch = white and pcolor = white and pcolor-of-rightpatch = white)
- [
- ask patch-at 0 -1[
- set pcolor white
- ]
- ]
- [
- ask patch-at 0 -1[
- set pcolor black
- ]
- ]
- end
界面
多分析几个模型好开始自己的工作
这里的rule30是wolfman最喜欢的规则,体现了“复杂”这一特性
rule254就是秩序
rule90呈现周期性
rule110就是秩序和混沌之间的,略微复杂
据说混沌的边缘就是复杂
有点神奇的
运行模型的时候别忘了在主界面的设置选项值把patch的大小设小一点,好观察
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。