Reputation: 891
I have a model where agents or patches have an opinion of two things A and B:
patches-own[
OpinionA
OpinionB
]
And then the values for these are set randomly at the start.
How could I change this so that there could be more than two things? In fact, I'd like to have that number set by a slider. How can I dynamically change the number of variable an agent or patch has?
Upvotes: 1
Views: 504
Reputation: 7232
You could use table
extension:
extensions [table]
patches-own[
opinions
]
to setup
ask patches [
set opinions table:make
]
; ...
end
Set opinions with "key-value" pair where key is the opinion type and value the actual attitude. For example:
ask n-of 5 patches [
table:put opinions "favorite-color" "blue"
table:put opinions "food" "pizza"
]
You can set different number of opinions to another group of patches:
ask n-of 5 patches [
table:put opinions "favorite-color" "red"
table:put opinions "food" "chocolate"
table:put opinions "age" 5
]
Show opinion about food (for patches with opinion about food):
ask patches with [ table:has-key? opinions "food" ]
[ show table:get opinions "food ]
Example, how to set n
opinions randomly:
let number-of-opinions 3
ask n-of 5 patches [
(foreach n-values 3 [?] n-values 3 [random 10]
[ table:put opinions ?1 ?2 ])
]
Here 3 opinions (named 0, 1 and 2) have been set with random numbers (from 0 to 9). To check opinion 1 use:
ask patches with [ table:has-key? opinions 1 ]
[ show table:get opinions 1 ]
Note that with
checks if specific opinion type is set before asking for value.
See also table examples in documentation.
Upvotes: 1
Reputation: 1209
I don`t think it is possible to create new agent-variables on the fly, but you could instead use just one agent-variable and use a list to store multiple Opinions. You could define the length of that list by a slider and randomly set each position of the list to a specific Opinion. For example if you want the patches to have x different Opinions (defined by the slider x) which have either "a", "b" or "c" you could do that as follows:
patches-own[
opinion
]
to setup
... (some setup procedure stuff)
let opinion-options (list "a" "b" "c")
ask patches[
set opinion n-values x [one-of opinion-options]
]
Upvotes: 4