NetLogo监视器中数字和”NA”值的总和

huangapple go评论49阅读模式
英文:

Sum of numeric and "NA" values in NetLogo monitors

问题

我有三种类型的代理:男性、女性和雌雄同体。所有这些代理都有属性“weight”。

在模拟过程中,有时只有男性代理,其他时期只有女性代理,其他时期只有雌雄同体代理,以及同时存在所有这些代理的时期。

我想在NetLogo中有一个监视器,显示男性和女性代理(不包括雌雄同体)的平均体重,但当系统中只有一种这些代理(男性或女性)时,监视器返回NA。

有办法告诉NetLogo将这些NA视为0吗?

-----------------EDIT----------------

感谢Toby_Stoe的帮助!我尝试了你的代码,但我认为在“to-report get-average-weight”过程中存在问题。当只有男性或男性+女性时,它运行得很完美,但当只有女性时,它将weight / 2,我不知道为什么,因为在你的代码中清楚地说明了当只有男性和女性在一起时才进行/ 2的除法。我在这里放了与过程“to-report get-average-weight”相关的代码,以防我在你的代码中犯了错误!

英文:

I have three types of agents: male, female, and hermaphrodite. All these agents have the attribute "weight".

During the simulation, there are periods when there are only male agents, other periods when there are only female agents, other periods when there are only hermaphrodite agents, and periods when all of them are present at the same time.

I would like to have a monitor in NetLogo that shows the average weight of male and female agents (no hermaphrodites), but when there is only one type of these agents (male or female) in the system, the monitor returns NA.

Is there a way to tell NetLogo to treat these NA as 0?

-----------------EDIT----------------

Thanks for the help Toby_Stoe! I tried your code but I think there is an issue in the procedure "to-report get-average-weight". It works perfect when there are only males or males + females, but when there are only females, it divided the weight / 2, and I don't know why because in your code clearly you are stating to divide / 2 when there are only males and females together. I put here the code related to the procedure "to-report get-average-weight" just in case I made a mistake in your code!

to-report get-average-weight
  let averageMaleWeight 0
  let averageFemaleWeight 0

  if maleCount > 0 [set averageMaleWeight totalMaleWeight / maleCount]
  
  if femaleCount > 0 [set averageFemaleWeight totalFemaleWeight / femaleCount]

  if maleCount > 0 and femaleCount > 0 [report (averageMaleWeight + averageFemaleWeight) / 2] 
  
  ifelse maleCount > 0 
  [report averageMaleWeight] 
  [ifelse femaleCount > 0 
    [report averageFemaleWeight] 
    [report 0]]
  end

答案1

得分: 2

以下是您提供的代码部分的中文翻译:

请注意,我已经将代码中的英文部分翻译成了中文,但代码中的注释部分保留了英文注释,因为它们通常是开发人员使用的标准编程术语,不需要翻译。

英文:
globals [
  totalMaleWeight
  totalFemaleWeight
  maleCount
  femaleCount
]

to setup
  clear-all
  ; Your setup code goes here
  reset-counters
  reset-ticks
end

to reset-counters
  set totalMaleWeight 0
  set totalFemaleWeight 0
  set maleCount 0
  set femaleCount 0
end

to update-weights
  reset-counters
  ask turtles [
    if agent-type = "male" [
      set totalMaleWeight totalMaleWeight + weight
      set maleCount maleCount + 1
    ]
    if agent-type = "female" [
      set totalFemaleWeight totalFemaleWeight + weight
      set femaleCount femaleCount + 1
    ]
  ]
end

to-report get-average-weight
  let averageMaleWeight 0
  let averageFemaleWeight 0

  if maleCount > 0 [
    set averageMaleWeight totalMaleWeight / maleCount
  ]
  
  if femaleCount > 0 [
    set averageFemaleWeight totalFemaleWeight / femaleCount
  ]

  if maleCount > 0 and femaleCount > 0 [
    report (averageMaleWeight + averageFemaleWeight) / 2
  ] else if maleCount > 0 [
    report averageMaleWeight
  ] else if femaleCount > 0 [
    report averageFemaleWeight
  ] else [
    report 0
  ]
end

In this example, I've added a reset-counters procedure that resets the counters for male and female weights and counts. Then, in the update-weights procedure, the counters are updated based on the agents' attributes.

The get-average-weight reporter calculates the average weights based on the counters. If both male and female counts are greater than 0, it returns the average of both weights. If only the male count is greater than 0, it returns the average male weight. If only the female count is greater than 0, it returns the average female weight. Otherwise, if both counts are 0 (indicating no male or female agents), it returns 0.

By setting "0" as the default value in the last report statement, it will handle cases where there are no male or female agents and display 0 instead of "NA".

huangapple
  • 本文由 发表于 2023年5月11日 08:16:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76223341.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定