Reciprocity evolves more readily in competitive than cooperative socio-ecologies
Data files
Jun 02, 2025 version files 620.25 MB
-
README.md
10.73 KB
-
reciprocity_under_conflict.zip
620.23 MB
Abstract
Tracking what others did and matching other’s expected actions is seen across a range of biological systems. As reciprocal matching rewards and reinforces cooperators and punishes and discourages non-cooperators, reciprocal matching can help communal living. The strength of reciprocity as a social strategy also comes from its success in protecting the individual against the risk of exploitation by punishing defectors. Although often overlooked, this feature carries a strong weight when exploitation risk is high. Here, we use evolutionary agent-based simulations to examine how reciprocal matching evolves across competitive socio-ecologies with a high risk of exploitation and cooperative socio-ecologies with a lower risk of exploitation. Results show that reciprocal matching as a social strategy for communal living evolves more readily in more competitive socio-ecologies where risk of exploitation is high. Results also hold in standard prisoner’s dilemmas with its equilibrium in single strategies (i.e., unconditional non-cooperation), for specific forms of reciprocity (i.e., tit-for-tat), and likelihood of repeated interactions. Because reciprocal matching requires some capacity for social perception and memory, these findings suggest that such capacities for social cognition likewise serve to protect against exploitation and evolved in more competitive socio-ecologies as well.
This repository contains all materials necessary to replicate the study:
Reciprocity as a social strategy evolves more readily under conflict rather than cooperation
by A. Romano, A. S. Saral, and C. K. W. De Dreu
The study is an agent-based simulation. The data contained in this repository is the output of the simulation. It also contains files to run the simulations locally or on a cloud.
We used Python for the simulations and R for the analysis/plots.
Description of the data and file structure
Code
code/simulation/– Simulation code (Python3). This folder contains the code for the agent-based simulations. The main files,simulate.pyandsimulate_tft.pyfiles execute the two simulations we used in the paper. See the content of these two files for the parameter.code/simulation/attackerdefenderandcode/simulation/attackerdefendertftfolders contain the definitions of the components of the agent based model. The code follows object-oriented structure and each class defines a component in the interaction.agent.pydefines the agent at the individual levelgame.pydefines the games the agents play.group.pydefines the groups of interaction.population.pydefines the population.field.pydefines the field that the groups/populations interact.simulations.pydefines the simulations as a whole.__init__.pyis a necessary (in our case, empty) file for the Python package structure. It allows the folder to be treated as a package/module in Python.
There are other helper files such as core.py, setup.py, terminalcolors.py which are either some helpers or necessary files for the python package structure. requirements.txt contains the list of required Python packages to run the simulation code.
code/analysis/– R scripts for data analysis and plotting.0_install_required_packages.Ris a helper for installing required R packages01_combine_csv_to_parquet.Rand its twin for TfT variant02_combine_csv_to_parquet.Rcombines simulation output data files to a single parquet file in thedata/processedfolder.03_reshape_data.Rand its twin for TfT variant04_reshape_data_tft.Rprepares the data for the analysis and outupts the long data indata/processedfolderdefinitions.Ris a helper file for the R codes, which defines the components of the data (such as agent types, popluation names, and so on) in the data and links them with labels, plot colors, facet orders and so on.paper_plots...andsi_plots...files generate the plots in the paper and the supplementary information.paper_plots.Rgenerates Figure 5 and Figure 7 in the paper.paper_plots_tft.Rgenerates Figure 6 in the paper.si_plots.Rgenerates the Figure S7 in the supplementary information.si_plots_tft.Rgenerates the Figure S18 and Figure S19 in the supplementary information.si_plots_optional_robustness.Rgenerates Figure S12-S17 in the supplementary information.
Data
data/processed– Simulation data. This folder contains the complete simulation data in open-source apache parquet format. (To read the data in parquet format, you can usearrowpackage in R,pandasin package in Python, andParquet.jlin Julia.)df_long.parquet- The main data file for the simulation. It contains the long format of the simulation data, which is used to generate the plots in the paper and supplementary information.df_long_tft.parquet- The data file for the simulations with Tit-for-Tat (TfT) strategy. It contains the long format of the simulation data for the TfT variant, which is used to generate the plots in the paper and supplementary information.
These files will be overwritten if you run simulations locally and process your data files.
data/simulation- Empty directory reserved for the main simulation raw data. A number of data files (csv) will appear after simulation code is executed. This folder should be preserved.data/simulation_tftEmpty directory reserved for the tit-for-tat variant of the simulation raw data. A number of data files (csv) will appear after simulation code is executed. This folder should be preserved.
Codebook for the data (df_long.parquet/ df_long_tft.parquet)
population: population (attacker (Hobbesian), or defender (Rousseauian))generation: generation number of agentstotalsize: total number of agents in the populations(s)size: number in the current popluationnum_generations: total number of generationsprob_rep: repetition probabilitymoran_r: replacement rate/death ratebase_fit: base fitnesspopulation_str: Structure of the simulation/gameonlydefender: Only Hobbesian populationattackermin: Hobbesian majority populationequal: Equal proportionattackermaj: Rousseauian majority populatioenonlyattacker: Only Rousseauianprisoner: Prisoner's dilemma
type:strategies of agents:cooperatordefectormatchmismatchprob05(Random)payoff_std: standard deviation of the total payoff of the typepayoff_total: average total payoff of the type in generationpayoff_total_min: minimum toatl payoff of the typepayoff_total_max: maximum total payoff of the typestd: standard deviation of number of agentsmean: mean number of agents per typemin: minimum number of agentsmax: maximum number of agentspayoff: average payoff of the type in generation
Plots
plots/– Directory reserved for plots. We have placed the output filfes for reference. Plots will appear/be overwritten in here after code in the analysis folder is executed (and the current plots will be overwritten). This folder should be preserved.
In the root directory the following wrappers facititate reproduction steps:
generate_all_plots.Rrun_simulations.Rprepare_simulation_data.R
Very short version of reproduction steps;
To regenerate plots in the paper using our data once the requirements are installed run:
Rscript generate_all_plots.R
To run the simulations locally on your on machine (see parameters in simulate.py and simulate_tft.py)
python3 run_simulations.py
and then:
Rscript prepare_simulation_data.files.R
After that you can generate plots for your local simulations like the step 1.
How to Reproduce the Study
Option 1 - Use Precomputed Data
You can reproduce the plots in the paper by using our precomputed data files. If you have downloaded this repository from Dryad, those files are already in the data/processed/ folder.
Instructions:
Extract the contents of this repository
You can download the zip and extract the contents.
Install required R packages
We use a number of R packages (list below). You can install those packages by running 0_install_required_packages.R.
You can run it on the terminal. (Make sure you are on the project directory)
Rscript code/analysis/0_install_required_packages.R
Run the analysis files to regenerate the plots
You can run these files using your R environment or using terminal (order does not matter):
Rscript code/analysis/paper_plots.R
Rscript code/analysis/paper_plots_tft.R
Rscript code/analysis/si_plots.R
Rscript code/analysis/si_plots_tft.R
Rscript code/analysis/si_plots_optional_robustness.R
You will then find the plots in the plots folder in PNG (and some also EPS) format
Option 2 - Run the Simulation Locally
To reproduce the results, run the Python simulation code. Note that running the full simulation with all parameters (as in the paper) may take several days on a standard 8-core machine. The default settings in this repository use reduced parameters for faster testing.
Steps:
Make sure you are in the project root folder reciprocity_under_conflict
(Optional but recommended) Set up a virtual environment
python3 -m venv venv
source venv/bin/activate # On Linux/Mac
# or
venv\Scripts\activate # On Windows
Install Python required packages
pip install -r code/simulation/requirements.txt
Run the main simulation (uses reduced parameters by default)
python3 code/simulation/simulate.py
Run the tit-for-tat simulation
python3 code/simulation/simulate_tft.py
Note:
To match the paper’s full parameters (1000 generations and 1000 iterations), edit lines 24–25 in simulate.py and simulate_tft.py and uncomment lines 28-32 for the full parameter range. Be aware that simulations after this can take 80–240 hours on a typical computer.
Process files
Run processing files:
- 01_combine_csv_to_parquet.R
- 02_combine_csv_to_parquet_tft.R
- 03_reshape_data.R
- 04_reshape_data_tft.R
Note: Processed files will replace the data files in the repository. Too keep the original data files, you can rename the data/processed as something like data/processed_paper and create an empty data/processed folder.
Run the analysis files to regenerate the plots
You can run these files using your R environment or using terminal (order does not matter):
Rscript code/analysis/paper_plots.R
Rscript code/analysis/paper_plots_tft.R
Rscript code/analysis/si_plots.R
Rscript code/analysis/si_plots_tft.R
Plots (robustness checks) requires a number of parameters to be present in the data. To reduce running time, by default, these combinations are not included in the default simulation files. For that reason, make sure you included parameter combinations in your simulations. Or you can simply skip this file.
Rscript code/analysis/si_plots_optional_robustness.R
You will then find the plots in the plots folder in PNG (and some also EPS) format
System Requirements
- Hardware:
- Modern PC/server (Windows, MacOS, or Linux).
- At least 4GB RAM (8GB+ recommended).
- Full simulation requires significant CPU time (~75 hours on a multi-core HPC cluster).
- Software:
- Python 3 (tested on 3.12.2).
- R (tested on 4.4.2).
- Python dependencies:
- numpy 2.2.6
- pandas 2.2.3
- joblib 1.5.1
- R dependencies:
- dplyr 1.1.4
- tidyr 1.3.1
- purrr 1.0.2
- forcats 1.0.0
- stringr 1.5.1
Sharing/Access Information
The code is available at the GitHub repository aseyq/reciprocity_under_conflict
