Netlogo:如何在不使用双连接的情况下将 5 个补丁连接到其他 5 个补丁?

问题描述 投票:0回答:1

我正在开发一个带有一定数量的超市和配送中心的 netlogo 世界。每个超市都有一个配送中心,每周送货一次。但我不知道如何将单个超市连接到单个配送中心。

其中之一始终具有双重连接。这是我当前的代码

`

  globals [
  households  ; random kleur huizen, achtergrond grijs
  supermarkets  ;; blauw
  distribution-centers  ; roze autos, achtergrond rood
]

patches-own[ ]

turtles-own [visited target block ]

to setup
  clear-all
  setup-households
  setup-supermarkets
  setup-distribution-centers
  reset-ticks
end

to setup-households
  ask n-of N patches with [pcolor = black] [
      set pcolor grey
    sprout 1 [
      set shape "house"
      set color orange
    ]
    ]
  set households patches with [pcolor = grey]
end

to setup-supermarkets

  ;; Blokje [groen] van 5x5 maken waar supermarkets inkunnen
  ask patches with[
    pxcor >= -2.5 and pxcor <= 2.5 and
    pycor >= -2.5 and pycor <= 2.5
  ] [
    set pcolor green
  ]
 
  
    
  ask n-of M patches with [pcolor = green] [
      set pcolor blue
  ]
  
 
  set supermarkets patches with [pcolor = blue]
end


to setup-distribution-centers
  ;; als blokje zwart of groen is creer dan m aantal distrubution-centers
   ask n-of M patches with [pcolor = black] [
      set pcolor red
    
      sprout 1 [
        set visited false
        set color pink
        set shape "car"
        set block patch-here
      set target min-one-of supermarkets [distance myself]
       
      ]
    ]
  set distribution-centers patches with [pcolor = red]
end

to go
  ask turtles with [color = pink] [
    if visited [
      move-to-target
      check-distribution-center

    ]

    if not visited [
      move-to-target
      check-supermarkets
    ]
  ]

  tick
  stop
end

to move-to-target
 if (color != green) [
    face target
    fd 1
  ]
end

to check-distribution-center
  if patch-here = block [
    set visited true
    set color green
  ]
end

to check-supermarkets
  if member? patch-here supermarkets [
    set visited true
    set target block
  ]
end  `
netlogo rnetlogo pynetlogo
1个回答
0
投票

由于

min-one-of supermarkets [distance myself]
返回最近的超市,因此两只海龟的情况很可能相同。一种方法是追踪哪个超市已经成为海龟的目标。以下是执行此操作的方法(请参阅第 7、63 和 64 行)。请小心,这要求与配送中心相比,超市的数量总是较少或相等,并且在一次运行中您仅连接它们一次。

globals [
  households  ; random kleur huizen, achtergrond grijs
  supermarkets  ;; blauw
  distribution-centers  ; roze autos, achtergrond rood
]

patches-own[connected] ; add to track connected supermarkets

turtles-own [visited target block ]

to setup
  clear-all
  setup-households
  setup-supermarkets
  setup-distribution-centers
  reset-ticks
end

to setup-households
  ask n-of N patches with [pcolor = black] [
      set pcolor grey
    sprout 1 [
      set shape "house"
      set color orange
    ]
    ]
  set households patches with [pcolor = grey]
end

to setup-supermarkets

  ;; Blokje [groen] van 5x5 maken waar supermarkets inkunnen
  ask patches with[
    pxcor >= -2.5 and pxcor <= 2.5 and
    pycor >= -2.5 and pycor <= 2.5
  ] [
    set pcolor green
  ]
 
  
    
  ask n-of M patches with [pcolor = green] [
      set pcolor blue
    set connected false
  ]
  
  
 
  set supermarkets patches with [pcolor = blue]
end


to setup-distribution-centers
  ;; als blokje zwart of groen is creer dan m aantal distrubution-centers
   ask n-of M patches with [pcolor = black] [
      set pcolor red
    
      sprout 1 [
        set visited false
        set color pink
        set shape "car"
        set block patch-here
      set target min-one-of supermarkets with [not connected] [distance myself] ;; only target non-connected supermarkets
      ask target [set connected true] ;; set false to make sure it is not targeted again
       
      ]
    ]
  set distribution-centers patches with [pcolor = red]
end

to go
  ask turtles with [color = pink] [
    if visited [
      move-to-target
      check-distribution-center

    ]

    if not visited [
      move-to-target
      check-supermarkets
    ]
  ]

  tick
  stop
end

to move-to-target
 if (color != green) [
    face target
    fd 1
  ]
end

to check-distribution-center
  if patch-here = block [
    set visited true
    set color green
  ]
end

to check-supermarkets
  if member? patch-here supermarkets [
    set visited true
    set target block
  ]
end 

顺便说一句。您的代码似乎可以从使用breeds(用于超市、配送中心和家庭)中受益匪浅!

© www.soinside.com 2019 - 2024. All rights reserved.