我需要计算一个家庭对商店的每次访问
I need to count each visit made by a household to a shop
我的模型中有两个不同的品种,家庭和商店。
我希望商店统计家庭的每次访问。每个刻度,家庭都会实际移动到商店的位置。
到目前为止我有:
to count-v
ask unhealthy-shops [set count-visit-nh count-visit-nh + count households-on self]
ask healthy-shops [set count-visit-h count-visit-h + count households-on self]
end
但是,这计算的是同一块区域的家庭数量,比实际家庭数量多了一个刻度。我只想统计和店铺在同一位置的户数
请帮忙!
提前致谢。
在评论中的交流之后:这里有一个普遍的想法,如果它适合您的代码,您将需要适应超出 count-v
.
的任何过程和变量
唯一的关键是确保计数行为不是基于补丁而是由家庭在访问商店时执行的。
例如,请参阅下面的可重现示例:
breed [shops shop]
breed [households household]
shops-own [
healthy?
visits-count
]
to setup
clear-all
; Here we make sure that both shops are on the same patch,
; and that one is healthy while the other is unhealthy.
ask patch 0 0 [
sprout-shops 2 [
ifelse (any? shops with [healthy? = TRUE])
[set healthy? FALSE]
[set healthy? TRUE]
set shape "house"
set size 2
set color blue
]
]
create-households 20 [
setxy random-xcor random-ycor
set color gray
set shape "person"
]
end
to go
ask households [
ifelse (random 2 < 1)
[visit-a-shop]
[move-to one-of patches with [not any? shops-here]]
]
end
to visit-a-shop
let target one-of shops
move-to target
ask target [
set visits-count (visits-count + 1)
]
end
当然这里的关键部分在visit-a-shop
。这样,只有访问过商店的家庭,并且只有在访问过商店后,才会更新其计数器。
我从您的代码中看到,对于两种不同类型的商店(即 count-visit-h
和 count-visit-nh
),您有两个不同的计数器变量。请问:真的有必要吗?因为如果商店的健康商店和不健康商店的计数器变量名称相同,那么就像我在示例中所做的那样,只需执行 set visits-count (visits-count + 1)
就很容易。否则,对于两个命名不同的变量,您将不得不引入一个条件来检查该家庭访问的商店类型,并根据该条件使用正确的变量。
啊,所以你把家庭搬到了与商店完全相同的位置,以子补丁的准确性来衡量。所以商店可能在补丁 3 4 中,但 xcor = 3.1 和 ycor = 4.2?
然后你需要比较x和y坐标,比较的是“我自己”而不是“自己”,
如:
ask turtle 0 [ print count turtles with [ xcor = [xcor] of myself and ycor = [ycor] of myself ]]
韦德
我的模型中有两个不同的品种,家庭和商店。 我希望商店统计家庭的每次访问。每个刻度,家庭都会实际移动到商店的位置。
到目前为止我有:
to count-v
ask unhealthy-shops [set count-visit-nh count-visit-nh + count households-on self]
ask healthy-shops [set count-visit-h count-visit-h + count households-on self]
end
但是,这计算的是同一块区域的家庭数量,比实际家庭数量多了一个刻度。我只想统计和店铺在同一位置的户数
请帮忙!
提前致谢。
在评论中的交流之后:这里有一个普遍的想法,如果它适合您的代码,您将需要适应超出 count-v
.
唯一的关键是确保计数行为不是基于补丁而是由家庭在访问商店时执行的。
例如,请参阅下面的可重现示例:
breed [shops shop]
breed [households household]
shops-own [
healthy?
visits-count
]
to setup
clear-all
; Here we make sure that both shops are on the same patch,
; and that one is healthy while the other is unhealthy.
ask patch 0 0 [
sprout-shops 2 [
ifelse (any? shops with [healthy? = TRUE])
[set healthy? FALSE]
[set healthy? TRUE]
set shape "house"
set size 2
set color blue
]
]
create-households 20 [
setxy random-xcor random-ycor
set color gray
set shape "person"
]
end
to go
ask households [
ifelse (random 2 < 1)
[visit-a-shop]
[move-to one-of patches with [not any? shops-here]]
]
end
to visit-a-shop
let target one-of shops
move-to target
ask target [
set visits-count (visits-count + 1)
]
end
当然这里的关键部分在visit-a-shop
。这样,只有访问过商店的家庭,并且只有在访问过商店后,才会更新其计数器。
我从您的代码中看到,对于两种不同类型的商店(即 count-visit-h
和 count-visit-nh
),您有两个不同的计数器变量。请问:真的有必要吗?因为如果商店的健康商店和不健康商店的计数器变量名称相同,那么就像我在示例中所做的那样,只需执行 set visits-count (visits-count + 1)
就很容易。否则,对于两个命名不同的变量,您将不得不引入一个条件来检查该家庭访问的商店类型,并根据该条件使用正确的变量。
啊,所以你把家庭搬到了与商店完全相同的位置,以子补丁的准确性来衡量。所以商店可能在补丁 3 4 中,但 xcor = 3.1 和 ycor = 4.2?
然后你需要比较x和y坐标,比较的是“我自己”而不是“自己”,
如:
ask turtle 0 [ print count turtles with [ xcor = [xcor] of myself and ycor = [ycor] of myself ]]
韦德