NetLogo:如何让乌龟识别一种颜色的任何阴影?
NetLogo: How to make a turtle recognise any shade of one color?
我是第一次使用 NetLogo,需要编写一个简单的程序,其中我有一个光源可以将光漫射到其源补丁之外,还有一只海龟可以避开光。
我可以通过使用基本 'set pcolor yellow' 然后使用 'if patch-ahead [pcolor] = yellow [right 45][fd speed]' 类型命令来实现。然而,这并没有给我漫射光。
通过调整 HeatBugs 代码,我可以将颜色扩散到源补丁之外,但是我认为漫游乌龟不再将颜色识别为黄色,因为它是一种比例色。我尝试将代码设置为 != black 但这也不起作用。我假设这是因为补丁在每次滴答后都被重新着色。
有没有办法让乌龟识别漫射色块从而避开它们?或者用更简单的方法将光散射出去。 (我想要可变强度,所以使用邻居和黄色 -1 不会这样做。)
这是我目前的代码:(这是一个浓缩版,因为我在主体中发生了其他事情,所以如果不清楚,我深表歉意)
globals [ color-by-unhappiness? ]
turtles-own[
speed
speed-limit
speed-min
ideal-temp ;; The temperature I want to be at
output-heat ;; How much heat I emit per time step
unhappiness ;; The magnitude of the difference between my ideal
;; temperature and the actual current temperature here
]
patches-own[
temp
]
to setup
clear-all
setup-turtles
;;creating diffused light
set color-by-unhappiness? false ;; button
ask n-of number-of-lights patches [
sprout 1 [
set color white
set shape "circle"
set ideal-temp min-ideal-temp + random (max-ideal-temp - min- ideal-temp) ;;these are all sliders
set output-heat min-output-heat + random (max-output-heat - min- output-heat) ;;these are all sliders
set unhappiness abs (ideal-temp - temp) ;;ideal-temp is a button
color-by-ideal-temp
set size 2
]
]
reset-ticks
end
to setup-turtles
create-fears number-of-fears [
set color violet
set shape "circle"
setxy random-xcor random-ycor
set speed 0.1 + random-float 0.9
set speed-limit 1
set speed-min 0.00
]
end
to go
ask turtles [
if speed > speed-limit [set speed speed-limit]
fd speed
ask fears[
if patch-ahead 1 = nobody [rt 135]
if patch-right-and-ahead 45 1 != nobody and [pcolor] of patch-right-and-ahead 45 1 != black[left 45]
if patch-left-and-ahead 45 1 != nobody and [pcolor] of patch-left-and-ahead 45 1 != black[right 45]
ifelse [pcolor] of patch-here = yellow [set speed speed-min][fd speed]
]
if not any? turtles [ stop ]
;; diffuse heat through world
diffuse temp diffusion-rate
ask patches [ set temp temp * (1 - evaporation-rate) ]
ask turtles [ set temp temp + output-heat ask bugs [bug-move patch-here]]
recolor-turtles
recolor-patches
tick
end
to recolor-patches
ask patches [ set pcolor scale-color yellow temp 0 150 ]
]
end
我不能按原样使用你的代码;查看 MCVE guidelines 了解一些关于将代码减少到只包含必要部分的提示。
Netlogo 中的颜色可以字符串形式给出,但它也只是一个数字范围。如果您查看“工具”>“色板”,您会看到 "Yellow" 颜色的范围大致对应于 40 ~ 50。因此,如果您愿意,可以让它们使用数值范围而不是颜色名称。因此,使用这个不必要的复杂示例设置:
patches-own [ light? temp]
to setup
ca
ask patches [
set light? false
]
ask n-of 5 patches [
set light? true
set temp 150
]
recolor-patches
crt 10 [
move-to one-of patches with [ not ( pcolor > 40 and pcolor < 49 ) ]
]
reset-ticks
end
to recolor-patches
ask n-of 3 patches with [ light? ] [
if temp < 20 [
set temp temp + random 20
]
]
repeat 5 [
diffuse temp 0.1
]
ask patches [
ifelse temp > 0.25 [
set temp temp - 0.005
] [
set temp 0
]
set pcolor scale-color yellow temp 0 15
]
end
您可以让您的海龟移动并避开落在该数值范围内的斑块:
to go
recolor-patches
ask turtles [
ifelse [pcolor] of patch-ahead 1 > 40 and [pcolor] of patch-ahead 1 < 49 [
let target min-one-of neighbors [pcolor]
if target != nobody [
face target
fd 1
]
] [
rt random 60 - 30
fd 1
]
]
tick
end
编辑
作为 Seth Tisue pointed out,shade-of?
原语可以完成大于/小于逻辑语句的作用:
to go
recolor-patches
ask turtles [
ifelse shade-of? ( [pcolor] of patch-ahead 1 ) yellow [
let target min-one-of neighbors [pcolor]
if target != nobody [
face target
fd 1
]
] [
rt random 60 - 30
fd 1
]
]
tick
end
然而,这确实需要对 recolor-patches
程序稍作修改,因为 scale-color
将基色设置为 40(在 'yellow' 的情况下);只需要求带有 pcolor
的补丁将其颜色设置为黑色 (0),以便移动按预期进行:
to recolor-patches
ask n-of 3 patches with [ light? ] [
if temp < 20 [
set temp temp + random 20
]
]
repeat 5 [
diffuse temp 0.1
]
ask patches [
ifelse temp > 0.25 [
set temp temp - 0.005
] [
set temp 0
]
set pcolor scale-color yellow temp 0 15
if pcolor = 40 [
set pcolor black
]
]
end
我是第一次使用 NetLogo,需要编写一个简单的程序,其中我有一个光源可以将光漫射到其源补丁之外,还有一只海龟可以避开光。
我可以通过使用基本 'set pcolor yellow' 然后使用 'if patch-ahead [pcolor] = yellow [right 45][fd speed]' 类型命令来实现。然而,这并没有给我漫射光。
通过调整 HeatBugs 代码,我可以将颜色扩散到源补丁之外,但是我认为漫游乌龟不再将颜色识别为黄色,因为它是一种比例色。我尝试将代码设置为 != black 但这也不起作用。我假设这是因为补丁在每次滴答后都被重新着色。
有没有办法让乌龟识别漫射色块从而避开它们?或者用更简单的方法将光散射出去。 (我想要可变强度,所以使用邻居和黄色 -1 不会这样做。)
这是我目前的代码:(这是一个浓缩版,因为我在主体中发生了其他事情,所以如果不清楚,我深表歉意)
globals [ color-by-unhappiness? ]
turtles-own[
speed
speed-limit
speed-min
ideal-temp ;; The temperature I want to be at
output-heat ;; How much heat I emit per time step
unhappiness ;; The magnitude of the difference between my ideal
;; temperature and the actual current temperature here
]
patches-own[
temp
]
to setup
clear-all
setup-turtles
;;creating diffused light
set color-by-unhappiness? false ;; button
ask n-of number-of-lights patches [
sprout 1 [
set color white
set shape "circle"
set ideal-temp min-ideal-temp + random (max-ideal-temp - min- ideal-temp) ;;these are all sliders
set output-heat min-output-heat + random (max-output-heat - min- output-heat) ;;these are all sliders
set unhappiness abs (ideal-temp - temp) ;;ideal-temp is a button
color-by-ideal-temp
set size 2
]
]
reset-ticks
end
to setup-turtles
create-fears number-of-fears [
set color violet
set shape "circle"
setxy random-xcor random-ycor
set speed 0.1 + random-float 0.9
set speed-limit 1
set speed-min 0.00
]
end
to go
ask turtles [
if speed > speed-limit [set speed speed-limit]
fd speed
ask fears[
if patch-ahead 1 = nobody [rt 135]
if patch-right-and-ahead 45 1 != nobody and [pcolor] of patch-right-and-ahead 45 1 != black[left 45]
if patch-left-and-ahead 45 1 != nobody and [pcolor] of patch-left-and-ahead 45 1 != black[right 45]
ifelse [pcolor] of patch-here = yellow [set speed speed-min][fd speed]
]
if not any? turtles [ stop ]
;; diffuse heat through world
diffuse temp diffusion-rate
ask patches [ set temp temp * (1 - evaporation-rate) ]
ask turtles [ set temp temp + output-heat ask bugs [bug-move patch-here]]
recolor-turtles
recolor-patches
tick
end
to recolor-patches
ask patches [ set pcolor scale-color yellow temp 0 150 ]
]
end
我不能按原样使用你的代码;查看 MCVE guidelines 了解一些关于将代码减少到只包含必要部分的提示。
Netlogo 中的颜色可以字符串形式给出,但它也只是一个数字范围。如果您查看“工具”>“色板”,您会看到 "Yellow" 颜色的范围大致对应于 40 ~ 50。因此,如果您愿意,可以让它们使用数值范围而不是颜色名称。因此,使用这个不必要的复杂示例设置:
patches-own [ light? temp]
to setup
ca
ask patches [
set light? false
]
ask n-of 5 patches [
set light? true
set temp 150
]
recolor-patches
crt 10 [
move-to one-of patches with [ not ( pcolor > 40 and pcolor < 49 ) ]
]
reset-ticks
end
to recolor-patches
ask n-of 3 patches with [ light? ] [
if temp < 20 [
set temp temp + random 20
]
]
repeat 5 [
diffuse temp 0.1
]
ask patches [
ifelse temp > 0.25 [
set temp temp - 0.005
] [
set temp 0
]
set pcolor scale-color yellow temp 0 15
]
end
您可以让您的海龟移动并避开落在该数值范围内的斑块:
to go
recolor-patches
ask turtles [
ifelse [pcolor] of patch-ahead 1 > 40 and [pcolor] of patch-ahead 1 < 49 [
let target min-one-of neighbors [pcolor]
if target != nobody [
face target
fd 1
]
] [
rt random 60 - 30
fd 1
]
]
tick
end
编辑
作为 Seth Tisue pointed out,shade-of?
原语可以完成大于/小于逻辑语句的作用:
to go
recolor-patches
ask turtles [
ifelse shade-of? ( [pcolor] of patch-ahead 1 ) yellow [
let target min-one-of neighbors [pcolor]
if target != nobody [
face target
fd 1
]
] [
rt random 60 - 30
fd 1
]
]
tick
end
然而,这确实需要对 recolor-patches
程序稍作修改,因为 scale-color
将基色设置为 40(在 'yellow' 的情况下);只需要求带有 pcolor
的补丁将其颜色设置为黑色 (0),以便移动按预期进行:
to recolor-patches
ask n-of 3 patches with [ light? ] [
if temp < 20 [
set temp temp + random 20
]
]
repeat 5 [
diffuse temp 0.1
]
ask patches [
ifelse temp > 0.25 [
set temp temp - 0.005
] [
set temp 0
]
set pcolor scale-color yellow temp 0 15
if pcolor = 40 [
set pcolor black
]
]
end