Model
- class Melodie.model.ModelRunRoutine(max_step: int, model: Model)
Bases:
object
A simple iterator for model run.
When calling
Model.iterator()
method, a ModelRunRoutine object will be created, yielding anint
value reprensenting the current number of step, ranging[0, max_step - 1]
.
- class Melodie.model.Model(config: Config, scenario: Scenario, run_id_in_scenario: int = 0, visualizer: Visualizer | None = None)
Bases:
object
The base class for Model.
There are three major methods,
create()
,setup()
andrun()
.create()
and thensetup()
are called when the model creates, andrun()
is called for model running.To build up your own model, inherit this class and override
create()
,setup()
andrun()
- Parameters:
config – Type
Melodie.Config
scenario – Type
Melodie.Scenario
containing model parameters.run_id_in_scenario – Current
run_id
in the current scenario, anint
from [0,number_of_run
), and 0 by default.visualizer –
Visualizer
instance if needs visualization,None
by default, indicating no need for visualization.
- create()
An initialization method, which is called immediately right after the
Model
object is created.- Returns:
None
- setup()
General method for model setup, which is called after
Model.create()
- Returns:
None
- create_db_conn() DBConn
Create a database connection with the project configuration.
- Returns:
DBConn object
- create_agent_list(agent_class: Type[Agent])
Create an agent list object. A model could contain multiple ``AgentList``s.
- Parameters:
agent_class – The class of desired agent type.
- Returns:
Agentlist object created
- create_environment(env_class: Type[Environment])
Create the environment of model. Notice that a model has only one environment.
- Parameters:
env_class –
- Returns:
Environment object created
- create_grid(grid_cls: Type[Grid] | None = None, spot_cls: Type[Spot] | None = None)
Create a grid.
- Parameters:
grid_cls – The class of grid,
Melodie.Grid
by default.spot_cls – The class of spot,
Melodie.Spot
by default.
- Returns:
Grid object.
- create_network(network_cls: Type[Network] | None = None, edge_cls: Type[Edge] | None = None)
Create the network of model.
- Parameters:
network_cls – The type of network object,
Melodie.Network
by default.edge_cls – The type of edge object,
Melodie.Edge
by default.
- Returns:
Network object created
- create_data_collector(data_collector_cls: Type[DataCollector])
Create the data collector of model.
- Parameters:
data_collector_cls – The datacollector class, must be a custom class inheriting
Melodie.DataCollector
.- Returns:
Datacollector object created.
- create_agent_container(agent_class: Type[Agent], initial_num: int, params_df: pd.DataFrame = None, container_type: str = 'list') AgentList | AgentDict
Create a container for agents.
- Parameters:
agent_class –
initial_num – Initial number of agents
params_df – Pandas DataFrame
container_type – a str, “list” or “dict”
- Returns:
Agent container created
- run()
Model run. Be sure to inherit this method on your model.
- Returns:
None
- iterator(period_num: int)
Return an iterator which iterates from 0 to period_num-1. In each iteration, the iterator updates the visualizer if it exists.
- Parameters:
period_num – How many periods will this model run.
- Returns:
None
- init_visualize()
Be sure to implement it if you would like to use visualizer.
- Returns: