agentframework module

Module requirements:
- numpy
- random

Includes:
- Agent class
- function for importing the environment: import_environment(path)

class agentframework.Agent(environment: list, agents: list, init_coords=None, sex=None)[source]

Bases: object

Provides the framework for sheep agents and their associated actions.

Arguments:
environment (matrix): list of lists of numbers corresponding to grass height at each pixel of environment
agents (list): list of Agent objects in the simulation
init_coords (2-tuple of integers): Determines (x,y) at which this agent is spawned. If None, random x and y are chosen between 0 and 300. (default None)
sex (str): ‘m’ or ‘f’, the sheep’s sex (default: None, causes _sex below to be set randomely).
Attributes:
_x (integer): The sheep’s x coordinate, between 0 and 300
_y (integer): The sheep’s y coordinate, between 0 and 300
_sex (str): ‘m’ or ‘f’, the sheep’s sex (default: random if sex argument is None)
_store (integer): Amount of grass eaten and stored by the sheep. Initiates at 0.
_pregnancy (integer): Stage of pregnancy the sheep is at. Initiates at 0.
_age (integer): Number of runs the sheep has lived for. Initiates at 0.
Methods:
set_methods:
set_x, set_y, set_store, set_pregnancy
get_ methods:
get_x, get_y, get_store, get_pregnancy, get_sex, get_age
“Action” methods:
move, eat, share_with_neighbours, mate
Other methods:
is_dead, increment_age, distance_to
distance_to(other)[source]

Given another agent, return the Euclidean distance between self and the given agent.

Arguments:
other (Agent class): Other sheep to get distance to.
Returns:
Euclidean distance (float) to the other sheep.
eat(max_grass_per_turn=20, sick_enabled=False)[source]

Calling this will cause the sheep to “eat grass” from the coordinate it is standing on in the environment.

If the environment has value equal to or more than max_grass_per_turn at the coordinate at which the sheep is currently standing, the sheep will increase its store by max_grass_per_turn, and the environment’s value here will decrease by max_grass_per_turn. If the value here is less than max_grass_per_turn, the sheep will add this value to its store and reduce the environment to 0 at this spot. If environment is at 0 at this coordinate, the sheep will not eat.

Arguments:
max_grass_per_turn (int or float): the maximum amount each sheep can eat per turn, if current coordinate has this available. Otherwise, the sheep consumes
what’s left of the grass beneath it (default 20)

sick_enabled (bool): if True, sheep sick up 50 onto their current coordinate in the environment if their store reaches 100 (default False)

get_age()[source]

Returns the private _age attribute

get_pregnancy()[source]

Returns the private _pregnancy attribute

get_sex()[source]

Returns the private _sex attribute. Note: This attribute does not have a set method - it is read-only.

get_store()[source]

Returns the private _store attribute

get_x()[source]

Returns the private _x attribute

get_y()[source]

Returns the private _y attribute

increment_age()[source]

Increments the agent’s age on which it was called.

is_dead(max_age=100)[source]

Checks if age has reached the maximum age (integer, default 100) and returns a bool answer.

mate(preg_duration=10, min_age=20, min_dist=10, min_store=50)[source]

Enables mating for the sheep, meaning that female sheep get pregnant if they come close enough to male sheep and thus give birth to new sheep after a given pegnancy duration.

If both self and other sheep are of age, have enough food store, are close enough, and are also of opposite sexes, the female one will get pregnant. Pregnancies progress with each iteration of the simulation and once the correct duration is reached, a new sheep (new instance of the Agent class) is initiated 5 positions to the right of the mother.

NOTES:
- At each run of this mating function on an agent (i.e. agent.mating()), that agent looks around it for possible mates. Thus must be run on each agent per update of the simulation.
- A pregnant sheep cannot get re-pregnant until it gives birth.

Arguments:
preg_duration (integer): number of turns that a pregnancy lasts from conception to giving birth (default 10)
min_age (integer): both sheep (male and female) must be of this age or more to be able to mate (default 20)
min_dist (integer or float): must be closer than this distance to be able to mate (default 10)
min_store (integer or float): both sheep must have this much store or more to be able to mate (default 50)
Raises:
ValueError: if pregnancy value becomes negative or goes above preg_duration
move(optimised=True)[source]

Moves the sheep it is called on one step.

Can be random (random walk, optimised=False), or towards the direction of most grass unless current position has more than surrounding areas, in which case the sheep does not move (greedy search, optimised=True).

Arguments:
optimised (bool): If False, sheep moves randomely to a neighbouring pixel. If True, sheep moves towards direction of most grass, or does not move if current pixel has most grass (default True)
set_age(val: int)[source]

Sets the private _age attribute to given integer.

set_pregnancy(val: int)[source]

Sets the private _pregnancy attribute to given integer.

set_store(val: int)[source]

Sets the private _store attribute to given integer.

set_x(x: int)[source]

Sets the private _x attribute to given integer.

set_y(y: int)[source]

Sets the private _y attribute to given integer.

share_with_neighbours(neighbourhood_size=20)[source]

Share store with nearby sheep by splitting resources with them.

Check if any other sheep are within a given radius of self, and if so, share stores by setting the value of the stores for self and the other sheep to the average of the stores between the two.

Arguments:
neighbourhood_size (integer or float): Radius below which sharing is triggered (default 20)
agentframework.import_environment(path='data/in.txt')[source]

Imports the 300x300 pixel environment from the given file path.

Arguments:
path (str): path to environment file (default ‘data/in.txt’)
Returns
environment (matrix of numbers): list of list of numbers imported from the file
Raises:
IOError: if file not found at the given path
agentframework.perturb(x)[source]

Given a number, returns a perturbed version of it with equal chance of increase or decrease by 1, mod300.

Arguments:
x (integer or float): a number
Returns:
Either (x+1)mod300 or (x-1)mod300, with equal probability.
Raises:
TypeError: if non-number is passed as argument.