A Framework from Peer-to-Peer Electricity Trading Based on Communities Transactions

Recently, several authors and reports inform about peer-to-peer transactions of goods and services. Usually, the electricity markets work trading energy at large scale in wholesale electricity markets. Given the integration of distributed energy resources such as PV generators, storage at small scale, demand response and electric vehicles then there are new options to trade energy at small scale by focusing in the producer-consumer relationship. This derives in alternatives based on peer-to-peer (P2P) electricity markets to trade energy employing tools as the blockchain and deployment of distributed energy resources (DERs). This paper presents a framework to trade energy at small scale with a flexible hybrid model of P2P based on transactions between communities and peers, for each period of time, each peer can change its role, the prosumer and producer peers can offer their generated energy. The consumer peers can adjust their consumption behavior based on price and quantity. Thus, the role of the community manager comes into play to associations between peers as an intermediary of the community and the grid. Therefore, a model of transactions with P2P offers was developed for different structures and sales prices. Finally, the framework to trade energy in a hybrid P2P model is evaluated in a demand curve considering a 24-h period for several cases.


INTRODUCTION
Due to the changes in energy consumption, consumer empowerment and the growing concern about the effects produced by climate change, meaningful efforts have been made for the power system operation in order to adapt and evolve accordingly. However, electricity markets have not gone through the same adaptation process to this scenario with its inherent challenges and opportunities, where electricity markets are expected to evolve from a producer-centric to a consumer-centric approach.
Thus, multiple economic models related to the electricity sector are being developed recently that consider elements such as: consumersupplier relationship, the capacity of the consumer to change his/ her role (producer-prosumer), the form of consumer participation (active or passive), the intensity of collaboration between peers (with contracts or not), the inclusion of new production assets such as renewable energy (Obando et al., 2020), energy storage systems (Cantillo and Moreno, 2021), and demand response , and the ownership and channels transfer used (blockchain among others) (Acquier et al., 2019;Athanassiou and Kotsi, 2018;Ertz et al., 2019;Ferraro and Conway, 2020;Garcia-Garcia et al., 2020;Hamari et al., 2016;Menor-Campos et al., 2019). All these elements were considered through different models made with different computing tools (Moreno-Chuquen and Cantillo-Luna, 2020). This proves that the electricity supply is undergoing a transformation process in all stages.
On the other hand, recent advances in electricity generation and the integration of distributed energy resources (DER) This Journal is licensed under a Creative Commons Attribution 4.0 International License such as photovoltaic generators, smart metering (or AMI) (Houda et al., 2020), small-scale storage systems, demand response, and electric vehicles, have produced a breakdown in traditional market paradigms by bringing about a cultural and economic transformation in consumption patterns (David et al., 2016;Laamanen et al., 2018). It is due to the transition from a centralized consumption scenario to new socially empowered peerto-peer models, with the specific purpose of aligning the interests and motivations of both producers and consumers to create fairer economic relations, which is compatible with technological disruptions such as blockchain technology.
Therefore, the adoption of the blockchain in the electric sector transition has allowed the decision making to be made faster and by each peer as an end-user. Thus, decisions are affected regarding the costs or the type of energy (for example, green energy) they want in their homes. Thus, decisions are affected in terms of costs or the type of energy (e.g., clean energy) they want in their homes. Due to the ease of storing relevant information through blockchain (which can be understood as a ledger that records all transactions made between peers in a network), the response times of electricity markets can be lower. By decentralizing the market, new smart grid structures can be introduced, such as full P2P, community-based (interactions through a community manager), or hybrid models using both, to optimize business activities and energy delivery, which can be done in inter-days or even inter-hour basis between small and medium energy markets (Petropoulos, 2017).
The peer-to-peer trading as an approach of electricity markets focused on collaborative economy has recently been discussed. Some authors are proposing exhaustive reviews of new designs of electricity markets focusing on collaborative economy, market objectives, and challenges in the virtual and the physical layers and systematic classification of the market players (Khorasany et al., 2018Tushar et al., 2020), These designs are called peer-to-peer markets, where there could be a direct interaction between peers through a blockchain system given their built-in features. Also, this type of market also offers some advantages such as the empowerment of the end user (ability to choose where the energy comes from and how much it costs, the energy resource and the distributed energy storage [DER]) (Abdella and Shuaib, 2018;Dib et al., 2018;Strbac et al., 2019).
On the other hand, In  is implemented an initial framework model where consumers and prosumers share their energy is shown in a P2P trading in communities. Other authors as (Rao et al., 2020) proposed a dynamic model where at the beginning of each trading period, prosumers and consumers send their respective amounts of surplus or demand and the system evaluates the optimal dispatch model. Finally, some authors as (Guo et al., 2020;Moret and Pinson, 2019;Sorin et al., 2019;Tushar et al., 2020) presented a comprehensive review for peerto-peer and community-based market designs where import, export and generation costs are considered and different market structures are proposed: full P2P (direct interaction between consumer-prosumer), community manager (Paudel et al., 2019) (intermediary between the community and the rest of the system) and hybrid (with intermediaries and individuals) . This paper proposed a detailed framework to develop peer-to-peer energy transactions in different decentralized market structures (i.e., full P2P and community-based markets) including distributed energy resources (DER) as energy storage systems (ESS) among others. The formulation determines the optimal outputs for all generation portfolio (i.e., producer/prosumer peers and grid) as well as power imported/exported from each peer (i.e., peer-to-peer transactions), and ESS charging/discharging schedules (only for the 17-peer system), all of them under different market structures and sales prices (i.e., same or different sales price for all peers).
The paper is organized as follows: the description and mathematical formulation of the problem is presented in Section 2. Section 3 describes the P2P system representation on 5 and 17-peer systems grouped by communities to test the model described above. At the end of this section, the results are analyzed and discussed. Section 4 provides some concluding remarks on this topic.

PEER-TO-PEER MODEL TO ELECTRICITY TRANSFER
This section shows the notation and the mathematical formulation for a peer-to-peer model including roles and distributed energy resources (DER) as storage, among others.

Number of peers n L n
Load of peer n (kWh) C g, n Generation cost from peer n ($) C α, n Imported cost to peer n from peer m ($) C , n Exported cost from peer m to peer n ($) P P g g max min , Maximum/Minimum power generation limits of peers (kW) P n Power generated by peer n (kWh) α n Power imported by peer n (kWh) β n Power exported by peer n (kWh) αl n, m Power imported to peer n from peer m (MWh) βl n, m Power exported to peer n from peer m (MWh) S ton Battery capacity f (S ton ) Function of battery capacity

Mathematical Formulation
The mathematical formulation for a peer-to-peer model to electricity transfer is expressed in this section. The community manager (CM) takes a role as operator. The CM is responsible of managing trading activities between peers as well as serving as intermediary between the community and the grid as shown in Figure 1.
The community manager ensures that a dispatch is found in an optimal way that solves the electricity requirements (i.e., it can be represented by an optimization problem). Considering the sum of the energy consumption and production of each peer n as well as the energy exchanges αl n,m and βl n,m associated with their interactions, the Eq. 1 as objective function is proposed: The model proposed in Eq.
To begin with, power generation must be between the generation limits of each peer (i.e., greater than or equal to its lower limit, as in turn, less than or equal to its greater limit), this restriction is shown in Eq. 2. Also, this formulation includes a global restriction for optimizing the minimum cost, where the total energy generated in the community must be equal to the load, as shown in Eq. 3.
On the other hand, market exchanges between peers are associated with the variables: alpha α (imported power) and beta β (exported power). Those variables have information about the direction, sense, and magnitude of the trading. Each peer can also choose who to trade with (i.e., αl 1,3 indicates power imported from peer 3 to peer 1). Thus, all vectors of imported and exported power must be equal to the total power in this peer. As shown in Eq. 4, 5, and 6, this indicates that α and β are equals in order to accomplish power balance.
However, each prosumer may or may not has an energy storage system. For those cases, the model displays an additional function associated with the battery charge and discharge tendency. Initial conditions must be considered as: Finally, it is necessary to consider power flow balance in each peer as well all system, for this purpose, the sum of the power must be equal to zero, as shown in Eq. 8.

RESULTS AND DISCUSSION
This section shows the result for small-scale P2P energy trading with a flexible hybrid model. As previous works have shown, a community manager plays a meaningful role in P2P transaction (Guo et al., 2020;Moret and Pinson, 2019;Sorin et al., 2019;Sousa et al., 2019). Therefore, some results some the interactions and trading with and without a community manager.
Study cases for transactions with 5 and 17 peers are proposed to evaluate these transactions. The following game rules are suggested: regarding a sale price to compare how peers interacted based on the most optimal generation and distribution costs for transactions derived from the use of a distribution network (C g + C b ). If the imported energy (α) comes from the same community associated, export cost might be the minimum possible value.
In this way, a block-chain model could be set up. In addition to, different export costs are handled between communities, which may open the possibility to energy contracts. As well, it could result in the entry of new agents. Hence, this problem represents a hybrid community model (where in each layer communities (or energy collectives) and single peers may interact directly) with each other. A 24-h period is displayed.
All simulations were completed by a computer (PC) running Windows ® with an Intel ® Core I5+ 8300H processor at 1.6 GHz with 16.00 GB RAM, using Gurobi ® Solver (8.1.1) under the JuMP 0.20.1 Julia platform.

5-Peer System
Considering a 5-peer power system where each peer plays a unique role: prosumer, consumer or producer. This market design is based on peers directly negotiating among themselves (i.e., without an intermediary). Their links are given according to transactions (i.e., production and distribution costs) constituting a graph (Chuquen and Chamorro, 2021). It is important to highlight, while prosumer peers have competitive costs compared to the grid, it can supply all or part of the consumer's energy demand with the addition of clean energy. The one-line diagram for a 5-peer system and its mathematical expression are given in Figure 2.
Some considerations for this power system are the following: to begin with, producer peers do not need to import electrical power, because the associated load is too low compared to their generation, then α=0. Likewise, consumer peers only use power. In this sense, both generation P and exported power β do not appear in this expression. Therefore, their energy demand must be assumed by the other pairs (e.g., α 2,1 , means power imported to peer 2 from peer 1). On the other hand, the prosumers pairs can generate, import, and distribute power as required.

Case I: Same sales price for the 5-peer system
This case presents a unique sales price in order to know how the peers interact with each other through the most efficient generation and distribution cost. For this trading model, once each pair has chosen a role as prosumer P, consumer C or producer G, prosumers and producers offer are generated for this period. These offers contain the maximum power supply and generation cost related. Thus, consumer peers can adjust their consuming behavior based on price and quantity. Once the initial conditions are established, trading activities are carried out, where consumer pairs rely on the cheapest supply to import their power load. Table 1 list the network information.
For this case, the peer interaction takes a unique sales price. When comparing the associated generation and distribution cost, normally the generation cost of a prosumer could be less than that of producers. Thus, while prosumers are priced competitively to the grid, they can enter in the market. Therefore, they will be used to supply power to the grid. Table  2 shows the power generated by the 5-peer system. As shown in Figure 3, the importance of small-scale prosumers can be seen since they meet the load of the system. It can be seen that peer 3 (P3) is favored because its costs are the cheapest in the system. Therefore, it produces energy up to its maximum limit (blue arrow). Once this peer supplies its load and delivers the remaining power, peer 5 (P5) produces the missing power (red arrow). It is interesting to see how the main grid does not have any participation in the trading.
At this point and when expanding the system (i.e., to include more peers), it is possible to enhance groupings (communities) among nearby peers that depend on cogeneration or blockchain markets among their members. Blockchain can be used for peer-to-peer energy exchange, where a credit-based payment plan improves the energy trading process (Aitzhan and Svetinovic, 2018;Alladi et al., 2019;Andoni et al., 2019;Ferrag et al., 2019). Briefly, blockchain are tamper-proof digital books implemented in distributed systems that record all transactions in a peer-to-peer network (Sarmah, 2018;Shafie-Khah, 2020;Teufel et al., 2019). For commercial activities management, it is proposed to use the community manager role. This market mechanism allows to distribute the computational load among the peers (participants) in the network, as well as the protection of the privacy of preferences or strategies of each one . This is presented in the following cases with 5 and 17 peers.

Case II: Different peer price for the 5-peer system
This case, unlike the previous one, presents a different export price due to preferences, contracts or groupings between pairs. Consequently, the exchanges or contracts will allow for a cost reduction in the system, since the association between peers allows for the efficient evaluation of the energy exchange both within and outside the community. Thus, when a pair has a surplus of energy for a certain period of time, it can be delivered to the system at minimum cost. The 5-peer system model with community approach is shown in Figure 4.       The role of community manager allows for efficient resources monitoring in the peer grouping, in this case pairs 2, 4 and 5 (i.e., P2, P4 and P5). Thus, when peer 5 presents a surplus of generation according to its demand, the price for export will be similar to the price of generation in his community. This is in order to minimize costs and coordinate the use of energy within the community. The results are shown in Figure 5.
Once this case has been developed, the total cost (i.e., the objective value) and all power transfers were compared. In relation to the total operating cost, for case I this value reached $ 31,936, while for case II it was $ 28,456. This reduction is due to the creation of the virtual community between peers 2, 4 and 5 as shown in Figure 4. Moreover, the power generated, as well as the imported  Table 3. After incorporating the clusters between peers into the system, it is important to highlight the meaningful increase of 140 kW in the power exported from peer 5 (from 40 kW in case I to 180 kW in case II), since this peer belongs to the same community as peers 2 and 4 (both consumer peers).

17-Peer System
For this case, the one-line diagram for 17-peer system is shown in Figure 6. This system includes 4 generation units (3 small-medium scale and the main grid), 6 prosumer and 7 consumer peers. Like the previous cases based on 5-peer system, the grouping between peers is not initially considered. However, the last of the cases presented, already presents this grouping: the 17 peers were distributed in 5 communities as shown in Figure 7, where we will analyze the different types of interaction and exchanges within and outside the community.

Case I: Same peer price for the 17-peer system
According to the same process of previous case (i.e., 5-peer system), Firstly, same sales price is fixed for all peers. Figure 8 shows the data result.

Case II: Different peer price for the 5-peer system
For this case, the 17 pairs were grouped into 5 communities composed as follows: Main Grid, community 1 (2 smallmedium generators, 2 prosumers and 2 consumers), community 2 (2 prosumers and 1 consumer), community 3 (one peer of each role), and community 4 (3 consumers and 1 prosumer). The results obtained are shown in Figure 9.
Thus, the imported power by consumer peers does not vary, since the same demand curve was used. When communities are presented, the energy production and export from certain peers becomes striking, as is the case with peers 6 and 8. The difference between the target values of both cases was $ 11.264 in benefit of the community case (i.e., the community approach presented in case II is cheaper).

Case III:
Demand curve for the 17-peer system Finally, this case presents an idea of how prosumer pairs work when they have associated energy storage systems (ESS). For this purpose, a 24-h load curve is used, where the ESS charge and discharge functions play an important role in delivering power to the system. In this case, 40 kW and 60 kW can be stored in peers 3 and 6 respectively. On the other hand, there may be contracts between communities. Therefore, energy transactions may favor one exchange over another (e.g., community 3 can sell energy to community 4 at a lower price compared to community 5). Thus, the corresponding 24-h load curve presented for the peers (along their communities) and their power generation scheduling for this case is shown in Figure 10. Likewise, the energy imported and exported by each peer is shown in Figure 11.
The prosumer peers with ESS (i.e., peers 3 and 6), has 4 and 1 charging cycles in this 24-h curve respectively. Furthermore, it can be seen remarkable behaviors in the curves as the presented in the peer 6 during off-peak hours (i.e., h 2-6) where it covers almost all the demand of the system except for the communities that have prosumer peers that can operate at night. On the other hand, close to h 7 there is a demand increase in the system. Therefore, the small generator in community 2 starts working reaching its peak at h 12. Once the demand curve falls after the peak hours (i.e., h 20 and 21), the production of peer 6 begins to decrease, in turn, it is important to highlight that community 2 supplies almost all the energy of the system due to its clustering (peers 4,5 and 6). In this sense, a technology cluster may be possible in that area.

CONCLUSIONS
In this paper, different detailed P2P models were presented that include scenarios such as the same sale price, different prices, grouping between communities and a final scenario for a 24-h demand curve. At some points, is interesting to see that while prosumers have competitive costs compared to the traditional network, the latter begins to become obsolete (e.g., 5-peer system). This not only leads to the transition from the traditional system to one where generation and distribution are close to the community, it also opens debate on the use of smart grids and microgrids than increase the flexibility and resilience of consumers and producers. It should be clarified that the analysis carried out was only proposed in a financial layer, future works may be presented when considering the electrical layer.
The grouping between close peers (communities) allows the resources integration from a complete zone for cogeneration and sustainability. This case could be seen by community 2 in the 24-h demand curve, the community advantages are not only focused on efficiently supplying all its demand. In addition, may seek economic benefits for the entire society allowing the emergence of energy cooperatives or tech clusters.
Collaborative economy and Blockchain, together with specific area advances such as smart meters, solar panels, and electric cars, favor the decentralization panorama of the electricity grid. However, for this purpose, a strong accompaniment of society is needed and while has its learning curve, the community manager model may give utilities. As also be an important transition agent.