Random
abses.utils.random.ListRandom ¶
Bases: Random
Create a random generator from an ActorsList.
Inherits from Python's Random class to provide Mesa-compatible shuffle() method. Extends Random with ABSESpy-specific methods for working with actors.
Source code in abses/utils/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/utils/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.
Source code in abses/utils/random.py
new ¶
Randomly creating new agents for a given actor type.
Source code in abses/utils/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/utils/random.py
assign ¶
Randomly assign a value to each actor.