If I select an agentset of patches, for example:
<observer> let myset patches with [abs pxcor < (grid-size / 2) and abs pycor < (grid-size / 2)]
<observer> ask myset [print self]
(patch 1 -1)
(patch -1 -1)
(patch 0 0)
(patch 1 0)
(patch 0 1)
(patch 0 -1)
(patch 1 1)
(patch -1 1)
(patch -1 0)
How can I move all my turtles (Which are as much as my selected patches, so 9) each one to a DIFFERENT patch?
I tried the following:
ask turtles [
move-to one-of myset
]
But now it is possible that different turtles are on the same patch. How can I avoid this?
Assuming that there aren't any other turtles on the patches, then just get them to choose an empty patch (not sure the syntax is quite right, but something like):
ask turtles [
move-to one-of myset with [not any? turtles-here]
]
Related
I ask a turtle A to move to patch that has a neighbor that has the same type of turtle with the turtle A. It is working well until the ERROR (MOVE-TO expected input to be an agent but got NOBODY instead) comes out. Visually there are still some available patches. How to code so that all available patches are occupied and report or stop the program when there is no more patch to occupy? Any comments will be really helpful. I did the following:
to set-move
ask migrants
[let pot-target patches with [value < 11 and not any? turtles-here]
let target pot-target with [count neighbors with [any? turtles-here with [value < 11]] = 1]
ifelse target != 0 and (status != "resident") [move-to min-one-of target [value]
set status "resident"
set color blue]
[]
]
end
This is the full code
breed [migrants migrant]
breed [residents resident]
patches-own [value]
turtles-own [income
status]
to setup
ca
let total problo + probmid + probhi
if (total != 100)
[print (word "prob is more than 100")]
ask patches [set value random-normal 10 3
let patch-value value
set pcolor scale-color (gray - 5) patch-value 10 3]
ask patches
[if random 100 < 3
[sprout-residents 1
[set color red
set shape "default"
set size 1
set status "resident"
]
]
]
end
to go
ask patches
[if random 100 < 1
[sprout-migrants 1
[set color green
set shape "default"
set size 1
set status "migrant"
set-move
]]]
end
to set-move
ask migrants
[let pot-target patches with [value < 11 and not any? turtles-here]
let target pot-target with [count neighbors with [any? turtles-here with [value < 11]] = 1]
ifelse target != 0 and (status != "resident") [move-to min-one-of target [value]
set status "resident"
set color blue]
[]
]
end
This line: let target pot-target with [count neighbors with [any? turtles-here with [value < 11]] = 1] identifies the patches where there is exactly 1 neighbour meeting those conditions. So a patch with 2 such neighbours is would not be available. From your description I think you really want >= instead of =:
let target pot-target with [count neighbors with [any? turtles-here with [value < 11]] >= 1]
I am trying to make a program where if a turtle detects a turtle ahead of it, it dies. Simple, but for some reason whenever one instance of this occurs, all of my turtles die, and I can't figure out how to correct this.
Here is my code:
to setup
ca
ask patches
[ set pcolor white
]
end
to spawn
crt 1
[ set color random 140
setxy random-xcor random-ycor
]
end
to wiggle
lt 100
rt 100
ifelse not any? turtles-on patch-ahead 1
[ fd 1
set pcolor color
]
[ die
]
end
to go
ask turtles
[ wiggle
]
end
It seems that ifelse not any? turtles-on patch-ahead 1 at one point always starts to evaluate as false, and I don't understand why.
I think the following example might shed a bit of light on what's happening to you:
to setup
clear-all
create-turtles 1 [
set xcor -0.5
set ycor -0.5
set heading 45
show (word "patch-here: " patch-here)
show (word "patch-ahead 1: " patch-ahead 1)
show (word "patch-ahead 1: " patch-ahead 1)
show (word
"not any? turtles-on patch-ahead 1: "
not any? turtles-on patch-ahead 1
)
show (word
"not any? other turtles-on patch-ahead 1: "
not any? other turtles-on patch-ahead 1
)
]
end
It's creating one turtle, placing it on the bottom left corner of the central patch and making it face north east, before checking for a few things. If you run the code, you'll get:
observer> setup
(turtle 0): "patch-here: (patch 0 0)"
(turtle 0): "patch-ahead 1: (patch 0 0)"
(turtle 0): "patch-ahead 1: (patch 0 0)"
(turtle 0): "not any? turtles-on patch-ahead 1: false"
(turtle 0): "not any? other turtles-on patch-ahead 1: true"
The key point is that the diagonal of a patch is longer than one (remember Pythagoras' theorem). This means that patch-ahead 1 can still be the same patch that the turtle is on! In this case, not any? turtles-on patch-ahead 1 will be false. Since your turtles are moving randomly across the world, this is bound to happen eventually.
Luckily for you, there is a simple solution. Just use other:
not any? other turtles-on patch-ahead 1
I want to ask to turtles who on the cells except the origin cell (x, y) = (0, 0) in one-dimensional cells spaces.
The following is sample code, however, this syntax includes the origin cell (x, y) = (0, 0).
ask turtles with [xcor < max-pxcor]
Please advice here. Thank you.
Do I have it right that you want to ask turtles that are not on patch 0 0? If so, maybe this will work for you:
to setup
ca
crt 100 [ move-to one-of patches ]
reset-ticks
end
to go
ask turtles with [ patch-here != patch 0 0 ] [
rt random 61 - 30
fd 1
]
tick
end
Edit: If you want to ask turtles not on the origin patch (in this example, 0 0) and turtles not on a patch with max-pxcor:
to go-2
ask turtles with [ floor xcor > 0 and xcor < max-pxcor ] [
rt random 61 - 30
fd 1
]
tick
end
I am struggling with coding identifying an expanding neighborhood based on an attribute of a patch. I need to check if there are a wall in my turtle's vision, if it is the case, my turtle should not be able to see through this wall.
Currently my recursive code works only with a vision's distance of 1 (that corresponds to neighbors), but over of 2 I get this error : A patch can't access a turtle or link variable without specifying which agent.
I don't know how it is possible to do this with an agent, do someone have an idea to do this?
breed[robots robot]
robots-own[step]
globals [ max-dist]
patches-own [ dist ]
to setup
ca
init-environement
create-robots 1 [init-robots]
end
to init-robots
set shape "person"
set size 4
move-to one-of patches with [no-wall? and (not any? turtles-here)]
set step 0
end
to init-environement
ask patches with [ (abs pxcor = max-pxcor) or (abs pycor = max-pycor) ]
[ set pcolor brown ]
ask patches with [ (abs pxcor = 20 and abs pycor > 15)
or (abs pycor = 10 and pxcor > 25)
or (pycor = 0 and pxcor < 1)][ set pcolor brown ]
ask n-of nbObstacles patches [ask patches in-radius random-float 2 [ set
pcolor brown ]]
end
to move-robot
let k 0
let v (neighbors with [no-wall? and (not any? turtles-here)])
if (any? v)[ move-to min-one-of v [dist] paint-agents k neighbors]
set step (step + 1)
output-show step
end
to paint-agents [k case]
let w ([neighbors] of case with [no-wall? and (not any? turtles-here)])
if (k < radius) [
set k k + 1
foreach w [
[x] ->
ask neighbors with [pcolor != brown][ set pcolor [color] of myself - 2
paint-agents k x]
]
]
end
to go
propagate
if any? patches with [pcolor = black] [ clear-output ask robots [move-robot] ]
end
to propagate
ask patches with [ no-wall? ][ set dist -1]
let p (patch-set patches with [pcolor = black])
let d 0
while [ any? p ]
[ ask p [ set dist d ]
set d d + 1
set p (patch-set [ neighbors with [no-wall? and ((dist = -1) or (dist > d))]] of p)
]
set max-dist max [ dist ] of patches
if (max-dist < 0) [ set max-dist 0 ]
ifelse (show-labels?)
[ ask patches with [no-wall?]
[ set plabel-color white
set plabel dist]
]
[]
end
to-report no-wall?
report pcolor != brown
end
There, my function which contains this problem is "paint-agents"
One way would be to get rid of patches in the sight that intersect a wall. You can imagine if you draw a line between each patch and if the wall lies on the drawn line, then that patch should be removed from things in vision.
This link may be useful on an implementation: Not see through the walls
I have this code in which turtles are suppose to change direction when they encounter walls and closed gates.
It is okay at the beginning but then it will give this message.
OF expected input to be a turtle agentset or patch agentset or turtle or patch but got NOBODY instead.
error while turtle 259 running OF
called by procedure GO
called by Button 'Go'
I can send the whole model.
if state = 1 [fd speed
ifelse [pcolor] of patch-at-heading-and-distance 0 1 = black or [pcolor] of patch-at-heading-and-distance 0 1 = red
[ lt random-float 90]
[fd 1]
; ifelse [pcolor] of patch-ahead 1 = blue
; [ lt random-float 360 ] ;; We see a blue patch in front of us. Turn a random amount.
; [ fd 1 ]
if (pxcor >= 89 and pxcor <= 90) and (pycor > 5 and pycor < 10) [facexy (87 + random 3) 25 fd speed]
;if (pxcor >= -10 and pxcor <= 1) and (pycor >= 6 and pycor <= 23 ) [facexy ( 7 + random 3) (26 + random 3) fd speed]
if (pxcor >= 85 and pxcor <= 90) and (pycor = 26) [
let choice random 2
fd speed
if choice = 1 [leftbench]
if choice = 0 [facexy (87 + random 3) 76]
]
if (pxcor >= 83 and pxcor <= 92) and (pycor >= 75 and pycor <= 77) [rightbench fd speed]
if pcolor = brown and not any? other turtles-here
[ move-to patch-here
set seated? true
set pcolor orange
]
]
Does your model have wrapping at the world edges turned off in one or both directions?
If so, then your code has to handle the possibility that sometimes this patch:
patch-at-heading-and-distance 0 1
won't exist, if the turtle is near a world edge. If that happens, then patch-at-heading-at-distance will return nobody, and when you try and do:
[pcolor] of patch-at-heading-and-distance 0 1 = black
you get the error, because no pcolor can be retrieved from a nonexistent patch.
The typical way to code around this is to do something like:
let target patch-at-heading-and-distance 0 1
if is-patch? target and ...
Finally, I wonder whether you really mean patch-at-heading-and-distance 0 1. It's equivalent to patch-at 0 1, which refers to the patch north of the turtle; not sure why you'd want that. If you want to test the patch ahead of the turtle (the patch the turtle will land in if it uses fd), use patch-ahead.