Reputation: 806
I want to sort a list of turtle's item sorted by decreasing attribute (from the highest value to the lowest), pick the item with the highest value (the first item of the sorted list; it may be not added recently but in previous step), and add it to the top of list. I am doing
ask one-of agents [
let sorted_list sort-on [values] items
]
to sort the items in the list. values
is defined in the range [0,1]
.
However, I do not know how to pick the item with the highest value and add it on the top of the list.
Furthermore, how can an agent remember which item he already picked from the list in order to not select the same twice (if the agent is selected again)?
I hope you can help me with this.
Upvotes: 0
Views: 247
Reputation: 1473
To sort a set of items in decreasing order by value, you need a minus sign as in:
ask one-of agents [
let sorted_list sort-on [(- values)] items
let best-choice first sorted_list
]
Or you could just sort in ascending order, and choose the last item instead of the first.
ask one-of agents [
let sorted_list sort-on [ values] items
let best-choice last sorted_list
]
I do not understand what you mean by "top".
add it on the top of the list.
I think maybe you mean that there is a different list of items, say "item-list", and you want to push the best-choice onto the front of this list. You could do this:
set item-list fput best-choice item-list
Furthermore, how can an agent remember which item he already picked from the list in order to not select the same twice (if the agent is selected again)?
Just give the agent a variable, most-recent-choice, and set that to best-choice as soon as you select it. Or if you want to recall every item the agent ever selected, give them a variable all-my-choices, and keep a list in there. Initialize it to [] and fput best-choice onto it at the time you select best-choice.
Good luck -- the problem you and Val are both working on seems to be a very complicated problem!
Upvotes: 2