如何改变特定区域中 n 只海龟的颜色?
How to change color of n number of turtles out of total in a specific region?
我正在研究传染病模型,我创建了一个有两个区域的世界。指定数量的人在他们的特定区域被初始化。
现在,我需要使用 initially_infected 全局变量在每个地区初始感染相同数量的人。例如,一个地区有 10 人感染,另一个地区有 10 人感染。
应通过将海龟的颜色变为红色来指示感染。
我试过使用“n-of”,但它无法正常工作,因为我想限制各自地区的最初感染者。
to setup_agents
create-humans green_population [
move-to one-of patches with [pcolor = green and not any? humans-here]
set shape "green person"
set color yellow
set size 1
set green_antibodies 0
]
ask n-of initially_infected humans [set color red]
create-humans blue_population [
move-to one-of patches with [pcolor = blue and not any? humans-here]
set shape "blue person"
set color yellow
set size 1
set blue_antibodies 0
]
ask n-of initially_infected humans [set color red]
end
您的代码无法正常工作,因为您一般询问 humans
(通过使用 ask n-of initially_infected humans [set color red]
),而不是基于它们所在的位置。
您可以通过各种几乎相同的方式来解决这个问题。要以整洁的方式做到这一点,您应该问自己 明确区分这两个组的主要编程特征是什么:它们的形状?他们所在的补丁的颜色?他们应该持有海龟自己的变量来区分它们吗?它们应该是不同的品种吗?
无论您选择此功能是什么,请在 ask n-of ...
语句中使用该功能。
在下面完全可重现的示例中,我只使用海龟的颜色作为区分元素:
breed [humans human]
globals [
green_population
blue_population
initially_infected
]
humans-own [
green_antibodies
starting_region
]
to setup
clear-all
ask patches [
ifelse (pxcor > 0)
[set pcolor green]
[set pcolor blue]
]
set green_population 300
set blue_population 270
set initially_infected 10
setup_agents
end
to setup_agents
create-humans green_population [
move-to one-of patches with [(pcolor = green) AND (not any? humans-here)]
set color green + 1
]
create-humans blue_population [
move-to one-of patches with [(pcolor = blue) AND (not any? humans-here)]
set color blue + 1
]
ask n-of initially_infected humans with [color = green + 1] [
set color red
]
ask n-of initially_infected humans with [color = blue + 1] [
set color red
]
end
也许更优雅的选择是创建一个小程序,将种群的颜色和要感染的数量作为输入。只修改代码的相关部分,它将是:
to setup_agents
create-humans green_population [
move-to one-of patches with [(pcolor = green) AND (not any? humans-here)]
set color green + 1
]
create-humans blue_population [
move-to one-of patches with [(pcolor = blue) AND (not any? humans-here)]
set color blue + 1
]
infect-humans (green + 1) initially_infected
infect-humans (blue + 1) initially_infected
end
to infect-humans [hue quantity]
ask n-of quantity humans with [color = hue] [
set color red
]
end
在我的示例中,我使用了人类的颜色作为区分因素,但您可以轻松地将其调整为您可能想要使用的任何其他事物。
我正在研究传染病模型,我创建了一个有两个区域的世界。指定数量的人在他们的特定区域被初始化。
现在,我需要使用 initially_infected 全局变量在每个地区初始感染相同数量的人。例如,一个地区有 10 人感染,另一个地区有 10 人感染。 应通过将海龟的颜色变为红色来指示感染。
我试过使用“n-of”,但它无法正常工作,因为我想限制各自地区的最初感染者。
to setup_agents
create-humans green_population [
move-to one-of patches with [pcolor = green and not any? humans-here]
set shape "green person"
set color yellow
set size 1
set green_antibodies 0
]
ask n-of initially_infected humans [set color red]
create-humans blue_population [
move-to one-of patches with [pcolor = blue and not any? humans-here]
set shape "blue person"
set color yellow
set size 1
set blue_antibodies 0
]
ask n-of initially_infected humans [set color red]
end
您的代码无法正常工作,因为您一般询问 humans
(通过使用 ask n-of initially_infected humans [set color red]
),而不是基于它们所在的位置。
您可以通过各种几乎相同的方式来解决这个问题。要以整洁的方式做到这一点,您应该问自己 明确区分这两个组的主要编程特征是什么:它们的形状?他们所在的补丁的颜色?他们应该持有海龟自己的变量来区分它们吗?它们应该是不同的品种吗?
无论您选择此功能是什么,请在 ask n-of ...
语句中使用该功能。
在下面完全可重现的示例中,我只使用海龟的颜色作为区分元素:
breed [humans human]
globals [
green_population
blue_population
initially_infected
]
humans-own [
green_antibodies
starting_region
]
to setup
clear-all
ask patches [
ifelse (pxcor > 0)
[set pcolor green]
[set pcolor blue]
]
set green_population 300
set blue_population 270
set initially_infected 10
setup_agents
end
to setup_agents
create-humans green_population [
move-to one-of patches with [(pcolor = green) AND (not any? humans-here)]
set color green + 1
]
create-humans blue_population [
move-to one-of patches with [(pcolor = blue) AND (not any? humans-here)]
set color blue + 1
]
ask n-of initially_infected humans with [color = green + 1] [
set color red
]
ask n-of initially_infected humans with [color = blue + 1] [
set color red
]
end
也许更优雅的选择是创建一个小程序,将种群的颜色和要感染的数量作为输入。只修改代码的相关部分,它将是:
to setup_agents
create-humans green_population [
move-to one-of patches with [(pcolor = green) AND (not any? humans-here)]
set color green + 1
]
create-humans blue_population [
move-to one-of patches with [(pcolor = blue) AND (not any? humans-here)]
set color blue + 1
]
infect-humans (green + 1) initially_infected
infect-humans (blue + 1) initially_infected
end
to infect-humans [hue quantity]
ask n-of quantity humans with [color = hue] [
set color red
]
end
在我的示例中,我使用了人类的颜色作为区分因素,但您可以轻松地将其调整为您可能想要使用的任何其他事物。