在 netlogo 中,我正在模拟一个人口,我希望 16 到 50 岁之间的个体与人口中的另一个个体随机结婚。每个人都有一个家庭 ID,我希望男性将其家庭 ID 更改为他的“妻子”家庭 ID,但我不知道该怎么做。现在我有这个代码
ask individuals [
if not married? and sex = "male" and age >= 16 and age <= 50 [
let potential-mate one-of individuals with [
not married? and age >= 16 and age <= 50
and sex = "female" and household-id != household-id
]
if potential-mate != nobody [
; this command do an Bernoulli equation,
; the relation is based on empirical data i have
ifelse random-bernoulli (- 0.765 * ln age + 2.9753) [
stop
] [
set my-mate potential-mate
set married? true
ask my-mate [ set married? true ]
ask my-mate [ set my-mate myself ]
]
]
]
]
最佳答案
Luke C 的评论是正确的:您需要的是 myself
,如:
household-id != [ household-id ] of myself
话虽如此,我强烈建议将婚姻之类的事物建模为 links 。这是一个工作示例:
breed [individuals individual]
individuals-own [age sex household-id]
undirected-link-breed [marriages marriage]
to setup
clear-all
create-individuals 100 [
set age random 100
set sex one-of ["male" "female"]
set household-id random 100
setxy random-xcor random-ycor
]
marry-individuals
reset-ticks
end
to marry-individuals
let bachelors individuals with [ not married? and age >= 16 and age <= 50 ]
ask bachelors with [ sex = "male" ] [
let potential-mates bachelors with [
sex = "female" and household-id != [ household-id ] of myself
]
if any? potential-mates [
if not random-bernoulli (- 0.765 * ln age + 2.9753) [
let mate one-of potential-mates
create-marriage-with mate
set household-id [ household-id ] of mate
]
]
]
end
to-report married? ; individual reporter
report any? my-marriages
end
to-report my-mate ; individual reporter
report [ other-end ] of one-of my-marriages
end
这样,您就不必为 married?
和 my-mate
管理单独的变量:单个链接告诉您所有需要了解的关系这两个人。主要优点是它更不容易出错:这些变量的值不存在不一致的风险。另请注意,married?
和 my-mate
如何使这些概念像以前一样容易理解。
关于您的代码的另外一些评论:
我通常避免使用
stop
如果可能的话。该原语的行为并不总是直观的,有时会导致错误。请注意我如何创建临时
bachelors
代理集。这可以避免两次检查age
和married?
条件,并使代码更具可读性。我不知道您打算如何处理它们,但您可能需要考虑创建
家庭
代理并通过创建指向家庭的链接来代表家庭成员资格。使用“id”数字并不是一种非常网络化的做事方式,有时会导致代码效率低下。
关于netlogo - 在netlogo中模拟结婚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49237536/