Is there a function that allows to sum a list with the correspondant value obtained by rnd:weighted-one-of-list? (Netlogo)

Viewed 37

I have a list with four values in it. I have then created an array made up of four 1's with their probabilities:

set startingcharact (list pricered qualityred innovationred sustainabred)
set probs-impr-red [[1 0.1] [1 0.1] [1 0.3] [1 0.5]]

Basically what I want is to "update" the startingcharact list by adding the 1 chosen with the specific function of the rnd extension to the characteristic with the same index. So for example, if the first 1 of the list is chosen, i want to have: pricered + 1. I tried to do this with the map function but I obviously get an error because I am trying to sum a list with a single value. Here what I have done:

set startingcharact (map + startingcharact first rnd:weighted-one-of-list probs-impr-red last)
1 Answers

I can think of two approaches, but both involve changing

set probs-impr-red [[1 0.1] [1 0.1] [1 0.3] [1 0.5]]

to

set probs-impr-red [[0 0.1] [1 0.1] [2 0.3] [3 0.5]]

as you are trying to find the right index into startingcharact. After that,

let z replace-item (first rnd:weighted-one-of-list probs-impr-red [a -> last a]) [0 0 0 0]  1
set startingcharact (map [[a b] -> a + b] startingcharact z)

Or, perhaps more directly,

let idx first rnd:weighted-one-of-list probs-impr-red [a -> last a]
set startingcharact replace-item idx startingcharact (item idx startingcharact + 1)

I'm sure there might be something more elegant, but I think this will work (although be warned that it is not tested).

Related