告诉netlogo中的一只乌龟在程序完成后要走什么位置
所以,我让我的机器人按照这个项目成功地给植物浇水 在完成所有植物(海龟)的“浇水”后,我希望海龟回到基地。 所以,我有一句话:告诉netlogo中的一只乌龟在程序完成后要走什么位置,netlogo,Netlogo,所以,我让我的机器人按照这个项目成功地给植物浇水 在完成所有植物(海龟)的“浇水”后,我希望海龟回到基地。 所以,我有一句话: 如果计数为0[移到基数0] 那么, 如果在基地没有离开[停止] 其中,基本无叶的定义为 to-report at-base-no-leaves ifelse any? bases-here and count leaves = 0 [report true] [report false] end 但我得到了这个错误: 您不能在海龟上下文中使用tick,因
如果计数为0[移到基数0]
那么,
如果在基地没有离开[停止]
其中,基本无叶的定义为
to-report at-base-no-leaves
ifelse any? bases-here and count leaves = 0
[report true]
[report false]
end
但我得到了这个错误:
您不能在海龟上下文中使用tick,因为tick只是观察者
我怎样才能让机器人在基地移动并停在那个里?感谢您的帮助
以下是我的完整代码:
;plant watering agent
breed [robots robot] ; Name of the breed of plant robots
breed [leaves leaf] ; Name of the breed of plants in need of water
breed [ watered water-1] ; Name of the breed of watered plants
breed [ bases base] ; Name of the breed of charging station
globals
[
background-colour ; colour of the background except for obstacles
obstacles-colour ; colour of the obstacles
robot-colour ; colour of the robot
clock ; tracking time
watered-plants ; plants watered
distance-traveled ; distance traveled by the robot
battery-life ; battery life left
water-poured ; how much water has been used
]
to setup
;clear all
clear-all
set-default-shape robots "ufo side" ; sets shapes for agent
set background-colour green + 3 ; set colour of background light green
set obstacles-colour brown ; set colour of obstacles brown
set robot-colour gray ; set colour of robot to gray
setup-base
set clock 0 ;initialize time
set distance-traveled 0 ;initialize distance
set battery-life 100 ;initialize battery life to 100%
set water-poured 0 ;initialize water used to 0
;set boundary obstacles. These patches tell the robot to stay within identified bounds.
ask patches
[
set pcolor background-colour ; set colour of background
if (pxcor >= max-pxcor - boundary-width) ; boundary width can vary
[ set pcolor brown ]
if (pxcor <= min-pxcor + boundary-width)
[ set pcolor brown ]
if (pycor >= max-pycor - boundary-width)
[ set pcolor brown ]
if (pycor <= min-pycor + boundary-width)
[ set pcolor brown ]
]
setup-leaves ;add plants to the world. These are added after the border is created so not to create the plants outside of the 'border of the house'
; creates colour, size and random location of single robot
create-robots 1
[
set size robot-size
set color robot-colour
;let this-patch one-of patches with [pcolor != obstacles-colour] ; sets an initial random position within the outside boundary; this works if we want to set a random position every time for the robot
;set xcor [pxcor] of this-patch
;set ycor [pycor] of this-patch
setxy 15 -14 ; robot starts at the charging station
]
end
; creating plants (leaves)
to setup-leaves
create-leaves num-plants [ ; number of plants can vary
rand-xy-co ; set random positions for the plants
set shape "flower" ; initialize the plant to color red and size 2
set color red
set size 2
]
end
;setup base (charging station)
to setup-base
create-bases 1 [
set shape "electric outlet"
set color white
setxy 16 -15
set heading 0
set size 3
]
end
;detecting a plant (leaf)
to-report detect-leaf
ifelse any? leaves-here
[report true] ; set as true if plant breed is detected
[report false]
end
;watering a leaf (plant)
to water-leaf
set watered-plants watered-plants + 1 ;when a plant is watered, increase counter by 1
set water-poured watered-plants * ((4 * (100 - soil-moisture)) / 50) ; when plant is watered, increase water-poured counter
; this formula is based on the average amount of water a houseplant needs >> 4 oz at 50% soil moisture
set clock clock + 10 ; add 10 ticks to the clock when watering plants since the robot has to spend time to water
set battery-life battery-life - 5 ; 5% of battery life is spent for each plant watered
ask one-of leaves-here [ ; mark breed as watered when plant is watered; change flower color to yellow
set breed watered
set shape "flower"
set color yellow
set size 3 ;increase the size of the flower (grown)
]
end
to-report at-base
ifelse any? bases-here
[report true]
[report false]
end
to-report at-base-no-leaves
ifelse any? bases-here and count leaves = 0
[report true]
[report false]
end
to charge
set battery-life 100 ;charge with 100% battery
set clock clock + 10 ;add additional time to the clock
end
; This defines how the robot should move.
to make-move
if battery-life <= 0 [stop] ; robot cannot run if it doesn't have any battery life
if at-base and battery-life <= 99 [charge]
;This behaviour is modified from the Look Ahead Example model in the Models Library
let this-patch patch-ahead 1
if detect-leaf [water-leaf stop]
ifelse (this-patch != nobody) and ([pcolor] of this-patch = obstacles-colour)
[ lt random-float 360 ] ; We see an obstacle patch in front of us. Turn a random amount.
[ fd 1 ] ; Otherwise, it is safe to move forward.
set distance-traveled distance-traveled + 1 ; every step increases distance-traveled counter by 1
end
;Procedure when the button Go is pressed
to go
;The robot moves around.
if count leaves = 0 [move-to base 0]
if at-base-no-leaves [stop] ;stop when all leaves are watered
set clock clock + 1 ;update time
;wait .05 ; wait for better viewing
set battery-life 100 - (distance-traveled / 900) ; battery life varies based on distance traveled
ask robots [without-interruption [make-move]] ;robots move
update-and-plot ;update the graphs
tick
end
;plot velocity graph
to update-and-plot-velocity
set-current-plot "Velocity"
plotxy distance-traveled clock
end
;plot water usage graph
to update-and-plot-water-usage
set-current-plot "Water Usage"
plotxy water-poured watered-plants
end
;plot energy usage graph
to update-and-plot-energy-usage
set-current-plot "Energy Usage"
plotxy clock (100 - battery-life)
end
;plot watered plants graph
to update-and-plot-watered-plants
set-current-plot "Watered Plants"
plotxy clock watered-plants
end
;plot wilting plants graph
to update-and-plot-wilting-plants
set-current-plot "Wilting Plants"
plotxy clock count leaves
end
;call all the graphs
to update-and-plot
update-and-plot-velocity
update-and-plot-water-usage
update-and-plot-energy-usage
update-and-plot-watered-plants
update-and-plot-wilting-plants
end
; Creates obstacles in the environment by drawing them on the world
to make-obstacles
if mouse-down?
[ ask patches
[ if ((abs (pxcor - mouse-xcor)) < 1) and ((abs (pycor - mouse-ycor)) < 1)
[set pcolor obstacles-colour]]
]
end
; Removes obtacles in the environment.
to erase-obstacles
if mouse-down?
[ ask patches
[ if ((abs (pxcor - mouse-xcor)) < 1) and ((abs (pycor - mouse-ycor)) < 1)
[set pcolor background-colour]]
]
end
; Detecting obstacles
; Obstacles are obstacles and other agents.
to-report detect-obstacle
ifelse any? patches in-cone 2 90 with [pcolor = brown] or any? other robots in-cone 2 90
[report true]
[report false]
end
; The robot avoids any obstacles in the environment.
;to avoid-obstacles
;if (count patches in-cone radius-length radius-angle with [pcolor = obstacles-colour] > 0) ; there is an obstacle nearby
;[
;set heading heading + random 45 - random 45
;]
;end
; This instructs the agent to move the pen up if it is down, or vice versa.
to plot-paths
ifelse (pen-mode = "up")
[ pen-down ]
[ pen-up ]
end
; Utilities. Positions a new turtle randomly on the grid, taking care not to
;place it on top of another turtle
to rand-xy-co
move-to one-of patches with [ pcolor != obstacles-colour and not any? turtles-here ]
end
;植物浇水剂
繁殖[机器人];植物机器人品种名称
繁殖[叶子];需要水的植物品种名称
品种[浇水-1];浇水植物的品种名称
繁殖[基地];充电站的品种名称
全局变量
[
背景颜色;除障碍物外的背景颜色
障碍物颜色;障碍物的颜色
机器人的颜色;机器人的颜色
时钟
浇水的植物;浇水的植物
行驶的距离;机器人行驶的距离
电池寿命;剩余电池寿命
倒出的水;用了多少水
]
设置
;清除所有
清除所有
设置默认形状机器人“ufo侧”;为代理设置形状
设置背景色绿色+3;设置背景颜色为浅绿色
设置障碍物颜色为棕色;设置障碍物的颜色为棕色
设置机器人颜色为灰色;将机器人的颜色设置为灰色
设置库
将时钟设置为0;初始化时间
将行驶距离设置为0;初始化距离
将电池寿命设置为100;将电池寿命初始化为100%
设置浇水0;将用水初始化为0
;设置边界障碍。这些补丁告诉机器人保持在确定的边界内。
询问补丁
[
设置pcolor背景颜色;设置背景颜色
如果(pxcor>=最大pxcor-边界宽度),则边界宽度可以变化
[设置颜色为棕色]
如果(pxcor=最大pycor-边界宽度)
[设置颜色为棕色]
如果(pycor>4盎司,土壤湿度为50%
将时钟设置为+10;由于机器人必须花时间浇水,因此在浇水时,在时钟上加上10个滴答声
设置电池寿命电池寿命-5;每个浇水的工厂消耗5%的电池寿命
在这里询问一片叶子【当植物浇水时,将品种标记为浇水;将花的颜色改为黄色】
给品种浇水
定形“花”
设置颜色为黄色
设置大小3;增加花的大小(已生长)
]
结束
报到
还有吗?这里是基地
[报告真实]
[虚假报道]
结束
在基地报告没有叶子
如果还有?此处为基数,计算叶数=0
[报告真实]
[虚假报道]
结束
控告
将电池寿命设置为100;使用100%电池充电
将时钟设置为+10;为时钟添加额外的时间
结束
;这定义了机器人应该如何移动。
行动
如果电池寿命您在at base no leaves
reporter中使用base here
,这使其成为“海龟或补丁”报告器。然后您在go
过程中直接调用at base no leaves
,这反过来又使go
成为“海龟或补丁”程序…在其中您不能调用tick
为了避免此问题,只需从应该从中调用的上下文(即海龟上下文)中调用at base no leaves
:
ask robots [
if count leaves = 0 [move-to base 0]
if at-base-no-leaves [stop]
]
作为一种内部模式,您在at base no leaves
和其他一些记者中使用了一种模式,您应该避免:
to-report at-base-no-leaves
ifelse any? bases-here and count leaves = 0
[report true]
[report false]
end
无需使用ifelse
,这里:您基本上只是使用它将布尔表达式转换为…具有相同值的布尔表达式(例如,如果为真,则为真,如果为假)。只需直接使用您的条件:
to-report at-base-no-leaves
report any? bases-here and count leaves = 0
end
您还可以将count leaves=0
替换为not any?leaves
,以保持一致性和清晰性
最后,您可能需要在设置过程的末尾添加内容。谢谢。这非常有帮助,而且很容易理解您的解释方式。我是NetLogo的新手,但这几天我非常喜欢它。