# Parallel Agent-Based Framework for Analyzing Urban Agricultural Supply Chains
## 5 Parallel Simulation
#### Load Balance Algorithm  
The parallelization of the simulator developed for the case study incurs load balance and communication overheads. The sequential simulator accesses data structures located in the RAM memory to process the events related to consulting the farmers' product stock. That is, the FW iteratively asks the farmers whether they have the products. However, in the case of the parallel simulator, the LPs are distributed among different processors, and consulting the available stock with the farmers may require several iterations, during which messages and synchronizations are required. This undoubtedly generates additional overhead that increases the execution time and causes load imbalance among the processors. To address this problem, we propose using the simulated annealing (SA) [8] metaheuristic combined with our proposed credit algorithm. We use the SA metaheuristic to distribute the LPs among the processors. Simulated annealing (SA) is an optimization metaheuristic inspired by the annealing process in metallurgy. It explores solutions by probabilistically accepting worse solutions to escape local optima, gradually reducing this probability as the temperature decreases. The algorithm iterates until a minimum temperature is reached. We use the temporal balance objective function, which distributes the workload over multiple time periods, ensuring that all processors maintain a balanced usage at all times. To achieve this, the coefficient of variation (\(CV=\frac{\varphi}{\varphi}\)) is calculated in each period. Where \(\sigma\) is the standard deviation and \(\mu\) is the mean load across the partitions. If, in a given period, the mean load drops to zero (i.e., all partitions become inactive), a penalty term is introduced to further discourage the absence of work. The final objective is based on a linear combination of the balance level (measured using the CV in each period), the total workload, and penalties for inactivity. Additionally, during the execution of the simulations, the credit algorithm works as follows. When an FW initiates a purchase with a farmer, they take a credit assuming that the purchase request will be answered favorably. In this way, transactions are not held up by waiting for the communication response from other LPs. If the response is favorable, the \"credit\" is cancelled. Otherwise, the FW has a debt on the product, so they cannot restart purchasing for the same product until the debt is canceled. The credit algorithm uses a log of historical transactions between the FWs and the farmers, recording when the purchase succeeded or when a credit was granted. This information feeds a Bayesian model to estimate the response of agents residing on other processors. Let \(S\) and \(F\) be the total number of successes and failures of the credits, respectively. That is, the number of products requested by a given farmer is successfully responded to or not. The prior probability of success is defined using the Laplace formulae:  
\[P(s)=\frac{S+\alpha}{S+F+2\alpha},\quad P(f)=1-P(s)=\frac{F+\alpha}{S+F+2 \alpha}, \tag{4}\]  
where \(\alpha\) is a parameter used to adjust observed data counts, effectively addressing the issue of zero-frequency values in categorical data. Setting \(\alpha=1\) corresponds to [PERSON]'s rule of succession, which assumes a uniform prior distribution over all possible events or categories, thereby ensuring that every outcome has a non-zero probability. For each product \(i\), \(S_{i}\) successes and \(F_{i}\) failures are counted, and for each process \(j\), \(s_{j}\) successes and \(f_{j}\) failures are counted. \(\alpha\) is the smoothing parameter, usually set to \(1\).