An economy simulator that models the emergent behavior of agents gathering and trading resources based on their resource bundle preferences in a bartering economy. Agents accumulate hunger (bias towards food) over time and have a static greed (bias towards gold). From these two biases, the usage of a commodity (gold, in this case) as a currency emerges naturally emerges in some worlds--demonstrating how monetary economies can emerge from bartering economies. Phenomena such as supply, demand, and price equilibrium emerge without any centralized control.
I made this to exercise the concepts I learned in the excellent MIT OpenCourseware Introduction to Microeconomics course.
This simulation implements several foundational microeconomic concepts to drive agent behavior:
Agents navigate a procedural grid world to collect resources. Food is necessary for survival and is consumed every "tick" to stave off starvation. Gold, on the other hand, is inherently useless (it is just shiny) but often emerges in some worlds with sufficiently greedy populations as a store of value and medium of exchange. The scarcity and location of these resources dictate the macro supply of the economy.
Every agent has assigned personality traits: hunger and greed. These variables act as parameters that shape an agent's utility function, yielding personalized indifference curves. An indifference curve represents combinations of food and gold (consumption bundles) that provide the exact same level of utility to an agent. For example, a starving agent will have a steep indifference curve strongly favoring food, while a well-fed but greedy agent will have a curve reflecting a strong preference for gold. Even staring greedy agents will have a strong indifference curve favoring food, so as to ensure survival.
The simulation models this preference using a modified Cobb-Douglas Utility Function:
Where
Derived directly from the indifference curve, the Marginal Rate of Substitution (MRS) represents the exact amount of gold an agent is willing to trade for one unit of food at their current state. The MRS corresponds to the slope of the indifference curve at the agent's current inventory levels. This dynamically changes as they consume food or acquire more gold.
Mathematically, the MRS is the ratio of the Marginal Utility of Food (
The Marginal Utility of Food (the partial derivative of the utility function with respect to food) is defined as:
The Marginal Utility of Gold (the partial derivative with respect to gold) is:
Thus, the Marginal Rate of Substitution simplifies to:
This calculated
At the end of each tick, agents participate in a global market. Using their MRS and the previous tick’s market price, agents submit bids (if they value food more than the market price) or asks (if they value it less). The market is implemented with an order book, matching the highest bids with the lowest asks to find an equilibrium clearing price. Through this mechanism, resources flow from those who have a surplus to those who need them most, driving economic efficiency and often enabling the survival and even thriving of overly greedy agents who are too gold-focused to want to collect food until they are about to starve to death.
Despite agents acting purely on simple, local utility maximization logic without any awareness of the global state, complex macroeconomic patterns naturally emerge. Wealth tends to concentrate among agents who are either geographically lucky or value gold/food appropriately based on the scaricty of each resource in the world and the greed/hunger of their peers. In a world of gold-scarcity and greedy agents, gold will be extremely valuable. In a world of food-scarcity and hungry agents, food will be extremely valuable. Market prices fluctuate dynamically based on total resource scarcity, and population dynamics are continuously shaped by starvation. The economy balances itself purely through decentralized rational choices.
To build and run the simulation locally, ensure you have Bun installed.
-
Install dependencies:
bun install
-
Start the development server:
bun run dev
-
Open the provided localhost URL in your browser to view the simulation (typically
http://localhost:5173). Click the Play button to begin the simulation.
