CoRobat
Do you have questions or comments about this model? Ask them here! (You'll first need to log in.)
WHAT IS IT?
(a general understanding of what the model is trying to show or explain)
HOW IT WORKS
(what rules the agents use to create the overall behavior of the model)
HOW TO USE IT
(how to use the model, including a description of each of the items in the Interface tab)
THINGS TO NOTICE
(suggested things for the user to notice while running the model)
THINGS TO TRY
(suggested things for the user to try to do (move sliders, switches, etc.) with the model)
EXTENDING THE MODEL
(suggested things to add or change in the Code tab to make the model more complicated, detailed, accurate, etc.)
NETLOGO FEATURES
(interesting or unusual features of NetLogo that the model uses, particularly in the Code tab; or where workarounds were needed for missing features)
RELATED MODELS
(models in the NetLogo Models Library and elsewhere which are of related interest)
CREDITS AND REFERENCES
(a reference to the model's URL on the web if it has one, as well as any other necessary credits, citations, and links)
Comments and Questions
turtles-own
[
robot-type ;;
vision-radius ;;
life ;;
if-find-prey? ;; no-use
prey-xcor ; no-use
prey-ycor ; no-use
prey-heading ; no-use
]
to init
clear-all
set-patch-color
create-Robots
end
to set-patch-color
ask patches
[
set pcolor 5
]
end
to create-Robots
;;1) create captures
create-turtles count-of-hunter
[
set robot-type "hunter"
set color 40 + random 100
set size 10
setxy random-xcor random-ycor
set shape "butterfly"
set vision-radius hunter-vision-radius
set life 100
set if-find-prey? false
]
;; 2) create invades
create-turtles count-of-prey
[
set robot-type "prey"
set color red
set size 10
setxy random-xcor random-ycor
set shape "bug"
set vision-radius prey-vision-radius
set life 100
]
end
to escape
ask turtles with [robot-type = "prey"]
[
ifelse (life > 0 )
[
find-escape-direction
adjust-heading-when-in-edge
let Nearist-Turtles-List (turtles-on (patches in-radius prey-vision-radius)) with [(robot-type = "hunter") and (life > 0 )]
draw-sensor-vision-field 5
ifelse ((count Nearist-Turtles-List) > 0)
[
fd prey-escape-speed * (life / 100)
]
[
ifelse ( (random 10) = 0 )
[ fd prey-escape-speed * (life / 100) ]
[ fd prey-normal-speed * (life / 100) ]
]
if (if-show-vision-filed? = true )
[ draw-sensor-vision-field 66 ]
] ;; else : life = 0 , when prey is dead
[
set label "Die!"
set label-color red
set heading 90
]
]
end
to capture
ask turtles with [ (robot-type = "hunter") ] ;;
[
ifelse (life > 0 )
[
find-capture-direction
adjust-heading-when-in-edge
;;draw-sensor-vision-field 5
let count-prey-in-vision count ( (turtles in-radius hunter-vision-radius ) with [(robot-type = "prey") and (life > 0)] )
draw-sensor-vision-field 5
ifelse (count-prey-in-vision > 0 )
[
fd hunter-capture-speed * (life / 100)
]
[
ifelse (random 10 = 0 )
[ fd hunter-capture-speed * (life / 100) ]
[ fd hunter-normal-speed * (life / 100) ]
]
if (if-show-vision-filed? = true )
[ draw-sensor-vision-field 66 ]
]
[
set label "Die!"
set label-color red
set heading 90
]
]
end
to find-capture-direction
let temp-xcor 99999
let temp-ycor 99999
let temp-distance 99999
let tempHeading 99999
let capture-prey-distance 99999
let prey-who 99999
let count-prey-in-vision count ( (turtles in-radius hunter-vision-radius ) with [(robot-type = "prey") and (life > 0) ])
ifelse (count-prey-in-vision = 0 ) ;; if not find any prey or prey's life = 0
[
if ( (random 100) < 1 )
[ set heading ( heading + (-1)^(random 2) * 40) mod 360 ]
set if-find-prey? false
]
;; else : select one active prey as capture target
[
ask one-of (turtles in-radius hunter-vision-radius) with [(robot-type = "prey") and (life > 0 )] ;; select one prey randomly , rather than select the nearest;; ;may can be improved
[ ;; //////////////////////;may can be improved/////////
set temp-ycor ycor
set tempHeading heading
set capture-prey-distance distance turtle who
set prey-who who
]
set prey-xcor temp-xcor
set prey-ycor temp-ycor
set prey-heading tempheading
set if-find-prey? true
face turtle prey-who
]
end
to find-escape-direction
let MinDistance 99999
let temp-heading heading
let Nearist-Turtles-List (turtles-on (patches in-radius prey-vision-radius)) with [(robot-type = "hunter") and (life > 0 ) ] ;; judge if exists hunters in vision field
;; if the prey finds hunter(s) in it's vision field, find the nearist neighbor and learn its heading
ifelse ((count Nearist-Turtles-List) > 1 )
[
ask Nearist-Turtles-List
[
if ( (distance myself != 0 ) and (distance myself < MinDistance ) )
[
set MinDistance (distance myself)
set temp-heading heading
]
]
]
;; else : if the prey can not find any hunter, flies freedomly
[
if ((random 100) < 1)
[ set temp-heading ( heading + (-1)^(random 2) * 40) mod 360]
]
set heading temp-heading
end
to escape-and-capture
escape
Hunter-commincate
Hunter-Coordinate
capture
life-change
end
to life-change
let is-captured? false
ask turtles with [robot-type = "prey"]
[
ask patch-here
[
;; if in the patch where the prey is , there exists hunter, then the prey is caputured
if (count (turtles-here with [(robot-type = "hunter") and (life > 0)]) > 0 ) [
set is-captured? true
ask turtles-here with [(robot-type = "hunter") and (life > 0)]
[ set life life - 1 ]
]
]
if is-captured?
[ set life life - 1 ]
set is-captured? false ;; judge the next prey, so reset is-captured? to false
]
end
to draw-sensor-vision-field [ field-color ]
let tempi 0
while [tempi <= 360]
[
let temp-patch ( patch-at-heading-and-distance tempi vision-radius)
if (temp-patch != nobody)
[ask temp-patch
[set pcolor field-color ]
]
set tempi tempi + 10
]
end
to Hunter-commincate
ask turtles with [ (robot-type ="hunter") and (life > 0) ]
[
;; get active prey List in the hunter's vision field
let preyList-in-vision-field (turtles in-radius hunter-vision-radius) with [(robot-type ="prey") and (life > 0)]
;; if there exists active preys in the hunter's vision field,
if ( count preyList-in-vision-field > 0 )
[
let temp-prey-who 99999
;; select one active prey from the preyList
ask one-of preyList-in-vision-field
[
set temp-prey-who who
]
;; broadcast the prey's infomation to the hunter's neighbors in it's vision field
ask turtles in-radius hunter-vision-radius with [(robot-type ="hunter") and (life > 0)]
[
set if-find-prey? true
face turtle temp-prey-who
]
]
]
end
to adjust-heading-when-in-edge ;; when turtle in edge (left Boundary, right Boundary, up Boundary, down Boundary), adjust it's heading
if (abs(xcor) < 0.5) and (heading > 179) ;; in left boundary and goes on to the left
[ set heading random 180 ]
if (xcor > Max-pxcor - 1) and (heading < 180) ;; ;; in right boundary and goes on to the right
[ set heading ( 180 + random 180) ]
if (abs(ycor)< 0.5 ) and (heading > 90 and heading < 270) ;; ;; ;; in down boundary and Continue to go down
[
let tempi random 2
ifelse (tempi = 0 )
[ set heading random 90 ]
[ set heading random (360 - random 90) ]
]
if (ycor > Max-pycor - 1) and ( (heading >= 0 and heading < 90) OR ( heading > 270 and heading <= 360) )
[ ;; in up boundary and Continue to go up
set heading (90 + random 180)
]
end ;
to Hunter-Coordinate
end
There is only one version of this model, created over 10 years ago by zengzhen shao.
Attached files
| File | Type | Description | Last updated | |
|---|---|---|---|---|
| CoRobat.png | preview | Preview for 'CoRobat' | over 10 years ago, by zengzhen shao | Download |
This model does not have any ancestors.
This model does not have any descendants.
Download this model