# Simulating the Cost of Cooperation: A Recipe for Collaborative Problem-Solving
## Appendix A  
Algorithm A1 presents the pseudocode for a single round. The procedure takes 6 inputs: \(N\) is the number of players, \(r\) is the complexity factor, \(s\) is the group size, \(P\) is a vector with the probability of cooperation for each player \(i\), \(C\) and \(F\) are vectors with the cardinality of the groups and the fitness of each group \(j\), respectively. First, we initialize a support vector \(groupList\) in which we store the index of the groups that solved the task (Line 2). Each player \(i\) faces the task cooperating within the group (Lines 4-15) or as an individualist (Lines 16-20). In the former case, the player pays the cost of cooperation (Line 5) and tries to solve the task (Lines 6-15). If the task is solved, the fitness \(F\) increments and the group is accounted for a reward. The group receives only one reward, independently of how many players of the group solved the task (Lines 12-14). In the latter case, the individualist player has a random expected gain which is a positive integer (Line 17), and the greater the expected gain is, the lower the chance of solving the task (Line 18) and receiving the gain (Line 19) is. ```
1:procedurePlayRound(\(N,r,s,P,C,F\))
2:\(groupList\gets empty\)\(\triangleright\) Groups which solved the task
3:for\(i=1\) to \(N\)do\(\triangleright\) The players try to solve the task
4:if\(p_{i}>rand\)then\(\triangleright\) The player cooperates
5:\(G_{i}=C_{j}+1\)\(\triangleright\) Expected gain
6:\(\pi_{i}\leftarrow\pi_{i}-G_{i}*\delta_{c}/s\)\(\triangleright\) Pays the Cost of cooperation
7:if\(r>rand\)then\(\triangleright\) The player solves the task
8:for\(k\) in \(N\)do
9:if\(groupOf(k)==groupOf(i)\)then\(\triangleright\) Gain for players in same group
10:\(\pi_{k}\leftarrow\pi_{k}+G_{i}/s\)
11:endif
12:endfor
13:\(\pi_{i}\leftarrow\pi_{i}+G_{i}/s\)\(\triangleright\) Player's gain for solving the task
14:if\(groupList.notContains(groupOf(i)\)then
15:\(groupList.add(groupOf(i)\)
16:endif
17:endif
18:else
19:\(G_{i}\gets randInteger([1\;10])\)\(\triangleright\) Expected gain
20:if\(r^{G_{i}}>rand(0,1))\)then\(\triangleright\) The player solves the task
21:\(\pi_{i}\leftarrow\pi_{i}+G_{i}\)
22:endif
23:endif
24:endif
25:return\(groupList\), \(C\)
26:endprocedure
```  
**Algorithm A1** Game Round Algorithm. Algorithm A2 presents the pseudocode for the simulation. The procedure takes 7 inputs: \(R\) is the vector with the complexity factors, \(S\) is the vector with the group sizes, \(Epochs\) and \(Rounds\) are the number of epochs and rounds, respectively, \(N\) is the number of players, \(f\) is the genetic evolution coefficient, and \(iteration\) is the number of repetitions in each experiment. The first lines, from 2 to 5, define the cycles to repeat the experiment for all conditions. We repeat the experiment for \(iteration\)times for statistical evidence (Line 2), with the different complexity factors in vector \(R\) (Line 4) and with groups of different size in vector \(S\). These cycles are independent of each other, so we use a parallel for (ParFor) to speed up the process. With a fixed complexity factor \(r\) and group size \(s\), the population of players is initialized (Lines 6-7): each player \(i\) has a collaborative strategy, which is the probability to collaborate \(CS(i)\) and belongs to a group identified by an index stored in \(groupOf(i)\). The experiment runs for a certain number of epochs (Line 9). At the start of each epoch \(e\), the cardinality of the group \(C_{j}\) and the fitness \(F\) are reset (Lines 10-11). The players then try to solve a task for a certain number of rounds (Lines 12-16). At the end of the \(e\) epoch, the population evolves keeping the best players and replacing the worst (Lines 18-22). More precisely, each player whose fitness is below the average fitness of the population has a chance of changing his \(p_{i}\). The change is regulated by the genetic evolution coefficient \(f=0.2\), which means we remove about 20% of the population below the average fitness.