Programming the movement of turtles

Hi,
maybe someon ecan help me to find a solution. I am trying to change the existing code of a wolf-sheep predation in NetLogo. I want to achive the goal that my wolves can only move on to a adjacent unoccupied patch without any wolves and the same for my sheep. The idea was to say that they should move to an empty patch, but the word empty is shown as not definied. So the coding below is the point where I do not what to do . Also I want to say, that the offspring of the sheep should be placed on a unoccupied patch. How can I change that in the coding?

to move ; turtle procedure
ask sheep [
rt random 50
lt random 50
fd 1
]
ask wolves [
rt random 50
lt random 50
fd 1
]
end

to reproduce-sheep ; sheep procedure
if random-float 100 < sheep-reproduce [ ; throw “dice” to see if you will reproduce
set energy (energy / 2) ; divide energy between parent and offspring
hatch 1 [ rt random-float 360 fd 1 ] ; hatch an offspring and move it forward 1 step
]
end

I would be thankful if somebody could help and explain.
Thanks, Nicole

1 Like

Here’s what I came up with:

The above code snippet includes a commented-out line of code, which I used for debugging this code. If you uncomment it, the problems with my code should become evident.

My code, as is, doesn’t work great, and I don’t recommend running it, since it will probably cause your NetLogo application to hang or crash.

In this model, it’s incredibly common for wolves to be near other wolves and sheep to be near other sheep. The rt and lt code says to only change trajectory by a small amount, but when they run into another agent, adjusting their heading by a small amount still means that they’re likely to be facing the same agent. If an agent is surrounded by other agents, the while loop will never stop running.

To make my code run more smoothly, you might want to limit how many times the while loop can run. You might also want to extend the range that it can check to be more than a 100-degree arc, or to sweep through the arc with more purpose than my code, which just tries normally-distributed random angles within that arc. And you might need to limit the number of agents, since, as the populations grow, it can become entirely impossible to find an open space to move to.

1 Like