Optimization of Energy Allocation Strategies in Spanish Collective Self-Consumption Photovoltaic Systems

: Collective self-consumption (CSC) systems o ﬀ er a great opportunity to increase the viability of photovoltaic installations by reducing costs and increasing pro ﬁ tability for consumers. In addition, CSC systems increase self-su ﬃ ciency (SS) and self-consumption (SC). These systems require a proper energy allocation strategy (EAS) to de ﬁ ne the energy distribution within the CSC. However, most EASs do not analyze the individual impact of the rules and mechanisms adopted. Therefore, six di ﬀ erent EASs are proposed and evaluated in terms of both collective and individual cost, SC, and SS. The results show that the EASs based on minimizing collective costs are the most bene ﬁ cial for the community, although they imply an unfair distribution of energy among users. On the other hand, the other EASs proposed stand out for reaching an equilibrium in terms of cost, SS, and SC, although the collective pro ﬁ tability is lower. The best results are achieved considering dynamic coe ﬃ cients, which are preferred over static ones.

implies the distribution of a fixed proportion of the energy to each participant for all the hours of a billing period. In 2021, the publication of Order TED/1247/2021 [22] introduced the possibility of using hourly dynamic allocation keys, optimizing the way of distributing the energy generated to each of the self-consumers who share a generation facility. Recently, through the publication of Royal Decree-Law 18/2022 [23] and Royal Decree-Law 20/2022 [24], the maximum distance for CSC has been increased from 500 m to 2000 m. This represents a great opportunity for the increase of CSC among industrial, residential, and tertiary consumers.
As can be seen, CSC installations offer a great opportunity to increase the viability of photovoltaic installations. Through this mechanism, different consumers will be able to share ownership of a photovoltaic installation and distribute the generation of electricity. In many cases, the reference situation will be characterized by the use of fixed allocation keys with a value equal to each owner's percentage of ownership. Thus, for each hour of the year, the generated energy will be allocated to each consumer proportionally to their ownership percentage. In this context, there is a need to investigate how to distribute resources satisfactorily for all participants of the CSC in order to improve the economic outcome in relation to the reference situation and, thus, optimize the profitability of the photovoltaic system.
The methods that can be found in the literature for modeling the distribution of energy and costs within the community are diverse. Many authors rely on optimization methods [25][26][27][28] and load scheduling, but simulation models [29] also have a presence in recent studies. Moreover, the dynamic allocation keys introduced under the new regulation open up a range of possibilities for a more efficient energy allocation. Several authors have conducted studies to analyze the effects on consumers of this type of allocation keys, especially in relation to static allocation [29][30][31][32][33]. The study conducted by Manso-Burgos [30] analyzes the impact of changes in Spanish regulation on self-consumption, concluding that the use of dynamic coefficients and tariffs with discriminatory pricing are very positive measures. Plaza et al. [31] introduce a blockchain-based solution to manage energy exchanges within the community using dynamic coefficients. Pedrero et al. [32] evaluate the profitability of shared SC in an industrial park, showing that dynamic allocation keys increase profitability. In addition, the use of dynamic allocation coefficients increases local electricity consumption, thereby promoting more efficient use of energy [29]. Dynamic allocation keys are also the gateway to more complex business models that encourage the development of energy communities and CSC, as evidenced in the study by Rocha et al. [33].
However, the studies mentioned above are focused on optimizing the allocation keys to obtain the maximum benefit for the CSC system as a whole, without taking into account the individual impact on the bill of each user in relation to a reference situation, where each participant acts as a hypothetical individual self-consumer, without sharing energy with the rest of the participants in the CSC system. Furthermore, these studies do not discuss the rules or mechanisms governing the sharing of energy within the CSC network taking into account specific tariffs and prices of each user. The distribution of renewable energy surpluses can be based on different criteria, such as electricity price or consumption of each participant, among others, which can affect each consumer in a different way.
This study evaluates various strategies for selecting energy allocation keys in shared ownership CSC photovoltaic systems. The study focuses on a use case that combines residential and commercial consumers with diverse consumption profiles, tariffs, and electricity prices. The first step involves calculating the reference baseline case for each consumer and the entire system, based on static allocation keys equal to each participant's ownership percentage. The next step compares the impact of different energy allocation strategies (EASs) on each consumer's bill with respect to the reference situation. The objective is to improve the reference situation for all consumers to obtain their acceptance for implementation. The evaluation includes both static and dynamic allocation keys, aiming to maximize the individual and collective benefit of all consumers. Additionally, the study assesses the impact on SS and SC for each consumer and the system as a whole.
The research's primary contribution lies in evaluating the impact of various EASs on the electricity bills of CSC system owners, compared to the reference case within the Spanish regulatory framework. The study demonstrates that global optimization strategies, which minimize the total cost for system owners, may lead to a negative economic outcome for certain consumers. Thus, alternative local optimization strategies need to be assessed, which, while not providing the best overall result, can enhance the billing impact for all consumers compared to the reference case. Although the case study focuses on a rooftop CSC system, the proposed approach is applicable to any type of photovoltaic installation.

Materials and Methods
This section describes the six different EASs employed to calculate the set of allocation coefficients used to distribute the photovoltaic energy production among the CSC users. As described below, some of them consider only the exchanges with the grid, while others also take into account internal exchanges between the members of the CSC.
The reference strategy (EAS0) defined static coefficients for each user considering their percentage of ownership over the photovoltaic facility. These static coefficients (Cn) took the same value for each hour of the year, and their sum must add up to one. The hourly allocation energy for each user (PVn,h) was calculated as: where PVh is the energy produced for the photovoltaic facility in each hour. Then, the hourly excess of deficit of energy for each user (En,h) was calculated comparing the allocation energy with their hourly demand (Ecn,h) as: Therefore, the electricity bought to the grid (Egrid n,h) was positive and corresponded with the maximum between zero and En,h, while the excess energy (Eexc n,h) was negative and was evaluated as the minimum between zero and En,h: , = min(0, , ) Thus, the total energy balance (Equation (5)) was fulfilled for each hour of the year.
The monthly energy cost for each user (Cost m,N) was calculated following the equation: where Pgrid n,h and Pexc n,h are the hourly prices of buying and selling energy to the grid for each user, respectively. If this monthly cost is lower than zero, a value of zero is considered (according to Royal Decree 244/2019 [21]). The total cost for each user (Costn) was then evaluated as the sum of each month and the total cost for the CSC (Cost) as the sum of the cost of all users: In the following strategies, EAS1 and EAS2, the set of allocation coefficients were optimized to reduce the total cost of the CSC)). A scheme for these strategies is shown in Figure 1. In both cases, the deficit and excess energy were bought or sold to the grid, respectively, without considering internal sharing between CSC members. The EAS1 strategy established that the set of coefficients was optimized every 4 months (according to Order TED/1247/2021 [22]), and fixed for each user during these 4 months. Therefore, 3 different sets of allocation keys were calculated for a year.
For each four-month period, the problem was formulated as: Restricted to: ∑ = 1, n = 1, …, N , 0, for j = 1, …, m; n = 1, …, N being Egrid n,h and Eexc n,h calculated according to Equations (1)- (5). m is the month and goes in this case from 1 to 4, and N the number of users of the CSC. The decision variables (Cn ∈ ) were the allocation keys for each user. The sequential least squares optimization method in Python was utilized for this optimization (package scipy.optimize).
On the other hand, EAS2 strategy considered variable coefficients, which took different values for each hour of the year (according to Order TED/1247/2021 [22]). The formulation of the problem was similar to the EAS1, although the decision variables (Cn,h ∈ ) and the restriction defined in Equation (10) were evaluated and applied for each hour of the year, respectively.
This second optimization was calculated monthly due to restriction (11). This formulation is equivalent to applying a series of rules for the distribution of energy. For each hour, the users were classified in descending order in function of their prices of buying and selling energy to the grid. Thus, the electricity was allocated to the user or users with the highest price. When their demand for energy was covered, the electricity was assigned to the user with the second higher price, and so on. Among users with the same price, the energy was allocated taking into account their percentage of ownership over the photovoltaic facility. If the next price was the price for selling a surplus of energy, the rest of the energy was allocated to this user or users as long as their monthly cost was still positive.
The EAS2 was calculated following these rules since the number of decision variables to solve the optimization problem was enormous (number of users × hours per day × days per month). EAS3, EAS4, and EAS5 constituted another group of strategies with similar characteristics. The scheme for these strategies is presented in Figure 2. In these cases, the same static coefficients as in EAS0 were considered to allocate the energy among users, although the excess energy was first exchanged with other members of the CSC. That is, internal exchange was considered. When the demand of the consumers was completely covered, the surplus of each user was sold to the grid.
The differences between the three mechanisms are in the surplus allocation criteria. In the first case, EAS3, equal distribution was considered. This means that if there were several consumers with surpluses, they were shared equally. And if there were several members of the CSC who demanded energy, the excess energy was distributed in the same amount to each one.
The allocation criteria in EAS4 were defined as proportional. If there were several consumers with surpluses, they were shared proportionally to the excess energy of each one; and if there were several consumers who demanded energy, it was distributed proportionally to the energy demanded.
Finally, EAS5 looked for the maximum profit. Among consumers with surpluses, the cheapest was distributed first, and between energy consumers, the most expensive energy was allocated first.
In these strategies, the exchange price (Pexc ni, nj, h) among user ni and user nj for hour h was calculated as the average between the energy purchase price from the grid of the demanded user and the energy selling price to the grid of the selling member in that hour: This exchange was carried out as long as this averaged price improved the grid price for both the purchasing consumer (ni) and the selling consumer (nj). When calculating the monthly costs for each user, the cost derived from exchanges with the grid had to be nonnegative. The cost of internal exchanges was not included in this restriction. These different strategies were evaluated and compared in terms of both individual cost for each user and total CSC cost. Table 1 shows a summary of the main characteristics of each strategy. Moreover, SC and SS were also considered in the analysis. The SC ratio was defined as the amount of energy consumed from the allocated renewable energy (Equation (13)), while the SS ratio was the part of the energy demand covered by the allocated photovoltaic energy production (Equation (14)).

Case Study
The allocation mechanisms developed and described in this paper are validated in a CSC system located in the north of Galicia, Spain. Thus, the methods can be compared, and the results can be analyzed. Based on the actual consumption data of 16 users, a hypothetical CSC system has been set up. This CSC comprises 11 residential consumers and five commercial consumers. The objective is to emulate a residential building with commercial stores on the ground floor and a generation system on the roof, which is a typical scenario in Spain but fairly complex due to the diversity of users' electricity consumption and prices.
The CSC system includes a shared 100 kW photovoltaic facility installed on the roof of the building and property of all users. It is considered that each user has made an investment in the photovoltaic system proportional to their consumption. Table 2 shows the ownership percentages of each user. This parameter is used to set the generated energy that is allocated to each user in the reference scenario. Table 2 also shows an estimate of the energy generated annually by the photovoltaic system. Electricity prices have been set based on the tariff and prices of each user. It should be noted that the tariff for residential consumers is the 2.0TD, segmented into three periods as indicated in Circular 3/2020 [34]. Therefore, the price of electricity varies depending on the time of day and the day of the week. However, commercial consumers are subject to the 3.0TD tariff, whereby electricity can be charged according to six different periods depending on the time of day, day of the week, and month of the year. Table 3 shows the electricity prices related to each period and consumer, as well as the price for surplus energy injected into the grid. The price of surplus energy has been set based on offers from different electricity provider companies. A price of 0.10 EUR/kWh was adopted for residential consumers and 0.15 EUR/kWh for commercial consumers.

Results and Discussion
The main findings of this research are presented and discussed in this section. Considering a CSC system that includes a shared photovoltaic generation facility, six different EASs are analyzed. These strategies are evaluated in terms of both collective and individual economic profitability. In addition, parameters related to the consumption and distribution of the photovoltaic energy produced within the CSC system are also studied.

Economic Profitability
Regarding the reference allocation strategy (EAS0), the implementation of the alternative strategies developed and presented in this study can lead to a reduction in the costs related to energy consumption in the CSC. The annual costs associated with each strategy and their distribution among CSC members are detailed in Figure 3. This figure shows that the scenario with higher costs for the CSC community is the reference scenario. This is because the allocation mechanism used does not involve the use of optimization tools and is based just on the distribution of energy according to the financial contributions of the users. On the contrary, the lowest cost is provided by EAS2, showing that dynamic coefficients are a very useful instrument to optimize the distribution of energy within the CSC community. The next in cost is EAS1. Both strategies are the most beneficial because the objective function of the optimization is to minimize the collective cost of the CSC community without taking into account the individual savings of the users. The individual strategies (EAS3, EAS4, and EAS5) result in very similar costs to each other but lower than the reference scenario, so their implementation also results in significant savings. The distribution of costs among consumers within the community shows that commercial users account for more than 60% of the cost due to their higher consumption. Figure 3 also reveals that the costs associated with commercial users are reduced by strategies that optimize collective costs. Therefore, in this case study, this type of strategy favors consumers with these characteristics.
Analyzing the economic savings from implementing these alternative strategies compared to the reference scenario is also interesting. This is detailed in Table 4, where the most profitable strategy for each user and for the community is highlighted in bold. Table 4. Economic savings of each strategy in comparison to the reference strategy (EAS0) for each CSC user and for the CSC as a whole. The most profitable strategy for each user and for the CSC community is highlighted in bold. The negative values indicate that the considered strategy entails a higher annual cost than EAS0.  Figure 4 graphically illustrates the annual savings for each user and clearly reveals that the most profitable strategies at the collective level do not always benefit all users. EAS1 and EAS2 result in significant savings for commercial users but at the expense of increased costs for residential users. Therefore, although they are economically the most profitable, they imply an unfair distribution of energy among users, so that not all of them benefit from the change. In contrast, EAS3, EAS4, and EAS5 provide savings to all users in the CSC community. It is also true that the cost reductions achieved by commercial users under these strategies are less significant than those achieved in the residential sector. Among the three strategies that involve an internal exchange of energy within the community, the most economically beneficial is EAS5.

Energy Distribution
The energy cost distribution presented and discussed above is closely related to the distribution of the energy produced by the photovoltaic system among the users of the CSC community. Figure 5 represents the annual distribution of energy derived from the distribution coefficients calculated under each strategy. Collective optimization strategies (EAS1 and EAS2) allocate more generated energy to commercial users. This is because these kinds of users pay a higher price for energy from the grid and obtain higher compensation for surplus energy than the residential users in this case study, and also because they have a more similar consumption profile to the photovoltaic generation profile. Therefore, allocating a greater amount of energy to this group of users means an increase in savings for the CSC community as a whole. In EAS1, more than 95% of the production is allocated to commercial users, while in EAS2 it is around 90%, and in the other strategies it is less than 80%. With a share of about 26%, EAS0 allocates the largest amount of energy to residential users.

Self-Consumption
The SC ratio can be a very interesting source of information about the use of renewable energy produced in the community. This parameter represents the amount of energy that each user consumes from the allocated renewable energy. The remainder is surplus energy that users supply to the grid in exchange for financial compensation. Figure 6 shows the share of energy consumed and injected into the grid by each user. EAS2 stands out in this analysis. The use of dynamic allocation coefficients to minimize the total cost to the community allows photovoltaic energy to be allocated to the users who demand it at that moment. This prioritizes SC and benefits users with a consumption profile similar to the generation profile. For residential users, the SC ratio under this strategy is close to 100% due to a lower energy allocation than with the other strategies. For commercial users, this value decreases but is still higher than 45%. EAS1 also provides a high SC ratio, but lower than EAS2. The reduction in SC is due to the use of static allocation coefficients, which limit the optimization potential, even when only the collective cost is evaluated. The reference strategy is the worst from an SC point of view. This is a logical consequence of the fact that the allocation mechanism does not take into account the consumption profile of the users. EAS3, EAS4, and EAS5 provide similar SC rates, lower than the collective optimization scenarios for residential users but higher than the reference strategy. Compared to collective strategies, the SC is reduced because more energy share is allocated to residential users. However, at the CSC scale, EAS3, EAS4, and EAS5 are similar to EAS2 in terms of SC since those are the best strategies for the commercial sector, where most photovoltaic energy is distributed. Figure 6. Share of energy consumed and injected into the grid by each user and the CSC as a whole, based on the energy allocation strategy used. The reference strategy SC ratio is indicated by the horizontal lines drawn as a baseline for each user.

Self-Sufficiency
The SS ratio also provides valuable information about the distribution of energy in the CSC community. This parameter represents the share of consumption that is covered by the renewable energy produced. The additional energy comes from the grid. Figure 7 details the amount and source of energy used annually to satisfy the consumption of CSC users. As a result of the reduced allocation of energy to some consumers, the SS ratio in EAS1 and EAS2 is very low. It is less than 25% for residential users with EAS1 and less than 40% with EAS2. For both strategies, SS is increased for commercial sector users due to the higher allocation of produced energy. In turn, the individual optimization strategies (EAS3, EAS4, and EAS5) provide the highest share of SS for the residential sector, due to the higher allocation of energy to this type of consumer, while achieving similar results for the commercial sector. Thus, these strategies stand out for providing a more balanced SS ratio for all CSC users. In addition, EAS2, EAS3, EAS4, and EAS5 are the best strategies in terms of SS at the community scale. Figure 7. Share of consumption that is covered by the renewable energy produced and by the grid for each user and the CSC as a whole, based on the energy allocation strategy used. The reference strategy SC ratio is indicated by the horizontal lines drawn as a baseline for each user.

Internal Energy Exchanges
The use of EAS3, EAS4, and EAS5 improves SS and SC through the internal exchange of energy between users within the CSC system. Figure 8 shows the amount of energy exchanged annually by each user. Negative amounts represent allocated energy that the user does not consume and, instead of injecting it into the grid as surplus energy, it is transferred to other users in exchange for financial compensation. Figure 8 also reveals that residential users give up a significant amount of energy to commercial users, probably because the consumption profile of commercial users is more similar to the generation profile, as well as because their consumption is higher. The effect of using different criteria for internal energy allocation is also shown in Figure 8. EAS3 benefits users with lower consumption by allocating them a greater amount of energy in this internal energy market. However, with EAS5, there is a greater flow of energy from lower-consumption users to higher-consumption users. EAS4 produces an intermediate situation since the energy distribution is proportional to energy consumption. It can be seen that under this strategy, commercial users give up more energy than under the other two strategies.
Users benefit from sharing energy with other consumers since they obtain greater benefits than with their energy supplier companies. The distribution of energy costs on the annual bill for strategies EAS3, EAS4, and EAS5 is shown in Figure 9. The cost is disaggregated between the internal exchanges and the network. Negative flows represent economic benefits. Some users have benefited from the internal exchanges, while others incur costs. However, considering that purchasing energy within the CSC network is less expensive than from the grid in all instances, it results in a reduction of the annual bill. Figure 9. Distribution of energy costs on the annual bill between the grid and the CSC internal exchanges for each user using EAS3, EAS4, and EAS5 energy allocation strategies.

Sensitivity Analysis
A sensitivity analysis was carried out to evaluate the effect of the different parameters on the optimal solution, analyzing the annual savings for each CSC user and the CSC as a whole. The different cases considered are shown in Table 5. These cases imply a variation of 20% in the prices of buying and selling electricity for all users. The results for the annual savings are shown in Figure 10 and indicate that the change in the prices does not modify the behavior of the different strategies. EAS1 and EAS2 still increase costs for some users, while EAS3, EAS4, and EAS5 provide savings for all users. Total costs are higher for case 1 and lower for case 2; however, the percentages keep the same value. Moreover, SS and SC do not vary with this modification either.

Case 3
The prices considered in this analysis are 0.19 €/kWh for buying energy from the grid and 0.1 €/kWh for selling energy to the grid. The results are shown in Figure 11 and indicate that when there is no difference between prices, the energy is distributed more evenly between users, with lower variations with respect to the reference strategy. In this case, the users that are favored by EAS1 and EAS2 are those that have a more similar consumption profile to the photovoltaic generation profile. Moreover, the total cost for EAS2, EAS3, EAS4, and EAS5 are almost equivalent, with lower differences due to monthly net cost restrictions (Equation (11)) in EAS3, EAS4, and EAS5. Finally, there is greater homogeneity for SC and SS values between users. This modification implies setting the ownership, then the static allocation coefficient of EAS0, to 0.02 for residential users and 0.156 for commercial users. The very high and low values shown in Figure 12 for some of them are due to the low value of the reference cost (EAS0). This is the case for R4, R6, R11, and even C1 and C2. The change of the static coefficient values modifies the values of the annual savings and SC and SS, although it does not modify the uneven behavior for EAS1 and EAS2.

Conclusions
This study introduced and analyzed six different EASs that govern the distribution of renewable energy production between the users of a CSC. These strategies differ in the objective and mechanisms involved. EAS1 and EAS2 are based on minimizing the collective cost of a CSC, while EAS3, EAS4, and EAS5 rely on some rules that define the internal exchange of surpluses. They were evaluated not only in terms of both collective and individual economic profitability but also considering other parameters such as the SC and SS ratios.
The results show that the EASs based on minimizing the collective cost of the CSC community, EAS1, and EAS2, are the most beneficial in terms of collective profitability. However, the analysis of the individual benefits indicates that these strategies imply an unfair distribution of energy among users, with increasing costs for some members. In this case, these strategies favor those members who pay a higher price for energy from the grid and obtain higher compensation for a surplus. On the other hand, EAS3, EAS4, and EAS5 provide savings for all users, although the reduction in the total CSC cost is less significant. Sensitivity analysis shows that the benefit obtained by these three strategies increases when the price of energy is different for the participants and when the amount of surplus energy increases. Among these three strategies, EAS5 achieves the greatest benefit for the entire community as it allows maximizing the savings obtained by consumers through energy sharing in each hour. It prioritizes those with higher electricity purchase prices and those with lower surplus prices.
EAS3, EAS4, and EAS5 also improve both SC and SS ratios with respect to the reference strategy for all members of the community, while for EAS1 and especially EAS2, the behavior is disparate. In the latter strategies, there are members with very high values of SC and very low values for SS, indicating a reduced allocation of energy to these users and a bias in the energy distribution. Thus, the use of EAS3, EAS4, and EAS5 improves SC and SC through the internal exchange of energy between users within the CSC system.
The results also indicate that dynamic coefficients are preferred over static coefficients since they allow the allocation of renewable energy production to the users that demand it at each period of time.
Therefore, it is important to select an EAS that provides an unbiased distribution of the renewable energy produced and takes into account individual ratios to evaluate its performance. The results obtained indicate that the EASs based on the internal exchange of surpluses may be a good option. On the other hand, EASs built upon an optimization of a total variable, such as cost, generate preferences among the members of the community depending on their characteristics.