NetLogo:如何使海龟(1)移动到具有基于海龟变量的补丁变量匹配条件的补丁以及(2)将补丁id作为变量

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

我正在尝试执行以下操作:

  1. 请海龟(这里
    households
    )检查是否有任何补丁具有(1)
    is-home = true
    和(2)成本低于家庭的钱;
  2. 将满足这些条件的第一个补丁的补丁id作为
    my-home
  3. 将家庭ID传递给补丁的
    my-owner
    变量;和
  4. 移动到那个补丁。
  5. 如果他们找不到符合标准的补丁,则家庭死亡并且流离失所的计数器增加 1.

我已经尝试了以下(并尝试了代码)并不断出错。

breed [ households household ]

globals [
  homes
  number-displaced
]

households-own
[ 
  my-home
  money
  owner?
]

patches-own
[
  is-home?
  cost
  my-owner
]

to-report random-exponential-in-bounds [mid mmin mmax]
  let result random-exponential mid
  if result < mmin or result > mmax
    [ report random-exponential-in-bounds mid mmin mmax ]
  report result
end

to setup-households
  ask households 
  [ set money round random-exponential-in-bounds 50000 0 1000000 ]
end

to setup-patches
  ask n-of 100 patches
  [ set is-home? one-of [ true false ]
    set cost random-normal 173000 43000 ] 
end

to setup
  ca
  reset-ticks
  set-default-shape households "person"
  setup-households
  setup-patches
end

to choose-house
  ifelse any? patches with [ is-home? = true and cost <= [money] of myself ]
  [ ask households [ move-to one-of patches with [ is-home? = true and cost <= [money] of myself ] ] 
    set owner? true
    set my-home patch-here
    set my-owner turtle-here
  ] 
  [ set number-displaced number-displaced + 1
    die ]
end

to go
  choose-house
end
netlogo agent-based-modeling
1个回答
0
投票

我发现你的代码有一些问题。

setup-households
:你实际上从未创造过任何海龟。你只要求(不存在的)海龟换钱。

setup-patches
:我不确定你在这里做什么。你要求 100 个补丁可能成为一个家,也可能不会成为一个家。何不索要50块补丁就可以成为一个家呢?或者让所有的补丁都变成一个家,但根据你想要的家的数量调整你的世界大小?我还建议使用
scale-color
,以可视化所有补丁的成本。

choose-house
:您正在尝试检查自己的钱,但实际上没有自己可用(目前由观察者调用)。将
ask households
移动到程序的开头可以解决此问题。我应该指出,目前多户人家可以搬进同一所房子。最后,你拼错了
turtles-here

to choose-house
  ask households [ 
    ifelse any? patches with [ is-home? = true and cost <= [money] of myself ] [ 
      
      move-to one-of patches with [ is-home? = true and cost <= [money] of myself ]
      set owner? true
      set my-home patch-here
      set my-owner turtles-here
    ] [
      set number-displaced number-displaced + 1
      die 
    ]
  ]
end
© www.soinside.com 2019 - 2024. All rights reserved.