# Parallel Agent-Based Framework for Analyzing Urban Agricultural Supply Chains
## 4 Agent-Based Framework for Analyzing Urban Agricultural Supply Chains
That is, there are different open-air fairs located in different places, and each one opens on a different weekday. The simulation inputs include Json files and databases. The Json files contain spatial data with information about the locations and the schedule of open-air fairs. It also includes information about the size and location of the agricultural land, along with its boundaries. The database includes information such as the probability of drought, heatwaves, and precipitation, as well as information on products and prices. Once the simulation begins, we set the initial values for all the variables and the agents:  
* Variable initialization: The variables are loaded into the Environment object. This includes climatic variables (probabilities of heat waves, frost, precipitation levels, prices) as well as information related to the products, lands, and fairs. * Agent initialization: We create the agents--farmers, consumers, and fairground workers. We link the farmers with their corresponding lands. We also link the fairground workers and the consumers with their corresponding open-air fairs. Figure 3 shows the main classes of our simulator. The Simulation class initializes the Environment class, reads parameters and configurations from Json files. It also manages the execution of events and routes them to the corresponding agents. The Environment class manages the instances of the entities (fairs, products, and land) and agents (consumers, fairground workers, and farmers). In addition, it manages the life cycle of the fairs and is responsible for making information available to the entities and agents during the simulation. The Agent class is an abstract class that is the basis for all the types of implemented agents. The Farmer, Fairground Worker (FW), and Consumer classes--rectangles  
Figure 2: Fruit and vegetable marketing chain. with dotted lines--are abstract classes that define the general life cycle of each agent. For each type of agent, we provide different implementations that can be used for simulation. The Simple Farmer class chooses a random product to grow from those available each month. The Farmer Finance class uses the cost model (expected profit and cost per square meter multiplied by the land size) to determine the product that gives the highest profit when choosing a crop. The Farmer Risk class follows the risk model described in the following section. The Static Fairground Worker (FW) class randomly chooses the number of products (defined in the configuration file) and iterates over the farmers to decide who to buy from. The Seasonal FW class randomly chooses the products to market based on their availability for sale, depending on the month of the year. The Seasonal FW class additionally uses the probability of consumption to select the products to sell. The Proportional FW class estimates the probability that a purchase will occur between the FW and a farmer for a given product, based on the average sale between them and the total volume traded during the previous year. The Simple Consumer class chooses the products and the number of products to buy at random. Although the Family Consumer class chooses the products  
Figure 3: Diagram of the main classes of our simulator. at random, it buys a proportional amount based on the number of inhabitants in their home. The number of inhabitants per home for each customer is randomly chosen when the agent is created, based on census distributions. Finally, the Consumer Family Budget class limits the number of products that can be purchased according to the family's budget. The Factories classes are responsible for initializing the correct type of agent. This allows for changing the behavior of the agents from the configuration file. The Product class is used to represent the simulated products. Each product has an ID, months of sowing and consumption, probability of consumption, and purchase volumes (for FW and consumer).