## 2 Materials and Methods
### Cost Optimization Algorithm  
To solve the optimization problem indicated in Equation (17), the following procedure is used. Due to the fact that it is an SAPV system, the useful usable energy will be taken into account (usable energy = total generated energy \(-\) ENU). To calculate the optimal size of the SAPV installation that minimizes investment and operation costs for a minimum established reliability objective, an evolutionary optimization algorithm based on PSO has been implemented. This algorithm starts from a swarm or society of particles initially uniformly distributed in the space of possible solutions for battery storage capacity and installed PV power, as seen in Figure 6. The particles meet the following requirements:  
* The dimensions of the space are known, as well as the value of the objective function for each particle, although its maximum/minimum is not known. * Particles can move freely through space. * Particles have memory, remembering the position of their own optimum. * There is communication between particles, knowing all the positions and values of the global optimum. For this initial distribution, the value of the objective function of each particle is calculated. This value becomes its own optimum and the global optimum is established among all of them. In the successive iterations of the algorithm, the particles will begin to move according to Equation (11). \[\overrightarrow{\mathrm{x_{i}}}(t+1)=\overrightarrow{\mathrm{x_{i}}}(t)+ \overrightarrow{\mathrm{v_{i}}}(t+1) \tag{18}\]  
where:  
\(\mathrm{x_{i}}(t)\) is the position of particle i at time t.  
\(\mathrm{v_{i}}(t+1)\) is the speed of particle i at time t + 1. Both position and velocity are treated as dimensionless parameters, since to be dimensionally correct, the velocity at one instant should be multiplied by the time at the next instant. The time between instants is considered to be one iteration, leaving the parameter multiplied by one, so it is a displacement to the next point. Thus, the behavior of the particle is defined with a simple kinematics equation where the new position is the current position plus a velocity vector (Equation (12)). \[\overrightarrow{\mathrm{v_{i}}}(t+1)=\mathrm{w_{damp}}^{t}*w*\overrightarrow {\mathrm{v_{i}}}(t)+\overrightarrow{\mathrm{r_{1}}}*\mathrm{c_{1}}\left( \overrightarrow{\mathrm{p_{i}}}(t)-\overrightarrow{\mathrm{x_{i}}}(t) \right)+\overrightarrow{\mathrm{r_{2}}}*\mathrm{c_{2}}\left(\overrightarrow {\mathrm{g_{i}}}(t)-\overrightarrow{\mathrm{x_{i}}}(t)\right) \tag{19}\]  
where:  
\(\mathrm{w_{damp}}^{t}\) is the damping coefficient raised to iteration t.  
\(\mathrm{w}\) is the coefficient of inertia of the particle. \(\mathrm{r_{1}}\) and \(\mathrm{r_{2}}\) are two normalized positive random vectors. \(\mathrm{c_{1}}\) and \(\mathrm{c_{2}}\) are the cognitive and social coefficients, respectively, of the particle. \(\mathrm{p_{i}}\) is the position of the particular optimum of the particle. Figure 6: Initial position of the particles in the state space of the PSO algorithm. \(\mathrm{g_{i}}\) is the position of the global optimum of the swarm. Three terms can be distinguished in the equation. The first of them is the inertial term (\(\mathrm{w_{damp}^{\ast}\ast w\ast\overset{\rightharpoonup}{\psi_{i}}(t)}\)), which considers the movement that the particle previously had and contains a damping coefficient that decreases it with the passing of the iterations in order to prevent the particle from oscillating as it converges to the final solution. The second term (\(\overset{\rightharpoonup}{r_{1}}\ast\mathrm{c_{1}}\left(\overset{\rightharpoonup}{ \mathrm{P_{i}}}(t)-\overset{\rightharpoonup}{\mathrm{x_{i}}}(t)\right)\)) is cognitive. It considers the distance from the particle's position to its own historical optimum, updating itself if it finds a new one.