我需要计算一户人家每次去商店购物的次数 [英] I need to count each visit made by a household to a shop

查看:16
本文介绍了我需要计算一户人家每次去商店购物的次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的模型中有两个不同的品种,家庭和商店。 我想让商店清点每一次来自家庭的来访。每家每户都会搬到商店所在的位置。

到目前为止:

    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-hcount-visit-nh),您有两个不同的计数器变量。让我问一下:这真的有必要吗?因为如果商店的计数器变量对于健康商店和不健康商店都有相同的名称,那么就很容易做set visits-count (visits-count + 1),就像我在示例中所做的那样。否则,由于两个变量的命名不同,您必须引入一个条件来检查家庭访问的商店类型,并基于此使用正确的变量。

这篇关于我需要计算一户人家每次去商店购物的次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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