netlogo模拟结婚
Simulating marry in netlogo
在 netlogo 中,我正在模拟一个人口,我希望 16 到 50 岁之间的人与人口中的另一个人随机结婚。每个人都有一个户号,我想让男的把户号改成他的"wife"户号,但我不知道怎么办。现在我有这个代码
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
管理单独的变量:一个 link 告诉您所有人都需要了解这两个人之间的关系.主要优点是它更不容易出错:这些变量的值不存在不一致的风险。另请注意,married?
和 my-mate
如何使这些概念像以前一样易于访问。
关于您的代码的另外几条评论:
我通常会尽可能避免使用 stop
。该原语的行为并不总是直观的,有时会导致错误。
请注意我是如何创建临时 bachelors
代理集的。这避免了两次检查 age
和 married?
条件并使代码更具可读性。
我不知道你打算用它们做什么,但你可能想考虑让 households
代理人并通过创建 link 来代表家庭成员给它。使用 "id" 数字并不是一种非常 netlogoish 的做事方式,有时会导致代码效率低下。
在 netlogo 中,我正在模拟一个人口,我希望 16 到 50 岁之间的人与人口中的另一个人随机结婚。每个人都有一个户号,我想让男的把户号改成他的"wife"户号,但我不知道怎么办。现在我有这个代码
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
管理单独的变量:一个 link 告诉您所有人都需要了解这两个人之间的关系.主要优点是它更不容易出错:这些变量的值不存在不一致的风险。另请注意,married?
和 my-mate
如何使这些概念像以前一样易于访问。
关于您的代码的另外几条评论:
我通常会尽可能避免使用
stop
。该原语的行为并不总是直观的,有时会导致错误。请注意我是如何创建临时
bachelors
代理集的。这避免了两次检查age
和married?
条件并使代码更具可读性。我不知道你打算用它们做什么,但你可能想考虑让
households
代理人并通过创建 link 来代表家庭成员给它。使用 "id" 数字并不是一种非常 netlogoish 的做事方式,有时会导致代码效率低下。