我有一个O / D矩阵,可能有助于将人们从一个地方转移到另一个地方。通过矩阵扩展,我尝试在进入实际模型之前构建一个简单的模型,但最后编写了详细的编码。
extensions [matrix]
globals [mat]
patches-own [location]
turtles-own [residency]
to setup
ca
reset-ticks
ask patches [
if pxcor >= 0 and pycor >= 0 [set pcolor black + 0 set location "ne" ]
if pxcor < 0 and pycor >= 0 [set pcolor black + 1 set location "nw" ]
if pxcor < 0 and pycor < 0 [set pcolor black + 2 set location "sw" ]
if pxcor >= 0 and pycor < 0 [set pcolor black + 3 set location "se" ]
]
ask n-of 40 patches [
sprout 1 [
set shape "person student"
set heading random 360
set residency [location] of patch-here
if residency = "nw" [set color yellow + 2]
]
]
set-matrix
end
to set-matrix
set mat matrix:from-row-list [[0.5 0.3 0.1 0.1][0.3 0.5 0.1 0.1][0.1 0.1 0.5 0.2][0.1 0.1 0.2 0.5]]
print matrix:pretty-print-text mat
;pretty text print looks something like this
; nw ne sw se
;nw 0.5 0.3 0.1 0.1
;ne 0.3 0.5 0.1 0.1
;sw 0.1 0.1 0.5 0.2
;se 0.1 0.1 0.2 0.5
end
to go
ifelse(ticks mod 240 <= 120)[move-out][come-home]
tick
end
to move-out
;; North West Residents
let n-of-nw count turtles with [residency = "nw"]
let %nw-nw matrix:get mat 0 0
let %nw-ne matrix:get mat 0 1
let %nw-sw matrix:get mat 0 2
let %nw-se matrix:get mat 0 3
ask n-of (%nw-nw * n-of-nw) turtles with [residency = "nw"]
[rt 45 lt 45 set heading random 360 fd 2
face min-one-of patches with [location = "nw"][distance myself]]
ask n-of (%nw-ne * n-of-nw) turtles with [residency = "nw"]
[ifelse location = "ne"[face min-one-of patches with [location != "nw" and location = "ne"][distance myself]
rt 45 lt 45 set heading random 360 fd 2]
[rt 45 lt 45 set heading random 360 fd 2]]
ask n-of (%nw-sw * n-of-nw) turtles with [residency = "nw"]
[ifelse location = "sw"[face min-one-of patches with [location != "nw" and location = "sw"][distance myself]
rt 45 lt 45 set heading random 360 fd 2]
[rt 45 lt 45 set heading random 360 fd 2]]
ask n-of (%nw-se * n-of-nw) turtles with [residency = "nw"]
[ifelse location = "se"[face min-one-of patches with [location != "nw" and location = "se"][distance myself]
rt 45 lt 45 set heading random 360 fd 2]
[rt 45 lt 45 set heading random 360 fd 2]]
ask turtles with [residency != "nw"][rt 45 lt 45 set heading random 360 fd 1]
end
to come-home
ask turtles with [residency = "nw"]
[ifelse location != "nw" [face min-one-of patches with [location = "nw"][distance myself] fd 1]
[move-to one-of neighbors with [location = "nw"]]]
ask turtles with [residency != "nw"][rt 45 lt 45 set heading random 360 fd 1]
end
在Netlogo世界中,四个区域在每个角落分割为northwest(nw), northeast(ne), southwest(sw), southeast(se)
。我在随机空间中创建了代理,并根据residency
分配了location
。然后,我写了一个起始 - 目的地矩阵,如下所示,
nw ne sw se
nw 0.5 0.3 0.1 0.1
ne 0.3 0.5 0.1 0.1
sw 0.1 0.1 0.5 0.2
se 0.1 0.1 0.2 0.5
例如,30%的nw
居民应该转移到ne
。我只编写了一个区域,但任何人都可以发表评论以改善我的代码更多声音吗?非常感谢提前。
答案 0 :(得分:1)
好的,如果你已经分开识别从目的地移动到哪里,这是一个更清洁的版本。它没有经过测试。我已经废弃了矩阵,因为这会让你选择去哪里。 NetLogo没有选择案例&#39;类型结构,所以嵌套ifelse
是可行的方法。
我还在方向上添加了一些随机性,因为我认为这是您尝试对所有heading
代码执行的操作。
patches-own [location] turtles-own [residency destination]
to setup
clear-all
ask patches [
if pxcor >= 0 and pycor >= 0 [set pcolor black + 0 set location "ne" ]
if pxcor < 0 and pycor >= 0 [set pcolor black + 1 set location "nw" ]
if pxcor < 0 and pycor < 0 [set pcolor black + 2 set location "sw" ]
if pxcor >= 0 and pycor < 0 [set pcolor black + 3 set location "se" ]
]
ask n-of 40 patches
[ sprout 1
[ set shape "person student"
set heading random 360
set residency [location] of patch-here
if residency = "nw" [set color yellow + 2]
choose-destination
]
]
reset-ticks
end
to go
ifelse(ticks mod 240 <= 120)[move-out][come-home]
tick
end
to choose-destination
ask turtles with [residency = "nw"]
[ let myrandom random-float 1
ifelse myrandom <= 0.5 [ set destination "nw" ] [
ifelse myrandom <= 0.8 [ set destination "ne" ] [
ifelse myrandom <= 0.0 [ set destination "sw" ] [
set destination "se" ]]]
; similar code for each residency
end
to move-out
ask turtles with [destination != location]
[ face min-one-of patches with [location = destination][distance myself]
set heading heading + 10 - random 20
forward 1 ]
end
to come-home
; code more like revised move-out
end