User847462
User847462

Reputation: 81

NetLogo: ask turtle to set destination and keep walking towards it until reached

Goal: I am attempting to make a turtle pick a destination, then continue walking towards it until the destination is reached. At that point, the turtle returns to its original patch and picks another destination, walks towards it, repeat.

Issue: The selected destination sometimes changes while the turtle is walking towards it. I need some means of telling the turtle to hold the original destination until it reaches it.

Details: Here is my relevant code. Turtles are building territories. They have a territory center-point ("start-patch") from which they choose a destination to walk to and claim. Destination is based on the patch with "highest-value" where value should be the patch's benefit ("benefit-to-me") divided by the distance away from the start-patch ("cost-to-me"). I think turtles are constantly reassessing cost-to-me while walking, however. They shouldn't do this--highest-value must be assessed while standing on the start-patch.

How can I fix this so a turtle assesses highest-value while standing on the start-patch, sets a destination, and moves towards it until reached?

patches-own
[
  owner  ;; once part of a territory, owner becomes the turtle.
  benefit  ;; i.e., food available in a patch; used to assess "highest-value" to the turtle.
]

turtles-own
[
  start-patch  ;; the territory center; turtle returns here after reaching destination.
  destination  ;; the patch turtle wants to claim for its territory. 
  territory  ;; the patches the turtle owns.
]

to go
   tick
   ask turtles
    [
     pick-patch
    ]
end

to pick-patch
     set destination highest-value  ;; calculated in reporters, below.
     ifelse destination != nobody [
       ask destination [set pcolor red]  ;; reveals that destination changes occasionally before original destination is reached.
       travel]  
     [give-up]  ;; (will reposition start-patch to a new site if no destinations available.)
end

to travel
     face destination forward 1   ;; **should** keep original destination, but it doesn't.
     if patch-here = destination
       [update-territory  
        move-to start-patch ]  ;; return to the start-patch, and should only NOW assess new destination.             
end

to update-territory
     set owner self ;; and so on....
end

;;;---Reporters for highest-value:---

to-report highest-value    ;; this appears to be changing while turtle moves...how fix this?
     let available-destinations edge-patches      
     report max-one-of available-destinations [benefit-to-me / cost-to-me]         
end

to-report benefit-to-me
     report mean [benefit] of patches in-radius 1  ;; i.e., moving window to find high-benefit cluster
end

to-report cost-to-me
     report distance myself
end

to-report edge-patches
     report (patch-set [neighbors4] of territory) with [owner = nobody]
end

(Note: instead of "forward 1," I realize I could just use "move-to." I will eventually build in obstacles, however, and turtles will need to walk towards the destination to check for obstacles.)

Update: I think the issue could be addressed within the "cost-to-me" reporter? I tried making this change:

to-report cost-to-me
     report distance [start-patch] of myself
end

Should this accomplish what I'm after? It would take away the "distance myself" part so that this cost remains constant. The other idea I've had is that "pick-patch" or "travel" may need something along the lines of "ifelse patch-here != destination [forward 1...]" but that doesn't seem to work either.

I tried the "while" loop idea recommended below (thanks!) and that seems to introduce a new host of odd behavior. I'm not sure how to code that out if I go that route. Something like this doesn't work (they just stop moving):

to travel
     while [distance destination > 1] 
     [face destination forward 1]  
     if patch-here = destination
       [update-territory  
        move-to start-patch ]     
end

I'm new to this; thanks in advance for any help!

Second update: I think that the change I made in the previous update (report distance [start-patch] of myself) fixed part of my problem (assuming that line makes sense?), but left one issue. If there is a tie among patches with highest-value, the turtle still switches destination midway to its selected patch. So it still goes back to the original problem of having the turtle set and keep a destination until it is reached. Any ideas on how to fix this?

Upvotes: 1

Views: 1925

Answers (2)

JenB
JenB

Reputation: 17678

The difficulty with using while is that it will move the whole way during the tick. Since the turtle returns to the start patch, why don't you simply add a condition that it only picks out a destination when it is at the start patch? So the code would look like this:

to go
   tick
   ask turtles
   [ if patch-here = start-patch [pick-patch]
   ]
end

Upvotes: 2

Luke C
Luke C

Reputation: 10301

You're right- every time a turtle runs pick-patch, it goes through the step of setting destination to highest-value. Then, it'll move forward one and check if it has arrived. At that point, whether or not it has reached its destination, the other turtles (if there are any) will have a chance to run pick-patch. Once all other turtles have done so, your original turtle will again set its destination to a freshly assessed highest value. So, since highest-value is dependent on distance, and the turtle's spatial coordinates change as it moves, some other patch might have the highest-value from the turtle's new position.

One way you could accomplish what you are after is to use while so that your turtle stays within the procedure until whatever criteria you designate are reached. For a very simple example:

to move-until
  ask turtles [
    let start-patch patch-here
    let destination patch-ahead 10
    while [ distance destination > 1 ] [
      fd 1
    ]
  ]
end

Obviously you will have to modify that to suit your needs, but it should get you started.

Upvotes: 1

Related Questions