# Optimal Integrated Single-Framework Algorithm for the Multi-Level School Bus Network Problem
## 4 Algorithm
origins = _set of available origins_, selected_origins set for school s = _Null_  
_Step 6.1:_**IF s \(<=\) S, **THEN**  
go to **step 6.2**, otherwise go to **step 7**  
**END IF**  
_Step 6.2:_**IF a \(<=\) desired_agents, **THEN**  
go to **step 6.3**, otherwise go to **step 6.1**  
**END IF**  
_Step 6.3:_**Find** origin o, from remained_origins set which is closest to the agent a (student) home. _Step 6.4:_**Remove** o from remained_origins set and go **step 6.5**  
_Step 6.5:_**Add** o to selected_origins set for school s  
_Step 6.6:_ set a \(=\) a\(+\)1 and go to **step 6.2**  
_Step 6.7:_ set s \(=\) s\(+\)1 and go to **step 6.1**  
**Step 7:**End**  
The second sub-algorithm defines the optimal routing of school buses. At first, this sub-algorithm creates a random series of integers for the initial solution. The algorithm allocates school buses to students depending on the location of greater integers in the generated permutation, and then, based on the order of integers in the generated permutation, determines the routes of vehicles. For instance, in the presence of two vehicles and 10 students, a permutation of integers from 1 to 11 is produced. Suppose that the generated permutation is as follows: Path = [10, 1, 3, 4, 8, 11, 2, 9, 5, 7, and 6], then the route of the first bus would be generated by serving passengers 10, 1, 3, 4, and 8, respectively, and the second vehicle route is made by serving passengers 2, 9, 5, 7, and 6, respectively. After generating an initial feasible solution (\(x^{initial}\)), the SA algorithm tries to create a neighboring solution by perturbing the initial solution according to the cost of the neighboring solution (the value of a hypothesized objective function, which includes penalties for modeling constraints). If this cost (\(x^{new}\)) is less than that of the initial solution, then this new cost will be accepted; otherwise, it will be accepted with probability \(P=e^{-\frac{\Delta f}{T}}\), where \(\Delta f\) is the change in cost between the new solution and the previous solution and \(T\) is the initial temperature. The SA algorithm decreases the temperature during the iteration process of the optimal solution with a controlled ratio of (\(\alpha=0.98\)). In each iteration, the algorithm tries to improve the solution by searching its neighborhoods. For this purpose, the SA algorithm uses common swap, insertion, and reversion methods randomly. The algorithm attempts to reduce the value of the  
two transformation methods, move and replace, to explore the solution space. The move transformation explores groups of passengers that have the shortest distances closest to each other, including their destinations. Then, it selects a random route and allocates the random passengers on the routes according to the constraints. The replace highest average transformation is based on the average distance of every group of passengers; therefore, the algorithm selects random routes and allocates selected passengers in the route in order to minimize the cost. This permutable process (cooling schedule) will be ended when the temperature reaches below 0.001 and the final solution does not change in iterations. Finally, the best feasible solution found during the total iterations is presented as the final solution proposed by the algorithm. The developed model was coded in MATLAB and run on a computer equipped with an Intel Core i7 4.0 GHz (16 GB of RAM) processor. Due to containing several binary variables, the model turns into an NP-hard problem. The pseudocode shown in Algorithm 2 describes the steps in the SA algorithm as applied to solve the proposed SBRP.