Set range of values for turtles - netlogo

I need to set a range of values for a turtle, the range must go from >= 3 to <= 5 .
I wrote this code
if ((a) + (b) + (c) >= 3 <= 5) [set pcolor gray]
But I did not get what I expected

let value a + b + c
if 3 <= value and value <= 5 [ set pcolor gray ]
Edit:
So I put in the let value a + b + c to simplify the code, but that might be confusing. Here's the version that most closely matches what you have in your question:
if ((3 <= a + b + c) and (a + b + c <= 5)) [ set pcolor gray ]
That said, I recommend using a local variable as I did above so you don't have to write out a + b + c multiple times.

Related

NetLogo: How do I build a new vector based on randomly chosen values from another vector?

I have a vector 'original' with 10 digits. Now I want to create vector 'adapted' based on 'original'. 'adapted' is supposed to take n random values that are larger than 0 from 'original' in the same position and fill up the rest with 0s, e.g.:
original = [2 3 6 2 0 5 7 2 4 8]
adapted = [2 0 0 0 0 5 0 2 0 0]
to go
let n 3
let vector-dimension 10
let original []
repeat vector-dimension
[set original lput random 10 original]
print original
let adapted []
while [sum (map [ [v1] -> ifelse-value (v1 > 0) [1] [0] ] (adapted)) != n]
[set adapted (map [ [v1] -> ifelse-value ( (vector-dimension / n) * (100 / vector-dimension) > random-float 100) [v1] [0] ] (original)) ]
print adapted
end
This Code works but is slow. How can I do it faster?
How about:
to-report report-rand-n [ base n ]
let indices ( range 0 (length base))
let subset n-of n indices
let out ( map [ [ i v ] -> ifelse-value ( member? i subset ) [v] [0] ] indices base)
report out
end
This reporter makes a list of indices (0 through the length of the base passed), then randomly selects n number of those indices to pass to ifelse-value to return either the original value in base (if i is one of the selected indices) or 0.
Testing:
to test
let original [2 3 6 2 0 5 7 2 4 8]
print report-rand-n original 3
print report-rand-n original 3
print report-rand-n original 5
print report-rand-n original 5
end
observer> test
[2 0 6 0 0 0 0 0 4 0]
[2 0 0 0 0 0 0 2 0 8]
[2 0 0 0 0 5 0 2 4 8]
[0 0 6 2 0 5 0 0 0 8]
Edit:
to test
let original [2 3 6 2 0 5 7 2 4 8]
print word "testing: " original
print report-rand-n original 3
let few-digits [ 0 0 0 1 0 0 0 ]
print word "testing: " few-digits
print report-rand-n few-digits 3
print ""
end
to-report report-rand-n [ base n ]
; create list of indices
let indices ( range 0 (length base))
; To address point 1) in your comment:
; keep only indices that correspond to a value > 0 in base
let indices-over-zero filter [ i -> item i base > 0 ] indices
; To address point 2 in your comment:
; If the length of indices over zero is less than n, replace n
; with the length of indices over zero
if length indices-over-zero < n [
set n length indices-over-zero
]
let subset n-of n indices-over-zero
let out ( map [ [ i v ] -> ifelse-value ( member? i subset ) [v] [0] ] indices base)
report out
end

NetLogo Random values based on probabilities in list

I try to figure out, how to create an output that has an equal distribution on the random sample. Based on the code snippet below, the function creates three random numbers for three items in a list. This values gets than compared to each other, and the maximal value of them, gets count. However I am trying to figuring out how to control the randomness by a probability for example 50% (50) 25% (25) 25% (25) N=100.
to numberGenerator
let i 0
set counter_red 0
set counter_blue 0
set counter_green 0
while [i < 100] [
let numberS_red1 random-float 1
let numberS_blue1 random-float 1
let numberS_green1 random-float 1
let usedcolors [red blue green]
let OPstrength1 ( list numberS_red1 numberS_blue1 numberS_green1)
let strategies (map list usedcolors OPstrength1)
print strategies
if (numberS_red1 > numberS_blue1) and (numberS_red1 > numberS_green1)
[set counter_red counter_red + 1]
if (numberS_blue1 > numberS_red1) and (numberS_blue1 > numberS_green1)
[set counter_blue counter_blue + 1]
if (numberS_green1 > numberS_red1) and (numberS_green1 > numberS_blue1)
[set counter_green counter_green + 1]
set i i + 1
]
print counter_red
print counter_blue
print counter_green
end
counter_red = 26
counter_blue = 36
counter_green = 38
you can implement a new function using the netlogo random-normal command which assigns a color. Documentation for the function is at link

Netlogo: How to get the weight of the link

I got a set of turtles with links connected to each other. I wanted to retrieve the weight of the link between two nodes, i've tried searching but couldn't find any info on how to do it. I'm not using nw cause i don't want the shortest path. Any ideas? This is a section of my code:
to calculate-oldpath
let oldList [ 25 0 1 2 3 4 9 8 7 6 5 10 11 12 13 14 19 18 17 16 15 20 21 22 23 24]
let weighted-dist 0
( foreach ( but-last oldList ) ( but-first oldList ) [
[ a b ] ->
ask turtle a [
let node-link link-with turtle b
;Then retrieve weight link to do adding
]
] )
print weighted-dist
end
enter image description here
The S is my starting point (25 in the list) and E is end (24 in the list) I wanted to calculate the weight of this "orange path"
Jen's answer about how to get the weight of a link is correct, but I would suggest an alternative way of computing the sum of these weights: using the sum primitive!
This requires turning your foreach into a map, but aside from that, it's pretty straightforward:
let weighted-dist sum (map [ [a b] ->
[ [ weight ] of link-with turtle b ] of turtle a
] (but-last oldList) (but-first oldList))
Another small comment: using a list of who numbers might not be the best way to approach things, but I don't know enough about your problem to suggest an alternative...
Assuming you called the weight weight (in your links-own statement that you haven't shown) then something like this should work:
to calculate-oldpath
let oldList [ 25 0 1 2 3 4 9 8 7 6 5 10 11 12 13 14 19 18 17 16 15 20 21 22 23 24]
let weighted-dist 0
( foreach ( but-last oldList ) ( but-first oldList ) [
[ a b ] ->
ask turtle a [
let node-link link-with turtle b
set weighted-dist weighted-dist + [weight] of node-link
]
] )
print weighted-dist
end
Getting the attribute value for a link is exactly the same as getting the attribute value for a turtle or patch, you use of

Netlogo reduce polynomial example

In the Netlogo dictionary for "reduce" they show an example with one "+" operator
reduce [?1 + ?2] [1 2 3 4]
which they expand as equivalent to (((1 + 2) + 3) + 4).
Later they give this example:
;; evaluate the polynomial, with given coefficients, at x
to-report evaluate-polynomial [coefficients x]
report reduce [(x * ?1) + ?2] coefficients
end
;; evaluate 3x^2 + 2x + 1 at x = 4
show evaluate-polynomial [3 2 1] 4
=> 57
What is the equivalent expansion (using parentheses) for that evaluation?
observer> show (4 * ((4 * 3) + 2)) + 1
observer: 57
The key to understand it is to do it step by step. reduce starts by taking the first two elements of the list and plugging them into ?1 and ?2, so
(x * ?1) + ?2
becomes
(x * 3) + 2
That whole expression then becomes ?1, and the last element of the list, 1, becomes ?2. Replacing ?1 and ?2 in the initial expression again, we get:
(x * ((x * 3) + 2)) + 1
All that's left is to replace x with 4:
(4 * ((4 * 3) + 2)) + 1

NetLogo histogram data

I tried posting this to the NetLogo user group on Yahoo but wasn't successful in getting the post accepted. So I'm trying here.
NetLogo can plot histograms. Is there any way to get access to the histogram data, i.e., the data generated for the histogram plot? Thanks.
Happy Holidays, Russ!
I don't think it's possible to get the values. Though if you wanted to implement your own histogram for data, you could use something like:
to-report calc-histogram [ aList numBars aMaxValue ]
let minValue min aList
let interval (aMaxValue - minValue) / numBars
let hist []
foreach n-values numBars [?] [
let lowerBound minValue + (? * interval)
let upperBound lowerBound + interval
let x (lowerBound + upperBound) / 2
let y length filter [? >= lowerBound and ? < upperBound] aList
set hist lput (list x y ) hist
]
report hist
end
example usage:
observer> calc-histogram [0 1 18 2 3 4 5 6 7 7 7 9 10 7 15 7 17 18 19 ] 5 20
observer: [[2 4] [6 8] [10 2] [14 1] [18 4]]