Random
Create a random generator from an ActorsList
Source code in abses/random.py
clean_p ¶
Clean the probabilities. Any negative values, NaN values, or zeros will be recognized as in-valid probabilities. For all valid probabilities, normalize them into a prob-array (the sum is equal to 1.0).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prob |
Union[ndarray, str]
|
An array-like numbers of probabilities. |
required |
Returns:
Type | Description |
---|---|
ndarray
|
The probabilities after cleaned. |
Example:
>>> clean_p([0, 0])
>>> [0.5, 0.5]
>>> clean_p([-1, np.nan])
>>> [0.5, 0.5]
>>> clean_p([3, 2])
>>> [0.6, 0.4]
Source code in abses/random.py
choice ¶
choice(size=1, prob=None, replace=False, as_list=False, when_empty='raise exception', double_check=False)
Randomly choose one or more actors from the current self object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
size |
int
|
The number of actors to choose. Defaults to 1. |
1
|
prob |
ndarray | None | str
|
A list of probabilities for each actor to be chosen. If None, all actors have equal probability. If is a string, will use the value of this attribute as the prob. Defaults to None. |
None
|
replace |
bool
|
Whether to sample with replacement. Defaults to True. |
False
|
as_list |
bool
|
Whether to return the result as a list of actors. Defaults to False. |
False
|
Returns:
Type | Description |
---|---|
Optional[Actor | ActorsList[Actor]]
|
An Actor or an ActorList of multiple actors. |
Notes
Given the parameter set size=1 and as_list=False, a single Actor object is returned. Given the parameter set size>1 and as_list=False, a Self (ActorsList) object is returned.
Raises:
Type | Description |
---|---|
ValueError
|
If size is not a positive integer. |
ABSESpyError
|
Not enough actors to choose in this |
Source code in abses/random.py
new ¶
Randomly creating new agents for a given actor type.
Source code in abses/random.py
link ¶
Random build links between actors.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
link |
str
|
Name of the link. |
required |
p |
float
|
Probability to generate a link. |
1.0
|
Returns:
Type | Description |
---|---|
List[Tuple[Actor, Actor]]
|
A list of tuple, in each tuple, there are two actors who got linked. |
Example
# generate three actors
actors = model.agents.new(Actor, 3)
# with `probability=1`, all possible actor-actor links would be generated.
>>> actors.random.link('test', p=1)
>>> a1, a2, a3 = actors
>>> assert a1.link.get('test) == [a2, a3]
>>> assert a2.link.get('test) == [a1, a3]
>>> assert a3.link.get('test) == [a1, a2]
Source code in abses/random.py
assign ¶
Randomly assign a value to each actor.