deivid87
deivid87

Reputation: 3

Sort list by one item and identify record that meets a condition in Netlogo

I have a list of sublists, something like the following

[[0 “coal” 500 1430] [0 “gas” 300 1300] [1 “coal” 600 1500] [1 “gas” 700 1400]]

I would like to do four things:
1. Sort the main list by item 3 of sublists
2. Cumulatively sum item 2 of sublists until a certain value is reached.
3. Identify item 3 of the last list added.
4. Then I’d like to identify items 0 and 1 of lists that were added to the loop in point 2 and ask those turtles to do something.

I've been exploring tables, lists, etc but struggling with this complex bit of code. Can people suggest how they would code this?

Thanks in advance for the help!

Upvotes: 0

Views: 63

Answers (1)

mikbink
mikbink

Reputation: 36

The following seems to answer the case, I think...though there may be more elegant ways

to go
  create-turtles 2
  let l [[0 "coal" 500 1430] [0 "gas" 300 1300] [1 "coal" 600 1500] [1 "gas" 700 1400]]
  ;sort the list by item 2 
  let sorted sort-by bigger l
  show sorted
  ;accumulate item 3 until limit reached
  let k 0
  let n 0
  let limit 2800
  let turtleNos []
  let fuels []
  while [k < limit]
  [
    set k k + item 3 ( item n sorted )
    ;accumulate item 0 and 1
    set turtleNos lput item 0 ( item n sorted ) turtleNos 
    set fuels lput item 1 ( item n sorted ) fuels 
    set n n + 1
  ]
  show k
  ;show item 3 for the last item added to k
  show item 3 (item ( n - 1 ) sorted)
  ;accumulated lists - note non-unique
  show turtleNos
  show fuels
  (foreach turtleNos fuels 
      [ [x y] -> ask turtle x [show y] ])

end
to-report bigger [l1 l2]
  report item 2 l1 > item 2 l2
end

Upvotes: 2

Related Questions