I am struggling in making program in netlogo which produces the different combinations of turtles into two sets.
For instance; there are total 10 turtles present in a system [0 1 2 3 4 5 6 7 8 9] and I want to produce different combinations of those turtles into two sets. {[0 6 7 8] [1 2 3 4 5 9]}, {[2 3 6 8 9] [0 1 4 5 7]}....... So on.
Any help would be really appreciated.
This may be what you want. The below code removes duplicates by sorting the lists and removing duplicates (i.e it would remove [[1 2] [3]] and [[2 1] [3]] because it would sort the first list which would then be [1 2] which would cause for the match). Chop off an element and insert it into the left sublist and recurse.
You may imagine that each number in the list corresponds with a who. so you could do
let alist [who] of turtles
Let me know if you have any questions
to-report permutate-sublist [added-whos remaining-whos]
ifelse length remaining-whos = 1
[
report (list (list (sort added-whos) (sort remaining-whos)))
]
[
let result (list)
foreach (sort remaining-whos)
[
let x ?
let new-whos (sentence added-whos x)
let new-remaining-whos (remove x remaining-whos)
set result (sentence (list (list (sort new-whos) (sort new-remaining-whos))) result)
set result (remove-duplicates (sentence (permutate-sublist new-whos new-remaining-whos) result))
]
report result
]
end
This is what happens when you print out the results for
let alist (list 1 2 3 )
show permutate-sublist (list) alist
[[[2 3] [1]]
[[1 3] [2]]
[[3] [1 2]]
[[1 2] [3]]
[[2] [1 3]]
[[1] [2 3]]]
Related
I am wondering whether if its possible to iterate over n item using for loop (foreach), where the list is of length x.
For example, for a list of length 6 (x), I want to iterate the first 4 items (n) and add them to another list B.
lets say list-A is [ 7 8 12 11 5 6]
output required: list-B should be [7 8 12 11]
The code below adds all the items from list A to list B, since iteration goes through the entire list. I want to stop it at the 4th iteration. so only the first 4 items will be added.
set list-A [ 7 8 12 11 5 6]
set list-B []
let n 4
foreach list-A [
i ->
set list-B lput i list-B
]
NetLogo has sublist to extract one list from the middle of another. In your case it would look like:
to testme
let list-A [ 7 8 12 11 5 6]
let list-B sublist list-A 0 4
print list-B
end
I am trying to ask Netlogo to generate a sequence of numbers with repeated elements, e.g.
[1 1 1 2 2 2 3 3 3]
I tried to use the n-values N [i -> i] syntax but it just gives a sequential list of numbers, 0 to N.
So far, I have tried using n-values primitive with sentence, e.g.
let mylist ( list sentence
n-values 3 [1] sentence
n-values 3 [2]
n-values 3 [3]
)
The problem is that this still returns a list of lists (i.e. [[1 1 1 2 2 2 3 3 3]]) and this causes problems for me later when trying to add this list into a matrix.
Thanks!
reduce sentence (map [x -> n-values 3 [x]] (range 1 4))
I have a list of lists of equal lengths, e.g.
[[0 1 0] [2 3 0] [4 4 2] [0 1 0]]
How can I get the list [6 9 2] which sums up the entries in the four lists entrywise?
let _lst [[0 1 0] [2 3 0] [4 4 2] [0 1 0]] show reduce [[?1 ?2] -> (map + ?1 ?2)] _lst
As a procedure:
to-report aggregate-lists [list-of-lists]
report reduce [[?1 ?2] -> (map + ?1 ?2)] list-of-lists
end
Someone who is better at lists will come along and do this more cleanly, but this works.
to testme
let inlist [[0 1 0] [2 3 0] [4 4 2] [0 1 0]]
let outlist []
let ii 0
while [ii < length item 1 inlist ]
[ let items map [ x -> item ii x ] inlist
print items
set outlist lput reduce [ [a b] -> a + b] items outlist
set ii ii + 1
]
print outlist
end
What it does it create a list of the first entries (with let items) and then sums them with the reduce, then moves to the second entries etc.
You may also want to look at this question Netlogo: How to compute sum of items of lists within a list?, which works with individual entries.
I was looking for a "one-liner" and finally produced it:
to-report aggregate-lists [list-of-lists]
report map [ i -> sum (map [li -> item i li] list-of-lists) ] range length item 0 list-of-lists
end
You map a reporter which sums item i of all lists to the vector of indices range length item 0 list-of-lists (would be [0 1 2] in the example).
so i'm a bit struggling with Lists in Netlogo, so basically i've two lists and i want to remove the items that are in List 1 from List 2, for example:
List 1 : [8 6 9 7 1 3]
List 2: [5 9 8]
Resulting List : [6 7 1 3]
I've tried the following code but it returns an empty list:
if List 2 != []
[
foreach List 2
[
let p position ? List 1
if p = true
[
set List 1 remove-item p List 1
]
]
]
Any ideas ?
A combination of member? and filter will get you there:
let list1 [8 6 9 7 1 3]
let list2 [5 9 8]
let result filter [ x -> not member? x list2 ] list1
print result
Will print the desired:
[6 7 1 3]
Tip: whenever you find yourself trying to use an index for anything in NetLogo, you are probably not doing things in the optimal way. NetLogo has tons of functions (like filter, in this case) that operate on lists as a whole. There is rarely a need to explicitly loop through them.
replacing in nested list ??
for example
[[1 2][3 4] [5 1]]
to
[[99 2][3 4] [5 1]]
Can anybody please clarify the use of lists in Netlogo?
Your question is somewhat underspecified. How do you want to decide which item gets changed?
I'll suppose that you want to do it by indices, e.g. "0th item of 0th sublist". Then that's:
to-report replace-subitem [index1 index2 lists value]
let old-sublist item index1 lists
report replace-item index1 lists (replace-item index2 old-sublist value)
end
observer> show replace-subitem 0 0 [[1 2] [3 4] [5 1]] 99
observer: [[99 2] [3 4] [5 1]]
You could also imagine doing the replacement according to other criteria. For example, suppose we want to change all of the occurrences of 1 in the first item of a sublist to 99. Then that's:
to-report replace-first [old new the-list]
if first the-list = old
[ report replace-item 0 the-list new ]
report the-list
end
to-report replace-firsts [old new lists]
report map [replace-first old new ?] lists
end
observer> show replace-firsts 1 99 [[1 2] [3 4] [1 1]]
observer: [[99 2] [3 4] [99 1]]
A lot of other answers are possible as well, depending on exactly what problem you're trying to solve.