|
104 | 104 | "\n",
|
105 | 105 | "class MoneyAgent(Agent):\n",
|
106 | 106 | " \"\"\"An agent with fixed initial wealth.\"\"\"\n",
|
107 |
| - " def __init__(self, unique_id):\n", |
108 |
| - " self.unique_id = unique_id\n", |
| 107 | + " def __init__(self, unique_id, model):\n", |
| 108 | + " super().__init__(unique_id, model)\n", |
109 | 109 | " self.wealth = 1\n",
|
110 | 110 | "\n",
|
111 | 111 | "class MoneyModel(Model):\n",
|
|
114 | 114 | " self.num_agents = N\n",
|
115 | 115 | " # Create agents\n",
|
116 | 116 | " for i in range(self.num_agents):\n",
|
117 |
| - " a = MoneyAgent(i)" |
| 117 | + " a = MoneyAgent(i, self)" |
118 | 118 | ]
|
119 | 119 | },
|
120 | 120 | {
|
|
145 | 145 | "\n",
|
146 | 146 | "class MoneyAgent(Agent):\n",
|
147 | 147 | " \"\"\" An agent with fixed initial wealth.\"\"\"\n",
|
148 |
| - " def __init__(self, unique_id):\n", |
149 |
| - " self.unique_id = unique_id\n", |
| 148 | + " def __init__(self, unique_id, model):\n", |
| 149 | + " super().__init__(unique_id, model)\n", |
150 | 150 | " self.wealth = 1\n",
|
151 | 151 | "\n",
|
152 |
| - " def step(self, model):\n", |
| 152 | + " def step(self):\n", |
153 | 153 | " # The agent's step will go here.\n",
|
154 | 154 | " pass\n",
|
155 | 155 | "\n",
|
|
160 | 160 | " self.schedule = RandomActivation(self)\n",
|
161 | 161 | " # Create agents\n",
|
162 | 162 | " for i in range(self.num_agents):\n",
|
163 |
| - " a = MoneyAgent(i)\n", |
| 163 | + " a = MoneyAgent(i, self)\n", |
164 | 164 | " self.schedule.add(a)\n",
|
165 | 165 | "\n",
|
166 | 166 | " def step(self):\n",
|
|
242 | 242 | "source": [
|
243 | 243 | "class MoneyAgent(Agent):\n",
|
244 | 244 | " \"\"\" An agent with fixed initial wealth.\"\"\"\n",
|
245 |
| - " def __init__(self, unique_id):\n", |
246 |
| - " self.unique_id = unique_id\n", |
| 245 | + " def __init__(self, unique_id, model):\n", |
| 246 | + " super().__init__(unique_id, model)\n", |
247 | 247 | " self.wealth = 1\n",
|
248 | 248 | "\n",
|
249 |
| - " def step(self, model):\n", |
| 249 | + " def step(self):\n", |
250 | 250 | " if self.wealth == 0:\n",
|
251 | 251 | " return\n",
|
252 |
| - " other_agent = random.choice(model.schedule.agents)\n", |
| 252 | + " other_agent = random.choice(self.model.schedule.agents)\n", |
253 | 253 | " other_agent.wealth += 1\n",
|
254 | 254 | " self.wealth -= 1"
|
255 | 255 | ]
|
|
449 | 449 | " \n",
|
450 | 450 | " # Create agents\n",
|
451 | 451 | " for i in range(self.num_agents):\n",
|
452 |
| - " a = MoneyAgent(i)\n", |
| 452 | + " a = MoneyAgent(i, self)\n", |
453 | 453 | " self.schedule.add(a)\n",
|
454 | 454 | " \n",
|
455 | 455 | " # Add the agent to a random grid cell\n",
|
|
483 | 483 | "```python\n",
|
484 | 484 | "class MoneyAgent(Agent):\n",
|
485 | 485 | " #...\n",
|
486 |
| - " def move(self, model):\n", |
487 |
| - " possible_steps = model.grid.get_neighborhood(\n", |
| 486 | + " def move(self):\n", |
| 487 | + " possible_steps = self.model.grid.get_neighborhood(\n", |
488 | 488 | " self.pos, \n",
|
489 | 489 | " moore=True,\n",
|
490 | 490 | " include_center=False)\n",
|
491 | 491 | " new_position = random.choice(possible_steps)\n",
|
492 |
| - " model.grid.move_agent(self, new_position)\n", |
| 492 | + " self.model.grid.move_agent(self, new_position)\n", |
493 | 493 | "```\n",
|
494 | 494 | "\n",
|
495 | 495 | "Next, we need to get all the other agents present in a cell, and give one of them some money. We can get the contents of one or more cells using the grid's `get_cell_list_contents` method, or by accessing a cell directly. The method accepts a list of cell coordinate tuples, or a single tuple if we only care about one cell.\n",
|
496 | 496 | "\n",
|
497 | 497 | "```python\n",
|
498 | 498 | "class MoneyAgent(Agent):\n",
|
499 | 499 | " #...\n",
|
500 |
| - " def give_money(self, model):\n", |
501 |
| - " cellmates = model.grid.get_cell_list_contents([self.pos])\n", |
| 500 | + " def give_money(self):\n", |
| 501 | + " cellmates = self.model.grid.get_cell_list_contents([self.pos])\n", |
502 | 502 | " if len(cellmates) > 1:\n",
|
503 | 503 | " other = random.choice(cellmates)\n",
|
504 | 504 | " other.wealth += 1\n",
|
|
510 | 510 | "```python\n",
|
511 | 511 | "class MoneyAgent(Agent):\n",
|
512 | 512 | " # ...\n",
|
513 |
| - " def step(self, model):\n", |
514 |
| - " self.move(model)\n", |
| 513 | + " def step(self):\n", |
| 514 | + " self.move()\n", |
515 | 515 | " if self.wealth > 0:\n",
|
516 |
| - " self.give_money(model)\n", |
| 516 | + " self.give_money()\n", |
517 | 517 | "```\n",
|
518 | 518 | "\n",
|
519 | 519 | "Now, putting that all together should look like this:"
|
|
535 | 535 | " self.schedule = RandomActivation(self)\n",
|
536 | 536 | " # Create agents\n",
|
537 | 537 | " for i in range(self.num_agents):\n",
|
538 |
| - " a = MoneyAgent(i)\n", |
| 538 | + " a = MoneyAgent(i, self)\n", |
539 | 539 | " self.schedule.add(a)\n",
|
540 | 540 | " # Add the agent to a random grid cell\n",
|
541 | 541 | " x = random.randrange(self.grid.width)\n",
|
|
547 | 547 | "\n",
|
548 | 548 | "class MoneyAgent(Agent):\n",
|
549 | 549 | " \"\"\" An agent with fixed initial wealth.\"\"\"\n",
|
550 |
| - " def __init__(self, unique_id):\n", |
551 |
| - " self.unique_id = unique_id\n", |
| 550 | + " def __init__(self, unique_id, model):\n", |
| 551 | + " super().__init__(unique_id, model)\n", |
552 | 552 | " self.wealth = 1\n",
|
553 | 553 | "\n",
|
554 |
| - " def move(self, model):\n", |
555 |
| - " possible_steps = model.grid.get_neighborhood(\n", |
| 554 | + " def move(self):\n", |
| 555 | + " possible_steps = self.model.grid.get_neighborhood(\n", |
556 | 556 | " self.pos, \n",
|
557 | 557 | " moore=True, \n",
|
558 | 558 | " include_center=False)\n",
|
559 | 559 | " new_position = random.choice(possible_steps)\n",
|
560 |
| - " model.grid.move_agent(self, new_position)\n", |
| 560 | + " self.model.grid.move_agent(self, new_position)\n", |
561 | 561 | "\n",
|
562 |
| - " def give_money(self, model):\n", |
563 |
| - " cellmates = model.grid.get_cell_list_contents([self.pos])\n", |
| 562 | + " def give_money(self):\n", |
| 563 | + " cellmates = self.model.grid.get_cell_list_contents([self.pos])\n", |
564 | 564 | " if len(cellmates) > 1:\n",
|
565 | 565 | " other = random.choice(cellmates)\n",
|
566 | 566 | " other.wealth += 1\n",
|
567 | 567 | " self.wealth -= 1\n",
|
568 | 568 | "\n",
|
569 |
| - " def step(self, model):\n", |
570 |
| - " self.move(model)\n", |
| 569 | + " def step(self):\n", |
| 570 | + " self.move()\n", |
571 | 571 | " if self.wealth > 0:\n",
|
572 |
| - " self.give_money(model)" |
| 572 | + " self.give_money()" |
573 | 573 | ]
|
574 | 574 | },
|
575 | 575 | {
|
|
676 | 676 | "\n",
|
677 | 677 | "class MoneyAgent(Agent):\n",
|
678 | 678 | " \"\"\" An agent with fixed initial wealth.\"\"\"\n",
|
679 |
| - " def __init__(self, unique_id):\n", |
680 |
| - " self.unique_id = unique_id\n", |
| 679 | + " def __init__(self, unique_id, model):\n", |
| 680 | + " super().__init__(unique_id, model)\n", |
681 | 681 | " self.wealth = 1\n",
|
682 | 682 | "\n",
|
683 |
| - " def move(self, model):\n", |
684 |
| - " possible_steps = model.grid.get_neighborhood(\n", |
| 683 | + " def move(self):\n", |
| 684 | + " possible_steps = self.model.grid.get_neighborhood(\n", |
685 | 685 | " self.pos, \n",
|
686 | 686 | " moore=True, \n",
|
687 | 687 | " include_center=False)\n",
|
688 | 688 | " new_position = random.choice(possible_steps)\n",
|
689 |
| - " model.grid.move_agent(self, new_position)\n", |
| 689 | + " self.model.grid.move_agent(self, new_position)\n", |
690 | 690 | "\n",
|
691 |
| - " def give_money(self, model):\n", |
692 |
| - " cellmates = model.grid.get_cell_list_contents([self.pos])\n", |
| 691 | + " def give_money(self):\n", |
| 692 | + " cellmates = self.model.grid.get_cell_list_contents([self.pos])\n", |
693 | 693 | " if len(cellmates) > 1:\n",
|
694 | 694 | " other = random.choice(cellmates)\n",
|
695 | 695 | " other.wealth += 1\n",
|
696 | 696 | " self.wealth -= 1\n",
|
697 | 697 | "\n",
|
698 |
| - " def step(self, model):\n", |
699 |
| - " self.move(model)\n", |
| 698 | + " def step(self):\n", |
| 699 | + " self.move()\n", |
700 | 700 | " if self.wealth > 0:\n",
|
701 |
| - " self.give_money(model)\n", |
| 701 | + " self.give_money()\n", |
702 | 702 | "\n",
|
703 | 703 | "class MoneyModel(Model):\n",
|
704 | 704 | " \"\"\"A model with some number of agents.\"\"\"\n",
|
|
709 | 709 | " \n",
|
710 | 710 | " # Create agents\n",
|
711 | 711 | " for i in range(self.num_agents):\n",
|
712 |
| - " a = MoneyAgent(i)\n", |
| 712 | + " a = MoneyAgent(i, self)\n", |
713 | 713 | " self.schedule.add(a)\n",
|
714 | 714 | " # Add the agent to a random grid cell\n",
|
715 | 715 | " x = random.randrange(self.grid.width)\n",
|
|
989 | 989 | " \n",
|
990 | 990 | " # Create agents\n",
|
991 | 991 | " for i in range(self.num_agents):\n",
|
992 |
| - " a = MoneyAgent(i)\n", |
| 992 | + " a = MoneyAgent(i, self)\n", |
993 | 993 | " self.schedule.add(a)\n",
|
994 | 994 | " # Add the agent to a random grid cell\n",
|
995 | 995 | " x = random.randrange(self.grid.width)\n",
|
|
0 commit comments