Matrix Netlogo:如何使用O/D矩阵将代理从“x区”发送到“y区”?

Matrix Netlogo:如何使用O/D矩阵将代理从“x区”发送到“y区”?,matrix,netlogo,Matrix,Netlogo,我有一个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 pcol

我有一个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

例如,30%的西北部居民应迁往东北部。我只对一个区域进行了编码,但有人能对我的代码提出更合理的改进意见吗?非常感谢。

好的,如果您已经将识别去哪里与向目的地移动分开,这是一个更干净的版本。它没有经过测试。我已经放弃了矩阵,因为它让你在选择去哪里的时候有三重把柄。NetLogo没有“choose case”类型的结构,因此嵌套的ifelse是一种选择

我还为方向添加了一些随机性,因为我认为这就是您试图对所有标题代码所做的


这不是对矩阵方法的评论,而是对您的总体设计的评论。按照你的设置方式,你的学生将选择一个新的方向来引导每一个滴答声。我不确定那是你想要的。从你的描述来看,你似乎更希望他们选择一个目的地,然后每个蜱虫朝它移动或回家。在这种情况下,你需要一个额外的海龟自己的属性作为他们的目的地,并在设置过程中分配它。还有一大堆变化的方向没有太大意义-你让他们先左转,然后右转,然后在前进之前选择一个随机的方向。因此,左转和右转没有任何作用,最终的移动与他们要去的地方无关。大概你想让他们选择一个与目的地大致方向一致的标题。谢谢@JenB的评论。我的意图是首先随机挑选xx%的海龟,送它们一段时间,然后强迫它们放学后回来。我将尝试分配他们所需目的地的另一个属性。正如你已经评论过的,我发现我的lt和rt根本不起作用。决定删除它们。请看我的答案,并选择一个方向
          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
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