## 3 Solution Algorithms
### Iterated Local Search  
To automate the search for optimal sizing decisions, the two-phase approach is implemented using an iterated local search (ILS). ILS is a metaheuristic based on constructing a sequence of locally optimal solutions by perturbing the current local minimum and applying local search after starting from the modified solution. In essence, given a current solution s*, the process begins by applying a change or perturbation that leads to an intermediate state s' (which belongs to the solution set S). Next, LocalSearch is applied to s' resulting in a solution s* in S*. If s* passes an acceptance test, it becomes the next element of the walk S*; otherwise, it reverts to s*. This resulting walk represents a case of stochastic search in S*. The search for optimal solutions occurs within a defined reduced space, with improvements sought at each iteration, and the process consists of an initial solution, perturbation, acceptance criteria, local search, and global optimization within ILS [41]. The first phase of the proposed approach guides the search, while the second phase evaluates the quality of solutions. The pseudo code (Algorithm 2) outlines the procedure of the ILS-optimization algorithm. Initially, an initial feasible solution (_S_init_) is provided, preferably with diesel generators. This solution temporarily serves as the current solution (_S_current_) and the best solution (_S_best_). Following this, a set number of iterations, \(n\), are performed, with one of two actions being taken in each. A generator is either removed or added, depending on whether the solution is feasible or not, respectively. When adding a generator, validation is performed to ensure there is space remaining for additional generators. Otherwise, the search reverts to the last feasible solution. Figure 3: Two-phase approach for the microgrid sizing problem. The object to remove from the current solution can be identified based on those that provide the least demand at the highest price. However, to ensure variability in the iterative search and prevent solution deadlocks, it is selected randomly. The variable _remove_report_ is calculated, serving as an auxiliary dataset providing insight into how much demand was satisfied by a removed generator or how much will be supplied by an added one. ```
\(G,B,U,T\) \(S\_init\gets initialSolution()\) \(S\_current\gets S\_init\) \(S\_best\gets S\_init\) whileStopping criterion is not met do if\(S\_current\) is Feasiblethen \(S\_feasible\gets S\_current\) \(S\_current\gets removeobject(S\_current)\) else if\(S\_available\
eq\emptyset\)then \(S\_current\gets addobject(S\_current)\) else \(S\_current\gets S\_feasible\) Break endif endif  evaluate(\(S\_current\)) if\(S\_current\) is Feasible & \(LCOE(S\_current)\leq LCOE(S\_best)\)then \(\overline{S}\_best\gets S\_current\) endif endwhile ReturnS\_best
```  
**Algorithm 2** Strategical Iterated local Search  
Similarly, the add function (Algorithm 3) randomly selects whether to add a generator or a battery, allowing for variety in the solution. If it chooses to add a battery, it randomly selects one from the set of available batteries and adds it to \(S\). If a generator is chosen, the function determines the period with the greatest demand supplied by the _remove_report_ (_max_generation_). It then examines the list of available generators of a randomly selected technology to find one with the best-rated power at the lowest cost based on the previous report, creating a list for this criteria. Additionally, it identifies the generator with the lowest variable operating cost at that specific point, creating a separate list for this criterion. The function then randomly selects one of four lists: _\"capacity\"_, _\"LCOE\"_, _\"technology\"_, _\"cost_vopm\"_ and chooses a generator from the selected list. Finally, it returns the updated \(S\) with the newly added object and updates the _remove_report_.