减去。设置变量X变量Y只

时间:2017-07-02 17:53:41

标签: netlogo

我试图设置资源变量。它将是time并且在sugarscape中将像糖一样起作用。其设置为:ask agentes [set time random-in-range 1 6]。 问题是......我希望agentes像我们所说here一样参与activities链接。但是,每次参与时,都应该减去agentes时间的统一。我想它一定是foreach,但我似乎无法理解它是如何工作的。

  ask n-of n-to-link agentes with [n-t-activity = [n-t-activity] of myself] in-radius sight-radius [
    while [time >= 2] [
      create-participation-with myself [ set color [color] of myself ] ]
      foreach (command I don't know)[
        set time time - count participations]]

基本上,我希望agentes看看他们是否有时间参与。如果他们这样做,他们创建链接并减去他们的时间1。每次参加只有一次。如果他们有3次,他们将有2次参与和1次。如果他们有一次,他们根本就没有联系。

修改 你是对的。我不需要while。关于foreach,我看的每个地方都说了同样的话,但我不能想到其他的方式。关于颜色,它们仅用于展示目的。 时间和参与计数之间的关系如下:agentes有时间在activities。如果时间> = 2,他们参与。但是当链接处于活动状态时,每个participation(与activity的链接)会消耗1次(我还没有编写衰减代码;它们会在关闭时重新获得时间) 。

EDIT V2

没什么,即使使用[]也会减去。也许最好的选择是,如果我给你代码,你可以试试。您必须设置5个滑块:prob-female(53%),initial-people(约200),num-activity(约20),n-capacity(约25)和sight-radius(约7)。还有两个按钮,setupgo。我还设置了patch size 10个,其​​中包含max-pxcormax-pycor。这是代码。对不起,如果我不够清楚的话!

undirected-link-breed [participations participation]

turtles-own [
  n-t-activity
]

breed [activities activity]
activities-own [
  t-culture-tags
  shared-culture
]

breed [agentes agente]
agentes-own [
  gender
  time
  culture-tags
  shared-culture
]

to setup
  clear-all
  setup-world
  setup-people-quotes
  setup-activities
  reset-ticks
END

to setup-world
  ask patches [set pcolor white]
END

to setup-people-quotes                                                      
  let quote (prob-female / 100 * initial-people)                            
  create-agentes initial-people
      [ while [any? other turtles-here ]
      [ setxy random-xcor random-ycor ]
      set gender "male" set color black
  ]
  ask n-of quote agentes
    [ set gender "female" set color blue
  ]
  ask agentes [
    set culture-tags n-values 11 [random 2]
        set shared-culture (filter [ i -> i = 0 ] culture-tags)
  ]
  ask agentes [
    set time random-in-range 1 6
  ]
  ask agentes [
    assign-n-t-activity
  ]
END

to setup-activities
  create-activities num-activity [
    set shape "box"
    set size 2
    set xcor random-xcor
    set ycor random-ycor
    ask activities [
      set t-culture-tags n-values 11 [random 2]
      set shared-culture (filter [i -> i = 0] t-culture-tags)
    ]
    ask activities [
      assign-n-t-activity]
  ]
END

to assign-n-t-activity                                                      
  if length shared-culture <= 4 [
    set n-t-activity ["red"]
    set color red
  ]
  if length shared-culture = 5 [
    set n-t-activity ["green"]
    set color green
  ]
  if length shared-culture = 6 [
    set n-t-activity ["green"]
    set color green
  ]
  if length shared-culture >= 7 [
    set n-t-activity ["black"]
    set color black
  ]
END

to go
  move-agentes
  participate
  tick
end

to move-agentes
  ask agentes [
    if time >= 2 [
    rt random 40
    lt random 40
    fd 0.3
  ]
  ]
end

to participate
  ask activities [
   if count my-links < n-capacity [
      let n-to-link ( n-capacity - count my-links)
      let n-agentes-in-radius count (
        agentes with [
          n-t-activity = [n-t-activity] of myself ] in-radius sight-radius)
      if n-agentes-in-radius < n-to-link [
        set n-to-link n-agentes-in-radius
      ]
      ask n-of n-to-link agentes with [
        n-t-activity = [n-t-activity] of myself] in-radius sight-radius [
        if time >= 2 [
          create-participation-with myself [
            set color [color] of myself ]
          ask agentes [set time time - count my-participations] ]
        ]
      ask activities [
        if not any? agentes in-radius sight-radius [
          ask participations [die]
          ]
        ]
      ]
    ]
end

to-report random-in-range [low high]
  report low + random (high - low + 1)
END

EDIT V3

我请比尔兰德帮助我,他解决了这个问题。问题出在这一行:let candidates agentes with [ n-t-activity = [n-t-activity] of myself ] in-radius sight-radius。他用这种方式解决了问题:let candidates agentes with [ n-t-activity = [n-t-activity] of myself and not participation-neighbor? myself ] in-radius sight-radius。这是and not participation-neighbor? myself条件,以确保agente不是activity的一部分。

1 个答案:

答案 0 :(得分:2)

在NetLogo中几乎不需要foreach。如果您发现自己认为自己需要foreach,那么您的立即反应应该是您需要ask。特别是,如果您正在迭代一组代理,这就是ask所做的事情,当您需要遍历列表时,您应该只使用foreach(并且该列表应该是除了{剂)。查看代码,您可能也不希望使用while循环。

更新评论和代码 - 您绝对不需要whileforeach。 您的问题是以下代码。您要求符合条件的agentes创建链接,但是然后您要求所有代理人更改他们的时间(我已标记的行),而不仅仅是创建参与链接的人员。

  ask n-of n-to-link agentes with [
    n-t-activity = [n-t-activity] of myself] in-radius sight-radius [
    if time >= 2 [
      create-participation-with myself [
        set color [color] of myself ]
      ask agentes [set time time - count my-participations] ]    ; THIS LINE
    ]

以下代码修复了此问题。我还做了一些其他事情来简化阅读并使代码更有效 - 我创建了满足条件的agentes的代理集(称为候选者)。在此代码中,候选集仅创建一次(对于每个活动)而不是两次(对于每个活动),因为您正在创建它以对其进行计数,然后再次创建它以用于参与链接生成。

to participate
  ask activities
   [ if count my-links < n-capacity
     [ let candidates agentes with [ 
           n-t-activity = [n-t-activity] of myself ] in-radius sight-radius
        let n-to-link min (list (n-capacity - count my-links) (count candidates ) )
        ask n-of n-to-link candidates
        [ if time >= 2
          [ create-participation-with myself [ set color [color] of myself ]
            set time time - count my-participations ]    ; REPLACED WITH THIS LINE
        ]
      ask activities [
        if not any? agentes in-radius sight-radius [
          ask participations [die]
          ]
        ]
      ]
    ]
end