英文:
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”相关的代码,以防我在你的代码中犯了错误!
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
英文:
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 [
总体男性体重
总体女性体重
男性计数
女性计数
]
to setup
清除所有
; 在此处添加您的设置代码
重置计数器
重置刻度
end
to 重置计数器
设定 总体男性体重 0
设定 总体女性体重 0
设定 男性计数 0
设定 女性计数 0
end
to 更新体重
重置计数器
询问 龟类 [
如果 代理类型 = "男性" [
设定 总体男性体重 总体男性体重 + 体重
设定 男性计数 男性计数 + 1
]
如果 代理类型 = "女性" [
设定 总体女性体重 总体女性体重 + 体重
设定 女性计数 女性计数 + 1
]
]
end
to-report 获取平均体重
让 平均男性体重 0
让 平均女性体重 0
如果 男性计数 > 0 [
设定 平均男性体重 总体男性体重 / 男性计数
]
如果 女性计数 > 0 [
设定 平均女性体重 总体女性体重 / 女性计数
]
如果 男性计数 > 0 和 女性计数 > 0 [
报告 (平均男性体重 + 平均女性体重) / 2
] 否则 如果 男性计数 > 0 [
报告 平均男性体重
] 否则 如果 女性计数 > 0 [
报告 平均女性体重
] 否则 [
报告 0
]
end
请注意,我已经将代码中的英文部分翻译成了中文,但代码中的注释部分保留了英文注释,因为它们通常是开发人员使用的标准编程术语,不需要翻译。
英文:
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".
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论