Amtul Samie Maqbool
Amtul Samie Maqbool

Reputation: 3

Netlogo code for sorting agents in lists according to their properties

I am trying to sort my agents [industries] into a list and make many further lists based on that initial list sequence. Each industry has two properties [bid-cap] and [bid-price]. I want first, a sorted list [low-bid-ind] of agents according to their ascending bid-prices, then I want to use the same sequence and fetch the values of [bid-cap] and make a list [low-cap-list], then I want to fetch bid-price for same sequence as [low-bid-ind] list and save it in a list [low-cap-list]. I only make this list so I can make another list with cumulative sum values of the [bid-cap] of agents. Following is my code. There's an error : LPUT expected input to be a list but got the number 0 instead. But it doesn't show where the error occurs in the code.

  set agentlist-low [] 
  set low-bid-ind [] 
  set low-cap-list []
  set sum-low-bid []

  set agentlist-low industries with [group = 0] 
  set low-bid-ind sort-on [bid-price] agentlist-low

  foreach low-bid-ind 
  [ the-industry -> 
    set low-cap-list lput ([bid-cap] of the-industry) low-cap-list ]

  let sum-low 0
  let m 0 
  foreach low-bid-ind
    [set m m + 1
      set sum-low sum-low + item m low-cap-list
      set sum-low-bid lput sum-low sum-low-bid ]
  print sum-low-bid

Can anyone help with this problem? I have looked at Netlogo help and other questions on the internet but I am unable to find an answer.

Upvotes: 0

Views: 100

Answers (1)

Charles
Charles

Reputation: 4168

I cannot reproduce the error you are getting. Are you sure that you are setting low-cap-list and sum-low-bid to empty lists in your actual code? That kind of error normally comes when lput is given a list which has not been initialized, as uninitialized variables in NetLogo are set to 0.

But more generally, NetLogo has a map operation that eliminates the need for looping over lists in most cases. So the loop

foreach low-bid-ind 
  [ the-industry -> 
    set low-cap-list lput ([bid-cap] of the-industry) low-cap-list ]

can be written more economically as

 set low-cap-list map [the-industry -> [bid-cap] of the-industry] low-bid-ind

and the code segment

 let sum-low 0
  let m 0 
  foreach low-bid-ind
    [set m m + 1
      set sum-low sum-low + item m low-cap-list
      set sum-low-bid lput sum-low sum-low-bid ]

can be written as a single line

set sum-low-bid map [m -> sum sublist low-cap-list 0 (m + 1)] range (length low-bid-ind)

map creates a list by performing the operation in brackets on each item in the list it is given. In the latter case, that input list is simply the numbers from 0 to the length of the low-bid-ind list.

Upvotes: 1

Related Questions