"""
Backend Classes for AGE-ABM Visual Interface
@Author Max Hall
@Co-Authors Meghan Ireland and Matthew Fleischman
"""
[docs]
class ParentAgent:
""" This acts as a super class which all the 'agents' of the agent based system can inherit
from allowing them to store a description of the 'agent' as this the only common attribute """
def __init__(self, description : str):
self.description = description
[docs]
def edit_description(self, description : str):
"""Edits the description of whatever class this represents.
Parameters
----------
description : str
A description of the created class.
"""
self.description = description
[docs]
def get_description(self):
"""Gets agents description as string.
Parameters
----------
Returns
-------
description : str
| Returns an agents description.
"""
return self.description
[docs]
class ImportDetails:
""" This class stores details about an import, it stores the libraries nickname, import type
and if required, a list of the methods to be imported """
def __init__(self, nickname : str, import_type : str, list_of_methods : list[str] = None):
self.nickname = nickname
self.import_type = import_type
self.list_of_methods = list_of_methods
[docs]
def edit_nickname(self, nickname : str):
"""Edits the nickname of the import.
Parameters
----------
nickname : str
The new nickname for the import.
"""
self.nickname = nickname
[docs]
def edit_import_type(self, import_type : str):
"""Edits the import_type of the import.
Parameters
----------
import_type : str
The new import_type for the import.
"""
self.import_type = import_type
[docs]
def edit_list_of_methods(self, list_of_methods : list[str]):
"""Edits the list of methods of the import.
Parameters
----------
list_of_methods : list[str]
The new list of methods for the import.
"""
self.list_of_methods = list_of_methods.copy() if list_of_methods is not None else None
[docs]
def get_nickname(self):
"""Gets imports nickname as string.
Returns
-------
nickname : str
| Returns an imports nickname.
"""
return self.nickname
[docs]
def get_import_type(self):
"""Gets imports import_type as string.
Returns
-------
import_type : str
| Returns an imports import_type.
"""
return self.import_type
[docs]
def get_list_of_methods(self):
"""Gets imports list_of_methods as list.
Returns
-------
list_of_methods : list[str]
| Returns an imports list of methods.
"""
return self.list_of_methods.copy() if self.list_of_methods is not None else None
[docs]
class Attribute(ParentAgent):
""" This class stores details about an attribute, it inherits the ParentAgent class
and additionally stores the data type the attribute is, if it's user defined as well as
a default value """
def __init__(self, type_attr : str, define_level : str, default_value : int = None):
super().__init__("")
self.type_attr = type_attr
self.default_value = default_value
self.define_level = define_level
[docs]
def edit_type_attr(self, type_attr : str):
"""Edits the type_attr of the attribute.
Parameters
----------
type_attr : str
The new type_attr for the attribute.
"""
self.type_attr = type_attr
[docs]
def edit_default_value(self, default_value : int):
"""Edits the default_value of the attribute.
Parameters
----------
default_value : int
The new default_value value for the attribute.
"""
self.default_value = default_value
[docs]
def edit_define_level(self, define_level : str):
"""Edits the define_level of the attribute.
Parameters
----------
define_level : str
The new define_level value for the attribute.
"""
self.define_level = define_level
[docs]
def get_type_attr(self):
"""Gets attributes type_attr as string.
Parameters
----------
Returns
-------
type_attr : str
| Returns an attributes type.
"""
return self.type_attr
[docs]
def get_default_value(self):
"""Gets attributes default_value type of type_attr.
Parameters
----------
Returns
-------
default_value : str
| Returns an attributes default value.
"""
return self.default_value
[docs]
def get_define_level(self):
"""Gets attributes of define_level as string.
Parameters
----------
Returns
-------
define_level : str
| Returns an attributes define level.
"""
return self.define_level
[docs]
class Component(ParentAgent):
""" This class stores details about a component, it inherits the ParentAgent class
and additionally stores if it's a class components and a list of attributes it has """
def __init__(self, description : str, is_class_component : bool, attributes : dict):
super().__init__(description)
self.is_class_component = is_class_component
self.attributes = attributes
[docs]
def edit_is_class_component(self, is_class_component : bool):
"""Edits the is_class_component value of the component.
Parameters
----------
is_class_component : bool
The new is_class_component boolean value for the component.
"""
self.is_class_component = is_class_component
[docs]
def edit_attributes(self, attributes : dict):
"""Edits the attributes dictionary of the component.
Parameters
----------
attributes : dict
The new attributes dictionary for the component.
"""
self.attributes = attributes
[docs]
def get_is_class_component(self):
"""Gets components boolean is_class_component.
Returns
-------
is_class_component : bool
| Returns if a components is a class component.
"""
return self.is_class_component
[docs]
def get_attributes(self):
"""Gets component's dictionary of attribute classes, with the keys being the
name of the class.
Returns
-------
attributes : dict[Attribute]
| Returns a dictionary containing the components attributes.
"""
return self.attributes
[docs]
class Agent(ParentAgent):
""" This class stores details about an agent, it inherits the ParentAgent class
and additionally stores a list of component names that it has been given """
def __init__(self, description : str, given_components : list[str]
, init_coordinates : list[str] = None):
super().__init__(description)
self.given_components = given_components.copy()
self.init_coordinates = init_coordinates.copy() if init_coordinates is not None else None
[docs]
def edit_given_components(self, given_components : list[str]):
"""Edits the given_components list of the agent.
Parameters
----------
given_components : list[str]
The new given_components list for the agent.
"""
self.given_components = given_components.copy()
[docs]
def edit_init_coordinates(self, init_coordinates : list[str]):
"""Edits the init_coordinates list of the agent.
Parameters
----------
init_coordinates : list[str]
The new init_coordinates list for the agent.
"""
self.init_coordinates = init_coordinates.copy() if init_coordinates is not None else None
[docs]
def get_given_components(self):
"""Gets agents list of given components names as list of strings.
Parameters
----------
Returns
-------
given_components : list[str]
| Returns a list containing the agents components.
"""
given_components = self.given_components.copy()
if "Counter" in given_components:
given_components.remove("Counter")
return given_components
[docs]
def get_init_coordinates(self):
"""Gets agents list of given init_coordinates names as list of strings.
Parameters
----------
Returns
-------
init_coordinates : list[str]
| Returns a list containing the agents initial coordinates.
"""
return self.init_coordinates.copy() if self.init_coordinates is not None else None
[docs]
class System(ParentAgent):
""" This class stores details about an agent, it inherits the ParentAgent class
and additionally stores the execute function body """
def __init__(self, description : str, execute_function_body : str = None, start : int = None
, end : int = None, frequency : int = None, generators : dict = None):
super().__init__(description)
if execute_function_body is not None:
self.execute_function_body = execute_function_body
else:
self.execute_function_body = "\tdef execute(self):\n\t\t# Do not edit above this line!\n\t\t# Code below and indent correctly using tabs, not spaces!\n\t\tpass"
self.start = start
self.end = end
self.frequency = frequency
self.generators = {} if generators is None else generators
[docs]
def edit_execute_function_body(self, execute_function_body : str):
"""Edits the execute_function_body of the system.
Parameters
----------
execute_function_body : str
The new execute_function_body for the system.
"""
self.execute_function_body = execute_function_body
[docs]
def edit_start(self, start : int):
"""Edits the start iteration of the system.
Parameters
----------
start : int
The new start iteration for the system.
"""
self.start = start
[docs]
def edit_end(self, end : int):
"""Edits the end iteration of the system.
Parameters
----------
end : int
The new end iteration for the system.
"""
self.end = end
[docs]
def edit_frequency(self, frequency : int):
"""Edits the frequency of the system.
Parameters
----------
frequency : int
The new frequency for the system.
"""
self.frequency = frequency
[docs]
def edit_generators(self, generators : dict):
"""Edits the generators dictionary of the system.
Parameters
----------
generators : dict
The new generators for the system.
"""
self.generators = generators
[docs]
def get_execute_function_body(self):
"""Gets systems execute_function_body as string.
Parameters
----------
Returns
-------
execute_function_body : str
| Returns the systems execute function body.
"""
return self.execute_function_body
[docs]
def get_start(self):
"""Gets systems start iteration as int.
Parameters
----------
Returns
-------
start : int
| Returns the systems starting iteration.
"""
return self.start
[docs]
def get_end(self):
"""Gets systems end iteration as int.
Parameters
----------
Returns
-------
end : int
| Returns the systems ending iteration.
"""
return self.end
[docs]
def get_frequency(self):
"""Gets systems frequency as int.
Parameters
----------
Returns
-------
frequency : int
| Returns the systems frequency of being run.
"""
return self.frequency
[docs]
def get_generators(self):
"""Gets systems generators as a dict.
Parameters
----------
Returns
-------
generators : dict[Generator]
| Returns a dictionary of the systems generator objects.
"""
return self.generators
[docs]
def remove_start(self):
"""Makes the start iteration of the system the default again.
Parameters
----------
"""
self.start = None
[docs]
def remove_end(self):
"""Makes the end iteration of the system the default again.
Parameters
----------
"""
self.end = None
[docs]
def remove_frequency(self):
"""Makes the systems frequency the default again.
Parameters
----------
"""
self.frequency = None
[docs]
class Generator(ParentAgent):
""" This class stores details about a system generator, it inherits the ParentAgent
class and additionally stores the attributes it requires as well as the code used
to generate the return value """
def __init__(self, description : str, nickname : str, return_value : str, attributes : dict):
super().__init__(description)
self.nickname = nickname
self.return_value = return_value
self.attributes = attributes if attributes is not None else {}
[docs]
def edit_nickname(self, nickname : str):
"""Edits the nickname of the Generator.
Parameters
----------
nickname : str
The new nickname for the Generator.
"""
self.nickname = nickname
[docs]
def edit_return_value(self, return_value : str):
"""Edits the return_value code of the Generator.
Parameters
----------
return_value : str
The new return_value code for the Generator.
"""
self.return_value = return_value
[docs]
def edit_attributes(self, attributes : dict):
"""Edits the attributes dictionary of the Generator.
Parameters
----------
attributes : dict
The new attributes dictionary for the Generator.
"""
self.attributes = attributes if attributes is not None else {}
[docs]
def get_nickname(self):
"""Gets Generator's nickname.
Returns
-------
nickname : str
| Returns the nickname for the generator.
"""
return self.nickname
[docs]
def get_return_value(self):
"""Gets Generator's return_value.
Returns
-------
return_value : str
| Returns the return value code for the generator.
"""
return self.return_value
[docs]
def get_attributes(self):
"""Gets Generator's dictionary of attribute classes, with the keys being the
name of the class.
Returns
-------
attributes : dict[Attribute]
| Returns a dictionary of the generators attribute objects.
"""
return self.attributes if self.attributes is not None else {}
[docs]
class AgentOfInterest:
""" This class is for data collection purposes and stores
details about an agent of interests and its desired components
"""
def __init__(self, comp_of_interest : list[str], count : bool, position : bool
, individual : bool, mean_val : bool, median_val : bool, total_val : bool
, max_val : bool, min_val : bool, variance : bool, std_dev : bool):
self.comp_of_interest = comp_of_interest.copy() if comp_of_interest is not None else []
self.count = count
self.position = position
self.individual = individual
self.mean_val = mean_val
self.median_val = median_val
self.total_val = total_val
self.max_val = max_val
self.min_val = min_val
self.variance = variance
self.std_dev = std_dev
[docs]
def edit_comp_of_interest(self, comp_of_interest : list[str]):
"""Edits the components of interest for the agent.
Parameters
----------
comp_of_interest : list[str]
The new list of components of interest.
"""
self.comp_of_interest = comp_of_interest
[docs]
def edit_count(self, count : bool):
"""Edits the keep count boolean.
Parameters
----------
count : bool
The new keep count boolean.
"""
self.count = count
[docs]
def edit_position(self, position : bool):
"""Edits the position boolean.
Parameters
----------
position : bool
The new postion boolean.
"""
self.position = position
[docs]
def edit_individual(self, individual : bool):
"""Edits the individual boolean.
Parameters
----------
individual : bool
The new individual boolean.
"""
self.individual = individual
[docs]
def edit_mean_val(self, mean_val : bool):
"""Edits the mean value boolean.
Parameters
----------
mean_val : bool
The new mean value boolean.
"""
self.mean_val = mean_val
[docs]
def edit_total_val(self, total_val : bool):
"""Edits the total value boolean.
Parameters
----------
total_val : bool
The new total value boolean.
"""
self.total_val = total_val
[docs]
def edit_max_val(self, max_val : bool):
"""Edits the max value boolean.
Parameters
----------
max_val : bool
The new max value boolean.
"""
self.max_val = max_val
[docs]
def edit_min_val(self, min_val : bool):
"""Edits the min value boolean.
Parameters
----------
min_val : bool
The new min value boolean.
"""
self.min_val = min_val
[docs]
def edit_variance(self, variance : bool):
"""Edits the variance boolean.
Parameters
----------
variance : bool
The new variance boolean.
"""
self.variance = variance
[docs]
def edit_std_dev(self, std_dev : bool):
"""Edits the standard deviation boolean.
Parameters
----------
std_dev : bool
The new standard deviation boolean.
"""
self.std_dev = std_dev
[docs]
def get_comp_of_interest(self):
"""Gets the components of interest list for the agent.
Returns
-------
comp_of_interest : list[str]
| Returns List of components of interest to return.
"""
return self.comp_of_interest
[docs]
def get_count(self):
"""Gets the keep count boolean for the agent.
Returns
-------
count : bool
| Returns keep count boolean to return.
"""
return self.count
[docs]
def get_position(self):
"""Gets the position boolean for the agent.
Returns
-------
position : bool
| Returns postion boolean to return.
"""
return self.position
[docs]
def get_individual(self):
"""Reurns the individual boolean for the agent.
Returns
-------
individual : bool
| Returns individual boolean to return.
"""
return self.individual
[docs]
def get_mean_val(self):
"""Reurns the mean boolean for the agent.
Returns
-------
mean_val : bool
| Returns mean value boolean to return.
"""
return self.mean_val
[docs]
def get_total_val(self):
"""Reurns the total boolean for the agent.
Returns
-------
total_val : bool
| Returns total value boolean to return.
"""
return self.total_val
[docs]
def get_max_val(self):
"""Reurns the max boolean for the agent.
Returns
-------
max_val : bool
| Returns max value boolean to return.
"""
return self.max_val
[docs]
def get_min_val(self):
"""Reurns the min boolean for the agent.
Returns
-------
min_val : bool
| Returns min value boolean to return.
"""
return self.min_val
[docs]
def get_variance(self):
"""Reurns the variance boolean for the agent.
Returns
-------
variance : bool
| Returns variance boolean to return.
"""
return self.variance
[docs]
def get_std_dev(self):
"""Reurns the standard deviation boolean for the agent.
Returns
-------
std_dev : bool
| Returns standard deviation boolean to return.
"""
return self.std_dev
[docs]
class ModelDetails(ParentAgent):
""" This class stores details about the model, it inherits the ParentAgent class
and additionally stores the models name, description, a dictionary of initial
parameters as well as the length of the simulation and the seed for its random
generator """
def __init__(self, name : str, description : str, initial_parameters : dict = None
, gridworld : bool = False, sim_length : int = 1, sim_seed : int = None):
super().__init__(description)
self.name = name
if initial_parameters is None:
self.initial_parameters = {"num_agents" : {}}
else:
self.initial_parameters = initial_parameters
self.gridworld = gridworld
self.sim_length = sim_length
self.sim_seed = sim_seed
[docs]
def get_list_initial_parameters(self):
"""Gets a list of all initial parameters for to the model.
Parameters
----------
Returns
-------
initial_parameters : list[str]
| Returns a list of initial_parameters
"""
if len(self.initial_parameters) != 1:
arr1 = list(self.initial_parameters)
arr1.remove("num_agents")
return arr1
return []
[docs]
def get_list_num_agents(self):
"""Gets a list of all number of agents names for to the model.
Parameters
----------
Returns
-------
num_agent_names : list[str]
| Returns a list of num_agent_names
"""
if self.initial_parameters["num_agents"]:
return list(self.initial_parameters["num_agents"])
return []
[docs]
def add_initial_parameter(self, initial_parameter_name : str, initial_parameter_type : str
, initial_parameter_value : int = None):
"""Adds an initial_parameter to the model.
Parameters
----------
initial_parameter_name : str
The name of the initial_parameter in the model.
initial_parameter_type : str
The new type for an initial_parameter for in the model.
initial_parameter_value : int
The new value for an initial_parameter for in the model.
Returns
-------
error term : int
| Returns 0 on success
| Returns 1 if initial parameter already exist
"""
if initial_parameter_name in self.initial_parameters:
return 1
self.initial_parameters[initial_parameter_name] = [initial_parameter_type
, initial_parameter_value]
return 0
[docs]
def add_num_agent(self, agent_name : str, init_num : int = None):
"""Adds an agents initial count to the model.
Parameters
----------
agent_name : str
The name of the agent in the model.
init_num : int, Optional
The new initial value for the agent.
Returns
-------
error term : int
| Returns 0 on success
| Returns 1 if num_agent already exist
"""
num_agents_dict = self.initial_parameters["num_agents"]
if agent_name in num_agents_dict:
return 1
num_agents_dict[agent_name] = init_num
return 0
[docs]
def edit_name(self, name : str):
"""Edits the name of the model.
Parameters
----------
name : str
The new name for the model.
"""
self.name = name
[docs]
def edit_initial_parameter(self, initial_parameter_name : str
, initial_parameter_type : str = None
, initial_parameter_value : int = None):
"""Edits an initial_parameter of the model.
Parameters
----------
initial_parameter_name : str
The name of the initial_parameter in the model.
initial_parameter_type : str, Optional
The new type for an initial_parameter for in the model.
initial_parameter_value : int, Optional
The new value for an initial_parameter for in the model.
Returns
-------
error term : int
| Returns 0 on success
| Returns 1 if initial parameter doesn't exist
"""
if initial_parameter_name in self.initial_parameters:
if initial_parameter_type is not None:
self.initial_parameters[initial_parameter_name][0] = initial_parameter_type
if initial_parameter_value is not None:
self.initial_parameters[initial_parameter_name][1] = initial_parameter_value
return 0
return 1
[docs]
def edit_num_agent(self, agent_name : str, init_num : int):
"""Edits an agents initial count for the model.
Parameters
----------
agent_name : str
The name of the agent in the model.
init_num : int
The new initial value for the agent.
Returns
-------
error term : int
| Returns 0 on success
| Returns 1 if num_agent doesn't already exist
"""
num_agents_dict = self.initial_parameters["num_agents"]
if agent_name in num_agents_dict:
num_agents_dict[agent_name] = init_num
return 0
return 1
[docs]
def edit_gridworld(self, gridworld : bool):
"""Edits the gridworld boolean of the model.
Parameters
----------
gridworld : bool
The new gridworld boolean for the model.
"""
self.gridworld = gridworld
[docs]
def edit_sim_length(self, sim_length : int):
"""Edits the sim_length of the model.
Parameters
----------
sim_length : int
The new sim_length for the model.
"""
self.sim_length = sim_length
[docs]
def edit_sim_seed(self, sim_seed : int):
"""Edits the sim_seed of the model.
Parameters
----------
sim_seed : int
The new sim_seed for the model.
"""
self.sim_seed = sim_seed
[docs]
def get_name(self):
"""Gets name of the model as string.
Parameters
----------
Returns
-------
name : str
| Returns the name for the model.
"""
return self.name
[docs]
def get_initial_parameter(self, initial_parameter_name : str):
"""Gets an array of an initial_parameter of the model, a list of
[parameter type, parameter value].
Parameters
----------
initial_parameter_name : str
The name of the initial_parameter in the model.
Returns
-------
initial_parameter : list[str,int]
| Returns the details about the initial parameter.
error term : int
| Returns 1 if initial parameter doesn't already exist
"""
if initial_parameter_name in self.initial_parameters:
return self.initial_parameters[initial_parameter_name].copy()
return 1
[docs]
def get_num_agent(self, agent_name : str):
"""Gets an int of an agent_name's number of agents at the start of the model.
Parameters
----------
agent_name : str
The agent_name of the initial count of that agent in the model.
Returns
-------
num_agent : int
| Returns the initial value for the num_agent.
error term : int
| Returns 1 if num_agent doesn't already exist
"""
num_agents_dict = self.initial_parameters["num_agents"]
if agent_name in num_agents_dict:
return num_agents_dict[agent_name]
return 1
[docs]
def get_gridworld(self):
"""Gets the gridworld boolean value.
Parameters
----------
Returns
-------
gridworld : bool
| Returns if the model is a gridworld.
"""
return self.gridworld
[docs]
def get_sim_length(self):
"""Gets the length of the simulation as an int.
Parameters
----------
Returns
-------
sim_length : int
| Returns the number of iterations of the model.
"""
return self.sim_length
[docs]
def get_sim_seed(self):
"""Gets the seed of the simulation as an int.
Parameters
----------
Returns
-------
sim_seed : int
| Returns the seed of the model.
"""
return self.sim_seed
[docs]
def remove_initial_parameter(self, initial_parameter_name : str):
"""Removes an initial_parameter of the model.
Parameters
----------
initial_parameter_name : str
The name of the initial_parameter in the model to be removed.
Returns
-------
error term : int
| Returns 0 on success
| Returns 1 if initial_parameter doesn't already exist
"""
if initial_parameter_name in self.get_list_initial_parameters():
del self.initial_parameters[initial_parameter_name]
return 0
return 1
[docs]
def remove_num_agent(self, agent_name : str):
"""Removes a number of agents at the start of the model.
Parameters
----------
agent_name : str
The agent_name of the initial count of that agent in the model to be removed.
Returns
-------
error term : int
| Returns 0 on success
| Returns 1 if num_agent doesn't already exist
"""
if agent_name in self.get_list_num_agents():
del self.initial_parameters["num_agents"][agent_name]
return 0
return 1