Class ParticipationModel
Bases: Model
The ParticipationModel class provides a base environment for multi-agent simulations within a grid-based world (split into territories) that reacts dynamically to frequently held collective decision-making processes ("elections"). It incorporates voting agents with personalities, color cells (grid fields), and areas (election territories). This model is designed to analyze different voting rules and their impact.
This class provides mechanisms for creating and managing cells, agents, and areas, along with data collection for analysis. Colors in the model mutate depending on a predefined mutation rate and are influenced by elections. Agents interact based on their personalities, knowledge, and experiences.
Attributes:
| Name | Type | Description |
|---|---|---|
grid |
SingleGrid
|
Grid representing the environment with a single occupancy per cell (the color). |
grid.height |
int
|
The height of the grid. |
grid.width |
int
|
The width of the grid. |
colors |
ndarray
|
Array containing the unique color identifiers. |
voting_rule |
Callable
|
A function defining the social welfare function to aggregate agent preferences. This callable typically takes agent rankings as input and returns a single aggregate result. |
distance_func |
Callable
|
A function used to calculate a distance metric when comparing rankings. It takes two rankings and returns a numeric distance score. |
mu |
float
|
Mutation rate; the probability of each color cell to mutate after an elections. |
color_probs |
ndarray
|
Probabilities used to determine individual color mutation outcomes. |
options |
ndarray
|
Matrix (array of arrays) where each subarray represents an option (color-ranking) available to agents. |
option_vec |
ndarray
|
Array holding the indices of the available options for computational efficiency. |
color_cells |
list[ColorCell]
|
List of all color cells. Initialized during the model setup. |
voting_agents |
list[VoteAgent]
|
List of all voting agents. Initialized during the model setup. |
personalities |
list
|
List of unique personalities available for agents. |
personality_distribution |
ndarray
|
The (global) probability distribution of personalities among all agents. |
areas |
list[Area]
|
List of areas (regions or territories within the grid) in which elections take place. Initialized during model setup. |
global_area |
Area
|
The area encompassing the entire grid. |
av_area_height |
int
|
Average height of areas in the simulation. |
av_area_width |
int
|
Average width of areas created in the simulation. |
area_size_variance |
float
|
Variance in area sizes to introduce non-uniformity among election territories. |
common_assets |
int
|
Total resources to be distributed among all agents. |
av_area_color_dst |
ndarray
|
Current (area)-average color distribution. |
election_costs |
float
|
Cost associated with participating in elections. |
max_reward |
float
|
Maximum reward possible for an agent each election. |
known_cells |
int
|
Number of cells each agent knows the color of. |
datacollector |
DataCollector
|
A tool for collecting data (metrics and statistics) at each simulation step. |
scheduler |
CustomScheduler
|
The scheduler responsible for executing the step function. |
_preset_color_dst |
ndarray
|
A predefined global color distribution (set randomly) that affects cell initialization globally. |
Source code in src/models/participation_model.py
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 | |
adjust_color_pattern(color_patches_steps, patch_power)
Adjusting the color pattern to make it less predictable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
color_patches_steps
|
int
|
How often to run the color-patches step. |
required |
patch_power
|
float
|
The power of the patching (like a radius of impact). |
required |
Source code in src/models/participation_model.py
422 423 424 425 426 427 428 429 430 431 432 433 434 435 | |
color_by_dst(color_distribution)
staticmethod
Selects a color (int) from range(len(color_distribution)) based on the given color_distribution array, where each entry represents the probability of selecting that index.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
color_distribution
|
ndarray
|
Array determining the selection probabilities. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The selected index based on the given probabilities. |
Example
color_distribution = [0.2, 0.3, 0.5] Color 1 will be selected with a probability of 0.3.
Source code in src/models/participation_model.py
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 | |
color_patches(cell, patch_power)
Meant to create a less random initial color distribution using a similar logic to the color patches model. It uses a (normalized) bias coordinate to center the impact of the color patches structures impact around.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cell
|
ColorCell
|
The cell possibly changing color. |
required |
patch_power
|
float
|
Radius-like impact around bias point. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
Consensus color or the cell's own color if no consensus. |
Source code in src/models/participation_model.py
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 | |
create_all_options(n, include_ties=False)
staticmethod
Creates a matrix (an array of all possible ranking vectors), if specified including ties. Rank values start from 0.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
The number of items to rank (number of colors in our case) |
required |
include_ties
|
bool
|
If True, rankings include ties. |
False
|
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: A matrix containing all possible ranking vectors. |
Source code in src/models/participation_model.py
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 | |
create_color_distribution(heterogeneity)
This method is used to create a color distribution that has a bias according to the given heterogeneity factor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
heterogeneity
|
float
|
Factor used as sigma in 'random.gauss'. |
required |
Source code in src/models/participation_model.py
438 439 440 441 442 443 444 445 446 447 448 449 450 451 | |
create_personalities(n)
Creates n unique personalities as permutations of color indices.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Number of unique personalities. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: Shape |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Example
for n=2 and self.num_colors=3, the function could return:
[[1, 0, 2], [2, 1, 0]]
Source code in src/models/participation_model.py
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 | |
init_color_probs(election_impact)
This method initializes a probability array for the mutation of colors. The probabilities reflect the election outcome with some impact factor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
election_impact
|
float
|
The impact the election has on the mutation. |
required |
Source code in src/models/participation_model.py
251 252 253 254 255 256 257 258 259 260 261 262 | |
initialize_all_areas()
Initializes all areas on the grid in the model.
This method divides the grid into approximately evenly distributed areas,
ensuring that the areas are spaced as uniformly as possible based
on the grid dimensions and the average area size specified by
av_area_width and av_area_height.
The grid may contain more or fewer areas than an exact square
grid arrangement due to num_areas not always being a perfect square.
If the number of areas is not a perfect square, the remaining areas
are placed randomly on the grid to ensure that num_areas
areas are initialized.
Initializes num_areas and places them directly on the grid.
But if self.num_areas == 0, the method exits early.
Example
- Given
num_areas = 4andgrid.width = grid.height = 10, this method might initialize areas with approximate distances to maximize uniform distribution (like a 2x2 grid). - For
num_areas = 5, four areas will be initialized evenly, and the fifth will be placed randomly due to the uneven distribution.
Source code in src/models/participation_model.py
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 | |
initialize_area(a_id, x_coord, y_coord)
This method initializes one area in the models' grid.
Source code in src/models/participation_model.py
264 265 266 267 268 269 270 271 272 273 274 | |
initialize_global_area()
Initializes the global area spanning the whole grid.
Returns:
| Name | Type | Description |
|---|---|---|
Area |
Area
|
The global area (with unique_id set to -1 and idx to (0, 0)). |
Source code in src/models/participation_model.py
330 331 332 333 334 335 336 337 338 339 340 341 | |
initialize_voting_agents(id_start=0)
This method initializes as many voting agents as set in the model with a randomly chosen personality. It places them randomly on the grid. It also ensures that each agent is assigned to the color cell it is standing on. Args: id_start (int): The starting ID for agents to ensure unique IDs.
Source code in src/models/participation_model.py
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 | |
pers_dist(size)
staticmethod
This method creates a normalized normal distribution array for picking and depicting the distribution of personalities in the model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
size
|
int
|
The mean value of the normal distribution. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: Normalized (sum is one) array mimicking a gaussian curve. |
Source code in src/models/participation_model.py
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 | |
step()
Advance the model by one step.
Source code in src/models/participation_model.py
408 409 410 411 412 413 414 415 416 417 418 419 | |
update_av_area_color_dst()
This method updates the av_area_color_dst attribute of the model. Beware: On overlapping areas, cells are counted several times.
Source code in src/models/participation_model.py
495 496 497 498 499 500 501 502 503 504 | |