econsimulacra 0.1.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- econsimulacra-0.1.1/LICENSE +21 -0
- econsimulacra-0.1.1/PKG-INFO +199 -0
- econsimulacra-0.1.1/README.md +165 -0
- econsimulacra-0.1.1/pyproject.toml +71 -0
- econsimulacra-0.1.1/src/econsimulacra/__init__.py +19 -0
- econsimulacra-0.1.1/src/econsimulacra/agents/__init__.py +4 -0
- econsimulacra-0.1.1/src/econsimulacra/agents/base.py +237 -0
- econsimulacra-0.1.1/src/econsimulacra/agents/llm_agent.py +99 -0
- econsimulacra-0.1.1/src/econsimulacra/animations/__init__.py +11 -0
- econsimulacra-0.1.1/src/econsimulacra/animations/base.py +93 -0
- econsimulacra-0.1.1/src/econsimulacra/animations/grid_space_animator.py +224 -0
- econsimulacra-0.1.1/src/econsimulacra/animations/social_network_animator.py +286 -0
- econsimulacra-0.1.1/src/econsimulacra/envs/__init__.py +77 -0
- econsimulacra-0.1.1/src/econsimulacra/envs/base.py +1758 -0
- econsimulacra-0.1.1/src/econsimulacra/envs/event.py +216 -0
- econsimulacra-0.1.1/src/econsimulacra/envs/memory.py +856 -0
- econsimulacra-0.1.1/src/econsimulacra/envs/obs_providers.py +579 -0
- econsimulacra-0.1.1/src/econsimulacra/envs/order.py +218 -0
- econsimulacra-0.1.1/src/econsimulacra/envs/social_networks/__init__.py +6 -0
- econsimulacra-0.1.1/src/econsimulacra/envs/social_networks/base.py +281 -0
- econsimulacra-0.1.1/src/econsimulacra/envs/social_networks/recsys.py +266 -0
- econsimulacra-0.1.1/src/econsimulacra/envs/space.py +163 -0
- econsimulacra-0.1.1/src/econsimulacra/envs/time_translator.py +71 -0
- econsimulacra-0.1.1/src/econsimulacra/items/__init__.py +3 -0
- econsimulacra-0.1.1/src/econsimulacra/items/base.py +54 -0
- econsimulacra-0.1.1/src/econsimulacra/llm_services/__init__.py +23 -0
- econsimulacra-0.1.1/src/econsimulacra/llm_services/clients/__init__.py +5 -0
- econsimulacra-0.1.1/src/econsimulacra/llm_services/clients/base.py +158 -0
- econsimulacra-0.1.1/src/econsimulacra/llm_services/clients/openai_client.py +117 -0
- econsimulacra-0.1.1/src/econsimulacra/llm_services/clients/transformers_client.py +102 -0
- econsimulacra-0.1.1/src/econsimulacra/llm_services/constant.py +216 -0
- econsimulacra-0.1.1/src/econsimulacra/llm_services/personas/__init__.py +4 -0
- econsimulacra-0.1.1/src/econsimulacra/llm_services/personas/base.py +79 -0
- econsimulacra-0.1.1/src/econsimulacra/llm_services/personas/big5.py +53 -0
- econsimulacra-0.1.1/src/econsimulacra/llm_services/prompts/__init__.py +3 -0
- econsimulacra-0.1.1/src/econsimulacra/llm_services/prompts/base.py +137 -0
- econsimulacra-0.1.1/src/econsimulacra/logs/__init__.py +35 -0
- econsimulacra-0.1.1/src/econsimulacra/logs/base.py +522 -0
- econsimulacra-0.1.1/src/econsimulacra/logs/dict_logger.py +41 -0
- econsimulacra-0.1.1/src/econsimulacra/py.typed +0 -0
- econsimulacra-0.1.1/src/econsimulacra/sim_utils.py +124 -0
- econsimulacra-0.1.1/src/econsimulacra/simulator.py +346 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 SimulacraBusiness
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: econsimulacra
|
|
3
|
+
Version: 0.1.1
|
|
4
|
+
Summary: EconSimulacra: A Digital Twin Platform of Consumer Economy Powered by LLM Agents
|
|
5
|
+
Home-page: https://github.com/SimulacraBusiness/econsimulacra
|
|
6
|
+
License: MIT
|
|
7
|
+
Author: Ryuji Hashimoto
|
|
8
|
+
Author-email: r_hashimoto@simulacra.co.jp
|
|
9
|
+
Requires-Python: >=3.10,<3.13
|
|
10
|
+
Classifier: Development Status :: 3 - Alpha
|
|
11
|
+
Classifier: Intended Audience :: Science/Research
|
|
12
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Classifier: Typing :: Typed
|
|
18
|
+
Requires-Dist: accelerate (>=1.12.0,<2.0.0)
|
|
19
|
+
Requires-Dist: datasets (>=4.5.0,<5.0.0)
|
|
20
|
+
Requires-Dist: numpy (>=1.26.1,<2.0.0)
|
|
21
|
+
Requires-Dist: openai (>=2.21.0,<3.0.0)
|
|
22
|
+
Requires-Dist: outlines (==0.1.11)
|
|
23
|
+
Requires-Dist: protobuf (>=7.34.0,<8.0.0)
|
|
24
|
+
Requires-Dist: rich (>=14.2.0,<15.0.0)
|
|
25
|
+
Requires-Dist: sentencepiece (>=0.2.1,<0.3.0)
|
|
26
|
+
Requires-Dist: statsmodels (>=0.14.6,<0.15.0)
|
|
27
|
+
Requires-Dist: torch (==2.2.1)
|
|
28
|
+
Requires-Dist: transformers (>=4.49.0,<5.0.0)
|
|
29
|
+
Requires-Dist: types-tqdm (>=4.67.3.20260205,<5.0.0.0)
|
|
30
|
+
Requires-Dist: typing-extensions (>=4.15.0,<5.0.0)
|
|
31
|
+
Project-URL: Repository, https://github.com/SimulacraBusiness/econsimulacra
|
|
32
|
+
Description-Content-Type: text/markdown
|
|
33
|
+
|
|
34
|
+
[](https://github.com/psf/black)
|
|
35
|
+
|
|
36
|
+
<p align="center">
|
|
37
|
+
<img src="imgs/SimpleGridAnimator.gif" width="48%" />
|
|
38
|
+
<img src="imgs/SimpleSocialAnimator.gif" width="48%" />
|
|
39
|
+
</p>
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
EconSimulacra is a simulation platform for studying complex socio-economic systems with large language model (LLM) agents. The framework enables researchers and practitioners to simulate:
|
|
43
|
+
|
|
44
|
+
- household consumption
|
|
45
|
+
- firm pricing strategies
|
|
46
|
+
- narrative diffusion through social networks
|
|
47
|
+
- spatial mobility of agents
|
|
48
|
+
|
|
49
|
+
By combining agent-based modeling with LLM reasoning, EconSimulacra allows researchers to study emergent macroeconomic phenomena from micro-level behavioral rules.
|
|
50
|
+
|
|
51
|
+
# Key Features
|
|
52
|
+
|
|
53
|
+
- 🧠 LLM-driven agents with internal states and reasoning
|
|
54
|
+
- 🏙 Spatial grid environments with agent mobility
|
|
55
|
+
- 🛒 Market interactions (consumption, pricing)
|
|
56
|
+
- 🌐 Social network dynamics (follow, unfollow, narrative diffusion)
|
|
57
|
+
- 📊 Structured simulation logs for analysis
|
|
58
|
+
- ⚡ Parallel simulation execution
|
|
59
|
+
- 🧩 Modular architecture for extensibility
|
|
60
|
+
|
|
61
|
+
# Conceptual Architecture
|
|
62
|
+
|
|
63
|
+
EconSimulacra consists of the following main components:
|
|
64
|
+
|
|
65
|
+
<p align="center">
|
|
66
|
+
<img src="imgs/framework_structure.png" width="100%" />
|
|
67
|
+
</p>
|
|
68
|
+
|
|
69
|
+
## [**Simulator**](https://github.com/SimulacraBusiness/econsimulacra/blob/main/src/econsimulacra/simulator.py)
|
|
70
|
+
|
|
71
|
+
The **Simulator** executes the simulation, manages temporal progression, and supports parallel execution. At each simulation step, the simulator collects actions from all agents based on their observations and applies them to the environment. The core logic of the simulator is conceptually as follows.
|
|
72
|
+
|
|
73
|
+
```python
|
|
74
|
+
num_steps: int
|
|
75
|
+
for _ in range(num_steps):
|
|
76
|
+
all_actions_dic = {}
|
|
77
|
+
for agent_id in env.agent_ids:
|
|
78
|
+
agent = self.env.agent_id2agent[agent_id]
|
|
79
|
+
obs = self.env.get_observations(agent_id=agent_id)
|
|
80
|
+
action_dic = agent.act(obs)
|
|
81
|
+
all_actions_dic[agent_id] = action_dic
|
|
82
|
+
self.env.step(all_actions_dic)
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
In each step:
|
|
86
|
+
1. The environment provides observations to each agent ```env.get_observations(agent_id=agent_id)```
|
|
87
|
+
2. Agents decide their actions based on these observations ```action_dic = agent.act(obs)```
|
|
88
|
+
3. The environment updates the global state according to the agents' actions. ```env.step(all_actions_dic)```
|
|
89
|
+
|
|
90
|
+
The Simulator requires ```config``` to apply your simulation settings. See [examples/openai/config.json](https://github.com/SimulacraBusiness/econsimulacra/blob/main/examples/openai/config.json) for an example.
|
|
91
|
+
|
|
92
|
+
```Python
|
|
93
|
+
simulator = Simulator(
|
|
94
|
+
config=config_dic_path,
|
|
95
|
+
env_class=Environment,
|
|
96
|
+
logger=logger,
|
|
97
|
+
summarizer_class=SimulationSummarizer,
|
|
98
|
+
)
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
You can introduce your custom classes in your simulation by specifying ```"type": "MyClass"``` in the config and register them to the Simulator. [examples/openai/main.py](https://github.com/SimulacraBusiness/econsimulacra/blob/main/examples/openai/main.py) provides an example to introduce the custom event ```SubsidyEvent```.
|
|
102
|
+
|
|
103
|
+
```python
|
|
104
|
+
simulator.register_classes([MyClass1, MyClass2])
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
## [**Environment**](https://github.com/SimulacraBusiness/econsimulacra/blob/main/src/econsimulacra/envs/base.py)
|
|
109
|
+
|
|
110
|
+
The **Environment** manages the global state of the simulated world, including the internal states of all agents. It is responsible for:
|
|
111
|
+
|
|
112
|
+
- providing observations to agents (```.get_observations```)
|
|
113
|
+
- applying agents’ actions and updating the world state (```.step```)
|
|
114
|
+
|
|
115
|
+
The environment includes multiple submodules, such as:
|
|
116
|
+
|
|
117
|
+
- [**GridSpace**](https://github.com/SimulacraBusiness/econsimulacra/blob/main/src/econsimulacra/envs/space.py): A spatial environment in which agents reside and move. This allows the simulation of spatial interactions, mobility, and location-dependent behaviors.
|
|
118
|
+
- [**SocialNetwork**](https://github.com/SimulacraBusiness/econsimulacra/blob/main/src/econsimulacra/envs/social_networks/base.py): A communication layer where agents can exchange messages and interact socially, enabling the study of information diffusion and social influence. The social network also includes a customizable [**RecommenderSystem**](https://github.com/SimulacraBusiness/econsimulacra/blob/main/src/econsimulacra/envs/social_networks/recsys.py) that can suggest other agents to follow.
|
|
119
|
+
|
|
120
|
+
## [**Agent**](https://github.com/SimulacraBusiness/econsimulacra/blob/main/src/econsimulacra/agents/base.py)
|
|
121
|
+
|
|
122
|
+
An **Agent** represents an autonomous decision-maker in the simulation Agents receive structured observations from the environment and determine their actions (```.act```).
|
|
123
|
+
|
|
124
|
+
The information available to each agent, both as observations received from the environment and as information disclosed to other agents, is fully configurable via the agent configuration. For example, ```requestObs``` field defines what an agent can perceive from the environment, while ```provideInfo*``` fields define what an agent reveals to others.
|
|
125
|
+
|
|
126
|
+
EconSimulacra provides a built-in [**LLMAgent**](https://github.com/SimulacraBusiness/econsimulacra/blob/main/src/econsimulacra/agents/llm_agent.py) implementation that leverages LLMs to generate agent behaviors. To ensure reliable and stable simulations, agent actions are generated as structured outputs using [Outlines](https://pypi.org/project/outlines/0.1.11/), which enforces predefined schemas for the generated actions.
|
|
127
|
+
|
|
128
|
+
The LLM-based agent system is modular and consists of several customizable submodules:
|
|
129
|
+
|
|
130
|
+
- [**LLMClient**](https://github.com/SimulacraBusiness/econsimulacra/blob/main/src/econsimulacra/llm_services/clients/base.py) – manages the underlying language model and inference settings
|
|
131
|
+
- [**PersonaBuilder**](https://github.com/SimulacraBusiness/econsimulacra/blob/main/src/econsimulacra/llm_services/personas/base.py) – assigns role-playing personas to agents
|
|
132
|
+
- [**PromptBuilder**](https://github.com/SimulacraBusiness/econsimulacra/blob/main/src/econsimulacra/llm_services/prompts/base.py) – constructs prompts used for agent reasoning
|
|
133
|
+
- [**MemoryHandler**](https://github.com/SimulacraBusiness/econsimulacra/blob/main/src/econsimulacra/envs/memory.py) - stores and provides each agent the sequence of their experience in the past time steps as memory
|
|
134
|
+
|
|
135
|
+
By customizing these components, users can easily modify LLM configurations and experiment with different prompting strategies, personas, and model backends without changing the core simulation logic.
|
|
136
|
+
|
|
137
|
+
## [**Event**](https://github.com/SimulacraBusiness/econsimulacra/blob/main/src/econsimulacra/envss/event.py)
|
|
138
|
+
|
|
139
|
+
An **EventManager** is responsible for managing events and triggering them at appropriate times during the simulation. Events can be scheduled based on:
|
|
140
|
+
|
|
141
|
+
- specific timestamps, (```at```)
|
|
142
|
+
- time intervals or durations, (```between```)
|
|
143
|
+
- periodic execution (e.g., every k steps), (```every```)
|
|
144
|
+
- or the occurrence of specific logs (e.g., when a transaction is generated). (```with```)
|
|
145
|
+
|
|
146
|
+
You can also adapt probabilistic triggering via ```probability```. By registering custom events, users can introduce exogenous dynamics into the simulation, such as policy interventions or regime shifts, in a flexible and extensible manner.
|
|
147
|
+
|
|
148
|
+
# Basic Usage
|
|
149
|
+
|
|
150
|
+
## OpenAI API
|
|
151
|
+
|
|
152
|
+
This section explains how to run EconSimulacra using the [OpenAI API](https://openai.com/index/openai-api/), based on the example in ```examples/openai```.
|
|
153
|
+
|
|
154
|
+
### 1. Set OpenAI API Key
|
|
155
|
+
|
|
156
|
+
You need to provide your OpenAI API key in one of the following ways:
|
|
157
|
+
|
|
158
|
+
**Option A: Environment Variable (recommended)**
|
|
159
|
+
|
|
160
|
+
```bash
|
|
161
|
+
export OPENAI_API_KEY="your_api_key"
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
**Option B: ```config.json```**
|
|
165
|
+
|
|
166
|
+
```json
|
|
167
|
+
{
|
|
168
|
+
"llmClient": {
|
|
169
|
+
"type": "OpenAIClient",
|
|
170
|
+
"modelName": "gpt-4o-mini",
|
|
171
|
+
"apiKey": "your_api_key",
|
|
172
|
+
...
|
|
173
|
+
},
|
|
174
|
+
}
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
### 2. Configure Simulation
|
|
178
|
+
|
|
179
|
+
An example configuration is provided at: ```config.json```. This file defines simulation parameters (e.g., number of steps), environment settings (e.g., agents, items, space) and LLM-related services (e.g., llm client, prompt settings). You can modify this file to design your own simulation scenario.
|
|
180
|
+
|
|
181
|
+
### 3. Set Log Output Path
|
|
182
|
+
|
|
183
|
+
To save simulation logs, set the following environment variable:
|
|
184
|
+
|
|
185
|
+
```bash
|
|
186
|
+
export LOG_TXT_PATH="path/to/output_log.txt"
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
### 4. Run Simulation
|
|
190
|
+
|
|
191
|
+
Move to the example directory and execute:
|
|
192
|
+
|
|
193
|
+
```python
|
|
194
|
+
cd examples/openai
|
|
195
|
+
python main.py
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
In this script, custom ```Event``` class: ```SubsidyEvent``` is implemented. The script will 1) load ```config.json```, 2) generate simulator and register the ```SubsidyEvent``` to the simulator, 3) reset the environment and run the simulation loop, and 4) output logs to the specified path.
|
|
199
|
+
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
[](https://github.com/psf/black)
|
|
2
|
+
|
|
3
|
+
<p align="center">
|
|
4
|
+
<img src="imgs/SimpleGridAnimator.gif" width="48%" />
|
|
5
|
+
<img src="imgs/SimpleSocialAnimator.gif" width="48%" />
|
|
6
|
+
</p>
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
EconSimulacra is a simulation platform for studying complex socio-economic systems with large language model (LLM) agents. The framework enables researchers and practitioners to simulate:
|
|
10
|
+
|
|
11
|
+
- household consumption
|
|
12
|
+
- firm pricing strategies
|
|
13
|
+
- narrative diffusion through social networks
|
|
14
|
+
- spatial mobility of agents
|
|
15
|
+
|
|
16
|
+
By combining agent-based modeling with LLM reasoning, EconSimulacra allows researchers to study emergent macroeconomic phenomena from micro-level behavioral rules.
|
|
17
|
+
|
|
18
|
+
# Key Features
|
|
19
|
+
|
|
20
|
+
- 🧠 LLM-driven agents with internal states and reasoning
|
|
21
|
+
- 🏙 Spatial grid environments with agent mobility
|
|
22
|
+
- 🛒 Market interactions (consumption, pricing)
|
|
23
|
+
- 🌐 Social network dynamics (follow, unfollow, narrative diffusion)
|
|
24
|
+
- 📊 Structured simulation logs for analysis
|
|
25
|
+
- ⚡ Parallel simulation execution
|
|
26
|
+
- 🧩 Modular architecture for extensibility
|
|
27
|
+
|
|
28
|
+
# Conceptual Architecture
|
|
29
|
+
|
|
30
|
+
EconSimulacra consists of the following main components:
|
|
31
|
+
|
|
32
|
+
<p align="center">
|
|
33
|
+
<img src="imgs/framework_structure.png" width="100%" />
|
|
34
|
+
</p>
|
|
35
|
+
|
|
36
|
+
## [**Simulator**](https://github.com/SimulacraBusiness/econsimulacra/blob/main/src/econsimulacra/simulator.py)
|
|
37
|
+
|
|
38
|
+
The **Simulator** executes the simulation, manages temporal progression, and supports parallel execution. At each simulation step, the simulator collects actions from all agents based on their observations and applies them to the environment. The core logic of the simulator is conceptually as follows.
|
|
39
|
+
|
|
40
|
+
```python
|
|
41
|
+
num_steps: int
|
|
42
|
+
for _ in range(num_steps):
|
|
43
|
+
all_actions_dic = {}
|
|
44
|
+
for agent_id in env.agent_ids:
|
|
45
|
+
agent = self.env.agent_id2agent[agent_id]
|
|
46
|
+
obs = self.env.get_observations(agent_id=agent_id)
|
|
47
|
+
action_dic = agent.act(obs)
|
|
48
|
+
all_actions_dic[agent_id] = action_dic
|
|
49
|
+
self.env.step(all_actions_dic)
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
In each step:
|
|
53
|
+
1. The environment provides observations to each agent ```env.get_observations(agent_id=agent_id)```
|
|
54
|
+
2. Agents decide their actions based on these observations ```action_dic = agent.act(obs)```
|
|
55
|
+
3. The environment updates the global state according to the agents' actions. ```env.step(all_actions_dic)```
|
|
56
|
+
|
|
57
|
+
The Simulator requires ```config``` to apply your simulation settings. See [examples/openai/config.json](https://github.com/SimulacraBusiness/econsimulacra/blob/main/examples/openai/config.json) for an example.
|
|
58
|
+
|
|
59
|
+
```Python
|
|
60
|
+
simulator = Simulator(
|
|
61
|
+
config=config_dic_path,
|
|
62
|
+
env_class=Environment,
|
|
63
|
+
logger=logger,
|
|
64
|
+
summarizer_class=SimulationSummarizer,
|
|
65
|
+
)
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
You can introduce your custom classes in your simulation by specifying ```"type": "MyClass"``` in the config and register them to the Simulator. [examples/openai/main.py](https://github.com/SimulacraBusiness/econsimulacra/blob/main/examples/openai/main.py) provides an example to introduce the custom event ```SubsidyEvent```.
|
|
69
|
+
|
|
70
|
+
```python
|
|
71
|
+
simulator.register_classes([MyClass1, MyClass2])
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
## [**Environment**](https://github.com/SimulacraBusiness/econsimulacra/blob/main/src/econsimulacra/envs/base.py)
|
|
76
|
+
|
|
77
|
+
The **Environment** manages the global state of the simulated world, including the internal states of all agents. It is responsible for:
|
|
78
|
+
|
|
79
|
+
- providing observations to agents (```.get_observations```)
|
|
80
|
+
- applying agents’ actions and updating the world state (```.step```)
|
|
81
|
+
|
|
82
|
+
The environment includes multiple submodules, such as:
|
|
83
|
+
|
|
84
|
+
- [**GridSpace**](https://github.com/SimulacraBusiness/econsimulacra/blob/main/src/econsimulacra/envs/space.py): A spatial environment in which agents reside and move. This allows the simulation of spatial interactions, mobility, and location-dependent behaviors.
|
|
85
|
+
- [**SocialNetwork**](https://github.com/SimulacraBusiness/econsimulacra/blob/main/src/econsimulacra/envs/social_networks/base.py): A communication layer where agents can exchange messages and interact socially, enabling the study of information diffusion and social influence. The social network also includes a customizable [**RecommenderSystem**](https://github.com/SimulacraBusiness/econsimulacra/blob/main/src/econsimulacra/envs/social_networks/recsys.py) that can suggest other agents to follow.
|
|
86
|
+
|
|
87
|
+
## [**Agent**](https://github.com/SimulacraBusiness/econsimulacra/blob/main/src/econsimulacra/agents/base.py)
|
|
88
|
+
|
|
89
|
+
An **Agent** represents an autonomous decision-maker in the simulation Agents receive structured observations from the environment and determine their actions (```.act```).
|
|
90
|
+
|
|
91
|
+
The information available to each agent, both as observations received from the environment and as information disclosed to other agents, is fully configurable via the agent configuration. For example, ```requestObs``` field defines what an agent can perceive from the environment, while ```provideInfo*``` fields define what an agent reveals to others.
|
|
92
|
+
|
|
93
|
+
EconSimulacra provides a built-in [**LLMAgent**](https://github.com/SimulacraBusiness/econsimulacra/blob/main/src/econsimulacra/agents/llm_agent.py) implementation that leverages LLMs to generate agent behaviors. To ensure reliable and stable simulations, agent actions are generated as structured outputs using [Outlines](https://pypi.org/project/outlines/0.1.11/), which enforces predefined schemas for the generated actions.
|
|
94
|
+
|
|
95
|
+
The LLM-based agent system is modular and consists of several customizable submodules:
|
|
96
|
+
|
|
97
|
+
- [**LLMClient**](https://github.com/SimulacraBusiness/econsimulacra/blob/main/src/econsimulacra/llm_services/clients/base.py) – manages the underlying language model and inference settings
|
|
98
|
+
- [**PersonaBuilder**](https://github.com/SimulacraBusiness/econsimulacra/blob/main/src/econsimulacra/llm_services/personas/base.py) – assigns role-playing personas to agents
|
|
99
|
+
- [**PromptBuilder**](https://github.com/SimulacraBusiness/econsimulacra/blob/main/src/econsimulacra/llm_services/prompts/base.py) – constructs prompts used for agent reasoning
|
|
100
|
+
- [**MemoryHandler**](https://github.com/SimulacraBusiness/econsimulacra/blob/main/src/econsimulacra/envs/memory.py) - stores and provides each agent the sequence of their experience in the past time steps as memory
|
|
101
|
+
|
|
102
|
+
By customizing these components, users can easily modify LLM configurations and experiment with different prompting strategies, personas, and model backends without changing the core simulation logic.
|
|
103
|
+
|
|
104
|
+
## [**Event**](https://github.com/SimulacraBusiness/econsimulacra/blob/main/src/econsimulacra/envss/event.py)
|
|
105
|
+
|
|
106
|
+
An **EventManager** is responsible for managing events and triggering them at appropriate times during the simulation. Events can be scheduled based on:
|
|
107
|
+
|
|
108
|
+
- specific timestamps, (```at```)
|
|
109
|
+
- time intervals or durations, (```between```)
|
|
110
|
+
- periodic execution (e.g., every k steps), (```every```)
|
|
111
|
+
- or the occurrence of specific logs (e.g., when a transaction is generated). (```with```)
|
|
112
|
+
|
|
113
|
+
You can also adapt probabilistic triggering via ```probability```. By registering custom events, users can introduce exogenous dynamics into the simulation, such as policy interventions or regime shifts, in a flexible and extensible manner.
|
|
114
|
+
|
|
115
|
+
# Basic Usage
|
|
116
|
+
|
|
117
|
+
## OpenAI API
|
|
118
|
+
|
|
119
|
+
This section explains how to run EconSimulacra using the [OpenAI API](https://openai.com/index/openai-api/), based on the example in ```examples/openai```.
|
|
120
|
+
|
|
121
|
+
### 1. Set OpenAI API Key
|
|
122
|
+
|
|
123
|
+
You need to provide your OpenAI API key in one of the following ways:
|
|
124
|
+
|
|
125
|
+
**Option A: Environment Variable (recommended)**
|
|
126
|
+
|
|
127
|
+
```bash
|
|
128
|
+
export OPENAI_API_KEY="your_api_key"
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
**Option B: ```config.json```**
|
|
132
|
+
|
|
133
|
+
```json
|
|
134
|
+
{
|
|
135
|
+
"llmClient": {
|
|
136
|
+
"type": "OpenAIClient",
|
|
137
|
+
"modelName": "gpt-4o-mini",
|
|
138
|
+
"apiKey": "your_api_key",
|
|
139
|
+
...
|
|
140
|
+
},
|
|
141
|
+
}
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
### 2. Configure Simulation
|
|
145
|
+
|
|
146
|
+
An example configuration is provided at: ```config.json```. This file defines simulation parameters (e.g., number of steps), environment settings (e.g., agents, items, space) and LLM-related services (e.g., llm client, prompt settings). You can modify this file to design your own simulation scenario.
|
|
147
|
+
|
|
148
|
+
### 3. Set Log Output Path
|
|
149
|
+
|
|
150
|
+
To save simulation logs, set the following environment variable:
|
|
151
|
+
|
|
152
|
+
```bash
|
|
153
|
+
export LOG_TXT_PATH="path/to/output_log.txt"
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
### 4. Run Simulation
|
|
157
|
+
|
|
158
|
+
Move to the example directory and execute:
|
|
159
|
+
|
|
160
|
+
```python
|
|
161
|
+
cd examples/openai
|
|
162
|
+
python main.py
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
In this script, custom ```Event``` class: ```SubsidyEvent``` is implemented. The script will 1) load ```config.json```, 2) generate simulator and register the ```SubsidyEvent``` to the simulator, 3) reset the environment and run the simulation loop, and 4) output logs to the specified path.
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["poetry-core"]
|
|
3
|
+
build-backend = "poetry.core.masonry.api"
|
|
4
|
+
|
|
5
|
+
[tool.poetry]
|
|
6
|
+
name = "econsimulacra"
|
|
7
|
+
version = "0.1.1"
|
|
8
|
+
description = "EconSimulacra: A Digital Twin Platform of Consumer Economy Powered by LLM Agents"
|
|
9
|
+
authors = ["Ryuji Hashimoto <r_hashimoto@simulacra.co.jp>"]
|
|
10
|
+
license = "MIT"
|
|
11
|
+
readme = "README.md"
|
|
12
|
+
packages = [{ include = "econsimulacra", from = "src" }]
|
|
13
|
+
include = ["src/econsimulacra/py.typed"]
|
|
14
|
+
repository = "https://github.com/SimulacraBusiness/econsimulacra"
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Development Status :: 3 - Alpha",
|
|
17
|
+
"Intended Audience :: Science/Research",
|
|
18
|
+
"License :: OSI Approved :: MIT License",
|
|
19
|
+
"Programming Language :: Python :: 3",
|
|
20
|
+
"Programming Language :: Python :: 3.10",
|
|
21
|
+
"Programming Language :: Python :: 3.11",
|
|
22
|
+
"Programming Language :: Python :: 3.12",
|
|
23
|
+
"Typing :: Typed",
|
|
24
|
+
]
|
|
25
|
+
|
|
26
|
+
# ---------------------------------------------------------
|
|
27
|
+
# Core dependencies (always installed)
|
|
28
|
+
# ---------------------------------------------------------
|
|
29
|
+
[tool.poetry.dependencies]
|
|
30
|
+
python = ">=3.10,<3.13"
|
|
31
|
+
typing-extensions = "^4.15.0"
|
|
32
|
+
torch = "2.2.1"
|
|
33
|
+
numpy = "^1.26.1"
|
|
34
|
+
openai = "^2.21.0"
|
|
35
|
+
outlines = "0.1.11"
|
|
36
|
+
rich = "^14.2.0"
|
|
37
|
+
accelerate = "^1.12.0"
|
|
38
|
+
datasets = "^4.5.0"
|
|
39
|
+
transformers = "^4.49.0"
|
|
40
|
+
statsmodels = "^0.14.6"
|
|
41
|
+
sentencepiece = "^0.2.1"
|
|
42
|
+
protobuf = "^7.34.0"
|
|
43
|
+
types-tqdm = "^4.67.3.20260205"
|
|
44
|
+
|
|
45
|
+
# ---------------------------------------------------------
|
|
46
|
+
# Development tools
|
|
47
|
+
# Install with:
|
|
48
|
+
# poetry install --with dev
|
|
49
|
+
# Used in CI (ruff, mypy, pytest)
|
|
50
|
+
# ---------------------------------------------------------
|
|
51
|
+
[tool.poetry.group.dev.dependencies]
|
|
52
|
+
pytest = "^9.0.2"
|
|
53
|
+
pytest-cov = "^7.0.0"
|
|
54
|
+
mypy = "^1.19.1"
|
|
55
|
+
ruff = "^0.14.13"
|
|
56
|
+
|
|
57
|
+
# ---------------------------------------------------------
|
|
58
|
+
# Mypy configuration
|
|
59
|
+
# Animations layer is excluded because manim lacks full type hints
|
|
60
|
+
# ---------------------------------------------------------
|
|
61
|
+
[tool.mypy]
|
|
62
|
+
python_version = "3.11"
|
|
63
|
+
exclude = ["src/econsimulacra/animations/"]
|
|
64
|
+
|
|
65
|
+
[[tool.mypy.overrides]]
|
|
66
|
+
module = ["manim", "manim.*"]
|
|
67
|
+
ignore_missing_imports = true
|
|
68
|
+
|
|
69
|
+
[[tool.mypy.overrides]]
|
|
70
|
+
module = ["econsimulacra.animations", "econsimulacra.animations.*"]
|
|
71
|
+
ignore_missing_imports = true
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
from econsimulacra import agents as agents
|
|
2
|
+
from econsimulacra import envs as envs
|
|
3
|
+
from econsimulacra import items as items
|
|
4
|
+
from econsimulacra import logs as logs
|
|
5
|
+
from econsimulacra.simulator import Simulator as Simulator
|
|
6
|
+
from econsimulacra.simulator import SimulationSummarizer as SimulationSummarizer
|
|
7
|
+
from econsimulacra.sim_utils import find_class as find_class
|
|
8
|
+
from econsimulacra.sim_utils import JsonRandom as JsonRandom
|
|
9
|
+
|
|
10
|
+
__all__ = [
|
|
11
|
+
"agents",
|
|
12
|
+
"envs",
|
|
13
|
+
"items",
|
|
14
|
+
"logs",
|
|
15
|
+
"Simulator",
|
|
16
|
+
"find_class",
|
|
17
|
+
"JsonRandom",
|
|
18
|
+
"SimulationSummarizer",
|
|
19
|
+
]
|