Tjaark Siemssen
Tjaark Siemssen

Reputation: 3

NetLogo: Creating Variable from Distance to Specific Patch

I have an issue in Netlogo giving agents the variable with distance (in patches) to a chosen patch that is water. The agents are supposed to act according to their distance to water. I want the agent to find the closest patch that has water = true, calculate the distance to that patch and save that specific distance in a variable that may change if the agent moves.

So the way I went about this is as follows:

turtles-own
[
next-river-patch
distance-to-river
]

to go
ask turtles
                                            ;find closest patch that has water = true
                                            ;calculate distance to that patch and save it in the turtles-own variable "distance-to-river"
    set next-river-patch (min-one-of patches in-radius 100 with [water = true] [distance myself])
    set distance-to-river distance next-river-patch
    
    
                                           ;check whether there is water in peception radius 
                                           ;and whether directed walk is initiated
    ifelse random-float 1 > water-directedness 
   and
    patches with [water = true] in-radius 300 = true

    [walk-directed]
    [walk-undirected]
    
    tick
    
end 


to walk-directed
                                            ;deviation of random normal is smaller, the further from next 1st order river and is raised by a high pioneering rate
                                            ;with half the distance of the perception radius of 300 resulting in a deviation of 45 degrees and 
                                            ;a pioneering rate of 0.5 leading to no influence onto the final deivation and a pioneering rate of 1 doubling the deviation. 
 let deviation 90 * ( 1 - distance-to-river / 300) * ( 2 * pioneering-along-rivers)
  
  face min-one-of patches with [ water  = true][distance myself]
   rt random-normal 0 (1 * deviation)
  fd 1
end

to walk-undirected
 ask turtles [
  
  rt random-normal 0 (2.25 * descision-randomness * 100)
    fd 1
    
                                            ;it turns into a cumulated normal direction (with the chance of moving straight ahead being highest) and the standard 
                                            ;deviation being defined by the descision-randomness-slider, with default being 45° and max being 225°
  ]
  
end

so the issue I am having is that the line

"set next-river-patch (min-one-of patches in-radius 100 with [water = true] [distance myself])"

returns a variable containing the coordinates of the next patch that is water. Anyhow, i dont want the coordinates, but the distance to that patch so that i can use it in my cumulated direction. set distance-to-river distance next-river-patch does not work.

Does anybody have an idea as to why that could be? I am relatively new to NetLogo so the answer may possibly seem very obvious.

Thank you very much!

P.S.: Another, possibly unrelated issue with my model is that agents turn dozens of times before each tick instead of each agent doing a single turn before going forward by 1. So if you happen to see the solution to that by looking over the code, i would also be forever grateful.

Upvotes: 0

Views: 300

Answers (1)

TurtleZero
TurtleZero

Reputation: 1064

Easiest problem first:

Why are some turtles moving more than once per tick?

walk-undirected is called by turtles, and it does ask turtles. So every turtle is making every turtle so the action. It should be like the other procedure and just do that action, no ask.

patches ...... = true

This is an error. You are comparing the agentset made by patches with ... to the boolean value true. An agentset can never be true OR false. An agentset can be empty, or have one or more agents in it. The any? reporter is used to determine if an agentset has any members.

The main question

It looks like you are already doing what you are asking. The line starting set nearest-river-patch gets the nearest water patch, and the line set distance-to-river calculates the distance to that patch. Not that if there is not any water in radius 100, next-river-patch will be nobody.

I don't see anything that would give you the coordinates (as a list?). Maybe try some parentheses to make sure the reporters are being evaluated in the right order:

set next-river-patch (min-one-of ((patches in-radius 100) with [water = true]) [distance myself])

You could also use with-min

set next-river-patch one-of ( ( ( patches in-radius 100 ) with [ water = true ] ) with-min [distance myself] )

Otherwise, correct the other errors and see what happens.

Upvotes: 0

Related Questions