Nw: weighted-path-to, nw: turtles-on-weight-path-to, and multiple equally weighted paths

I apologize in advance if this is a stupid question.

When nw: weighted-path-to is called, a list of links is returned describing the shortest path between the origin and destination turtles.

Likewise, calling nw: turtles-on-weighted-path-to returns a list of turtles on the shortest path between source and destination.

My understanding is that if there are two equally weighted paths between start and destination, both functions return one of those paths at random. This happens independently, and as such one set of links can be created for the shortest path, but a different set of turtles. This can be replicated using the following code:

extensions [nw]

links-own [ weight ]

to go
   clear-all
   create-turtles 4

   ask turtle 0 [ create-link-with turtle 1 [ set weight 2 ] ]
   ask turtle 0 [ create-link-with turtle 2 [ set weight 2 ] ]

   ask turtle 1 [ create-link-with turtle 3 [ set weight 2] ]
   ask turtle 2 [ create-link-with turtle 3 [ set weight 2] ]


   ask turtle 0 
   [
        let pathLinks nw:weighted-path-to turtle 3 "weight"
        let pathNodes nw:turtles-on-weighted-path-to turtle 3 "weight" 
        let pathUtility nw:weighted-distance-to turtle 3 "weight" 

        show pathLinks
        show pathNodes
        show pathUtility
   ]

end 

      

Which will happily produce:

(turtle 0): [(link 0 2) (link 2 3)]
(turtle 0): [(turtle 0) (turtle 1) (turtle 3)]
(turtle 0): 4

      

Obviously this is not a bug, but unfortunately it turned me off.

My question is, what's the smartest way to link these two routines with the goal of creating lists of links and turtles making up one randomly selected shortcut?

I guess it would be better to get the links back with nw: weighted-path-to and then ask the links to return both ends and do some unique operation to create a set of turtles along the way, if that is, I'm not sure how to save the order of the turtles. Does this make sense? So will you do it?

As always, thanks for reading.

Edit: This also applies to path-to and turtles-on-path-to in a topological network with multiple paths of equal length.

+3


source to share


1 answer


Good question! You can generate any list from another, but I think the turtle path to the link path is simpler:

;; Construct the turtle path, putting the current turtle on the front:
let turtle-path fput self nw:turtles-on-weight-path-to turtle 3 "weight"

;; Iterate through pairs of turtles, getting the link connecting them
let link-path (map [[link-with ?2] of ?1] but-last turtle-path but-first turtle-path)

      

Edit:

Nicolas is absolutely right about the "path to the turtle". However, his comment made me realize that you can use the almighty reduce

and always helpful other-end

to do this!



reduce [ lput [[other-end] of ?2] of (last ?1) ?1 ] fput (list self) nw:weighted-path-to turtle 3 "weight"

      

Edit2: The "link-path to turtle-path" code is pretty opaque. Here's an attempt to clarify this:

to-report add-link-to-turtle-path [ turtle-path next-link ]
  let last-turtle last turtle-path
  report lput [[other-end] of next-link] of last-turtle
end

;; turtle-procedure - Assumes the current turtle is the starting point of the path
to-report link-path-to-turtle-path [ link-path ]
  let start-of-path (list self)
  report reduce add-link-to-turtle-path fput start-of-path link-path
end

      

+3


source







All Articles