1

I got a set of turtles with links connected to each other. I wanted to retrieve the weight of the link between two nodes, i've tried searching but couldn't find any info on how to do it. I'm not using nw cause i don't want the shortest path. Any ideas? This is a section of my code:

to calculate-oldpath
let oldList [ 25 0 1 2 3 4 9 8 7 6 5 10 11 12 13 14 19 18 17 16 15 20 21 22 23 24]
let weighted-dist 0
( foreach ( but-last oldList ) ( but-first oldList ) [
[ a b ] ->
ask turtle a [
  let node-link link-with turtle b 
  ;Then retrieve weight link to do adding
]
] )
print weighted-dist
end

enter image description here

The S is my starting point (25 in the list) and E is end (24 in the list) I wanted to calculate the weight of this "orange path"

Alex Tan
  • 51
  • 1
  • 6

2 Answers2

2

Jen's answer about how to get the weight of a link is correct, but I would suggest an alternative way of computing the sum of these weights: using the sum primitive!

This requires turning your foreach into a map, but aside from that, it's pretty straightforward:

let weighted-dist sum (map [ [a b] ->
  [ [ weight ] of link-with turtle b ] of turtle a
] (but-last oldList) (but-first oldList))

Another small comment: using a list of who numbers might not be the best way to approach things, but I don't know enough about your problem to suggest an alternative...

Nicolas Payette
  • 14,847
  • 1
  • 27
  • 37
  • 1
    good point. I had assumed that it was a minimum working example but on re-read, it's clearly part of the real code. In which case, I would also switch to a `to-report` with a passed linkset – JenB May 17 '18 at 11:06
  • Sorry for the late reply, this list of numbers is a path in my program and i wanted to find the total amount of weight for this path. Also thank you for the suggestion – Alex Tan May 18 '18 at 08:18
  • A more precise question would be: how do you build that list in the first place? Unless it is hardcoded in your model like it is in your code example above, it would probably be a good idea to store direct references to turtles instead of storing their `who` number. It would make your code shorter and more expressive. It is generally better to avoid using `who` numbers if you can: 99.9% of the time, they're the wrong way to approach a problem, and using them may lead you down undesirable conceptual paths... – Nicolas Payette May 18 '18 at 14:44
  • Before that I've tried instead of hard-coding the list, i use 'one-of' to return a list of path, but it's not the path i wanted, so i resort to create my own list. I'll edit the post to insert another picture to show the reason for the list. – Alex Tan May 19 '18 at 06:20
1

Assuming you called the weight weight (in your links-own statement that you haven't shown) then something like this should work:

to calculate-oldpath
  let oldList [ 25 0 1 2 3 4 9 8 7 6 5 10 11 12 13 14 19 18 17 16 15 20 21 22 23 24]
  let weighted-dist 0
  ( foreach ( but-last oldList ) ( but-first oldList ) [
      [ a b ] ->
      ask turtle a [
        let node-link link-with turtle b 
        set weighted-dist weighted-dist + [weight] of node-link
      ]
  ] )
  print weighted-dist
end

Getting the attribute value for a link is exactly the same as getting the attribute value for a turtle or patch, you use of

JenB
  • 17,620
  • 2
  • 17
  • 45