Checking unique list in a patchset - netlogo

I have each patch containing a list of length 4. For exaample
state = [ 1 0 1 4 ]
I want to check how many patches have such unique lists, given the order of elements in the list matters.
Thus, for example in each of 4 patches with the following:
[1 2 4 1] [ 1 2 2 3] [1 3 2 2] [1 2 4 1]
The output should be 3.

This will do it:
length remove-duplicates [state] of patches

Related

How can I multiply n elements of a matrix by a number in NetLogo?

I have a matrix m and I want to have a user defined function which allows me to control the elements that are multiplied by a value.
The function matrix:set-and-report looks promising but I'm not sure how to implement this for multiple elements.
For example, I would like to multiply the first 3 elements of the matrix by -1 to move from this:
let m matrix:from-row-list [1 2 3 4 5 6]
print m
to this:
let n matrix:from-row-list [-1 -2 -3 4 5 6]
With matrix:set-and-report you were indeed pretty close to a solution. Please check the example, I hope this is what you were looking for. The report function has matrix as an input. Than you specify the row, than the index were you want to start the multiplication, where to end it, and finally the multiplier.
Extensions [
matrix
]
to test
let m matrix:from-row-list [ [1 2 3 4 5 6] [1 2 3 4 5 6] ]
print (word "original matrix " m)
print (word "modified matrix " matrix-row-manipulation m 0 0 3 -1)
end
to-report matrix-row-manipulation [matrix row columen-index-start columen-index-end multiplier]
let index (range columen-index-start columen-index-end 1)
foreach index [ i ->
set matrix matrix:set-and-report matrix row i (matrix:get matrix row i * multiplier )
]
report matrix
end
This will return you:
observer> test
original matrix {{matrix: [ [ 1 2 3 4 5 6 ][ 1 2 3 4 5 6 ] ]}}
modified matrix {{matrix: [ [ -1 -2 -3 4 5 6 ][ 1 2 3 4 5 6 ] ]}}

How can I change the elements of a matrix if they satisfy some condition?

I'm working with the matrix extension in NetLogo. I want to be able to modify specific elements of the matrix if they equal some number.
For instance if the value is 0.95 I want to run random 2 on it so it comes out as a 1 or a 0. And if it's a 1.75 it comes out as a 1 or a 2 with random (3 - 1) + 1
This would change my matrix m from this:
let m matrix:from-row-list [[1 0.95 0.95] [2 1 1.75] [1 2 1] ]
to this:
[[1 1 0] [.05 1 2] [.05 .25 1] ]
Thanks
I'm not sure if I understand your updated matrix example- for example, why does the 2 in the second row become 0.05 in the output? I'm assuming you have some other rules for dealing with those numbers. Anyway, I think you can use matrix:map to accomplish what you're after- you may just have to set up the rules in your anonymous reporter to reflect what you're after. Here is an example using the rules you supplied for values of 0.95 and 1.75:
extensions [ matrix ]
to matrix-manipulation
let m matrix:from-row-list [[1 0.95 0.95] [2 1 1.75] [1 2 1] ]
let m2 matrix:map [ i -> val-change i ] m
print matrix:pretty-print-text m2
end
to-report val-change [ val ]
if val = 0.95 [
report random 2
]
if val = 1.75 [
report 1 + random 2
]
report val
end
Output becomes:
[[ 1 0 1 ]
[ 2 1 2 ]
[ 1 2 1 ]]

Netlogo: sequence of numbers with repeated elements

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))

How to remove several items from a unsorted list in Netlogo

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.

Different combination of turtles in two sets in netlogo

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]]]