@@ -85,6 +85,54 @@ def step(self) -> None:
85
85
def advance (self ) -> None : # noqa: D102
86
86
pass
87
87
88
+ @classmethod
89
+ def create_agents (cls , model : Model , n : int , * args , ** kwargs ) -> AgentSet [Agent ]:
90
+ """Create N agents.
91
+
92
+ Args:
93
+ model: the model to which the agents belong
94
+ args: arguments to pass onto agent instances
95
+ each arg is either a single object or a sequence of length n
96
+ n: the number of agents to create
97
+ kwargs: keyword arguments to pass onto agent instances
98
+ each keyword arg is either a single object or a sequence of length n
99
+
100
+ Returns:
101
+ AgentSet containing the agents created.
102
+
103
+ """
104
+
105
+ class ListLike :
106
+ """Helper class to make default arguments act as if they are in a list of length N."""
107
+
108
+ def __init__ (self , value ):
109
+ self .value = value
110
+
111
+ def __getitem__ (self , i ):
112
+ return self .value
113
+
114
+ listlike_args = []
115
+ for arg in args :
116
+ if isinstance (arg , (list | np .ndarray | tuple )) and len (arg ) == n :
117
+ listlike_args .append (arg )
118
+ else :
119
+ listlike_args .append (ListLike (arg ))
120
+
121
+ listlike_kwargs = {}
122
+ for k , v in kwargs .items ():
123
+ if isinstance (v , (list | np .ndarray | tuple )) and len (v ) == n :
124
+ listlike_kwargs [k ] = v
125
+ else :
126
+ listlike_kwargs [k ] = ListLike (v )
127
+
128
+ agents = []
129
+ for i in range (n ):
130
+ instance_args = [arg [i ] for arg in listlike_args ]
131
+ instance_kwargs = {k : v [i ] for k , v in listlike_kwargs .items ()}
132
+ agent = cls (model , * instance_args , ** instance_kwargs )
133
+ agents .append (agent )
134
+ return AgentSet (agents , random = model .random )
135
+
88
136
@property
89
137
def random (self ) -> Random :
90
138
"""Return a seeded stdlib rng."""
0 commit comments