bksnlow
bksnlow

Reputation: 137

NetLogo: Set Direction of agents when condition is fulfilld. If the agent is at city there is a 60% chance that it leaves

Here im traing to say that if an agent has a certain amount of point he goes to a patch. If within that patch he has a 60% chance of leaving the city, to a diffente patch. For some reason when they leave the patch of city they return immediately instead of going to the new patch.

to go
ask turtles [
move
move-to-ciudad
move-to-nociudad
violencia
]
tick
end
to ciudad
ask patch -15 -15[ set pcolor white ]

end

to nociudad
ask patch 15 -15 [ set pcolor 98 ]

end


to move
ifelse ycor > 15
[ set heading 180 fd 10]
[ set heading 90 - (random 180)
fd 1;adelante
]
end

to move-to-ciudad
if puntos >= 60
[facexy -15 -15
fd 2
]

end

to move-to-nociudad
if (distancexy -15 -15) <= 1
[if random 100 > 40
  [facexy 15 -15
   fd 2]
  ]
  ]
end

Upvotes: 1

Views: 60

Answers (1)

JenB
JenB

Reputation: 17678

In the go procedure, you have

ask turtles
[ move
  move-to-ciudad
  move-to-nociudad
  violencia
]

So each turtle is asked to do that series of procedures. Imagine a turtle that has at least 60 puntos. What happens when it is the turtle that is being asked?

First it does the move procedure and chooses a direction either randomly or generally the direction it was already going. Then it does the move-to-ciudad procedure. It has enough puntos so faces the city and moves forward 2. But then it does the move-to-ciudad. This means that if it gets close to the city, it immediately (in the same tick) has a 60% probability of facing the other patch (at 15 -15). Fine, that's what you want and let's say it used that chance and left the city by forwarding 2.

Now imagine the same turtle on the next tick. It moves generally forward with the move procedure. But it still has 60 puntos, so when it does the move-to-ciudad procedure, it immediately turns around again and moves back to the city. Nothing has changed, the reason it went to the city in the first place is still true so it will still try to go to the city.

Upvotes: 2

Related Questions