Netlogo:如何从“区域x"发送代理?到“区域y"使用 O/D 矩阵? [英] Netlogo: How can send agents from "area x" to "area y" using an O/D matrix?

查看:66
本文介绍了Netlogo:如何从“区域x"发送代理?到“区域y"使用 O/D 矩阵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 O/D 矩阵,它可能有助于将人们从一个地方转移到另一个地方.使用矩阵扩展,我尝试在进入实际模型之前构建一个简单的模型,但最终编码变得冗长.

I have a O/D matrix that might be useful to move people from one place to another. With the matrix extension, I attempt to build a simple model before I progress to the actual model, but ended up coding verbosely.

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).我在随机空间中创建了代理,并根据他们的 location 分配了他们的 residency.然后,我写了一个如下的起点-终点矩阵,

Inside the Netlogo world, four regions splitted up at each corner as northwest(nw), northeast(ne), southwest(sw), southeast(se). I created agents in a random space and assigned their residency according to their location. Then, I wrote a origin-destination matrix as below,

          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.我只编码了一个区域,但有人可以发表评论以改进我的代码更合理吗?非常感谢.

where, for example, 30% of nw residents should move to ne. I only coded one region, but can anyone give comments to improve my code more sound? Many thanks in advance.

推荐答案

好吧,如果你把确定去哪里和朝着目的地移动分开,这是一个更简洁的版本.它没有经过测试.我已经废弃了矩阵,因为这让你不得不选择去哪里.NetLogo 没有选择大小写"类型结构,因此嵌套 ifelse 是要走的路.

Okay, if you have separated identifying where to go from moving toward the destination, this is a cleaner version. It is not tested. I have scrapped the matrix since that was making you triple-handle choosing where to go. NetLogo doesn't have a 'choose case' type structure so nested ifelse is the way to go.

我还为方向添加了一些随机性,因为我认为这就是您试图对所有 heading 代码执行的操作.

I also added some randomness to the direction since I think that was what you were trying to do with all the heading code.

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.9 [ 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

这篇关于Netlogo:如何从“区域x"发送代理?到“区域y"使用 O/D 矩阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆