DOI: 10.5061/dryad.bcc2fqzrb
title: Reciprocity evolves more readily in competitive than cooperative socio-ecologies

description: 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.
description: # Reciprocity evolves more readily in competitive than cooperative
 socio-ecologies [Access this dataset on
 Dryad](https://datadryad.org/dataset/doi:10.5061/dryad.bcc2fqzrb) 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.py` and `simulate_tft.py` files execute the two simulations we
 used in the paper. See the content of these two files for the parameter. *
 `code/simulation/attackerdefender` and
 `code/simulation/attackerdefendertft` folders 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.py` defines the agent at the individual level * `game.py` defines
 the games the agents play. * `group.py` defines the groups of interaction.
 * `population.py` defines the population. * `field.py` defines the field
 that the groups/populations interact. * `simulations.py` defines the
 simulations as a whole. * `__init__.py` is 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.R` is a helper for installing
 required R packages * `01_combine_csv_to_parquet.R` and its twin for TfT
 variant `02_combine_csv_to_parquet.R` combines simulation output data
 files to a single parquet file in the `data/processed` folder. *
 `03_reshape_data.R` and its twin for TfT variant `04_reshape_data_tft.R`
 prepares the data for the analysis and outupts the long data in
 `data/processed` folder * `definitions.R` is 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...` and `si_plots...` files
 generate the plots in the paper and the supplementary information. *
 `paper_plots.R` generates Figure 5 and Figure 7 in the paper. *
 `paper_plots_tft.R` generates Figure 6 in the paper. * `si_plots.R`
 generates the Figure S7 in the supplementary information. *
 `si_plots_tft.R` generates the Figure S18 and Figure S19 in the
 supplementary information. * `si_plots_optional_robustness.R` generates
 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 use `arrow` package in R, `pandas` in package in
 Python, and `Parquet.jl` in 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_tft` Empty
 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 agents * `totalsize`: total number of agents in the
 populations(s) * `size`: number in the current popluation *
 `num_generations`: total number of generations * `prob_rep`: repetition
 probability * `moran_r`: replacement rate/death rate * `base_fit`: base
 fitness * `population_str`: Structure of the simulation/game *
 `onlydefender`: Only Hobbesian population * `attackermin`: Hobbesian
 majority population * `equal` : Equal proportion * `attackermaj`:
 Rousseauian majority populatioen * `onlyattacker`: Only Rousseauian *
 `prisoner`: Prisoner's dilemma * `type:` strategies of agents:
 `cooperator` `defector` `match` `mismatch` `prob05` (Random) *
 `payoff_std`: standard deviation of the total payoff of the type *
 `payoff_total`: average total payoff of the type in generation *
 `payoff_total_min`: minimum toatl payoff of the type * `payoff_total_max`:
 maximum total payoff of the type * `std`: standard deviation of number of
 agents * `mean`: mean number of agents per type * `min`: minimum number of
 agents * `max`: maximum number of agents * `payoff`: 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.R` * `run_simulations.R` * `prepare_simulation_data.R`
 --- ## Very short version of reproduction steps; To regenerate plots in
 the paper using our data once the requirements are installed run: ```bash
 Rscript generate_all_plots.R ``` To run the simulations locally on your on
 machine (see parameters in `simulate.py` and `simulate_tft.py`) ```bash
 python3 run_simulations.py ``` and then: ```bash 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 ```bash python3 -m venv venv source venv/bin/activate
 # On Linux/Mac # or venv\Scripts\activate # On Windows ``` #### Install
 Python required packages ```bash pip install -r
 code/simulation/requirements.txt ``` #### Run the main simulation (uses
 reduced parameters by default) ```bash python3 code/simulation/simulate.py
 ``` #### Run the tit-for-tat simulation ```bash 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](https://github.com/aseyq/reciprocity_under_conflict/)