diff --git a/gis/geo_schelling_experimental/GIS_Schelling.ipynb b/gis/geo_schelling_experimental/GIS_Schelling.ipynb deleted file mode 100644 index 3976be79..00000000 --- a/gis/geo_schelling_experimental/GIS_Schelling.ipynb +++ /dev/null @@ -1,267 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 1, - "id": "37528711-4873-42e7-be7f-926130026dd8", - "metadata": { - "jupyter": { - "source_hidden": true - } - }, - "outputs": [], - "source": [ - "import random\n", - "\n", - "import mesa\n", - "import mesa_geo as mg\n", - "import mesa_geo.geoexperimental as mge" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "id": "27cac12c-515e-4ef4-83cc-d06918921c70", - "metadata": { - "jupyter": { - "source_hidden": true - } - }, - "outputs": [], - "source": [ - "class SchellingAgent(mg.GeoAgent):\n", - " \"\"\"Schelling segregation agent.\"\"\"\n", - "\n", - " def __init__(self, unique_id, model, geometry, crs, agent_type=None):\n", - " \"\"\"Create a new Schelling agent.\n", - "\n", - " Args:\n", - " unique_id: Unique identifier for the agent.\n", - " agent_type: Indicator for the agent's type (minority=1, majority=0)\n", - " \"\"\"\n", - " super().__init__(unique_id, model, geometry, crs)\n", - " self.atype = agent_type\n", - "\n", - " def step(self):\n", - " \"\"\"Advance agent one step.\"\"\"\n", - " similar = 0\n", - " different = 0\n", - " neighbors = self.model.space.get_neighbors(self)\n", - " if neighbors:\n", - " for neighbor in neighbors:\n", - " if neighbor.atype is None:\n", - " continue\n", - " elif neighbor.atype == self.atype:\n", - " similar += 1\n", - " else:\n", - " different += 1\n", - "\n", - " # If unhappy, move:\n", - " if similar < different:\n", - " # Select an empty region\n", - " empties = [a for a in self.model.space.agents if a.atype is None]\n", - " # Switch atypes and add/remove from scheduler\n", - " new_region = random.choice(empties)\n", - " new_region.atype = self.atype\n", - " self.model.schedule.add(new_region)\n", - " self.atype = None\n", - " self.model.schedule.remove(self)\n", - " else:\n", - " self.model.happy += 1\n", - "\n", - " def __repr__(self):\n", - " return \"Agent \" + str(self.unique_id)" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "id": "9c9a9738-7e8f-458c-b0aa-cfc69c04ee87", - "metadata": { - "jupyter": { - "source_hidden": true - } - }, - "outputs": [], - "source": [ - "class GeoSchelling(mesa.Model):\n", - " \"\"\"Model class for the Schelling segregation model.\"\"\"\n", - "\n", - " def __init__(self, density=0.6, minority_pc=0.2, export_data=False):\n", - " super().__init__()\n", - " self.density = density\n", - " self.minority_pc = minority_pc\n", - " self.export_data = export_data\n", - "\n", - " self.schedule = mesa.time.RandomActivation(self)\n", - " self.space = mg.GeoSpace(crs=\"EPSG:4326\", warn_crs_conversion=True)\n", - "\n", - " self.happy = 0\n", - " self.datacollector = mesa.DataCollector({\"happy\": \"happy\"})\n", - "\n", - " self.running = True\n", - "\n", - " # Set up the grid with patches for every NUTS region\n", - " ac = mg.AgentCreator(SchellingAgent, model=self)\n", - " agents = ac.from_file(\"data/nuts_rg_60M_2013_lvl_2.geojson\")\n", - " self.space.add_agents(agents)\n", - "\n", - " # Set up agents\n", - " for agent in agents:\n", - " if random.random() < self.density:\n", - " if random.random() < self.minority_pc:\n", - " agent.atype = 1\n", - " else:\n", - " agent.atype = 0\n", - " self.schedule.add(agent)\n", - "\n", - " def export_agents_to_file(self) -> None:\n", - " self.space.get_agents_as_GeoDataFrame(agent_cls=SchellingAgent).to_crs(\n", - " \"epsg:4326\"\n", - " ).to_file(\"data/schelling_agents.geojson\", driver=\"GeoJSON\")\n", - "\n", - " def step(self):\n", - " \"\"\"Run one step of the model.\n", - "\n", - " If All agents are happy, halt the model.\n", - " \"\"\"\n", - "\n", - " self.happy = 0 # Reset counter of happy agents\n", - " self.schedule.step()\n", - " self.datacollector.collect(self)\n", - "\n", - " if self.happy == self.schedule.get_agent_count():\n", - " self.running = False\n", - "\n", - " if not self.running and self.export_data:\n", - " self.export_agents_to_file()" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "id": "0bf74b4c-36ab-416c-a4db-ab5104189d92", - "metadata": { - "jupyter": { - "source_hidden": true - } - }, - "outputs": [], - "source": [ - "def schelling_draw(agent):\n", - " \"\"\"\n", - " Portrayal Method for canvas\n", - " \"\"\"\n", - " portrayal = {}\n", - " if agent.atype is None:\n", - " portrayal[\"color\"] = \"Grey\"\n", - " elif agent.atype == 0:\n", - " portrayal[\"color\"] = \"Red\"\n", - " else:\n", - " portrayal[\"color\"] = \"Blue\"\n", - " return portrayal\n", - "\n", - "\n", - "model_params = {\n", - " \"density\": {\n", - " \"type\": \"SliderFloat\",\n", - " \"value\": 0.6,\n", - " \"label\": \"Population Density\",\n", - " \"min\": 0.0,\n", - " \"max\": 1.0,\n", - " \"step\": 0.1,\n", - " },\n", - " \"minority_pc\": {\n", - " \"type\": \"SliderFloat\",\n", - " \"value\": 0.2,\n", - " \"label\": \"Fraction Minority\",\n", - " \"min\": 0.0,\n", - " \"max\": 1.0,\n", - " \"step\": 0.05,\n", - " },\n", - " \"export_data\": {\n", - " \"type\": \"Checkbox\",\n", - " \"value\": False,\n", - " \"description\": \"Export Data\",\n", - " \"disabled\": False,\n", - " },\n", - "}" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "id": "c8a2533e-95ba-4a2b-b21a-56e05fe3b7b2", - "metadata": { - "jupyter": { - "source_hidden": true - } - }, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "d4853c72527245678d5cb9d38d4a15ec", - "version_major": 2, - "version_minor": 0 - }, - "text/html": [ - "Cannot show widget. You probably want to rerun the code cell above (Click in the code cell, and press Shift+Enter +)." - ], - "text/plain": [ - "Cannot show ipywidgets in text" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "page = mge.GeoJupyterViz(\n", - " GeoSchelling,\n", - " model_params,\n", - " measures=[\"happy\"],\n", - " name=\"Geo-Schelling Model\",\n", - " agent_portrayal=schelling_draw,\n", - " zoom=3,\n", - " center_point=[52, 12],\n", - ")\n", - "# This is required to render the visualization in the Jupyter notebook\n", - "page" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "eb7fbf17-30a9-4c02-a2b8-c211ab16d32b", - "metadata": { - "jupyter": { - "source_hidden": true - } - }, - "outputs": [], - "source": [] - } - ], - "metadata": { - "kernelspec": { - "display_name": "MesaGeoDev", - "language": "python", - "name": "mesageodev" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.12.2" - } - }, - "nbformat": 4, - "nbformat_minor": 5 -}