|
| 1 | +# Sugarscape Constant Growback Model with Traders |
| 2 | + |
| 3 | +## Summary |
| 4 | + |
| 5 | +This model is based on Epstein & Axtell's classic "Sugarscape" simulation from Growing Artificial Societies (1996), specifically the G1MT (Growback 1, Metabolism, Trade) variation. Trader agents wander a grid populated with two unevenly distributed resources: Sugar and Spice. Agents are endowed with individual metabolic rates for each resource and a vision range; there are also Resource agents, which represent the landscape and regenerate food over time. |
| 6 | + |
| 7 | +The model generates emergent economic dynamics through decentralized interactions. Traders must constantly harvest resources to satisfy their metabolic needs; if they run out of either sugar or spice, they starve. Crucially, agents can trade with neighbors. Decisions are governed by the Marginal Rate of Substitution (MRS); agents rich in sugar but poor in spice will trade sugar to acquire spice, and vice versa. Over time, this decentralized trading allows for the emergence of a price equilibrium and wealth distribution patterns. |
| 8 | + |
| 9 | +This model is implemented using Mesa-LLM, unlike the original deterministic versions. All Trader agents use Large Language Models to "think" about their survival. They observe their internal inventory and MRS, then autonomously decide to use tools to move to high-value resource tiles or propose trades to neighbors to ensure their continued existence. |
| 10 | + |
| 11 | +## Technical Details |
| 12 | + |
| 13 | +Agents |
| 14 | + |
| 15 | +- `Trader (LLMAgent):` The primary actor equipped with STLTMemory and ReActReasoning. |
| 16 | + |
| 17 | + Internal State: Dynamically updates a context string with current inventory (Sugar, Spice) and hunger warnings to guide the LLM. |
| 18 | + |
| 19 | + Metabolism: Consumes a fixed amount of resources per step. Zero inventory results in agent removal (death). |
| 20 | + |
| 21 | + MRS Calculation: Computes the Marginal Rate of Substitution (MRS) using the Cobb-Douglas formula to value Sugar vs. Spice relative to biological needs. |
| 22 | + |
| 23 | +- `Resource (CellAgent):` A passive environmental agent that acts as a container for resources. It regenerates its current_amount by 1 unit per step up to a max_capacity. |
| 24 | + |
| 25 | +Tools |
| 26 | + |
| 27 | +- `move_to_best_resource:` |
| 28 | + |
| 29 | + Function: Scans the local grid within the agent's vision radius. |
| 30 | + |
| 31 | + Action: Identifies the cell with the highest current_amount of resources, moves the agent there, and automatically harvests the full amount into the agent's inventory. |
| 32 | + |
| 33 | +- `propose_trade:` |
| 34 | + |
| 35 | + Function: Targets a specific neighbor by unique_id. |
| 36 | + |
| 37 | + Logic: Executes a trade only if the partner's MRS is higher than the proposer's (indicating the partner values Sugar more highly). This ensures trades are mathematically rational and mutually beneficial. |
| 38 | + |
| 39 | + |
| 40 | +### Movement Rule (Rule M) |
| 41 | + |
| 42 | +A Trader agent moves to a new location and harvests resources if the following logic, executed by the *move_to_best_resource tool*, is satisfied: |
| 43 | + |
| 44 | +Scan: The agent inspects all cells within its *vision range* (von Neumann neighborhood). |
| 45 | + |
| 46 | +Identify: It identifies the cell containing a *Resource* agent with the highest *current_amount* of sugar/spice. |
| 47 | + |
| 48 | +Harvest: The agent moves to that cell, sets the resource's amount to 0, and adds the harvested amount to its own inventory. |
| 49 | + |
| 50 | +### Trade Rule (Rule T) |
| 51 | + |
| 52 | +Agents determine whether to trade based on their Marginal Rate of Substitution (MRS). A trade is proposed via the propose_trade tool and accepted if it is mutually beneficial: |
| 53 | + |
| 54 | +``` |
| 55 | +Trade occurs if: Partner_MRS > Agent_MRS |
| 56 | +``` |
| 57 | + |
| 58 | +Where the MRS is calculated using the agent's inventory and metabolism: |
| 59 | + |
| 60 | +``` |
| 61 | +MRS = (spice_inventory / spice_metabolism) / (sugar_inventory / sugar_metabolism) |
| 62 | +``` |
| 63 | + |
| 64 | +In this implementation: |
| 65 | + |
| 66 | +- `Agent (Proposer):` Gives Sugar, Receives Spice. |
| 67 | + |
| 68 | +- `Partner (Receiver):` Receives Sugar, Gives Spice. |
| 69 | + |
| 70 | +- This flow ensures resources move from agents who value them less to agents who value them more. |
| 71 | + |
| 72 | +### Resource Behavior |
| 73 | + |
| 74 | +Resource Agents represent the landscape. They are passive agents that regenerate wealth over time: |
| 75 | + |
| 76 | +- `Growback:` At every step, a Resource agent increases its current_amount by growback (default: 1). |
| 77 | + |
| 78 | +- `Capacity:` This growth is capped at the agent's max_capacity. |
| 79 | + |
| 80 | +### LLM-Powered Agents |
| 81 | + |
| 82 | +Both Traders and the simulation logic are driven by LLM-powered agents, meaning: |
| 83 | + |
| 84 | +- Their actions (e.g., `move_to_best_resource`, `propose_trade`) are determined by a ReAct reasoning module. |
| 85 | + |
| 86 | +- This module takes as input: |
| 87 | + |
| 88 | + The agent’s internal state (current inventory, metabolic warnings, and calculated MRS). |
| 89 | + |
| 90 | + Local observations of the grid. |
| 91 | + |
| 92 | +- A set of available tools defined in `tools.py`. |
| 93 | + |
| 94 | + |
| 95 | + |
| 96 | +## How to Run |
| 97 | + |
| 98 | +If you have cloned the repo into your local machine, ensure you run the following command from the root of the library: ``pip install -e . ``. Then, you will need an api key of an LLM-provider of your choice. (This model in particular makes a large amount of calls per minute and we therefore recommend getting a paid version of an api-key that can offer high rate-limits). Once you have obtained the api-key follow the below steps to set it up for this model. |
| 99 | +1) Ensure the dotenv package is installed. If not, run ``pip install python-dotenv``. |
| 100 | +2) In the root folder of the project, create a file named .env. |
| 101 | +3) If you are using openAI's api key, add the following command in the .env file: ``OPENAI_API_KEY=sk-xxxxxxxxxxxxxxxxxxxxxxxx``. If you have the paid version of Gemini, use this line instead: ``GEMINI_API_KEY=your-gemini-api-key-here``(the free ones tend to not work with this model). |
| 102 | +4) Change the ``api_key`` specification in app.py according to the provider you have chosen. |
| 103 | +5) Similarly change the ``llm_model`` attribute as well in app.py to the name of a model you have access to. Ensure it is in the form of {provider}/{model_name}. For e.g. ``openai/gpt-4o-mini``. |
| 104 | + |
| 105 | +Once you have set up the api-key in your system, run the following command from this directory: |
| 106 | + |
| 107 | +``` |
| 108 | + $ solara run app.py |
| 109 | +``` |
| 110 | + |
| 111 | + |
| 112 | +## Files |
| 113 | + |
| 114 | +* ``model.py``: Core model code. |
| 115 | +* ``agents.py``: Agent classes. |
| 116 | +* ``app.py``: Sets up the interactive visualization. |
| 117 | +* ``tools.py``: Tools for the llm-agents to use. |
| 118 | + |
| 119 | + |
| 120 | +## Further Reading |
| 121 | + |
| 122 | +[Growing Artificial Societies](https://mitpress.mit.edu/9780262550253/growing-artificial-societies/) |
| 123 | +[Complexity Explorer Sugarscape with Traders Tutorial](https://www.complexityexplorer.org/courses/172-agent-based-models-with-python-an-introduction-to-mesa#gsc.tab=0) |
0 commit comments